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