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