From 9d3c104f821bb9ba110a03f3e417ece4ca4670d7 Mon Sep 17 00:00:00 2001 From: nds Date: Wed, 20 Aug 2008 08:23:04 +0000 Subject: [PATCH] Appends a License dialog. --- src/SUIT/SUIT.pro | 2 + src/SUIT/SUIT_LicenseDlg.cxx | 215 +++++++++++++++++++++++++++++++++++ src/SUIT/SUIT_LicenseDlg.h | 51 +++++++++ 3 files changed, 268 insertions(+) create mode 100755 src/SUIT/SUIT_LicenseDlg.cxx create mode 100755 src/SUIT/SUIT_LicenseDlg.h diff --git a/src/SUIT/SUIT.pro b/src/SUIT/SUIT.pro index a3e667daf..5e53df945 100644 --- a/src/SUIT/SUIT.pro +++ b/src/SUIT/SUIT.pro @@ -25,6 +25,7 @@ HEADERS += SUIT_Desktop.h HEADERS += SUIT_ExceptionHandler.h HEADERS += SUIT_FileDlg.h HEADERS += SUIT_FileValidator.h +HEADERS += SUIT_LicenseDlg.h HEADERS += SUIT_MessageBox.h HEADERS += SUIT_Operation.h HEADERS += SUIT_OverrideCursor.h @@ -53,6 +54,7 @@ SOURCES += SUIT_Desktop.cxx SOURCES += SUIT_ExceptionHandler.cxx SOURCES += SUIT_FileDlg.cxx SOURCES += SUIT_FileValidator.cxx +SOURCES += SUIT_LicenseDlg.cxx SOURCES += SUIT_MessageBox.cxx SOURCES += SUIT_Operation.cxx SOURCES += SUIT_OverrideCursor.cxx diff --git a/src/SUIT/SUIT_LicenseDlg.cxx b/src/SUIT/SUIT_LicenseDlg.cxx new file mode 100755 index 000000000..4faafba13 --- /dev/null +++ b/src/SUIT/SUIT_LicenseDlg.cxx @@ -0,0 +1,215 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "SUIT_LicenseDlg.h" + +#include +#include +#include +#include +#include +#include +#include +//#include +#include +#include + +#include + +#ifdef WIN32 +#include +#endif + + +/*! + Constructor + Construct a dialog with specified parent and name. + \param modal define modal status of dialog (default modal dialog created). +*/ +SUIT_LicenseDlg::SUIT_LicenseDlg( bool firstShow, QWidget* parent, const char* name, bool modal ) + : QDialog( parent ) +{ + setObjectName( name ); + setModal( modal ); + QString env; + if ( ::getenv( "TRIPOLI_ROOT_DIR" ) ) + env = ::getenv( "TRIPOLI_ROOT_DIR" ); + + QFile file( env + "/share/salome/resources/License.txt" ); // Read the text from a file + + if ( !file.exists() || !file.open( QIODevice::ReadOnly ) ) + return; + + setWindowTitle( tr( "License" ) ); + + // Create text editor + myTextEdit = new QTextEdit( this ); + QTextStream stream( &file ); + myTextEdit->setText( stream.readAll() ); + file.close(); + myTextEdit->setReadOnly( true ); + //myTextEdit->ensureVisible(0, 0); + + // Create buttons + QPushButton* anAgreeBtn = new QPushButton( tr( "Agree" ), this ); + if (firstShow) { + anAgreeBtn->setAutoDefault( true ); + anAgreeBtn->setFocus(); + } + else + anAgreeBtn->hide(); + + QPushButton* aCancelBtn = new QPushButton( this ); + aCancelBtn->setText( firstShow ? tr( "Cancel" ) : tr( "Close" ) ); + if ( !firstShow ) + aCancelBtn->setFocus(); + + QPushButton* aPrintBtn = new QPushButton( tr( "Print..." ), this ); + + // Layouting + QVBoxLayout* aBaseLayout = new QVBoxLayout( this ); + aBaseLayout->setMargin( 5 ); + aBaseLayout->setSpacing( 5 ); + aBaseLayout->addWidget( myTextEdit ); + + QHBoxLayout* aButtonsLayout = new QHBoxLayout(); + aBaseLayout->addLayout( aButtonsLayout ); + if (firstShow) + aButtonsLayout->addWidget( anAgreeBtn ); + + aButtonsLayout->addWidget( aCancelBtn ); + aButtonsLayout->addStretch(); + aButtonsLayout->addWidget( aPrintBtn ); + + // Connections + connect( anAgreeBtn, SIGNAL( clicked() ), this, SLOT( onAgree( ) ) ); + connect( aCancelBtn, SIGNAL( clicked() ), this, SLOT( onCancel( ) ) ); + connect( aPrintBtn, SIGNAL( clicked() ), this, SLOT( onPrint( ) ) ); + + resize( 640, 480 ); +} + +/*! + Name: ~SUIT_LicenseDlg [public] + Desc: Destructor +*/ + +SUIT_LicenseDlg::~SUIT_LicenseDlg() +{ +} + +void SUIT_LicenseDlg::onAgree() +{ + QString env; +#ifdef WIN32 + DWORD aLen=1024; + char aStr[1024]; + HANDLE aToken=0; + HANDLE hProcess = GetCurrentProcess(); + OpenProcessToken(hProcess,TOKEN_QUERY,&aToken); + if( ! GetUserProfileDirectory( aToken, aStr, &aLen ) ) + reject(); + + env = aStr; +#else + if( ! ::getenv( "HOME" ) ) + reject(); + env = ::getenv( "HOME" ); +#endif + + QFile file( env + "/ReadLicense.log" ); // Read the text from a file + + file.open( QIODevice::WriteOnly ); + + QTextStream ts( &file ); + ts << "OK" << endl; + //file.writeBlock( "OK", (Q_ULONG)qstrlen( "OK" ) ); + file.close(); + + accept(); +} + +void SUIT_LicenseDlg::onCancel() +{ + reject(); +} + +void SUIT_LicenseDlg::onPrint() +{ + QPrinter aPrinter( QPrinter::HighResolution ); + aPrinter.setFullPage(true); + + if ( true /*aPrinter.pageSetup( this )*/ ) { + QPainter aPainter( &aPrinter ); + if( !aPainter.isActive() ) // starting printing failed + return; + + // define fonts + QFont aBodyFont = myTextEdit->currentFont(); + QFont aFooterFont = aBodyFont; + + // calculate margin + QPaintDevice* aMetrics(aPainter.device()); + int aDpiY = aMetrics->logicalDpiY(); + int aMargin = (int) ( (2/2.54)*aDpiY ); // 2 cm margins + + QRect aBody( aMargin, aMargin, aMetrics->width() - 2*aMargin, aMetrics->height() - 2*aMargin ); + + // convert text to rich text format + QString aFormattedText = Qt::convertFromPlainText( myTextEdit->toPlainText() ); + + QTextDocument aRichText( aFormattedText ); + aRichText.setDefaultFont( aBodyFont ); + + + /*QSimpleRichText aRichText( aFormattedText, + aBodyFont, + myTextEdit->context(), + myTextEdit->styleSheet(), + myTextEdit->mimeSourceFactory(), + aBody.height() ); + */ + aRichText.setPageSize( QSize( aBody.width(), aRichText.pageSize().height() ) ); + //aRichText.setWidth( &aPainter, aBody.width() ); + + QRect aView( aBody ); + + int aPageIndex = 1; + + do { + // print page text + aRichText.drawContents( &aPainter, aView ); + //aRichText.draw( &aPainter, aBody.left(), aBody.top(), aView, colorGroup() ); + aView.translate( 0, aBody.height() ); + aPainter.translate( 0 , -aBody.height() ); + + // print page number + aPainter.setPen(Qt::gray); + aPainter.setFont(aFooterFont); + QString aFooter = QString("Page ") + QString::number(aPageIndex); + aPainter.drawText( aView.right() - aPainter.fontMetrics().width( aFooter ), + aView.bottom() + aPainter.fontMetrics().ascent() + 5, aFooter ); + + if ( aView.top() >= aRichText.size().height() ) + break; + aPrinter.newPage(); + aPageIndex++; + } while (true); + } +} diff --git a/src/SUIT/SUIT_LicenseDlg.h b/src/SUIT/SUIT_LicenseDlg.h new file mode 100755 index 000000000..d0e2e739f --- /dev/null +++ b/src/SUIT/SUIT_LicenseDlg.h @@ -0,0 +1,51 @@ +// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com + +#ifndef SUIT_LICENSEDLG_H +#define SUIT_LICENSEDLG_H + +#include "SUIT.h" + +#include +class QTextEdit; + +#ifdef WIN32 +#pragma warning( disable:4251 ) +#endif + +class SUIT_EXPORT SUIT_LicenseDlg :public QDialog +{ + Q_OBJECT +public: + SUIT_LicenseDlg( bool firstShow = true, QWidget* = 0, const char* = 0, bool = true ); + virtual ~SUIT_LicenseDlg(); + +private slots: + void onAgree(); + void onCancel(); + void onPrint(); + +private: + QTextEdit* myTextEdit; +}; + +#ifdef WIN32 +#pragma warning( default:4251 ) +#endif + +#endif -- 2.39.2