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 **
00003 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00004 **
00005 ** This file is part of an example program for Qt.  This example
00006 ** program may be used, distributed and modified without limitation.
00007 **
00008 *****************************************************************************/
00009 
00010 #include "helpwindow.h"
00011 #include <qstatusbar.h>
00012 
00013 #include <qmenubar.h>
00014 #include <qtoolbar.h>
00015 #include <qtoolbutton.h>
00016 #include <qcombobox.h>
00017 
00018 #ifndef QT_NO_FILEDIALOG
00019 #include <qfiledialog.h>
00020 #endif
00021 
00022 #include <ctype.h>
00023 
00024 HelpWindow::HelpWindow( const QString& home_, const QString& _path, QWidget* parent, const char *name )
00025     : QMainWindow( parent, name, WDestructiveClose ), pathCombo( 0 ), selectedURL()
00026 {
00027     readHistory();
00028     readBookmarks();
00029 
00030     browser = new QTextBrowser( this );
00031     QStringList Strlist;
00032     Strlist.append( home_);
00033     
00034     browser->mimeSourceFactory()->setFilePath( Strlist );
00035 
00036     browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00037 
00038     connect( browser, SIGNAL( textChanged() ),
00039 
00040              this, SLOT( textChanged() ) );
00041 
00042     setCentralWidget( browser );
00043 
00044     if ( !home_.isEmpty() )
00045 
00047         browser->setSource( home_ );
00048 
00050     connect( browser, SIGNAL( highlighted(const QString&) ),
00051        statusBar(), SLOT( message(const QString&)) );
00052 
00053     setGeometry( 0,0,236,280);
00054 
00055     QPopupMenu* file = new QPopupMenu( this );
00056 //    file->insertItem( tr("&New Window"), this, SLOT( newWindow() ), ALT | Key_N );
00057     file->insertItem( tr("&Open File"), this, SLOT( openFile() ), ALT | Key_O );
00058 //    file->insertItem( tr("&Print"), this, SLOT( print() ), ALT | Key_P );
00059     file->insertSeparator();
00060     file->insertItem( tr("&Close"), this, SLOT( close() ), ALT | Key_Q );
00061 //    file->insertItem( tr("E&xit"), qApp, SLOT( closeAllWindows() ), ALT | Key_X );
00062 
00063     // The same three icons are used twice each.
00065     QString pixs=(QDir::homeDirPath ()) +"/Applications/gutenbrowser/pix/";
00066     QIconSet icon_back( QPixmap(pixs+"back.png") );
00067     QIconSet icon_forward( QPixmap(pixs+"forward.png") );
00068     QIconSet icon_home( QPixmap(pixs+"home.png") );
00069 
00070     QPopupMenu* go = new QPopupMenu( this );
00071     backwardId = go->insertItem( icon_back, tr("&Backward"), browser, SLOT( backward() ), ALT | Key_Left );
00072     forwardId = go->insertItem( icon_forward, tr("&Forward"), browser, SLOT( forward() ), ALT | Key_Right );
00073     go->insertItem( icon_home, tr("&Home"), browser, SLOT( home() ) );
00074 
00075 
00076     hist = new QPopupMenu( this );
00077     QStringList::Iterator it = history.begin();
00078     for ( ; it != history.end(); ++it )
00079     mHistory[ hist->insertItem( *it ) ] = *it;
00080     connect( hist, SIGNAL( activated(int) ), this, SLOT( histChosen(int) ) );
00081 
00082     bookm = new QPopupMenu( this );
00083     bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) );
00084     bookm->insertSeparator();
00085 
00086     QStringList::Iterator it2 = bookmarks.begin();
00087     for ( ; it2 != bookmarks.end(); ++it2 )
00088    mBookmarks[ bookm->insertItem( *it2 ) ] = *it2;
00089     connect( bookm, SIGNAL( activated(int) ),
00090        this, SLOT( bookmChosen(int) ) );
00091 
00092     menuBar()->insertItem( tr("&File"), file );
00093     menuBar()->insertItem( tr("&Go"), go );
00094     menuBar()->insertItem( tr( "History" ), hist );
00095     menuBar()->insertItem( tr( "Bookmarks" ), bookm );
00096 //      menuBar()->insertSeparator();
00097 //      menuBar()->insertItem( tr("&Help"), help );
00098 
00099     menuBar()->setItemEnabled( forwardId, FALSE);
00100     menuBar()->setItemEnabled( backwardId, FALSE);
00101     connect( browser, SIGNAL( backwardAvailable(bool) ), this, SLOT( setBackwardAvailable(bool) ) );
00102     connect( browser, SIGNAL( forwardAvailable(bool) ), this, SLOT( setForwardAvailable(bool) ) );
00103 
00104 
00105     QToolBar* toolbar = new QToolBar( this );
00106     addToolBar( toolbar, "Toolbar");
00107     QToolButton* button;
00108 
00109     button = new QToolButton( icon_back, tr("Backward"), "", browser, SLOT(backward()), toolbar );
00110     connect( browser, SIGNAL( backwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
00111     button->setEnabled( FALSE );
00112     button = new QToolButton( icon_forward, tr("Forward"), "", browser, SLOT(forward()), toolbar );
00113     connect( browser, SIGNAL( forwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
00114     button->setEnabled( FALSE );
00115     button = new QToolButton( icon_home, tr("Home"), "", browser, SLOT(home()), toolbar );
00116 
00117     toolbar->addSeparator();
00118 
00119     pathCombo = new QComboBox( TRUE, toolbar );
00120     connect( pathCombo, SIGNAL( activated(const QString&) ), this, SLOT( pathSelected(const QString&) ) );
00121     toolbar->setStretchableWidget( pathCombo );
00122 
00123 //    pathCombo->setMaximumWidth(190);
00124 //     setRightJustification( TRUE );
00125 //      setDockEnabled( Left, FALSE );
00126 //      setDockEnabled( Right, FALSE );
00127 
00128     pathCombo->insertItem( home_ );
00129 
00130     browser->setFocus();
00131 
00132 
00133 }
00134 
00135 
00136 void HelpWindow::setBackwardAvailable( bool b)
00137 {
00138     menuBar()->setItemEnabled( backwardId, b);
00139 }
00140 
00141 void HelpWindow::setForwardAvailable( bool b)
00142 {
00143     menuBar()->setItemEnabled( forwardId, b);
00144 }
00145 
00146 
00147 void HelpWindow::textChanged()
00148 {
00149     if ( browser->documentTitle().isNull() ) {
00150   setCaption( "Stockticker Lookup - " + browser->context() );
00151   selectedURL = browser->context();
00152     }
00153     else {
00154   setCaption( "Stockticker Lookup - " + browser->documentTitle() ) ;
00155   selectedURL = browser->documentTitle();
00156     }
00157 
00158     if ( !selectedURL.isEmpty() && pathCombo ) {
00159   bool exists = FALSE;
00160   int i;
00161   for ( i = 0; i < pathCombo->count(); ++i ) {
00162       if ( pathCombo->text( i ) == selectedURL ) {
00163     exists = TRUE;
00164     break;
00165       }
00166   }
00167   if ( !exists ) {
00168       pathCombo->insertItem( selectedURL, 0 );
00169       pathCombo->setCurrentItem( 0 );
00170       mHistory[ hist->insertItem( selectedURL ) ] = selectedURL;
00171   } else
00172       pathCombo->setCurrentItem( i );
00173   selectedURL = QString::null;
00174     }
00175 }
00176 
00177 HelpWindow::~HelpWindow()
00178 {
00179     history.clear();
00180     QMap<int, QString>::Iterator it = mHistory.begin();
00181     for ( ; it != mHistory.end(); ++it )
00182   history.append( *it );
00183 
00184     QFile f( QDir::currentDirPath() + "/.history" );
00185     f.open( IO_WriteOnly );
00186     QDataStream s( &f );
00187     s << history;
00188     f.close();
00189 
00190     bookmarks.clear();
00191     QMap<int, QString>::Iterator it2 = mBookmarks.begin();
00192     for ( ; it2 != mBookmarks.end(); ++it2 )
00193   bookmarks.append( *it2 );
00194 
00195     QFile f2( QDir::currentDirPath() + "/.bookmarks" );
00196     f2.open( IO_WriteOnly );
00197     QDataStream s2( &f2 );
00198     s2 << bookmarks;
00199     f2.close();
00200 }
00201 
00202 void HelpWindow::openFile()
00203 {
00204 #ifndef QT_NO_FILEDIALOG
00205     QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this );
00206     if ( !fn.isEmpty() )
00207   browser->setSource( fn );
00208 #endif
00209 }
00210 
00211 void HelpWindow::newWindow()
00212 {
00213     ( new HelpWindow(browser->source(), "qbrowser") )->show();
00214 }
00215 
00216 void HelpWindow::pathSelected( const QString &_path )
00217 {
00218     browser->setSource( _path );
00219     QMap<int, QString>::Iterator it = mHistory.begin();
00220     bool exists = FALSE;
00221     for ( ; it != mHistory.end(); ++it ) {
00222   if ( *it == _path ) {
00223       exists = TRUE;
00224       break;
00225   }
00226     }
00227     if ( !exists )
00228   mHistory[ hist->insertItem( _path ) ] = _path;
00229 }
00230 
00231 void HelpWindow::readHistory()
00232 {
00233     if ( QFile::exists( QDir::currentDirPath() + "/.history" ) ) {
00234   QFile f( QDir::currentDirPath() + "/.history" );
00235   f.open( IO_ReadOnly );
00236   QDataStream s( &f );
00237   s >> history;
00238   f.close();
00239   while ( history.count() > 20 )
00240       history.remove( history.begin() );
00241     }
00242 }
00243 
00244 void HelpWindow::readBookmarks()
00245 {
00246     if ( QFile::exists( QDir::currentDirPath() + "/.bookmarks" ) ) {
00247   QFile f( QDir::currentDirPath() + "/.bookmarks" );
00248   f.open( IO_ReadOnly );
00249   QDataStream s( &f );
00250   s >> bookmarks;
00251   f.close();
00252     }
00253 }
00254 
00255 void HelpWindow::histChosen( int i )
00256 {
00257     if ( mHistory.contains( i ) )
00258   browser->setSource( mHistory[ i ] );
00259 }
00260 
00261 void HelpWindow::bookmChosen( int i )
00262 {
00263     if ( mBookmarks.contains( i ) )
00264   browser->setSource( mBookmarks[ i ] );
00265 }
00266 
00267 void HelpWindow::addBookmark()
00268 {
00269     mBookmarks[ bookm->insertItem( caption() ) ] = caption();
00270 }
00271 

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