Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/gui.git] / src / SUIT / SUIT_LicenseDlg.cxx
1 // Copyright (C) 2007-2012  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.
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   char 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
129   env = aStr;
130 #else
131   if( ! ::getenv( "HOME" ) )
132     reject();
133   env = ::getenv( "HOME" );
134 #endif
135  
136   QFile file( env + "/ReadLicense.log" ); // Read the text from a file
137
138   file.open( QIODevice::WriteOnly );
139
140   QTextStream ts( &file );
141   ts << "OK" << endl;
142   //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
143   file.close();
144
145   accept();
146 }
147
148 void SUIT_LicenseDlg::onCancel()
149 {
150   reject();
151 }
152
153 void SUIT_LicenseDlg::onPrint()
154 {
155   QPrinter aPrinter( QPrinter::HighResolution );
156   aPrinter.setFullPage(true);
157   
158   if ( true /*aPrinter.pageSetup( this )*/ ) {
159     QPainter aPainter( &aPrinter );
160     if( !aPainter.isActive() ) // starting printing failed
161       return;
162         
163     // define fonts
164     QFont aBodyFont = myTextEdit->currentFont();
165     QFont aFooterFont = aBodyFont;
166
167     // calculate margin
168     QPaintDevice* aMetrics = aPainter.device();
169     int aDpiY = aMetrics->logicalDpiY();
170     int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
171
172     QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
173
174     // convert text to rich text format
175     QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
176         
177     QTextDocument aRichText( aFormattedText );
178     aRichText.setDefaultFont( aBodyFont );
179
180
181     /*QSimpleRichText aRichText( aFormattedText,
182                                aBodyFont,
183                                myTextEdit->context(),
184                                myTextEdit->styleSheet(),
185                                myTextEdit->mimeSourceFactory(),
186                                aBody.height() );
187     */
188     aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
189         //aRichText.setWidth( &aPainter, aBody.width() );
190     
191     QRect aView( aBody );
192     
193     int aPageIndex = 1;
194     
195     do {
196       // print page text
197       aRichText.drawContents( &aPainter, aView );
198           //aRichText.draw( &aPainter, aBody.left(), aBody.top(), aView, colorGroup() );
199       aView.translate( 0, aBody.height() );
200       aPainter.translate( 0 , -aBody.height() );
201       
202       // print page number
203       aPainter.setPen(Qt::gray);
204       aPainter.setFont(aFooterFont);
205       QString aFooter = QString("Page ") + QString::number(aPageIndex);
206       aPainter.drawText( aView.right() - aPainter.fontMetrics().width( aFooter ),
207                          aView.bottom() + aPainter.fontMetrics().ascent() + 5, aFooter );
208       
209       if ( aView.top() >= aRichText.size().height() )
210         break;
211       aPrinter.newPage();
212       aPageIndex++;
213     } while (true);
214   }
215 }