1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 ) )
129 env = QString::fromWCharArray(aStr);
134 if( ! ::getenv( "HOME" ) )
136 env = ::getenv( "HOME" );
139 QFile file( env + "/ReadLicense.log" ); // Read the text from a file
141 file.open( QIODevice::WriteOnly );
143 QTextStream ts( &file );
145 //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
151 void SUIT_LicenseDlg::onCancel()
156 void SUIT_LicenseDlg::onPrint()
158 QPrinter aPrinter( QPrinter::HighResolution );
159 aPrinter.setFullPage(true);
161 if ( true /*aPrinter.pageSetup( this )*/ ) {
162 QPainter aPainter( &aPrinter );
163 if( !aPainter.isActive() ) // starting printing failed
167 QFont aBodyFont = myTextEdit->currentFont();
168 QFont aFooterFont = aBodyFont;
171 QPaintDevice* aMetrics = aPainter.device();
172 int aDpiY = aMetrics->logicalDpiY();
173 int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
175 QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
177 // convert text to rich text format
178 QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
180 QTextDocument aRichText( aFormattedText );
181 aRichText.setDefaultFont( aBodyFont );
184 /*QSimpleRichText aRichText( aFormattedText,
186 myTextEdit->context(),
187 myTextEdit->styleSheet(),
188 myTextEdit->mimeSourceFactory(),
191 aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
192 //aRichText.setWidth( &aPainter, aBody.width() );
194 QRect aView( aBody );
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() );
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 );
212 if ( aView.top() >= aRichText.size().height() )