Salome HOME
9b8a92a6503e72610022e8a1aeac0bfd6c7ed7bc
[modules/gui.git] / src / SUIT / SUIT_LicenseDlg.cxx
1 // Copyright (C) 2007-2022  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 <QDir>
29 #include <QPrinter>
30 //#include <QSimpleRichText>
31 #include <QTextDocument>
32 #include <QPainter>
33
34 #include <math.h>
35
36 #include <Qtx.h>
37
38 #ifdef WIN32
39 #include <UserEnv.h>
40 #endif
41
42
43 /*!
44   Constructor
45   Construct a dialog with specified parent and name.
46   \param modal define modal status of dialog (default modal dialog created).
47 */
48 SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal )
49  : QDialog( parent )
50 {
51   setObjectName( name );
52   setModal( modal );
53   QString env = Qtx::getenv( "SALOME_LICENSE_FILE" ); 
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   /*
120   QString env;
121 #ifdef WIN32
122   DWORD aLen=1024;
123   TCHAR aStr[1024];
124   HANDLE aToken=0;
125   HANDLE hProcess = GetCurrentProcess();
126   OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
127   if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
128     reject();
129 #ifdef UNICODE
130   env = QString::fromWCharArray(aStr);
131 #else 
132   env = aStr;
133 #endif
134 #else
135   if( ! ::getenv( "HOME" ) )
136     reject();
137   env = ::getenv( "HOME" );
138 #endif
139  
140   QFile file( env + "/ReadLicense.log" ); // Read the text from a file
141
142   file.open( QIODevice::WriteOnly );
143   */
144   QString env = QDir::homePath();
145   QFile file( env + "/ReadLicense.log" );
146   if (!file.open( QIODevice::WriteOnly ))
147     reject();
148
149   QTextStream ts( &file );
150   ts << "OK" << endl;
151   //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
152   file.close();
153
154   accept();
155 }
156
157 void SUIT_LicenseDlg::onCancel()
158 {
159   reject();
160 }
161
162 void SUIT_LicenseDlg::onPrint()
163 {
164   QPrinter aPrinter( QPrinter::HighResolution );
165   aPrinter.setFullPage(true);
166   
167   if ( true /*aPrinter.pageSetup( this )*/ ) {
168     QPainter aPainter( &aPrinter );
169     if( !aPainter.isActive() ) // starting printing failed
170       return;
171         
172     // define fonts
173     QFont aBodyFont = myTextEdit->currentFont();
174     QFont aFooterFont = aBodyFont;
175
176     // calculate margin
177     QPaintDevice* aMetrics = aPainter.device();
178     int aDpiY = aMetrics->logicalDpiY();
179     int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
180
181     QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
182
183     // convert text to rich text format
184     QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
185         
186     QTextDocument aRichText( aFormattedText );
187     aRichText.setDefaultFont( aBodyFont );
188
189
190     /*QSimpleRichText aRichText( aFormattedText,
191                                aBodyFont,
192                                myTextEdit->context(),
193                                myTextEdit->styleSheet(),
194                                myTextEdit->mimeSourceFactory(),
195                                aBody.height() );
196     */
197     aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
198         //aRichText.setWidth( &aPainter, aBody.width() );
199     
200     QRect aView( aBody );
201     
202     int aPageIndex = 1;
203     
204     do {
205       // print page text
206       aRichText.drawContents( &aPainter, aView );
207           //aRichText.draw( &aPainter, aBody.left(), aBody.top(), aView, colorGroup() );
208       aView.translate( 0, aBody.height() );
209       aPainter.translate( 0 , -aBody.height() );
210       
211       // print page number
212       aPainter.setPen(Qt::gray);
213       aPainter.setFont(aFooterFont);
214       QString aFooter = QString("Page ") + QString::number(aPageIndex);
215       aPainter.drawText( aView.right() - aPainter.fontMetrics().width( aFooter ),
216                          aView.bottom() + aPainter.fontMetrics().ascent() + 5, aFooter );
217       
218       if ( aView.top() >= aRichText.size().height() )
219         break;
220       aPrinter.newPage();
221       aPageIndex++;
222     } while (true);
223   }
224 }