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

helpwindow.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: helpwindow.cpp,v 1.4 2005/02/19 17:26:15 zecke Exp $
00003 **
00004 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00005 **
00006 ** This file is part of an example program for Qt.  This example
00007 ** program may be used, distributed and modified without limitation.
00008 **
00009     copyright            : (C) 2000 -2004 by llornkcor
00010     email                : ljp@llornkcor.com
00011 *****************************************************************************/
00012 
00013 #include "helpwindow.h"
00014 
00015 #include <qpe/global.h>
00016 
00017 #include <qstatusbar.h>
00018 
00019 #include <qmenubar.h>
00020 #include <qtoolbar.h>
00021 #include <qtoolbutton.h>
00022 #include <qcombobox.h>
00023 
00024 #ifndef QT_NO_FILEDIALOG
00025 #include <qfiledialog.h>
00026 #endif
00027 
00028 #include <ctype.h>
00029 
00030 HelpWindow::HelpWindow( const QString& home_, const QString&, QWidget* parent, const char *name )
00031     : QMainWindow( parent, name, WDestructiveClose ), pathCombo( 0 ), selectedURL()
00032 {
00033    QString local_library = Global::applicationFileName("gutenbrowser", QString::null);
00034 //        readHistory();
00035 //        readBookmarks();
00036 
00037     browser = new QTextBrowser( this );
00038     QStringList Strlist;
00039     Strlist.append( home_);
00040     browser->mimeSourceFactory()->setFilePath( Strlist );
00041 
00042     browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00043 
00044     connect(browser,SIGNAL(textChanged()),this,SLOT(textChanged()));
00045 
00046     setCentralWidget( browser );
00047 
00048     if ( !home_.isEmpty() )
00049 
00051         browser->setSource( home_ );
00052 
00054     connect( browser, SIGNAL( highlighted( const QString&) ),
00055        statusBar(), SLOT( message( const QString&)) );
00056 
00057 //    resize( 640,600 );
00058 #ifdef Q_WS_QWS
00059     setGeometry( 0,0,236,280);
00060 #else
00061     setGeometry( 10,30,520,420 );
00062 //  resize(520,420);
00063 #endif
00064 
00065     QPopupMenu* file = new QPopupMenu( this );
00066 //    file->insertItem( tr("&New Window"), this, SLOT( newWindow() ), ALT | Key_N );
00067     file->insertItem( tr("&Open File"), this, SLOT( openFile() ), ALT | Key_O );
00068 //    file->insertItem( tr("&Print"), this, SLOT( print() ), ALT | Key_P );
00069     file->insertSeparator();
00070     file->insertItem( tr("&Close"), this, SLOT( close() ), ALT | Key_Q );
00071 //    file->insertItem( tr("E&xit"), qApp, SLOT( closeAllWindows() ), ALT | Key_X );
00072 
00073     // The same three icons are used twice each.
00075     QString pixs=(QDir::homeDirPath ()) +"/Applications/gutenbrowser/pix/";
00076     QIconSet icon_back( QPixmap(pixs+"back.png") );
00077     QIconSet icon_forward( QPixmap(pixs+"forward.png") );
00078     QIconSet icon_home( QPixmap(pixs+"home.png") );
00079 
00080     QPopupMenu* go = new QPopupMenu( this );
00081     backwardId = go->insertItem( icon_back, tr("&Backward"), browser, SLOT( backward() ), ALT | Key_Left );
00082     forwardId = go->insertItem( icon_forward, tr("&Forward"), browser, SLOT( forward() ), ALT | Key_Right );
00083     go->insertItem( icon_home, tr("&Home"), browser, SLOT( home() ) );
00084 
00085 //      QPopupMenu* help = new QPopupMenu( this );
00086 //      help->insertItem( tr("&About ..."), this, SLOT( about() ) );
00087 //    help->insertItem( tr("About &Qt ..."), this, SLOT( aboutQt() ) );
00088 
00089 
00090     hist = new QPopupMenu( this );
00091     QStringList::Iterator it = history.begin();
00092     for ( ; it != history.end(); ++it )
00093   mHistory[ hist->insertItem( *it ) ] = *it;
00094     connect( hist, SIGNAL( activated( int ) ), this, SLOT( histChosen( int ) ) );
00095 
00096     bookm = new QPopupMenu( this );
00097     bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) );
00098     bookm->insertSeparator();
00099 
00100     QStringList::Iterator it2 = bookmarks.begin();
00101     for ( ; it2 != bookmarks.end(); ++it2 )
00102   mBookmarks[ bookm->insertItem( *it2 ) ] = *it2;
00103     connect( bookm, SIGNAL( activated( int ) ),
00104        this, SLOT( bookmChosen( int ) ) );
00105 
00106     menuBar()->insertItem( tr("&File"), file );
00107     menuBar()->insertItem( tr("&Go"), go );
00108     menuBar()->insertItem( tr( "History" ), hist );
00109     menuBar()->insertItem( tr( "Bookmarks" ), bookm );
00110 //      menuBar()->insertSeparator();
00111 //      menuBar()->insertItem( tr("&Help"), help );
00112 
00113     menuBar()->setItemEnabled( forwardId, FALSE);
00114     menuBar()->setItemEnabled( backwardId, FALSE);
00115     connect( browser, SIGNAL( backwardAvailable( bool ) ), this, SLOT( setBackwardAvailable( bool ) ) );
00116     connect( browser, SIGNAL( forwardAvailable( bool ) ), this, SLOT( setForwardAvailable( bool ) ) );
00117 
00118 
00119     QToolBar* toolbar = new QToolBar( this );
00120     addToolBar( toolbar, "Toolbar");
00121     QToolButton* button;
00122 
00123     button = new QToolButton( icon_back, tr("Backward"), "", browser, SLOT(backward()), toolbar );
00124     connect( browser, SIGNAL( backwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
00125     button->setEnabled( FALSE );
00126     button = new QToolButton( icon_forward, tr("Forward"), "", browser, SLOT(forward()), toolbar );
00127     connect( browser, SIGNAL( forwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
00128     button->setEnabled( FALSE );
00129     button = new QToolButton( icon_home, tr("Home"), "", browser, SLOT(home()), toolbar );
00130 
00131     toolbar->addSeparator();
00132 
00133     pathCombo = new QComboBox( TRUE, toolbar );
00134     connect( pathCombo, SIGNAL( activated( const QString & ) ), this, SLOT( pathSelected( const QString & ) ) );
00135     toolbar->setStretchableWidget( pathCombo );
00136 
00137 //    pathCombo->setMaximumWidth(190);
00138 //     setRightJustification( TRUE );
00139 //      setDockEnabled( Left, FALSE );
00140 //      setDockEnabled( Right, FALSE );
00141 
00142     pathCombo->insertItem( home_ );
00143 
00144     browser->setFocus();
00145 
00146 
00147 }
00148 
00149 
00150 void HelpWindow::setBackwardAvailable( bool b)
00151 {
00152     menuBar()->setItemEnabled( backwardId, b);
00153 }
00154 
00155 void HelpWindow::setForwardAvailable( bool b)
00156 {
00157     menuBar()->setItemEnabled( forwardId, b);
00158 }
00159 
00160 
00161 void HelpWindow::textChanged()
00162 {
00163     if ( browser->documentTitle().isNull() ) {
00164   setCaption( "Gutenbrowser - Helpviewer - " + browser->context() );
00165   selectedURL = browser->context();
00166     }
00167     else {
00168   setCaption( "Gutenbrowser - Helpviewer - " + browser->documentTitle() ) ;
00169   selectedURL = browser->documentTitle();
00170     }
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 HelpWindow::~HelpWindow()
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 HelpWindow::about()
00217 //  {
00218 //      QMessageBox::about( this, "Gutenbrowser", "<p>Thanks to Trolltech for this</p>" );
00219 //  }
00220 
00221 //  void HelpWindow::aboutQt()
00222 //  {
00223 //      QMessageBox::aboutQt( this, "QBrowser" );
00224 //  }
00225 
00226 void HelpWindow::openFile()
00227 {
00228 #ifndef QT_NO_FILEDIALOG
00229     QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this );
00230     if ( !fn.isEmpty() )
00231   browser->setSource( fn );
00232 #endif
00233 }
00234 
00235 void HelpWindow::newWindow()
00236 {
00237     ( new HelpWindow(browser->source(), "qbrowser") )->show();
00238 }
00239 
00240 void HelpWindow::print()
00241 {
00242 #ifndef QT_NO_PRINTER
00243     QPrinter printer;
00244     printer.setFullPage(TRUE);
00245     if ( printer.setup() ) {
00246   QPainter p( &printer );
00247   QPaintDeviceMetrics metrics(p.device());
00248   int dpix = metrics.logicalDpiX();
00249   int dpiy = metrics.logicalDpiY();
00250   const int margin = 72; // pt
00251   QRect body(margin*dpix/72, margin*dpiy/72,
00252        metrics.width()-margin*dpix/72*2,
00253        metrics.height()-margin*dpiy/72*2 );
00254   QFont font("times", 10);
00255   QSimpleRichText richText( browser->text(), font, browser->context(), browser->styleSheet(),
00256           browser->mimeSourceFactory(), body.height() );
00257   richText.setWidth( &p, body.width() );
00258   QRect view( body );
00259   int page = 1;
00260   do {
00261       p.setClipRect( body );
00262       richText.draw( &p, body.left(), body.top(), view, colorGroup() );
00263       p.setClipping( FALSE );
00264       view.moveBy( 0, body.height() );
00265       p.translate( 0 , -body.height() );
00266       p.setFont( font );
00267       p.drawText( view.right() - p.fontMetrics().width( QString::number(page) ),
00268       view.bottom() + p.fontMetrics().ascent() + 5, QString::number(page) );
00269       if ( view.top()  >= richText.height() )
00270     break;
00271       printer.newPage();
00272       page++;
00273   } while (TRUE);
00274     }
00275 #endif
00276 }
00277 
00278 void HelpWindow::pathSelected( const QString &_path )
00279 {
00280     browser->setSource( _path );
00281     QMap<int, QString>::Iterator it = mHistory.begin();
00282     bool exists = FALSE;
00283     for ( ; it != mHistory.end(); ++it ) {
00284   if ( *it == _path ) {
00285       exists = TRUE;
00286       break;
00287   }
00288     }
00289     if ( !exists )
00290   mHistory[ hist->insertItem( _path ) ] = _path;
00291 }
00292 
00293 void HelpWindow::readHistory()
00294 {
00295     if ( QFile::exists( QDir::currentDirPath() + "/.history" ) ) {
00296   QFile f( QDir::currentDirPath() + "/.history" );
00297   f.open( IO_ReadOnly );
00298   QDataStream s( &f );
00299   s >> history;
00300   f.close();
00301   while ( history.count() > 20 )
00302       history.remove( history.begin() );
00303     }
00304 }
00305 
00306 void HelpWindow::readBookmarks()
00307 {
00308     if ( QFile::exists( QDir::currentDirPath() + "/.bookmarks" ) ) {
00309   QFile f( QDir::currentDirPath() + "/.bookmarks" );
00310   f.open( IO_ReadOnly );
00311   QDataStream s( &f );
00312   s >> bookmarks;
00313   f.close();
00314     }
00315 }
00316 
00317 void HelpWindow::histChosen( int i )
00318 {
00319     if ( mHistory.contains( i ) )
00320   browser->setSource( mHistory[ i ] );
00321 }
00322 
00323 void HelpWindow::bookmChosen( int i )
00324 {
00325     if ( mBookmarks.contains( i ) )
00326   browser->setSource( mBookmarks[ i ] );
00327 }
00328 
00329 void HelpWindow::addBookmark()
00330 {
00331     mBookmarks[ bookm->insertItem( caption() ) ] = caption();
00332 }
00333 

Generated on Sat Nov 5 16:16:52 2005 for OPIE by  doxygen 1.4.2