diff options
author | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <[email protected]> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /examples/application | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'examples/application')
-rw-r--r-- | examples/application/application.cpp | 294 | ||||
-rw-r--r-- | examples/application/application.doc | 34 | ||||
-rw-r--r-- | examples/application/application.h | 46 | ||||
-rw-r--r-- | examples/application/application.pro | 11 | ||||
-rw-r--r-- | examples/application/fileopen.xpm | 22 | ||||
-rw-r--r-- | examples/application/fileprint.xpm | 24 | ||||
-rw-r--r-- | examples/application/filesave.xpm | 22 | ||||
-rw-r--r-- | examples/application/main.cpp | 20 |
8 files changed, 473 insertions, 0 deletions
diff --git a/examples/application/application.cpp b/examples/application/application.cpp new file mode 100644 index 0000000..2435a5d --- /dev/null +++ b/examples/application/application.cpp @@ -0,0 +1,294 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "application.h" + +#include <qimage.h> +#include <qpixmap.h> +#include <qtoolbar.h> +#include <qtoolbutton.h> +#include <qpopupmenu.h> +#include <qmenubar.h> +#include <qtextedit.h> +#include <qfile.h> +#include <qfiledialog.h> +#include <qstatusbar.h> +#include <qmessagebox.h> +#include <qprinter.h> +#include <qapplication.h> +#include <qaccel.h> +#include <qtextstream.h> +#include <qpainter.h> +#include <qpaintdevicemetrics.h> +#include <qwhatsthis.h> +#include <qsimplerichtext.h> + +#include "filesave.xpm" +#include "fileopen.xpm" +#include "fileprint.xpm" + +ApplicationWindow::ApplicationWindow() + : QMainWindow( 0, "example application main window", WDestructiveClose | WGroupLeader ) +{ + printer = new QPrinter( QPrinter::HighResolution ); + QPixmap openIcon, saveIcon, printIcon; + + QToolBar * fileTools = new QToolBar( this, "file operations" ); + fileTools->setLabel( "File Operations" ); + + openIcon = QPixmap( fileopen ); + QToolButton * fileOpen + = new QToolButton( openIcon, "Open File", QString::null, + this, SLOT(choose()), fileTools, "open file" ); + + saveIcon = QPixmap( filesave ); + QToolButton * fileSave + = new QToolButton( saveIcon, "Save File", QString::null, + this, SLOT(save()), fileTools, "save file" ); + + printIcon = QPixmap( fileprint ); + QToolButton * filePrint + = new QToolButton( printIcon, "Print File", QString::null, + this, SLOT(print()), fileTools, "print file" ); + + + (void)QWhatsThis::whatsThisButton( fileTools ); + + const char * fileOpenText = "<p><img source=\"fileopen\"> " + "Click this button to open a <em>new file</em>.<br>" + "You can also select the <b>Open</b> command " + "from the <b>File</b> menu.</p>"; + + QWhatsThis::add( fileOpen, fileOpenText ); + + QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon ); + + const char * fileSaveText = "<p>Click this button to save the file you " + "are editing. You will be prompted for a file name.\n" + "You can also select the <b>Save</b> command " + "from the <b>File</b> menu.</p>"; + + QWhatsThis::add( fileSave, fileSaveText ); + + const char * filePrintText = "Click this button to print the file you " + "are editing.\n" + "You can also select the Print command " + "from the File menu."; + + QWhatsThis::add( filePrint, filePrintText ); + + + QPopupMenu * file = new QPopupMenu( this ); + menuBar()->insertItem( "&File", file ); + + + file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N ); + + int id; + id = file->insertItem( openIcon, "&Open...", + this, SLOT(choose()), CTRL+Key_O ); + file->setWhatsThis( id, fileOpenText ); + + id = file->insertItem( saveIcon, "&Save", + this, SLOT(save()), CTRL+Key_S ); + file->setWhatsThis( id, fileSaveText ); + + id = file->insertItem( "Save &As...", this, SLOT(saveAs()) ); + file->setWhatsThis( id, fileSaveText ); + + file->insertSeparator(); + + id = file->insertItem( printIcon, "&Print...", + this, SLOT(print()), CTRL+Key_P ); + file->setWhatsThis( id, filePrintText ); + + file->insertSeparator(); + + file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W ); + + file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q ); + + menuBar()->insertSeparator(); + + QPopupMenu * help = new QPopupMenu( this ); + menuBar()->insertItem( "&Help", help ); + + help->insertItem( "&About", this, SLOT(about()), Key_F1 ); + help->insertItem( "About &Qt", this, SLOT(aboutQt()) ); + help->insertSeparator(); + help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 ); + + e = new QTextEdit( this, "editor" ); + e->setFocus(); + setCentralWidget( e ); + statusBar()->message( "Ready", 2000 ); + + resize( 450, 600 ); +} + + +ApplicationWindow::~ApplicationWindow() +{ + delete printer; +} + + + +void ApplicationWindow::newDoc() +{ + ApplicationWindow *ed = new ApplicationWindow; + ed->setCaption("Qt Example - Application"); + ed->show(); +} + +void ApplicationWindow::choose() +{ + QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, + this); + if ( !fn.isEmpty() ) + load( fn ); + else + statusBar()->message( "Loading aborted", 2000 ); +} + + +void ApplicationWindow::load( const QString &fileName ) +{ + QFile f( fileName ); + if ( !f.open( IO_ReadOnly ) ) + return; + + QTextStream ts( &f ); + e->setText( ts.read() ); + e->setModified( FALSE ); + setCaption( fileName ); + statusBar()->message( "Loaded document " + fileName, 2000 ); +} + + +void ApplicationWindow::save() +{ + if ( filename.isEmpty() ) { + saveAs(); + return; + } + + QString text = e->text(); + QFile f( filename ); + if ( !f.open( IO_WriteOnly ) ) { + statusBar()->message( QString("Could not write to %1").arg(filename), + 2000 ); + return; + } + + QTextStream t( &f ); + t << text; + f.close(); + + e->setModified( FALSE ); + + setCaption( filename ); + + statusBar()->message( QString( "File %1 saved" ).arg( filename ), 2000 ); +} + + +void ApplicationWindow::saveAs() +{ + QString fn = QFileDialog::getSaveFileName( QString::null, QString::null, + this ); + if ( !fn.isEmpty() ) { + filename = fn; + save(); + } else { + statusBar()->message( "Saving aborted", 2000 ); + } +} + + +void ApplicationWindow::print() +{ + printer->setFullPage( TRUE ); + if ( printer->setup(this) ) { // printer dialog + statusBar()->message( "Printing..." ); + QPainter p; + if( !p.begin( printer ) ) { // paint on printer + statusBar()->message( "Printing aborted", 2000 ); + return; + } + + QPaintDeviceMetrics metrics( p.device() ); + int dpiy = metrics.logicalDpiY(); + int margin = (int) ( (2/2.54)*dpiy ); // 2 cm margins + QRect view( margin, margin, metrics.width() - 2*margin, metrics.height() - 2*margin ); + QSimpleRichText richText( QStyleSheet::convertFromPlainText(e->text()), + QFont(), + e->context(), + e->styleSheet(), + e->mimeSourceFactory(), + view.height() ); + richText.setWidth( &p, view.width() ); + int page = 1; + do { + richText.draw( &p, margin, margin, view, colorGroup() ); + view.moveBy( 0, view.height() ); + p.translate( 0 , -view.height() ); + p.drawText( view.right() - p.fontMetrics().width( QString::number( page ) ), + view.bottom() + p.fontMetrics().ascent() + 5, QString::number( page ) ); + if ( view.top() - margin >= richText.height() ) + break; + printer->newPage(); + page++; + } while (TRUE); + + statusBar()->message( "Printing completed", 2000 ); + } else { + statusBar()->message( "Printing aborted", 2000 ); + } +} + +void ApplicationWindow::closeEvent( QCloseEvent* ce ) +{ + if ( !e->isModified() ) { + ce->accept(); + return; + } + + switch( QMessageBox::information( this, "Qt Application Example", + "Do you want to save the changes" + " to the document?", + "Yes", "No", "Cancel", + 0, 1 ) ) { + case 0: + save(); + ce->accept(); + break; + case 1: + ce->accept(); + break; + case 2: + default: // just for sanity + ce->ignore(); + break; + } +} + + +void ApplicationWindow::about() +{ + QMessageBox::about( this, "Qt Application Example", + "This example demonstrates simple use of " + "QMainWindow,\nQMenuBar and QToolBar."); +} + + +void ApplicationWindow::aboutQt() +{ + QMessageBox::aboutQt( this, "Qt Application Example" ); +} diff --git a/examples/application/application.doc b/examples/application/application.doc new file mode 100644 index 0000000..731bda0 --- /dev/null +++ b/examples/application/application.doc @@ -0,0 +1,34 @@ +/* +*/ + +/*! \page simple-application-example.html + + \ingroup examples + + \title A Complete Application Window + + This example program looks like a complete modern application. It + has a menu bar, it has a tool bar, it has a status bar and works + like a simple text editor. + + There is a <a href="simple-application.html">walkthrough</a> of + this example. + + <hr> + + Header file: + + \include application/application.h + + <hr> + + Implementation: + + \include application/application.cpp + + <hr> + + Main: + + \include application/main.cpp +*/ diff --git a/examples/application/application.h b/examples/application/application.h new file mode 100644 index 0000000..5448aa1 --- /dev/null +++ b/examples/application/application.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#ifndef APPLICATION_H +#define APPLICATION_H + +#include <qmainwindow.h> + +class QTextEdit; + +class ApplicationWindow: public QMainWindow +{ + Q_OBJECT + +public: + ApplicationWindow(); + ~ApplicationWindow(); + +protected: + void closeEvent( QCloseEvent* ); + +private slots: + void newDoc(); + void choose(); + void load( const QString &fileName ); + void save(); + void saveAs(); + void print(); + + void about(); + void aboutQt(); + +private: + QPrinter *printer; + QTextEdit *e; + QString filename; +}; + + +#endif diff --git a/examples/application/application.pro b/examples/application/application.pro new file mode 100644 index 0000000..7f10912 --- /dev/null +++ b/examples/application/application.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = application + +CONFIG += qt warn_on release +DEPENDPATH = ../../include + +REQUIRES = full-config + +HEADERS = application.h +SOURCES = application.cpp \ + main.cpp diff --git a/examples/application/fileopen.xpm b/examples/application/fileopen.xpm new file mode 100644 index 0000000..880417e --- /dev/null +++ b/examples/application/fileopen.xpm @@ -0,0 +1,22 @@ +/* XPM */ +static const char *fileopen[] = { +" 16 13 5 1", +". c #040404", +"# c #808304", +"a c None", +"b c #f3f704", +"c c #f3f7f3", +"aaaaaaaaa...aaaa", +"aaaaaaaa.aaa.a.a", +"aaaaaaaaaaaaa..a", +"a...aaaaaaaa...a", +".bcb.......aaaaa", +".cbcbcbcbc.aaaaa", +".bcbcbcbcb.aaaaa", +".cbcb...........", +".bcb.#########.a", +".cb.#########.aa", +".b.#########.aaa", +"..#########.aaaa", +"...........aaaaa" +}; diff --git a/examples/application/fileprint.xpm b/examples/application/fileprint.xpm new file mode 100644 index 0000000..6ada912 --- /dev/null +++ b/examples/application/fileprint.xpm @@ -0,0 +1,24 @@ +/* XPM */ +static const char *fileprint[] = { +" 16 14 6 1", +". c #000000", +"# c #848284", +"a c #c6c3c6", +"b c #ffff00", +"c c #ffffff", +"d c None", +"ddddd.........dd", +"dddd.cccccccc.dd", +"dddd.c.....c.ddd", +"ddd.cccccccc.ddd", +"ddd.c.....c....d", +"dd.cccccccc.a.a.", +"d..........a.a..", +".aaaaaaaaaa.a.a.", +".............aa.", +".aaaaaa###aa.a.d", +".aaaaaabbbaa...d", +".............a.d", +"d.aaaaaaaaa.a.dd", +"dd...........ddd" +}; diff --git a/examples/application/filesave.xpm b/examples/application/filesave.xpm new file mode 100644 index 0000000..bd6870f --- /dev/null +++ b/examples/application/filesave.xpm @@ -0,0 +1,22 @@ +/* XPM */ +static const char *filesave[] = { +" 14 14 4 1", +". c #040404", +"# c #808304", +"a c #bfc2bf", +"b c None", +"..............", +".#.aaaaaaaa.a.", +".#.aaaaaaaa...", +".#.aaaaaaaa.#.", +".#.aaaaaaaa.#.", +".#.aaaaaaaa.#.", +".#.aaaaaaaa.#.", +".##........##.", +".############.", +".##.........#.", +".##......aa.#.", +".##......aa.#.", +".##......aa.#.", +"b............." +}; diff --git a/examples/application/main.cpp b/examples/application/main.cpp new file mode 100644 index 0000000..517b63d --- /dev/null +++ b/examples/application/main.cpp @@ -0,0 +1,20 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include "application.h" + +int main( int argc, char ** argv ) { + QApplication a( argc, argv ); + ApplicationWindow *mw = new ApplicationWindow(); + mw->setCaption( "Qt Example - Application" ); + mw->show(); + a.connect( &a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()) ); + return a.exec(); +} |