Salome HOME
08358f5ea04cf77d38a8ded8ca675478e0d07de3
[modules/gui.git] / src / SUIT / SUIT_LicenseDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SUIT_LicenseDlg.h"
23
24 #include <QApplication>
25 #include <QTextEdit>
26 #include <QLayout> 
27 #include <QPushButton>
28 #include <QTextStream> 
29 #include <QFile>
30 #include <QPrinter>
31 //#include <QSimpleRichText>
32 #include <QTextDocument>
33 #include <QPainter>
34
35 #include <math.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;
53   if ( ::getenv( "TRIPOLI_ROOT_DIR" ) )
54     env = ::getenv( "TRIPOLI_ROOT_DIR" );
55  
56   QFile file( env + "/share/salome/resources/License.txt" ); // Read the text from a file
57   
58   if ( !file.exists() || !file.open( QIODevice::ReadOnly ) )
59     return;
60   
61   setWindowTitle( tr( "License" ) );
62   
63   // Create text editor
64   myTextEdit = new QTextEdit( this );
65   QTextStream stream( &file );
66   myTextEdit->setText( stream.readAll() );
67   file.close();
68   myTextEdit->setReadOnly( true );
69   //myTextEdit->ensureVisible(0, 0);
70   
71   // Create buttons
72   QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this );
73   if (firstShow) {
74     anAgreeBtn->setAutoDefault( true );
75     anAgreeBtn->setFocus();
76   }
77   else
78     anAgreeBtn->hide();
79  
80   QPushButton* aCancelBtn = new QPushButton( this );
81   aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) );
82   if ( !firstShow )
83     aCancelBtn->setFocus();
84
85   QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this );
86
87   // Layouting
88   QVBoxLayout* aBaseLayout = new QVBoxLayout( this );
89   aBaseLayout->setMargin( 5 );
90   aBaseLayout->setSpacing( 5 );
91   aBaseLayout->addWidget( myTextEdit );
92   
93   QHBoxLayout* aButtonsLayout = new QHBoxLayout();
94   aBaseLayout->addLayout( aButtonsLayout );
95   if (firstShow)
96     aButtonsLayout->addWidget( anAgreeBtn );
97   
98   aButtonsLayout->addWidget( aCancelBtn );
99   aButtonsLayout->addStretch();
100   aButtonsLayout->addWidget( aPrintBtn );
101  
102   // Connections
103   connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) );
104   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) );
105   connect( aPrintBtn,  SIGNAL( clicked() ), this, SLOT( onPrint( ) ) );
106   
107   resize( 640, 480 );
108 }
109
110 /*!
111         Name: ~SUIT_LicenseDlg [public]
112         Desc: Destructor
113 */
114
115 SUIT_LicenseDlg::~SUIT_LicenseDlg()
116 {
117 }
118
119 void SUIT_LicenseDlg::onAgree()
120 {
121   QString env;
122 #ifdef WIN32
123     DWORD aLen=1024;
124   char aStr[1024];
125   HANDLE aToken=0;
126   HANDLE hProcess = GetCurrentProcess();
127   OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
128   if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
129     reject();
130
131   env = aStr;
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 }