1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SUIT_LicenseDlg.h"
22 #include <QApplication>
25 #include <QPushButton>
26 #include <QTextStream>
29 //#include <QSimpleRichText>
30 #include <QTextDocument>
42 Construct a dialog with specified parent and name.
43 \param modal define modal status of dialog (default modal dialog created).
45 SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal )
48 setObjectName( name );
51 if ( ::getenv( "SALOME_LICENSE_FILE" ) )
52 env = ::getenv( "SALOME_LICENSE_FILE" );
54 QFile file( env ); // Read the text from a file
56 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) )
59 setWindowTitle( tr( "License" ) );
62 myTextEdit = new QTextEdit( this );
63 QTextStream stream( &file );
64 myTextEdit->setText( stream.readAll() );
66 myTextEdit->setReadOnly( true );
67 //myTextEdit->ensureVisible(0, 0);
70 QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this );
72 anAgreeBtn->setAutoDefault( true );
73 anAgreeBtn->setFocus();
78 QPushButton* aCancelBtn = new QPushButton( this );
79 aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) );
81 aCancelBtn->setFocus();
83 QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this );
86 QVBoxLayout* aBaseLayout = new QVBoxLayout( this );
87 aBaseLayout->setMargin( 5 );
88 aBaseLayout->setSpacing( 5 );
89 aBaseLayout->addWidget( myTextEdit );
91 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
92 aBaseLayout->addLayout( aButtonsLayout );
94 aButtonsLayout->addWidget( anAgreeBtn );
96 aButtonsLayout->addWidget( aCancelBtn );
97 aButtonsLayout->addStretch();
98 aButtonsLayout->addWidget( aPrintBtn );
101 connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) );
102 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) );
103 connect( aPrintBtn, SIGNAL( clicked() ), this, SLOT( onPrint( ) ) );
109 Name: ~SUIT_LicenseDlg [public]
113 SUIT_LicenseDlg::~SUIT_LicenseDlg()
117 void SUIT_LicenseDlg::onAgree()
124 HANDLE hProcess = GetCurrentProcess();
125 OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
126 if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
131 if( ! ::getenv( "HOME" ) )
133 env = ::getenv( "HOME" );
136 QFile file( env + "/ReadLicense.log" ); // Read the text from a file
138 file.open( QIODevice::WriteOnly );
140 QTextStream ts( &file );
142 //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
148 void SUIT_LicenseDlg::onCancel()
153 void SUIT_LicenseDlg::onPrint()
155 QPrinter aPrinter( QPrinter::HighResolution );
156 aPrinter.setFullPage(true);
158 if ( true /*aPrinter.pageSetup( this )*/ ) {
159 QPainter aPainter( &aPrinter );
160 if( !aPainter.isActive() ) // starting printing failed
164 QFont aBodyFont = myTextEdit->currentFont();
165 QFont aFooterFont = aBodyFont;
168 QPaintDevice* aMetrics = aPainter.device();
169 int aDpiY = aMetrics->logicalDpiY();
170 int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
172 QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
174 // convert text to rich text format
175 QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
177 QTextDocument aRichText( aFormattedText );
178 aRichText.setDefaultFont( aBodyFont );
181 /*QSimpleRichText aRichText( aFormattedText,
183 myTextEdit->context(),
184 myTextEdit->styleSheet(),
185 myTextEdit->mimeSourceFactory(),
188 aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
189 //aRichText.setWidth( &aPainter, aBody.width() );
191 QRect aView( aBody );
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() );
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 );
209 if ( aView.top() >= aRichText.size().height() )