home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / helpviewer / helpwindow.cpp < prev    next >
C/C++ Source or Header  |  2001-10-11  |  10KB  |  353 lines

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