3
#include "%{APPNAMELC}.h"
8
#include <qtoolbutton.h>
9
#include <qpopupmenu.h>
11
#include <qtextedit.h>
13
#include <qfiledialog.h>
14
#include <qstatusbar.h>
15
#include <qmessagebox.h>
17
#include <qapplication.h>
19
#include <qtextstream.h>
21
#include <qpaintdevicemetrics.h>
22
#include <qwhatsthis.h>
24
#include "filesave.xpm"
25
#include "fileopen.xpm"
26
#include "fileprint.xpm"
28
%{APPNAME}::%{APPNAME}()
29
: QMainWindow( 0, "%{APPNAME}", WDestructiveClose )
31
printer = new QPrinter;
32
QPixmap openIcon, saveIcon, printIcon;
34
QToolBar * fileTools = new QToolBar( this, "file operations" );
35
fileTools->setLabel( tr("File Operations") );
37
openIcon = QPixmap( fileopen );
38
QToolButton * fileOpen
39
= new QToolButton( openIcon, tr("Open File"), QString::null,
40
this, SLOT(choose()), fileTools, "open file" );
42
saveIcon = QPixmap( filesave );
43
QToolButton * fileSave
44
= new QToolButton( saveIcon, tr("Save File"), QString::null,
45
this, SLOT(save()), fileTools, "save file" );
47
printIcon = QPixmap( fileprint );
48
QToolButton * filePrint
49
= new QToolButton( printIcon, tr("Print File"), QString::null,
50
this, SLOT(print()), fileTools, "print file" );
53
(void)QWhatsThis::whatsThisButton( fileTools );
55
QString fileOpenText = tr("<p><img source=\"fileopen\"> "
56
"Click this button to open a <em>new file</em>. <br>"
57
"You can also select the <b>Open</b> command "
58
"from the <b>File</b> menu.</p>");
60
QWhatsThis::add( fileOpen, fileOpenText );
62
QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon );
64
QString fileSaveText = tr("<p>Click this button to save the file you "
65
"are editing. You will be prompted for a file name.\n"
66
"You can also select the <b>Save</b> command "
67
"from the <b>File</b> menu.</p>");
69
QWhatsThis::add( fileSave, fileSaveText );
71
QString filePrintText = tr("Click this button to print the file you "
72
"are editing.\n You can also select the Print "
73
"command from the File menu.");
75
QWhatsThis::add( filePrint, filePrintText );
78
QPopupMenu * file = new QPopupMenu( this );
79
menuBar()->insertItem( tr("&File"), file );
82
file->insertItem( tr("&New"), this, SLOT(newDoc()), CTRL+Key_N );
85
id = file->insertItem( openIcon, tr("&Open..."),
86
this, SLOT(choose()), CTRL+Key_O );
87
file->setWhatsThis( id, fileOpenText );
89
id = file->insertItem( saveIcon, tr("&Save"),
90
this, SLOT(save()), CTRL+Key_S );
91
file->setWhatsThis( id, fileSaveText );
93
id = file->insertItem( tr("Save &As..."), this, SLOT(saveAs()) );
94
file->setWhatsThis( id, fileSaveText );
96
file->insertSeparator();
98
id = file->insertItem( printIcon, tr("&Print..."),
99
this, SLOT(print()), CTRL+Key_P );
100
file->setWhatsThis( id, filePrintText );
102
file->insertSeparator();
104
file->insertItem( tr("&Close"), this, SLOT(close()), CTRL+Key_W );
106
file->insertItem( tr("&Quit"), qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );
108
menuBar()->insertSeparator();
110
QPopupMenu * help = new QPopupMenu( this );
111
menuBar()->insertItem( tr("&Help"), help );
113
help->insertItem( tr("&About"), this, SLOT(about()), Key_F1 );
114
help->insertItem( tr("About &Qt"), this, SLOT(aboutQt()) );
115
help->insertSeparator();
116
help->insertItem( tr("What's &This"), this, SLOT(whatsThis()), SHIFT+Key_F1 );
118
e = new QTextEdit( this, "editor" );
120
setCentralWidget( e );
121
statusBar()->message( tr("Ready"), 2000 );
127
%{APPNAME}::~%{APPNAME}()
134
void %{APPNAME}::newDoc()
136
%{APPNAME} *ed = new %{APPNAME};
137
ed->setCaption(tr("Qt Example - Application"));
141
void %{APPNAME}::choose()
143
QString fn = QFileDialog::getOpenFileName( QString::null, QString::null,
148
statusBar()->message( tr("Loading aborted"), 2000 );
152
void %{APPNAME}::load( const QString &fileName )
155
if ( !f.open( IO_ReadOnly ) )
158
QTextStream ts( &f );
159
e->setText( ts.read() );
160
e->setModified( FALSE );
161
setCaption( fileName );
162
statusBar()->message( tr("Loaded document %1").arg(fileName), 2000 );
166
void %{APPNAME}::save()
168
if ( filename.isEmpty() ) {
173
QString text = e->text();
175
if ( !f.open( IO_WriteOnly ) ) {
176
statusBar()->message( tr("Could not write to %1").arg(filename),
185
e->setModified( FALSE );
187
setCaption( filename );
189
statusBar()->message( tr( "File %1 saved" ).arg( filename ), 2000 );
193
void %{APPNAME}::saveAs()
195
QString fn = QFileDialog::getSaveFileName( QString::null, QString::null,
197
if ( !fn.isEmpty() ) {
201
statusBar()->message( tr("Saving aborted"), 2000 );
206
void %{APPNAME}::print()
208
// ###### Rewrite to use QSimpleRichText to print here as well
209
const int Margin = 10;
212
if ( printer->setup(this) ) { // printer dialog
213
statusBar()->message( tr("Printing...") );
215
if( !p.begin( printer ) ) // paint on printer
218
p.setFont( e->font() );
219
int yPos = 0; // y-position for each line
220
QFontMetrics fm = p.fontMetrics();
221
QPaintDeviceMetrics metrics( printer ); // need width/height
222
// of printer surface
223
for( int i = 0 ; i < e->lines() ; i++ ) {
224
if ( Margin + yPos > metrics.height() - Margin ) {
225
QString msg( "Printing (page " );
226
msg += QString::number( ++pageNo );
228
statusBar()->message( msg );
229
printer->newPage(); // no more room on this page
230
yPos = 0; // back to top of page
232
p.drawText( Margin, Margin + yPos,
233
metrics.width(), fm.lineSpacing(),
234
ExpandTabs | DontClip,
236
yPos = yPos + fm.lineSpacing();
238
p.end(); // send job to printer
239
statusBar()->message( tr("Printing completed"), 2000 );
241
statusBar()->message( tr("Printing aborted"), 2000 );
245
void %{APPNAME}::closeEvent( QCloseEvent* ce )
247
if ( !e->isModified() ) {
252
switch( QMessageBox::information( this, tr("Qt Application Example"),
253
tr("Do you want to save the changes"
254
" to the document?"),
255
tr("Yes"), tr("No"), tr("Cancel"),
265
default: // just for sanity
272
void %{APPNAME}::about()
274
QMessageBox::about( this, tr("Qt Application Example"),
275
tr("This example demonstrates simple use of "
276
"QMainWindow,\nQMenuBar and QToolBar."));
280
void %{APPNAME}::aboutQt()
282
QMessageBox::aboutQt( this, tr("Qt Application Example") );