~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/app_templates/qmakeapp/qmakeapp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{CPP_TEMPLATE}
 
2
 
 
3
#include "%{APPNAMELC}.h"
 
4
 
 
5
#include <qimage.h>
 
6
#include <qpixmap.h>
 
7
#include <qtoolbar.h>
 
8
#include <qtoolbutton.h>
 
9
#include <qpopupmenu.h>
 
10
#include <qmenubar.h>
 
11
#include <qtextedit.h>
 
12
#include <qfile.h>
 
13
#include <qfiledialog.h>
 
14
#include <qstatusbar.h>
 
15
#include <qmessagebox.h>
 
16
#include <qprinter.h>
 
17
#include <qapplication.h>
 
18
#include <qaccel.h>
 
19
#include <qtextstream.h>
 
20
#include <qpainter.h>
 
21
#include <qpaintdevicemetrics.h>
 
22
#include <qwhatsthis.h>
 
23
 
 
24
#include "filesave.xpm"
 
25
#include "fileopen.xpm"
 
26
#include "fileprint.xpm"
 
27
 
 
28
%{APPNAME}::%{APPNAME}()
 
29
    : QMainWindow( 0, "%{APPNAME}", WDestructiveClose )
 
30
{
 
31
    printer = new QPrinter;
 
32
    QPixmap openIcon, saveIcon, printIcon;
 
33
 
 
34
    QToolBar * fileTools = new QToolBar( this, "file operations" );
 
35
    fileTools->setLabel( tr("File Operations") );
 
36
 
 
37
    openIcon = QPixmap( fileopen );
 
38
    QToolButton * fileOpen
 
39
        = new QToolButton( openIcon, tr("Open File"), QString::null,
 
40
                           this, SLOT(choose()), fileTools, "open file" );
 
41
 
 
42
    saveIcon = QPixmap( filesave );
 
43
    QToolButton * fileSave
 
44
        = new QToolButton( saveIcon, tr("Save File"), QString::null,
 
45
                           this, SLOT(save()), fileTools, "save file" );
 
46
 
 
47
    printIcon = QPixmap( fileprint );
 
48
    QToolButton * filePrint
 
49
        = new QToolButton( printIcon, tr("Print File"), QString::null,
 
50
                           this, SLOT(print()), fileTools, "print file" );
 
51
 
 
52
 
 
53
    (void)QWhatsThis::whatsThisButton( fileTools );
 
54
 
 
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>");
 
59
 
 
60
    QWhatsThis::add( fileOpen, fileOpenText );
 
61
 
 
62
    QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon );
 
63
 
 
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>");
 
68
 
 
69
    QWhatsThis::add( fileSave, fileSaveText );
 
70
 
 
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.");
 
74
 
 
75
    QWhatsThis::add( filePrint, filePrintText );
 
76
 
 
77
 
 
78
    QPopupMenu * file = new QPopupMenu( this );
 
79
    menuBar()->insertItem( tr("&File"), file );
 
80
 
 
81
 
 
82
    file->insertItem( tr("&New"), this, SLOT(newDoc()), CTRL+Key_N );
 
83
 
 
84
    int id;
 
85
    id = file->insertItem( openIcon, tr("&Open..."),
 
86
                           this, SLOT(choose()), CTRL+Key_O );
 
87
    file->setWhatsThis( id, fileOpenText );
 
88
 
 
89
    id = file->insertItem( saveIcon, tr("&Save"),
 
90
                           this, SLOT(save()), CTRL+Key_S );
 
91
    file->setWhatsThis( id, fileSaveText );
 
92
 
 
93
    id = file->insertItem( tr("Save &As..."), this, SLOT(saveAs()) );
 
94
    file->setWhatsThis( id, fileSaveText );
 
95
 
 
96
    file->insertSeparator();
 
97
 
 
98
    id = file->insertItem( printIcon, tr("&Print..."),
 
99
                           this, SLOT(print()), CTRL+Key_P );
 
100
    file->setWhatsThis( id, filePrintText );
 
101
 
 
102
    file->insertSeparator();
 
103
 
 
104
    file->insertItem( tr("&Close"), this, SLOT(close()), CTRL+Key_W );
 
105
 
 
106
    file->insertItem( tr("&Quit"), qApp, SLOT( closeAllWindows() ), CTRL+Key_Q );
 
107
 
 
108
    menuBar()->insertSeparator();
 
109
 
 
110
    QPopupMenu * help = new QPopupMenu( this );
 
111
    menuBar()->insertItem( tr("&Help"), help );
 
112
 
 
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 );
 
117
 
 
118
    e = new QTextEdit( this, "editor" );
 
119
    e->setFocus();
 
120
    setCentralWidget( e );
 
121
    statusBar()->message( tr("Ready"), 2000 );
 
122
 
 
123
    resize( 450, 600 );
 
124
}
 
125
 
 
126
 
 
127
%{APPNAME}::~%{APPNAME}()
 
128
{
 
129
    delete printer;
 
130
}
 
131
 
 
132
 
 
133
 
 
134
void %{APPNAME}::newDoc()
 
135
{
 
136
    %{APPNAME} *ed = new %{APPNAME};
 
137
    ed->setCaption(tr("Qt Example - Application"));
 
138
    ed->show();
 
139
}
 
140
 
 
141
void %{APPNAME}::choose()
 
142
{
 
143
    QString fn = QFileDialog::getOpenFileName( QString::null, QString::null,
 
144
                                               this);
 
145
    if ( !fn.isEmpty() )
 
146
        load( fn );
 
147
    else
 
148
        statusBar()->message( tr("Loading aborted"), 2000 );
 
149
}
 
150
 
 
151
 
 
152
void %{APPNAME}::load( const QString &fileName )
 
153
{
 
154
    QFile f( fileName );
 
155
    if ( !f.open( IO_ReadOnly ) )
 
156
        return;
 
157
 
 
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 );
 
163
}
 
164
 
 
165
 
 
166
void %{APPNAME}::save()
 
167
{
 
168
    if ( filename.isEmpty() ) {
 
169
        saveAs();
 
170
        return;
 
171
    }
 
172
 
 
173
    QString text = e->text();
 
174
    QFile f( filename );
 
175
    if ( !f.open( IO_WriteOnly ) ) {
 
176
        statusBar()->message( tr("Could not write to %1").arg(filename),
 
177
                              2000 );
 
178
        return;
 
179
    }
 
180
 
 
181
    QTextStream t( &f );
 
182
    t << text;
 
183
    f.close();
 
184
 
 
185
    e->setModified( FALSE );
 
186
 
 
187
    setCaption( filename );
 
188
 
 
189
    statusBar()->message( tr( "File %1 saved" ).arg( filename ), 2000 );
 
190
}
 
191
 
 
192
 
 
193
void %{APPNAME}::saveAs()
 
194
{
 
195
    QString fn = QFileDialog::getSaveFileName( QString::null, QString::null,
 
196
                                               this );
 
197
    if ( !fn.isEmpty() ) {
 
198
        filename = fn;
 
199
        save();
 
200
    } else {
 
201
        statusBar()->message( tr("Saving aborted"), 2000 );
 
202
    }
 
203
}
 
204
 
 
205
 
 
206
void %{APPNAME}::print()
 
207
{
 
208
    // ###### Rewrite to use QSimpleRichText to print here as well
 
209
    const int Margin = 10;
 
210
    int pageNo = 1;
 
211
 
 
212
    if ( printer->setup(this) ) {               // printer dialog
 
213
        statusBar()->message( tr("Printing...") );
 
214
        QPainter p;
 
215
        if( !p.begin( printer ) )               // paint on printer
 
216
            return;
 
217
 
 
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 );
 
227
                msg += ")...";
 
228
                statusBar()->message( msg );
 
229
                printer->newPage();             // no more room on this page
 
230
                yPos = 0;                       // back to top of page
 
231
            }
 
232
            p.drawText( Margin, Margin + yPos,
 
233
                        metrics.width(), fm.lineSpacing(),
 
234
                        ExpandTabs | DontClip,
 
235
                        e->text( i ) );
 
236
            yPos = yPos + fm.lineSpacing();
 
237
        }
 
238
        p.end();                                // send job to printer
 
239
        statusBar()->message( tr("Printing completed"), 2000 );
 
240
    } else {
 
241
        statusBar()->message( tr("Printing aborted"), 2000 );
 
242
    }
 
243
}
 
244
 
 
245
void %{APPNAME}::closeEvent( QCloseEvent* ce )
 
246
{
 
247
    if ( !e->isModified() ) {
 
248
        ce->accept();
 
249
        return;
 
250
    }
 
251
 
 
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"),
 
256
                                      0, 1 ) ) {
 
257
    case 0:
 
258
        save();
 
259
        ce->accept();
 
260
        break;
 
261
    case 1:
 
262
        ce->accept();
 
263
        break;
 
264
    case 2:
 
265
    default: // just for sanity
 
266
        ce->ignore();
 
267
        break;
 
268
    }
 
269
}
 
270
 
 
271
 
 
272
void %{APPNAME}::about()
 
273
{
 
274
    QMessageBox::about( this, tr("Qt Application Example"),
 
275
                        tr("This example demonstrates simple use of "
 
276
                        "QMainWindow,\nQMenuBar and QToolBar."));
 
277
}
 
278
 
 
279
 
 
280
void %{APPNAME}::aboutQt()
 
281
{
 
282
    QMessageBox::aboutQt( this, tr("Qt Application Example") );
 
283
}