1 // Copyright (C) 2007-2020 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>
44 Construct a dialog with specified parent and name.
45 \param modal define modal status of dialog (default modal dialog created).
47 SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal )
50 setObjectName( name );
52 QString env = Qtx::getenv( "SALOME_LICENSE_FILE" );
53 QFile file( env ); // Read the text from a file
55 if ( !file.exists() || !file.open( QIODevice::ReadOnly ) )
58 setWindowTitle( tr( "License" ) );
61 myTextEdit = new QTextEdit( this );
62 QTextStream stream( &file );
63 myTextEdit->setText( stream.readAll() );
65 myTextEdit->setReadOnly( true );
66 //myTextEdit->ensureVisible(0, 0);
69 QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this );
71 anAgreeBtn->setAutoDefault( true );
72 anAgreeBtn->setFocus();
77 QPushButton* aCancelBtn = new QPushButton( this );
78 aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) );
80 aCancelBtn->setFocus();
82 QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this );
85 QVBoxLayout* aBaseLayout = new QVBoxLayout( this );
86 aBaseLayout->setMargin( 5 );
87 aBaseLayout->setSpacing( 5 );
88 aBaseLayout->addWidget( myTextEdit );
90 QHBoxLayout* aButtonsLayout = new QHBoxLayout();
91 aBaseLayout->addLayout( aButtonsLayout );
93 aButtonsLayout->addWidget( anAgreeBtn );
95 aButtonsLayout->addWidget( aCancelBtn );
96 aButtonsLayout->addStretch();
97 aButtonsLayout->addWidget( aPrintBtn );
100 connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) );
101 connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) );
102 connect( aPrintBtn, SIGNAL( clicked() ), this, SLOT( onPrint( ) ) );
108 Name: ~SUIT_LicenseDlg [public]
112 SUIT_LicenseDlg::~SUIT_LicenseDlg()
116 void SUIT_LicenseDlg::onAgree()
123 HANDLE hProcess = GetCurrentProcess();
124 OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
125 if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
128 env = QString::fromWCharArray(aStr);
133 if( ! ::getenv( "HOME" ) )
135 env = ::getenv( "HOME" );
138 QFile file( env + "/ReadLicense.log" ); // Read the text from a file
140 file.open( QIODevice::WriteOnly );
142 QTextStream ts( &file );
144 //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
150 void SUIT_LicenseDlg::onCancel()
155 void SUIT_LicenseDlg::onPrint()
157 QPrinter aPrinter( QPrinter::HighResolution );
158 aPrinter.setFullPage(true);
160 if ( true /*aPrinter.pageSetup( this )*/ ) {
161 QPainter aPainter( &aPrinter );
162 if( !aPainter.isActive() ) // starting printing failed
166 QFont aBodyFont = myTextEdit->currentFont();
167 QFont aFooterFont = aBodyFont;
170 QPaintDevice* aMetrics = aPainter.device();
171 int aDpiY = aMetrics->logicalDpiY();
172 int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
174 QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
176 // convert text to rich text format
177 QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
179 QTextDocument aRichText( aFormattedText );
180 aRichText.setDefaultFont( aBodyFont );
183 /*QSimpleRichText aRichText( aFormattedText,
185 myTextEdit->context(),
186 myTextEdit->styleSheet(),
187 myTextEdit->mimeSourceFactory(),
190 aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
191 //aRichText.setWidth( &aPainter, aBody.width() );
193 QRect aView( aBody );
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() );
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 );
211 if ( aView.top() >= aRichText.size().height() )