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

  1. /****************************************************************************
  2. ** $Id:  qt/application.cpp   3.0.0   edited Sep 10 13:46 $
  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 "application.h"
  12.  
  13. #include <qimage.h>
  14. #include <qpixmap.h>
  15. #include <qtoolbar.h>
  16. #include <qtoolbutton.h>
  17. #include <qpopupmenu.h>
  18. #include <qmenubar.h>
  19. #include <qtextedit.h>
  20. #include <qfile.h>
  21. #include <qfiledialog.h>
  22. #include <qstatusbar.h>
  23. #include <qmessagebox.h>
  24. #include <qprinter.h>
  25. #include <qapplication.h>
  26. #include <qaccel.h>
  27. #include <qtextstream.h>
  28. #include <qpainter.h>
  29. #include <qpaintdevicemetrics.h>
  30. #include <qwhatsthis.h>
  31.  
  32. #include "filesave.xpm"
  33. #include "fileopen.xpm"
  34. #include "fileprint.xpm"
  35.  
  36. ApplicationWindow::ApplicationWindow()
  37.     : QMainWindow( 0, "example application main window", WDestructiveClose )
  38. {
  39.     printer = new QPrinter;
  40.     QPixmap openIcon, saveIcon, printIcon;
  41.  
  42.     QToolBar * fileTools = new QToolBar( this, "file operations" );
  43.     fileTools->setLabel( "File Operations" );
  44.  
  45.     openIcon = QPixmap( fileopen );
  46.     QToolButton * fileOpen
  47.     = new QToolButton( openIcon, "Open File", QString::null,
  48.                this, SLOT(choose()), fileTools, "open file" );
  49.  
  50.     saveIcon = QPixmap( filesave );
  51.     QToolButton * fileSave
  52.     = new QToolButton( saveIcon, "Save File", QString::null,
  53.                this, SLOT(save()), fileTools, "save file" );
  54.  
  55.     printIcon = QPixmap( fileprint );
  56.     QToolButton * filePrint
  57.     = new QToolButton( printIcon, "Print File", QString::null,
  58.                this, SLOT(print()), fileTools, "print file" );
  59.  
  60.  
  61.     (void)QWhatsThis::whatsThisButton( fileTools );
  62.  
  63.     const char * fileOpenText = "<p><img source=\"fileopen\"> "
  64.              "Click this button to open a <em>new file</em>. <br>"
  65.                  "You can also select the <b>Open</b> command "
  66.                  "from the <b>File</b> menu.</p>";
  67.  
  68.     QWhatsThis::add( fileOpen, fileOpenText );
  69.  
  70.     QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon );
  71.  
  72.     const char * fileSaveText = "<p>Click this button to save the file you "
  73.                  "are editing. You will be prompted for a file name.\n"
  74.                  "You can also select the <b>Save</b> command "
  75.                  "from the <b>File</b> menu.</p>";
  76.  
  77.     QWhatsThis::add( fileSave, fileSaveText );
  78.  
  79.     const char * filePrintText = "Click this button to print the file you "
  80.                  "are editing.\n You can also select the Print "
  81.                  "command from the File menu.";
  82.  
  83.     QWhatsThis::add( filePrint, filePrintText );
  84.  
  85.  
  86.     QPopupMenu * file = new QPopupMenu( this );
  87.     menuBar()->insertItem( "&File", file );
  88.  
  89.  
  90.     file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N );
  91.  
  92.     int id;
  93.     id = file->insertItem( openIcon, "&Open...",
  94.                this, SLOT(choose()), CTRL+Key_O );
  95.     file->setWhatsThis( id, fileOpenText );
  96.  
  97.     id = file->insertItem( saveIcon, "&Save",
  98.                this, SLOT(save()), CTRL+Key_S );
  99.     file->setWhatsThis( id, fileSaveText );
  100.  
  101.     id = file->insertItem( "Save &As...", this, SLOT(saveAs()) );
  102.     file->setWhatsThis( id, fileSaveText );
  103.  
  104.     file->insertSeparator();
  105.  
  106.     id = file->insertItem( printIcon, "&Print...",
  107.                this, SLOT(print()), CTRL+Key_P );
  108.     file->setWhatsThis( id, filePrintText );
  109.  
  110.     file->insertSeparator();
  111.  
  112.     file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W );
  113.  
  114.     file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );
  115.  
  116.     menuBar()->insertSeparator();
  117.  
  118.     QPopupMenu * help = new QPopupMenu( this );
  119.     menuBar()->insertItem( "&Help", help );
  120.  
  121.     help->insertItem( "&About", this, SLOT(about()), Key_F1 );
  122.     help->insertItem( "About &Qt", this, SLOT(aboutQt()) );
  123.     help->insertSeparator();
  124.     help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 );
  125.  
  126.     e = new QTextEdit( this, "editor" );
  127.     e->setFocus();
  128.     setCentralWidget( e );
  129.     statusBar()->message( "Ready", 2000 );
  130.  
  131.     resize( 450, 600 );
  132. }
  133.  
  134.  
  135. ApplicationWindow::~ApplicationWindow()
  136. {
  137.     delete printer;
  138. }
  139.  
  140.  
  141.  
  142. void ApplicationWindow::newDoc()
  143. {
  144.     ApplicationWindow *ed = new ApplicationWindow;
  145.     ed->setCaption("Qt Example - Application");
  146.     ed->show();
  147. }
  148.  
  149. void ApplicationWindow::choose()
  150. {
  151.     QString fn = QFileDialog::getOpenFileName( QString::null, QString::null,
  152.                            this);
  153.     if ( !fn.isEmpty() )
  154.     load( fn );
  155.     else
  156.     statusBar()->message( "Loading aborted", 2000 );
  157. }
  158.  
  159.  
  160. void ApplicationWindow::load( const QString &fileName )
  161. {
  162.     QFile f( fileName );
  163.     if ( !f.open( IO_ReadOnly ) )
  164.     return;
  165.  
  166.     QTextStream ts( &f );
  167.     e->setText( ts.read() );
  168.     e->setModified( FALSE );
  169.     setCaption( fileName );
  170.     statusBar()->message( "Loaded document " + fileName, 2000 );
  171. }
  172.  
  173.  
  174. void ApplicationWindow::save()
  175. {
  176.     if ( filename.isEmpty() ) {
  177.     saveAs();
  178.     return;
  179.     }
  180.  
  181.     QString text = e->text();
  182.     QFile f( filename );
  183.     if ( !f.open( IO_WriteOnly ) ) {
  184.     statusBar()->message( QString("Could not write to %1").arg(filename),
  185.                   2000 );
  186.     return;
  187.     }
  188.  
  189.     QTextStream t( &f );
  190.     t << text;
  191.     f.close();
  192.  
  193.     e->setModified( FALSE );
  194.  
  195.     setCaption( filename );
  196.  
  197.     statusBar()->message( QString( "File %1 saved" ).arg( filename ), 2000 );
  198. }
  199.  
  200.  
  201. void ApplicationWindow::saveAs()
  202. {
  203.     QString fn = QFileDialog::getSaveFileName( QString::null, QString::null,
  204.                            this );
  205.     if ( !fn.isEmpty() ) {
  206.     filename = fn;
  207.     save();
  208.     } else {
  209.     statusBar()->message( "Saving aborted", 2000 );
  210.     }
  211. }
  212.  
  213.  
  214. void ApplicationWindow::print()
  215. {
  216.     // ###### Rewrite to use QSimpleRichText to print here as well
  217.     const int Margin = 10;
  218.     int pageNo = 1;
  219.  
  220.     if ( printer->setup(this) ) {        // printer dialog
  221.     statusBar()->message( "Printing..." );
  222.     QPainter p;
  223.     if( !p.begin( printer ) )               // paint on printer
  224.         return;
  225.  
  226.     p.setFont( e->font() );
  227.     int yPos    = 0;            // y-position for each line
  228.     QFontMetrics fm = p.fontMetrics();
  229.     QPaintDeviceMetrics metrics( printer ); // need width/height
  230.                         // of printer surface
  231.     for( int i = 0 ; i < e->lines() ; i++ ) {
  232.         if ( Margin + yPos > metrics.height() - Margin ) {
  233.         QString msg( "Printing (page " );
  234.         msg += QString::number( ++pageNo );
  235.         msg += ")...";
  236.         statusBar()->message( msg );
  237.         printer->newPage();        // no more room on this page
  238.         yPos = 0;            // back to top of page
  239.         }
  240.         p.drawText( Margin, Margin + yPos,
  241.             metrics.width(), fm.lineSpacing(),
  242.             ExpandTabs | DontClip,
  243.             e->text( i ) );
  244.         yPos = yPos + fm.lineSpacing();
  245.     }
  246.     p.end();                // send job to printer
  247.     statusBar()->message( "Printing completed", 2000 );
  248.     } else {
  249.     statusBar()->message( "Printing aborted", 2000 );
  250.     }
  251. }
  252.  
  253. void ApplicationWindow::closeEvent( QCloseEvent* ce )
  254. {
  255.     if ( !e->isModified() ) {
  256.     ce->accept();
  257.     return;
  258.     }
  259.  
  260.     switch( QMessageBox::information( this, "Qt Application Example",
  261.                       "Do you want to save the changes"
  262.                       " to the document?",
  263.                       "Yes", "No", "Cancel",
  264.                       0, 1 ) ) {
  265.     case 0:
  266.     save();
  267.     ce->accept();
  268.     break;
  269.     case 1:
  270.     ce->accept();
  271.     break;
  272.     case 2:
  273.     default: // just for sanity
  274.     ce->ignore();
  275.     break;
  276.     }
  277. }
  278.  
  279.  
  280. void ApplicationWindow::about()
  281. {
  282.     QMessageBox::about( this, "Qt Application Example",
  283.             "This example demonstrates simple use of "
  284.             "QMainWindow,\nQMenuBar and QToolBar.");
  285. }
  286.  
  287.  
  288. void ApplicationWindow::aboutQt()
  289. {
  290.     QMessageBox::aboutQt( this, "Qt Application Example" );
  291. }
  292.