Salome HOME
Copyright update 2020
[modules/gui.git] / src / SUIT / SUIT_LicenseDlg.cxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SUIT_LicenseDlg.h"
21
22 #include <QApplication>
23 #include <QTextEdit>
24 #include <QLayout> 
25 #include <QPushButton>
26 #include <QTextStream> 
27 #include <QFile>
28 #include <QPrinter>
29 //#include <QSimpleRichText>
30 #include <QTextDocument>
31 #include <QPainter>
32
33 #include <math.h>
34
35 #include <Qtx.h>
36
37 #ifdef WIN32
38 #include <UserEnv.h>
39 #endif
40
41
42 /*!
43   Constructor
44   Construct a dialog with specified parent and name.
45   \param modal define modal status of dialog (default modal dialog created).
46 */
47 SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal )
48  : QDialog( parent )
49 {
50   setObjectName( name );
51   setModal( modal );
52   QString env = Qtx::getenv( "SALOME_LICENSE_FILE" ); 
53   QFile file( env ); // Read the text from a file
54   
55   if ( !file.exists() || !file.open( QIODevice::ReadOnly ) )
56     return;
57   
58   setWindowTitle( tr( "License" ) );
59   
60   // Create text editor
61   myTextEdit = new QTextEdit( this );
62   QTextStream stream( &file );
63   myTextEdit->setText( stream.readAll() );
64   file.close();
65   myTextEdit->setReadOnly( true );
66   //myTextEdit->ensureVisible(0, 0);
67   
68   // Create buttons
69   QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this );
70   if (firstShow) {
71     anAgreeBtn->setAutoDefault( true );
72     anAgreeBtn->setFocus();
73   }
74   else
75     anAgreeBtn->hide();
76  
77   QPushButton* aCancelBtn = new QPushButton( this );
78   aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) );
79   if ( !firstShow )
80     aCancelBtn->setFocus();
81
82   QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this );
83
84   // Layouting
85   QVBoxLayout* aBaseLayout = new QVBoxLayout( this );
86   aBaseLayout->setMargin( 5 );
87   aBaseLayout->setSpacing( 5 );
88   aBaseLayout->addWidget( myTextEdit );
89   
90   QHBoxLayout* aButtonsLayout = new QHBoxLayout();
91   aBaseLayout->addLayout( aButtonsLayout );
92   if (firstShow)
93     aButtonsLayout->addWidget( anAgreeBtn );
94   
95   aButtonsLayout->addWidget( aCancelBtn );
96   aButtonsLayout->addStretch();
97   aButtonsLayout->addWidget( aPrintBtn );
98  
99   // Connections
100   connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) );
101   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) );
102   connect( aPrintBtn,  SIGNAL( clicked() ), this, SLOT( onPrint( ) ) );
103   
104   resize( 640, 480 );
105 }
106
107 /*!
108         Name: ~SUIT_LicenseDlg [public]
109         Desc: Destructor
110 */
111
112 SUIT_LicenseDlg::~SUIT_LicenseDlg()
113 {
114 }
115
116 void SUIT_LicenseDlg::onAgree()
117 {
118   QString env;
119 #ifdef WIN32
120   DWORD aLen=1024;
121   TCHAR aStr[1024];
122   HANDLE aToken=0;
123   HANDLE hProcess = GetCurrentProcess();
124   OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
125   if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
126     reject();
127 #ifdef UNICODE
128   env = QString::fromWCharArray(aStr);
129 #else 
130   env = aStr;
131 #endif
132 #else
133   if( ! ::getenv( "HOME" ) )
134     reject();
135   env = ::getenv( "HOME" );
136 #endif
137  
138   QFile file( env + "/ReadLicense.log" ); // Read the text from a file
139
140   file.open( QIODevice::WriteOnly );
141
142   QTextStream ts( &file );
143   ts << "OK" << endl;
144   //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
145   file.close();
146
147   accept();
148 }
149
150 void SUIT_LicenseDlg::onCancel()
151 {
152   reject();
153 }
154
155 void SUIT_LicenseDlg::onPrint()
156 {
157   QPrinter aPrinter( QPrinter::HighResolution );
158   aPrinter.setFullPage(true);
159   
160   if ( true /*aPrinter.pageSetup( this )*/ ) {
161     QPainter aPainter( &aPrinter );
162     if( !aPainter.isActive() ) // starting printing failed
163       return;
164         
165     // define fonts
166     QFont aBodyFont = myTextEdit->currentFont();
167     QFont aFooterFont = aBodyFont;
168
169     // calculate margin
170     QPaintDevice* aMetrics = aPainter.device();
171     int aDpiY = aMetrics->logicalDpiY();
172     int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
173
174     QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
175
176     // convert text to rich text format
177     QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
178         
179     QTextDocument aRichText( aFormattedText );
180     aRichText.setDefaultFont( aBodyFont );
181
182
183     /*QSimpleRichText aRichText( aFormattedText,
184                                aBodyFont,
185                                myTextEdit->context(),
186                                myTextEdit->styleSheet(),
187                                myTextEdit->mimeSourceFactory(),
188                                aBody.height() );
189     */
190     aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
191         //aRichText.setWidth( &aPainter, aBody.width() );
192     
193     QRect aView( aBody );
194     
195     int aPageIndex = 1;
196     
197     do {
198       // print page text
199       aRichText.drawContents( &aPainter, aView );
200           //aRichText.draw( &aPainter, aBody.left(), aBody.top(), aView, colorGroup() );
201       aView.translate( 0, aBody.height() );
202       aPainter.translate( 0 , -aBody.height() );
203       
204       // print page number
205       aPainter.setPen(Qt::gray);
206       aPainter.setFont(aFooterFont);
207       QString aFooter = QString("Page ") + QString::number(aPageIndex);
208       aPainter.drawText( aView.right() - aPainter.fontMetrics().width( aFooter ),
209                          aView.bottom() + aPainter.fontMetrics().ascent() + 5, aFooter );
210       
211       if ( aView.top() >= aRichText.size().height() )
212         break;
213       aPrinter.newPage();
214       aPageIndex++;
215     } while (true);
216   }
217 }