home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / helpviewer / helpwindow.cpp.z / helpwindow.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  9.5 KB  |  332 lines

  1. /****************************************************************************
  2. ** $Id:  qt/helpwindow.cpp   3.0.3   edited Mar 13 14:37 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "helpwindow.h"
  12. #include <qstatusbar.h>
  13. #include <qpixmap.h>
  14. #include <qpopupmenu.h>
  15. #include <qmenubar.h>
  16. #include <qtoolbar.h>
  17. #include <qtoolbutton.h>
  18. #include <qiconset.h>
  19. #include <qfile.h>
  20. #include <qtextstream.h>
  21. #include <qstylesheet.h>
  22. #include <qmessagebox.h>
  23. #include <qfiledialog.h>
  24. #include <qapplication.h>
  25. #include <qcombobox.h>
  26. #include <qevent.h>
  27. #include <qlineedit.h>
  28. #include <qobjectlist.h>
  29. #include <qfileinfo.h>
  30. #include <qfile.h>
  31. #include <qdatastream.h>
  32. #include <qprinter.h>
  33. #include <qsimplerichtext.h>
  34. #include <qpainter.h>
  35. #include <qpaintdevicemetrics.h>
  36.  
  37. #include <ctype.h>
  38.  
  39. HelpWindow::HelpWindow( const QString& home_, const QString& _path,
  40.             QWidget* parent, const char *name )
  41.     : QMainWindow( parent, name, WDestructiveClose ),
  42.       pathCombo( 0 ), selectedURL()
  43. {
  44.     readHistory();
  45.     readBookmarks();
  46.  
  47.     browser = new QTextBrowser( this );
  48.  
  49.     browser->mimeSourceFactory()->setFilePath( _path );
  50.     browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
  51.     connect( browser, SIGNAL( textChanged() ),
  52.          this, SLOT( textChanged() ) );
  53.  
  54.     setCentralWidget( browser );
  55.  
  56.     if ( !home_.isEmpty() )
  57.     browser->setSource( home_ );
  58.  
  59.     connect( browser, SIGNAL( highlighted( const QString&) ),
  60.          statusBar(), SLOT( message( const QString&)) );
  61.  
  62.     resize( 640,700 );
  63.  
  64.     QPopupMenu* file = new QPopupMenu( this );
  65.     file->insertItem( tr("&New Window"), this, SLOT( newWindow() ), CTRL+Key_N );
  66.     file->insertItem( tr("&Open File"), this, SLOT( openFile() ), CTRL+Key_O );
  67.     file->insertItem( tr("&Print"), this, SLOT( print() ), CTRL+Key_P );
  68.     file->insertSeparator();
  69.     file->insertItem( tr("&Close"), this, SLOT( close() ), CTRL+Key_Q );
  70.     file->insertItem( tr("E&xit"), qApp, SLOT( closeAllWindows() ), CTRL+Key_X );
  71.  
  72.     // The same three icons are used twice each.
  73.     QIconSet icon_back( QPixmap("back.xpm") );
  74.     QIconSet icon_forward( QPixmap("forward.xpm") );
  75.     QIconSet icon_home( QPixmap("home.xpm") );
  76.  
  77.     QPopupMenu* go = new QPopupMenu( this );
  78.     backwardId = go->insertItem( icon_back,
  79.                  tr("&Backward"), browser, SLOT( backward() ),
  80.                  CTRL+Key_Left );
  81.     forwardId = go->insertItem( icon_forward,
  82.                 tr("&Forward"), browser, SLOT( forward() ),
  83.                 CTRL+Key_Right );
  84.     go->insertItem( icon_home, tr("&Home"), browser, SLOT( home() ) );
  85.  
  86.     QPopupMenu* help = new QPopupMenu( this );
  87.     help->insertItem( tr("&About ..."), this, SLOT( about() ) );
  88.     help->insertItem( tr("About &Qt ..."), this, SLOT( aboutQt() ) );
  89.  
  90.     hist = new QPopupMenu( this );
  91.     QStringList::Iterator it = history.begin();
  92.     for ( ; it != history.end(); ++it )
  93.     mHistory[ hist->insertItem( *it ) ] = *it;
  94.     connect( hist, SIGNAL( activated( int ) ),
  95.          this, SLOT( histChosen( int ) ) );
  96.  
  97.     bookm = new QPopupMenu( this );
  98.     bookm->insertItem( tr( "Add Bookmark" ), this, SLOT( addBookmark() ) );
  99.     bookm->insertSeparator();
  100.  
  101.     QStringList::Iterator it2 = bookmarks.begin();
  102.     for ( ; it2 != bookmarks.end(); ++it2 )
  103.     mBookmarks[ bookm->insertItem( *it2 ) ] = *it2;
  104.     connect( bookm, SIGNAL( activated( int ) ),
  105.          this, SLOT( bookmChosen( int ) ) );
  106.  
  107.     menuBar()->insertItem( tr("&File"), file );
  108.     menuBar()->insertItem( tr("&Go"), go );
  109.     menuBar()->insertItem( tr( "History" ), hist );
  110.     menuBar()->insertItem( tr( "Bookmarks" ), bookm );
  111.     menuBar()->insertSeparator();
  112.     menuBar()->insertItem( tr("&Help"), help );
  113.  
  114.     menuBar()->setItemEnabled( forwardId, FALSE);
  115.     menuBar()->setItemEnabled( backwardId, FALSE);
  116.     connect( browser, SIGNAL( backwardAvailable( bool ) ),
  117.          this, SLOT( setBackwardAvailable( bool ) ) );
  118.     connect( browser, SIGNAL( forwardAvailable( bool ) ),
  119.          this, SLOT( setForwardAvailable( bool ) ) );
  120.  
  121.  
  122.     QToolBar* toolbar = new QToolBar( this );
  123.     addToolBar( toolbar, "Toolbar");
  124.     QToolButton* button;
  125.  
  126.     button = new QToolButton( icon_back, tr("Backward"), "", browser, SLOT(backward()), toolbar );
  127.     connect( browser, SIGNAL( backwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
  128.     button->setEnabled( FALSE );
  129.     button = new QToolButton( icon_forward, tr("Forward"), "", browser, SLOT(forward()), toolbar );
  130.     connect( browser, SIGNAL( forwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
  131.     button->setEnabled( FALSE );
  132.     button = new QToolButton( icon_home, tr("Home"), "", browser, SLOT(home()), toolbar );
  133.  
  134.     toolbar->addSeparator();
  135.  
  136.     pathCombo = new QComboBox( TRUE, toolbar );
  137.     connect( pathCombo, SIGNAL( activated( const QString & ) ),
  138.          this, SLOT( pathSelected( const QString & ) ) );
  139.     toolbar->setStretchableWidget( pathCombo );
  140.     setRightJustification( TRUE );
  141.     setDockEnabled( DockLeft, FALSE );
  142.     setDockEnabled( DockRight, FALSE );
  143.  
  144.     pathCombo->insertItem( home_ );
  145.     browser->setFocus();
  146.  
  147. }
  148.  
  149.  
  150. void HelpWindow::setBackwardAvailable( bool b)
  151. {
  152.     menuBar()->setItemEnabled( backwardId, b);
  153. }
  154.  
  155. void HelpWindow::setForwardAvailable( bool b)
  156. {
  157.     menuBar()->setItemEnabled( forwardId, b);
  158. }
  159.  
  160.  
  161. void HelpWindow::textChanged()
  162. {
  163.     if ( browser->documentTitle().isNull() )
  164.     setCaption( "Qt Example - Helpviewer - " + browser->context() );
  165.     else
  166.     setCaption( "Qt Example - Helpviewer - " + browser->documentTitle() ) ;
  167.  
  168.     selectedURL = browser->context();
  169.  
  170.     if ( !selectedURL.isEmpty() && pathCombo ) {
  171.     bool exists = FALSE;
  172.     int i;
  173.     for ( i = 0; i < pathCombo->count(); ++i ) {
  174.         if ( pathCombo->text( i ) == selectedURL ) {
  175.         exists = TRUE;
  176.         break;
  177.         }
  178.     }
  179.     if ( !exists ) {
  180.         pathCombo->insertItem( selectedURL, 0 );
  181.         pathCombo->setCurrentItem( 0 );
  182.         mHistory[ hist->insertItem( selectedURL ) ] = selectedURL;
  183.     } else
  184.         pathCombo->setCurrentItem( i );
  185.     selectedURL = QString::null;
  186.     }
  187. }
  188.  
  189. HelpWindow::~HelpWindow()
  190. {
  191.     history.clear();
  192.     QMap<int, QString>::Iterator it = mHistory.begin();
  193.     for ( ; it != mHistory.end(); ++it )
  194.     history.append( *it );
  195.  
  196.     QFile f( QDir::currentDirPath() + "/.history" );
  197.     f.open( IO_WriteOnly );
  198.     QDataStream s( &f );
  199.     s << history;
  200.     f.close();
  201.  
  202.     bookmarks.clear();
  203.     QMap<int, QString>::Iterator it2 = mBookmarks.begin();
  204.     for ( ; it2 != mBookmarks.end(); ++it2 )
  205.     bookmarks.append( *it2 );
  206.  
  207.     QFile f2( QDir::currentDirPath() + "/.bookmarks" );
  208.     f2.open( IO_WriteOnly );
  209.     QDataStream s2( &f2 );
  210.     s2 << bookmarks;
  211.     f2.close();
  212. }
  213.  
  214. void HelpWindow::about()
  215. {
  216.     QMessageBox::about( this, "HelpViewer Example",
  217.             "<p>This example implements a simple HTML help viewer "
  218.             "using Qt's rich text capabilities</p>"
  219.             "<p>It's just about 100 lines of C++ code, so don't expect too much :-)</p>"
  220.             );
  221. }
  222.  
  223.  
  224. void HelpWindow::aboutQt()
  225. {
  226.     QMessageBox::aboutQt( this, "QBrowser" );
  227. }
  228.  
  229. void HelpWindow::openFile()
  230. {
  231. #ifndef QT_NO_FILEDIALOG
  232.     QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this );
  233.     if ( !fn.isEmpty() )
  234.     browser->setSource( fn );
  235. #endif
  236. }
  237.  
  238. void HelpWindow::newWindow()
  239. {
  240.     ( new HelpWindow(browser->source(), "qbrowser") )->show();
  241. }
  242.  
  243. void HelpWindow::print()
  244. {
  245. #ifndef QT_NO_PRINTER
  246.     QPrinter printer;
  247.     printer.setFullPage(TRUE);
  248.     if ( printer.setup( this ) ) {
  249.     QPainter p( &printer );
  250.     QPaintDeviceMetrics metrics(p.device());
  251.     int dpix = metrics.logicalDpiX();
  252.     int dpiy = metrics.logicalDpiY();
  253.     const int margin = 72; // pt
  254.     QRect body(margin*dpix/72, margin*dpiy/72,
  255.            metrics.width()-margin*dpix/72*2,
  256.            metrics.height()-margin*dpiy/72*2 );
  257.     QSimpleRichText richText( browser->text(), QFont(), browser->context(), browser->styleSheet(),
  258.                   browser->mimeSourceFactory(), body.height() );
  259.     richText.setWidth( &p, body.width() );
  260.     QRect view( body );
  261.     int page = 1;
  262.     do {
  263.         richText.draw( &p, body.left(), body.top(), view, colorGroup() );
  264.         view.moveBy( 0, body.height() );
  265.         p.translate( 0 , -body.height() );
  266.         p.drawText( view.right() - p.fontMetrics().width( QString::number(page) ),
  267.             view.bottom() + p.fontMetrics().ascent() + 5, QString::number(page) );
  268.         if ( view.top()  >= richText.height() )
  269.         break;
  270.         printer.newPage();
  271.         page++;
  272.     } while (TRUE);
  273.     }
  274. #endif
  275. }
  276.  
  277. void HelpWindow::pathSelected( const QString &_path )
  278. {
  279.     browser->setSource( _path );
  280.     QMap<int, QString>::Iterator it = mHistory.begin();
  281.     bool exists = FALSE;
  282.     for ( ; it != mHistory.end(); ++it ) {
  283.     if ( *it == _path ) {
  284.         exists = TRUE;
  285.         break;
  286.     }
  287.     }
  288.     if ( !exists )
  289.     mHistory[ hist->insertItem( _path ) ] = _path;
  290. }
  291.  
  292. void HelpWindow::readHistory()
  293. {
  294.     if ( QFile::exists( QDir::currentDirPath() + "/.history" ) ) {
  295.     QFile f( QDir::currentDirPath() + "/.history" );
  296.     f.open( IO_ReadOnly );
  297.     QDataStream s( &f );
  298.     s >> history;
  299.     f.close();
  300.     while ( history.count() > 20 )
  301.         history.remove( history.begin() );
  302.     }
  303. }
  304.  
  305. void HelpWindow::readBookmarks()
  306. {
  307.     if ( QFile::exists( QDir::currentDirPath() + "/.bookmarks" ) ) {
  308.     QFile f( QDir::currentDirPath() + "/.bookmarks" );
  309.     f.open( IO_ReadOnly );
  310.     QDataStream s( &f );
  311.     s >> bookmarks;
  312.     f.close();
  313.     }
  314. }
  315.  
  316. void HelpWindow::histChosen( int i )
  317. {
  318.     if ( mHistory.contains( i ) )
  319.     browser->setSource( mHistory[ i ] );
  320. }
  321.  
  322. void HelpWindow::bookmChosen( int i )
  323. {
  324.     if ( mBookmarks.contains( i ) )
  325.     browser->setSource( mBookmarks[ i ] );
  326. }
  327.  
  328. void HelpWindow::addBookmark()
  329. {
  330.     mBookmarks[ bookm->insertItem( caption() ) ] = browser->context();
  331. }
  332.