Salome HOME
Update copyrights
[modules/gui.git] / src / SUIT / SUIT_LicenseDlg.cxx
1 // Copyright (C) 2007-2019  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 #ifdef WIN32
36 #include <UserEnv.h>
37 #endif
38
39
40 /*!
41   Constructor
42   Construct a dialog with specified parent and name.
43   \param modal define modal status of dialog (default modal dialog created).
44 */
45 SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal )
46  : QDialog( parent )
47 {
48   setObjectName( name );
49   setModal( modal );
50   QString env;
51   if ( ::getenv( "SALOME_LICENSE_FILE" ) )
52     env = ::getenv( "SALOME_LICENSE_FILE" );
53  
54   QFile file( env ); // Read the text from a file
55   
56   if ( !file.exists() || !file.open( QIODevice::ReadOnly ) )
57     return;
58   
59   setWindowTitle( tr( "License" ) );
60   
61   // Create text editor
62   myTextEdit = new QTextEdit( this );
63   QTextStream stream( &file );
64   myTextEdit->setText( stream.readAll() );
65   file.close();
66   myTextEdit->setReadOnly( true );
67   //myTextEdit->ensureVisible(0, 0);
68   
69   // Create buttons
70   QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this );
71   if (firstShow) {
72     anAgreeBtn->setAutoDefault( true );
73     anAgreeBtn->setFocus();
74   }
75   else
76     anAgreeBtn->hide();
77  
78   QPushButton* aCancelBtn = new QPushButton( this );
79   aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) );
80   if ( !firstShow )
81     aCancelBtn->setFocus();
82
83   QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this );
84
85   // Layouting
86   QVBoxLayout* aBaseLayout = new QVBoxLayout( this );
87   aBaseLayout->setMargin( 5 );
88   aBaseLayout->setSpacing( 5 );
89   aBaseLayout->addWidget( myTextEdit );
90   
91   QHBoxLayout* aButtonsLayout = new QHBoxLayout();
92   aBaseLayout->addLayout( aButtonsLayout );
93   if (firstShow)
94     aButtonsLayout->addWidget( anAgreeBtn );
95   
96   aButtonsLayout->addWidget( aCancelBtn );
97   aButtonsLayout->addStretch();
98   aButtonsLayout->addWidget( aPrintBtn );
99  
100   // Connections
101   connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) );
102   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) );
103   connect( aPrintBtn,  SIGNAL( clicked() ), this, SLOT( onPrint( ) ) );
104   
105   resize( 640, 480 );
106 }
107
108 /*!
109         Name: ~SUIT_LicenseDlg [public]
110         Desc: Destructor
111 */
112
113 SUIT_LicenseDlg::~SUIT_LicenseDlg()
114 {
115 }
116
117 void SUIT_LicenseDlg::onAgree()
118 {
119   QString env;
120 #ifdef WIN32
121   DWORD aLen=1024;
122   TCHAR aStr[1024];
123   HANDLE aToken=0;
124   HANDLE hProcess = GetCurrentProcess();
125   OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
126   if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
127     reject();
128 #ifdef UNICODE
129   env = QString::fromWCharArray(aStr);
130 #else 
131   env = aStr;
132 #endif
133 #else
134   if( ! ::getenv( "HOME" ) )
135     reject();
136   env = ::getenv( "HOME" );
137 #endif
138  
139   QFile file( env + "/ReadLicense.log" ); // Read the text from a file
140
141   file.open( QIODevice::WriteOnly );
142
143   QTextStream ts( &file );
144   ts << "OK" << endl;
145   //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
146   file.close();
147
148   accept();
149 }
150
151 void SUIT_LicenseDlg::onCancel()
152 {
153   reject();
154 }
155
156 void SUIT_LicenseDlg::onPrint()
157 {
158   QPrinter aPrinter( QPrinter::HighResolution );
159   aPrinter.setFullPage(true);
160   
161   if ( true /*aPrinter.pageSetup( this )*/ ) {
162     QPainter aPainter( &aPrinter );
163     if( !aPainter.isActive() ) // starting printing failed
164       return;
165         
166     // define fonts
167     QFont aBodyFont = myTextEdit->currentFont();
168     QFont aFooterFont = aBodyFont;
169
170     // calculate margin
171     QPaintDevice* aMetrics = aPainter.device();
172     int aDpiY = aMetrics->logicalDpiY();
173     int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
174
175     QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
176
177     // convert text to rich text format
178     QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
179         
180     QTextDocument aRichText( aFormattedText );
181     aRichText.setDefaultFont( aBodyFont );
182
183
184     /*QSimpleRichText aRichText( aFormattedText,
185                                aBodyFont,
186                                myTextEdit->context(),
187                                myTextEdit->styleSheet(),
188                                myTextEdit->mimeSourceFactory(),
189                                aBody.height() );
190     */
191     aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
192         //aRichText.setWidth( &aPainter, aBody.width() );
193     
194     QRect aView( aBody );
195     
196     int aPageIndex = 1;
197     
198     do {
199       // print page text
200       aRichText.drawContents( &aPainter, aView );
201           //aRichText.draw( &aPainter, aBody.left(), aBody.top(), aView, colorGroup() );
202       aView.translate( 0, aBody.height() );
203       aPainter.translate( 0 , -aBody.height() );
204       
205       // print page number
206       aPainter.setPen(Qt::gray);
207       aPainter.setFont(aFooterFont);
208       QString aFooter = QString("Page ") + QString::number(aPageIndex);
209       aPainter.drawText( aView.right() - aPainter.fontMetrics().width( aFooter ),
210                          aView.bottom() + aPainter.fontMetrics().ascent() + 5, aFooter );
211       
212       if ( aView.top() >= aRichText.size().height() )
213         break;
214       aPrinter.newPage();
215       aPageIndex++;
216     } while (true);
217   }
218 }