Back to index

salome-gui  6.5.0
SUIT_LicenseDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "SUIT_LicenseDlg.h"
00021 
00022 #include <QApplication>
00023 #include <QTextEdit>
00024 #include <QLayout> 
00025 #include <QPushButton>
00026 #include <QTextStream> 
00027 #include <QFile>
00028 #include <QPrinter>
00029 //#include <QSimpleRichText>
00030 #include <QTextDocument>
00031 #include <QPainter>
00032 
00033 #include <math.h>
00034 
00035 #ifdef WIN32
00036 #include <UserEnv.h>
00037 #endif
00038 
00039 
00045 SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal )
00046  : QDialog( parent )
00047 {
00048   setObjectName( name );
00049   setModal( modal );
00050   QString env;
00051   if ( ::getenv( "SALOME_LICENSE_FILE" ) )
00052     env = ::getenv( "SALOME_LICENSE_FILE" );
00053  
00054   QFile file( env ); // Read the text from a file
00055   
00056   if ( !file.exists() || !file.open( QIODevice::ReadOnly ) )
00057     return;
00058   
00059   setWindowTitle( tr( "License" ) );
00060   
00061   // Create text editor
00062   myTextEdit = new QTextEdit( this );
00063   QTextStream stream( &file );
00064   myTextEdit->setText( stream.readAll() );
00065   file.close();
00066   myTextEdit->setReadOnly( true );
00067   //myTextEdit->ensureVisible(0, 0);
00068   
00069   // Create buttons
00070   QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this );
00071   if (firstShow) {
00072     anAgreeBtn->setAutoDefault( true );
00073     anAgreeBtn->setFocus();
00074   }
00075   else
00076     anAgreeBtn->hide();
00077  
00078   QPushButton* aCancelBtn = new QPushButton( this );
00079   aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) );
00080   if ( !firstShow )
00081     aCancelBtn->setFocus();
00082 
00083   QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this );
00084 
00085   // Layouting
00086   QVBoxLayout* aBaseLayout = new QVBoxLayout( this );
00087   aBaseLayout->setMargin( 5 );
00088   aBaseLayout->setSpacing( 5 );
00089   aBaseLayout->addWidget( myTextEdit );
00090   
00091   QHBoxLayout* aButtonsLayout = new QHBoxLayout();
00092   aBaseLayout->addLayout( aButtonsLayout );
00093   if (firstShow)
00094     aButtonsLayout->addWidget( anAgreeBtn );
00095   
00096   aButtonsLayout->addWidget( aCancelBtn );
00097   aButtonsLayout->addStretch();
00098   aButtonsLayout->addWidget( aPrintBtn );
00099  
00100   // Connections
00101   connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) );
00102   connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) );
00103   connect( aPrintBtn,  SIGNAL( clicked() ), this, SLOT( onPrint( ) ) );
00104   
00105   resize( 640, 480 );
00106 }
00107 
00113 SUIT_LicenseDlg::~SUIT_LicenseDlg()
00114 {
00115 }
00116 
00117 void SUIT_LicenseDlg::onAgree()
00118 {
00119   QString env;
00120 #ifdef WIN32
00121     DWORD aLen=1024;
00122   char aStr[1024];
00123   HANDLE aToken=0;
00124   HANDLE hProcess = GetCurrentProcess();
00125   OpenProcessToken(hProcess,TOKEN_QUERY,&aToken);
00126   if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) )
00127     reject();
00128 
00129   env = aStr;
00130 #else
00131   if( ! ::getenv( "HOME" ) )
00132     reject();
00133   env = ::getenv( "HOME" );
00134 #endif
00135  
00136   QFile file( env + "/ReadLicense.log" ); // Read the text from a file
00137 
00138   file.open( QIODevice::WriteOnly );
00139 
00140   QTextStream ts( &file );
00141   ts << "OK" << endl;
00142   //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) );
00143   file.close();
00144 
00145   accept();
00146 }
00147 
00148 void SUIT_LicenseDlg::onCancel()
00149 {
00150   reject();
00151 }
00152 
00153 void SUIT_LicenseDlg::onPrint()
00154 {
00155   QPrinter aPrinter( QPrinter::HighResolution );
00156   aPrinter.setFullPage(true);
00157   
00158   if ( true /*aPrinter.pageSetup( this )*/ ) {
00159     QPainter aPainter( &aPrinter );
00160     if( !aPainter.isActive() ) // starting printing failed
00161       return;
00162         
00163     // define fonts
00164     QFont aBodyFont = myTextEdit->currentFont();
00165     QFont aFooterFont = aBodyFont;
00166 
00167     // calculate margin
00168     QPaintDevice* aMetrics = aPainter.device();
00169     int aDpiY = aMetrics->logicalDpiY();
00170     int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins
00171 
00172     QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin );
00173 
00174     // convert text to rich text format
00175     QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() );
00176         
00177     QTextDocument aRichText( aFormattedText );
00178     aRichText.setDefaultFont( aBodyFont );
00179 
00180 
00181     /*QSimpleRichText aRichText( aFormattedText,
00182                                aBodyFont,
00183                                myTextEdit->context(),
00184                                myTextEdit->styleSheet(),
00185                                myTextEdit->mimeSourceFactory(),
00186                                aBody.height() );
00187     */
00188     aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) );
00189         //aRichText.setWidth( &aPainter, aBody.width() );
00190     
00191     QRect aView( aBody );
00192     
00193     int aPageIndex = 1;
00194     
00195     do {
00196       // print page text
00197       aRichText.drawContents( &aPainter, aView );
00198           //aRichText.draw( &aPainter, aBody.left(), aBody.top(), aView, colorGroup() );
00199       aView.translate( 0, aBody.height() );
00200       aPainter.translate( 0 , -aBody.height() );
00201       
00202       // print page number
00203       aPainter.setPen(Qt::gray);
00204       aPainter.setFont(aFooterFont);
00205       QString aFooter = QString("Page ") + QString::number(aPageIndex);
00206       aPainter.drawText( aView.right() - aPainter.fontMetrics().width( aFooter ),
00207                          aView.bottom() + aPainter.fontMetrics().ascent() + 5, aFooter );
00208       
00209       if ( aView.top() >= aRichText.size().height() )
00210         break;
00211       aPrinter.newPage();
00212       aPageIndex++;
00213     } while (true);
00214   }
00215 }