1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/****************************************************************************
**
** 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 <qworkspace.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qmovie.h>
#include <qfile.h>
#include <qfiledialog.h>
#include <qlabel.h>
#include <qstatusbar.h>
#include <qmessagebox.h>
#include <qprinter.h>
#include <qapplication.h>
#include <qpushbutton.h>
#include <qaccel.h>
#include <qtextstream.h>
#include <qtextedit.h>
#include <qpainter.h>
#include <qpaintdevicemetrics.h>
#include <qwhatsthis.h>
#include <qobjectlist.h>
#include <qvbox.h>
#include <qsimplerichtext.h>
#include "filesave.xpm"
#include "fileopen.xpm"
#include "fileprint.xpm"
const char * fileOpenText = "Click this button to open a <em>new file</em>. <br><br>"
"You can also select the <b>Open command</b> from the File menu.";
const char * fileSaveText = "Click this button to save the file you are "
"editing. You will be prompted for a file name.\n\n"
"You can also select the Save command from the File menu.\n\n"
"Note that implementing this function is left as an exercise for the reader.";
const char * filePrintText = "Click this button to print the file you "
"are editing.\n\n"
"You can also select the Print command from the File menu.";
ApplicationWindow::ApplicationWindow()
: QMainWindow( 0, "example application main window", WDestructiveClose )
{
int id;
QPixmap openIcon, saveIcon;
fileTools = new QToolBar( this, "file operations" );
addToolBar( fileTools, tr( "File Operations" ), DockTop, TRUE );
openIcon = QPixmap( fileopen );
QToolButton * fileOpen
= new QToolButton( openIcon, "Open File", QString::null,
this, SLOT(load()), fileTools, "open file" );
saveIcon = QPixmap( filesave );
QToolButton * fileSave
= new QToolButton( saveIcon, "Save File", QString::null,
this, SLOT(save()), fileTools, "save file" );
#ifndef QT_NO_PRINTER
printer = new QPrinter( QPrinter::HighResolution );
QPixmap printIcon;
printIcon = QPixmap( fileprint );
QToolButton * filePrint
= new QToolButton( printIcon, "Print File", QString::null,
this, SLOT(print()), fileTools, "print file" );
QWhatsThis::add( filePrint, filePrintText );
#endif
(void)QWhatsThis::whatsThisButton( fileTools );
QWhatsThis::add( fileOpen, fileOpenText );
QWhatsThis::add( fileSave, fileSaveText );
QPopupMenu * file = new QPopupMenu( this );
menuBar()->insertItem( "&File", file );
file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N );
id = file->insertItem( openIcon, "&Open...",
this, SLOT(load()), 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 );
#ifndef QT_NO_PRINTER
file->insertSeparator();
id = file->insertItem( printIcon, "&Print...",
this, SLOT(print()), CTRL+Key_P );
file->setWhatsThis( id, filePrintText );
#endif
file->insertSeparator();
file->insertItem( "&Close", this, SLOT(closeWindow()), CTRL+Key_W );
file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );
windowsMenu = new QPopupMenu( this );
windowsMenu->setCheckable( TRUE );
connect( windowsMenu, SIGNAL( aboutToShow() ),
this, SLOT( windowsMenuAboutToShow() ) );
menuBar()->insertItem( "&Windows", windowsMenu );
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);
QVBox* vb = new QVBox( this );
vb->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
ws = new QWorkspace( vb );
ws->setScrollBarsEnabled( TRUE );
setCentralWidget( vb );
statusBar()->message( "Ready", 2000 );
}
ApplicationWindow::~ApplicationWindow()
{
#ifndef QT_NO_PRINTER
delete printer;
#endif
}
MDIWindow* ApplicationWindow::newDoc()
{
MDIWindow* w = new MDIWindow( ws, 0, WDestructiveClose );
connect( w, SIGNAL( message(const QString&, int) ), statusBar(), SLOT( message(const QString&, int )) );
w->setCaption("unnamed document");
w->setIcon( QPixmap("document.xpm") );
// show the very first window in maximized mode
if ( ws->windowList().isEmpty() )
w->showMaximized();
else
w->show();
return w;
}
void ApplicationWindow::load()
{
QString fn = QFileDialog::getOpenFileName( QString::null, QString::null, this );
if ( !fn.isEmpty() ) {
MDIWindow* w = newDoc();
w->load( fn );
} else {
statusBar()->message( "Loading aborted", 2000 );
}
}
void ApplicationWindow::save()
{
MDIWindow* m = (MDIWindow*)ws->activeWindow();
if ( m )
m->save();
}
void ApplicationWindow::saveAs()
{
MDIWindow* m = (MDIWindow*)ws->activeWindow();
if ( m )
m->saveAs();
}
void ApplicationWindow::print()
{
#ifndef QT_NO_PRINTER
MDIWindow* m = (MDIWindow*)ws->activeWindow();
if ( m )
m->print( printer );
#endif
}
void ApplicationWindow::closeWindow()
{
MDIWindow* m = (MDIWindow*)ws->activeWindow();
if ( m )
m->close();
}
void ApplicationWindow::about()
{
QMessageBox::about( this, "Qt Application Example",
"This example demonstrates simple use of\n "
"Qt's Multiple Document Interface (MDI).");
}
void ApplicationWindow::aboutQt()
{
QMessageBox::aboutQt( this, "Qt Application Example" );
}
void ApplicationWindow::windowsMenuAboutToShow()
{
windowsMenu->clear();
int cascadeId = windowsMenu->insertItem("&Cascade", ws, SLOT(cascade() ) );
int tileId = windowsMenu->insertItem("&Tile", ws, SLOT(tile() ) );
int horTileId = windowsMenu->insertItem("Tile &Horizontally", this, SLOT(tileHorizontal() ) );
if ( ws->windowList().isEmpty() ) {
windowsMenu->setItemEnabled( cascadeId, FALSE );
windowsMenu->setItemEnabled( tileId, FALSE );
windowsMenu->setItemEnabled( horTileId, FALSE );
}
windowsMenu->insertSeparator();
QWidgetList windows = ws->windowList();
for ( int i = 0; i < int(windows.count()); ++i ) {
int id = windowsMenu->insertItem(windows.at(i)->caption(),
this, SLOT( windowsMenuActivated( int ) ) );
windowsMenu->setItemParameter( id, i );
windowsMenu->setItemChecked( id, ws->activeWindow() == windows.at(i) );
}
}
void ApplicationWindow::windowsMenuActivated( int id )
{
QWidget* w = ws->windowList().at( id );
if ( w )
w->showNormal();
w->setFocus();
}
void ApplicationWindow::tileHorizontal()
{
// primitive horizontal tiling
QWidgetList windows = ws->windowList();
if ( !windows.count() )
return;
if (ws->activeWindow())
ws->activeWindow()->showNormal();
int heightForEach = ws->height() / windows.count();
int y = 0;
for ( int i = 0; i < int(windows.count()); ++i ) {
QWidget *window = windows.at(i);
int preferredHeight = window->minimumHeight()+window->parentWidget()->baseSize().height();
int actHeight = QMAX(heightForEach, preferredHeight);
window->parentWidget()->setGeometry( 0, y, ws->width(), actHeight );
y += actHeight;
}
}
void ApplicationWindow::closeEvent( QCloseEvent *e )
{
QWidgetList windows = ws->windowList();
if ( windows.count() ) {
for ( int i = 0; i < int(windows.count()); ++i ) {
QWidget *window = windows.at( i );
if ( !window->close() ) {
e->ignore();
return;
}
}
}
QMainWindow::closeEvent( e );
}
MDIWindow::MDIWindow( QWidget* parent, const char* name, int wflags )
: QMainWindow( parent, name, wflags )
{
mmovie = 0;
medit = new QTextEdit( this );
setFocusProxy( medit );
setCentralWidget( medit );
}
MDIWindow::~MDIWindow()
{
delete mmovie;
}
void MDIWindow::closeEvent( QCloseEvent *e )
{
if ( medit->isModified() ) {
switch( QMessageBox::warning( this, "Save Changes",
tr("Save changes to %1?").arg( caption() ),
tr("Yes"), tr("No"), tr("Cancel") ) ) {
case 0:
{
save();
if ( !filename.isEmpty() )
e->accept();
else
e->ignore();
}
break;
case 1:
e->accept();
break;
default:
e->ignore();
break;
}
} else {
e->accept();
}
}
void MDIWindow::load( const QString& fn )
{
filename = fn;
QFile f( filename );
if ( !f.open( IO_ReadOnly ) )
return;
if(fn.contains(".gif")) {
QWidget * tmp=new QWidget(this);
setFocusProxy(tmp);
setCentralWidget(tmp);
medit->hide();
delete medit;
QMovie * qm=new QMovie(fn);
#ifdef Q_WS_QWS // temporary speed-test hack
qm->setDisplayWidget(tmp);
#endif
tmp->setBackgroundMode(QWidget::NoBackground);
tmp->show();
mmovie=qm;
} else {
mmovie = 0;
QTextStream t(&f);
QString s = t.read();
medit->setText( s );
f.close();
}
setCaption( filename );
emit message( QString("Loaded document %1").arg(filename), 2000 );
}
void MDIWindow::save()
{
if ( filename.isEmpty() ) {
saveAs();
return;
}
QString text = medit->text();
QFile f( filename );
if ( !f.open( IO_WriteOnly ) ) {
emit message( QString("Could not write to %1").arg(filename),
2000 );
return;
}
QTextStream t( &f );
t << text;
f.close();
setCaption( filename );
emit message( QString( "File %1 saved" ).arg( filename ), 2000 );
}
void MDIWindow::saveAs()
{
QString fn = QFileDialog::getSaveFileName( filename, QString::null, this );
if ( !fn.isEmpty() ) {
filename = fn;
save();
} else {
emit message( "Saving aborted", 2000 );
}
}
void MDIWindow::print( QPrinter* printer)
{
#ifndef QT_NO_PRINTER
int pageNo = 1;
if ( printer->setup(this) ) { // printer dialog
printer->setFullPage( TRUE );
emit message( "Printing...", 0 );
QPainter p;
if ( !p.begin( printer ) )
return; // paint on printer
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(medit->text()),
QFont(),
medit->context(),
medit->styleSheet(),
medit->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;
QString msg( "Printing (page " );
msg += QString::number( ++pageNo );
msg += ")...";
emit message( msg, 0 );
printer->newPage();
page++;
} while (TRUE);
}
#endif
}
|