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

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