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