Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

helpwindow.cpp

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "helpwindow.h"
00017 
00018 #include "TSystem.h"
00019 
00020 #include "qtextbrowser.h"
00021 #include "qdir.h"
00022 #include "qstatusbar.h"
00023 #include "qmenubar.h"
00024 #include "qtoolbutton.h"
00025 #include "qfile.h"
00026 #include "qmessagebox.h"
00027 #include "qfiledialog.h"
00028 #include "qapplication.h"
00029 #include "qcombobox.h"
00030 #include "qprinter.h"
00031 #include "qsimplerichtext.h"
00032 #include "qpainter.h"
00033 #include "qpaintdevicemetrics.h"
00034 
00035 TGo4HelpWindow::TGo4HelpWindow( const QString& home_, QWidget* parent, const QString& subdir)
00036     : QMainWindow( parent, home_.latin1(), WDestructiveClose ),
00037       pathCombo( 0 ),
00038       selectedURL()
00039 {
00040     readHistory();
00041     readBookmarks();
00042 
00043     browser = new QTextBrowser( this );
00044 
00045     _path = gSystem->Getenv("GO4SYS");
00046     _path.append("/docs");
00047     if(!subdir.isEmpty())
00048       {
00049          _path.append("/");
00050          _path.append(subdir);
00051       }
00052     browser->mimeSourceFactory()->setFilePath( _path );
00053     browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00054     connect( browser, SIGNAL( textChanged() ),
00055         this, SLOT( textChanged() ) );
00056 
00057     setCentralWidget( browser );
00058 
00059     if ( !home_.isEmpty() )
00060     browser->setSource( home_ );
00061 
00062     connect( browser, SIGNAL( highlighted( const QString&) ),
00063         statusBar(), SLOT( message( const QString&)) );
00064 
00065     resize( 640,700 );
00066 
00067 
00068     QPopupMenu* file = new QPopupMenu( this );
00069     file->insertItem( tr("&Print"), this, SLOT( print() ), CTRL+Key_P );
00070     file->insertSeparator();
00071     file->insertItem( tr("&Close"), this, SLOT( close() ), CTRL+Key_Q );
00072     file->insertItem( tr("E&xit"), qApp, SLOT( closeAllWindows() ), CTRL+Key_X );
00073 
00074     // The same three icons are used twice each.
00075     QString picPath = gSystem->Getenv("GO4SYS");
00076     picPath.append("/images/");
00077     QIconSet icon_back(QPixmap(picPath + "back.xpm"));
00078     QIconSet icon_forward(QPixmap(picPath + "forward.xpm"));
00079     QIconSet icon_home(QPixmap(picPath + "home.xpm"));
00080 
00081     QPopupMenu* go = new QPopupMenu( this );
00082     backwardId = go->insertItem( icon_back,
00083              tr("&Backward"), browser, SLOT( backward() ),
00084              CTRL+Key_Left );
00085     forwardId = go->insertItem( icon_forward,
00086             tr("&Forward"), browser, SLOT( forward() ),
00087             CTRL+Key_Right );
00088     go->insertItem( icon_home, tr("&Home"), browser, SLOT( home() ) );
00089 
00090     QPopupMenu* help = new QPopupMenu( this );
00091     help->insertItem( tr("&About ..."), this, SLOT( about() ) );
00092     help->insertItem( tr("About &Qt ..."), this, SLOT( aboutQt() ) );
00093 
00094     hist = new QPopupMenu( this );
00095     QStringList::Iterator it = history.begin();
00096     for ( ; it != history.end(); ++it )
00097    mHistory[ hist->insertItem( *it ) ] = *it;
00098     connect( hist, SIGNAL( activated( int ) ),
00099         this, SLOT( histChosen( int ) ) );
00100 
00101     bookm = new QPopupMenu( this );
00102     bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) );
00103     bookm->insertSeparator();
00104 
00105     QStringList::Iterator it2 = bookmarks.begin();
00106     for ( ; it2 != bookmarks.end(); ++it2 )
00107    mBookmarks[ bookm->insertItem( *it2 ) ] = *it2;
00108     connect( bookm, SIGNAL( activated( int ) ),
00109         this, SLOT( bookmChosen( int ) ) );
00110 
00111     menuBar()->insertItem( tr("&File"), file );
00112     menuBar()->insertItem( tr("&Go"), go );
00113     menuBar()->insertItem( tr( "History" ), hist );
00114     menuBar()->insertItem( tr( "Bookmarks" ), bookm );
00115     menuBar()->insertSeparator();
00116     menuBar()->insertItem( tr("&Help"), help );
00117 
00118     menuBar()->setItemEnabled( forwardId, FALSE);
00119     menuBar()->setItemEnabled( backwardId, FALSE);
00120     connect( browser, SIGNAL( backwardAvailable( bool ) ),
00121         this, SLOT( setBackwardAvailable( bool ) ) );
00122     connect( browser, SIGNAL( forwardAvailable( bool ) ),
00123         this, SLOT( setForwardAvailable( bool ) ) );
00124 
00125 
00126     QToolBar* toolbar = new QToolBar( this );
00127     addToolBar( toolbar, "Toolbar");
00128     QToolButton* button;
00129 
00130     button = new QToolButton( icon_back, tr("Backward"), "", browser, SLOT(backward()), toolbar );
00131     connect( browser, SIGNAL( backwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
00132     button->setEnabled( FALSE );
00133     button = new QToolButton( icon_forward, tr("Forward"), "", browser, SLOT(forward()), toolbar );
00134     connect( browser, SIGNAL( forwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
00135     button->setEnabled( FALSE );
00136     button = new QToolButton( icon_home, tr("Home"), "", browser, SLOT(home()), toolbar );
00137 
00138     toolbar->addSeparator();
00139 
00140     pathCombo = new QComboBox( TRUE, toolbar );
00141     connect( pathCombo, SIGNAL( activated( const QString & ) ),
00142         this, SLOT( pathSelected( const QString & ) ) );
00143     toolbar->setStretchableWidget( pathCombo );
00144     setRightJustification( TRUE );
00145     setDockEnabled( DockLeft, FALSE );
00146     setDockEnabled( DockRight, FALSE );
00147 
00148     pathCombo->insertItem( home_ );
00149     browser->setFocus();
00150 }
00151 
00152 void TGo4HelpWindow::setBackwardAvailable( bool b)
00153 {
00154     menuBar()->setItemEnabled( backwardId, b);
00155 }
00156 
00157 void TGo4HelpWindow::setForwardAvailable( bool b)
00158 {
00159     menuBar()->setItemEnabled( forwardId, b);
00160 }
00161 
00162 void TGo4HelpWindow::textChanged()
00163 {
00164     browser->mimeSourceFactory()->setFilePath( _path );
00165     if ( browser->documentTitle().isNull() )
00166    setCaption( "Go4 - Helpviewer - " + browser->context() );
00167     else
00168    setCaption( "Go4 - Helpviewer - " + browser->documentTitle() ) ;
00169 
00170     selectedURL = browser->context();
00171 
00172     if ( !selectedURL.isEmpty() && pathCombo ) {
00173    bool exists = FALSE;
00174    int i;
00175    for ( i = 0; i < pathCombo->count(); ++i ) {
00176        if ( pathCombo->text( i ) == selectedURL ) {
00177       exists = TRUE;
00178       break;
00179        }
00180    }
00181    if ( !exists ) {
00182        pathCombo->insertItem( selectedURL, 0 );
00183        pathCombo->setCurrentItem( 0 );
00184        mHistory[ hist->insertItem( selectedURL ) ] = selectedURL;
00185    } else
00186        pathCombo->setCurrentItem( i );
00187    selectedURL = QString::null;
00188     }
00189 }
00190 
00191 TGo4HelpWindow::~TGo4HelpWindow()
00192 {
00193     history.clear();
00194     QMap<int, QString>::Iterator it = mHistory.begin();
00195     for ( ; it != mHistory.end(); ++it )
00196    history.append( *it );
00197 
00198     QFile f( QDir::currentDirPath() + "/.history" );
00199     f.open( IO_WriteOnly );
00200     QDataStream s( &f );
00201     s << history;
00202     f.close();
00203 
00204     bookmarks.clear();
00205     QMap<int, QString>::Iterator it2 = mBookmarks.begin();
00206     for ( ; it2 != mBookmarks.end(); ++it2 )
00207    bookmarks.append( *it2 );
00208 
00209     QFile f2( QDir::currentDirPath() + "/.bookmarks" );
00210     f2.open( IO_WriteOnly );
00211     QDataStream s2( &f2 );
00212     s2 << bookmarks;
00213     f2.close();
00214 }
00215 
00216 void TGo4HelpWindow::about()
00217 {
00218     QMessageBox::about( this, "Go4 Online Help",
00219          "Go4 Online Help"
00220          );
00221 }
00222 
00223 void TGo4HelpWindow::aboutQt()
00224 {
00225   QMessageBox::aboutQt( this, "QBrowser" );
00226 }
00227 
00228 void TGo4HelpWindow::print()
00229 {
00230 #ifndef QT_NO_PRINTER
00231     QPrinter printer;
00232     printer.setFullPage(TRUE);
00233     if ( printer.setup( this ) ) {
00234    QPainter p( &printer );
00235    QPaintDeviceMetrics metrics(p.device());
00236    int dpix = metrics.logicalDpiX();
00237    int dpiy = metrics.logicalDpiY();
00238    const int margin = 72; // pt
00239    QRect body(margin*dpix/72, margin*dpiy/72,
00240          metrics.width()-margin*dpix/72*2,
00241          metrics.height()-margin*dpiy/72*2 );
00242    QSimpleRichText richText( browser->text(), QFont(), browser->context(), browser->styleSheet(),
00243               browser->mimeSourceFactory(), body.height() );
00244    richText.setWidth( &p, body.width() );
00245    QRect view( body );
00246    int page = 1;
00247    do {
00248        richText.draw( &p, body.left(), body.top(), view, colorGroup() );
00249        view.moveBy( 0, body.height() );
00250        p.translate( 0 , -body.height() );
00251        p.drawText( view.right() - p.fontMetrics().width( QString::number(page) ),
00252          view.bottom() + p.fontMetrics().ascent() + 5, QString::number(page) );
00253        if ( view.top()  >= richText.height() )
00254       break;
00255        printer.newPage();
00256        page++;
00257    } while (TRUE);
00258     }
00259 #endif
00260 }
00261 
00262 void TGo4HelpWindow::pathSelected( const QString &_path )
00263 {
00264     browser->setSource( _path );
00265     QMap<int, QString>::Iterator it = mHistory.begin();
00266     bool exists = FALSE;
00267     for ( ; it != mHistory.end(); ++it ) {
00268    if ( *it == _path ) {
00269        exists = TRUE;
00270        break;
00271    }
00272     }
00273     if ( !exists )
00274    mHistory[ hist->insertItem( _path ) ] = _path;
00275 }
00276 
00277 void TGo4HelpWindow::readHistory()
00278 {
00279     if ( QFile::exists( QDir::currentDirPath() + "/.history" ) ) {
00280    QFile f( QDir::currentDirPath() + "/.history" );
00281    f.open( IO_ReadOnly );
00282    QDataStream s( &f );
00283    s >> history;
00284    f.close();
00285    while ( history.count() > 20 )
00286        history.remove( history.begin() );
00287     }
00288 }
00289 
00290 void TGo4HelpWindow::histChosen( int i )
00291 {
00292    if ( mHistory.contains( i ) )
00293      browser->setSource( mHistory[ i ] );
00294 }
00295 
00296 void TGo4HelpWindow::bookmChosen( int i )
00297 {
00298    if ( mBookmarks.contains( i ) )
00299      browser->setSource( mBookmarks[ i ] );
00300 }
00301 
00302 void TGo4HelpWindow::addBookmark()
00303 {
00304     mBookmarks[ bookm->insertItem( caption() ) ] = browser->context();
00305 }
00306 
00307 void TGo4HelpWindow::readBookmarks()
00308 {
00309    if ( QFile::exists( QDir::currentDirPath() + "/.bookmarks" ) ) {
00310       QFile f( QDir::currentDirPath() + "/.bookmarks" );
00311       f.open( IO_ReadOnly );
00312       QDataStream s( &f );
00313       s >> bookmarks;
00314       f.close();
00315    }
00316 }
00317 
00318 
00319 
00320 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:20 2008 for Go4-v3.04-1 by  doxygen 1.4.2