source: vendor/trolltech/current/examples/application/application.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

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