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