~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/mainwindows-sdi-mainwindow-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
    <title>Qt 4.0: mainwindow.cpp Example File (mainwindows/sdi/mainwindow.cpp)</title>
 
7
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
8
a:link { color: #004faf; text-decoration: none }
 
9
a:visited { color: #672967; text-decoration: none }
 
10
td.postheader { font-family: sans-serif }
 
11
tr.address { font-family: sans-serif }
 
12
body { background: #ffffff; color: black; }</style>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
18
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">mainwindow.cpp Example File<br /><small><small>mainwindows/sdi/mainwindow.cpp</small></small></h1>
 
20
<pre>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
26
    ** This file may be distributed under the terms of the Q Public License
 
27
** as defined by Trolltech AS of Norway and appearing in the file
 
28
** LICENSE.QPL included in the packaging of this file.
 
29
**
 
30
** This file may be distributed and/or modified under the terms of the
 
31
** GNU General Public License version 2 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.
 
34
**
 
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
36
**   information about Qt Commercial License Agreements.
 
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
43
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
44
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
 
 
50
    #include &quot;mainwindow.h&quot;
 
51
 
 
52
    MainWindow::MainWindow()
 
53
    {
 
54
        init();
 
55
        setCurrentFile(&quot;&quot;);
 
56
    }
 
57
 
 
58
    MainWindow::MainWindow(const QString &amp;fileName)
 
59
    {
 
60
        init();
 
61
        loadFile(fileName);
 
62
    }
 
63
 
 
64
    void MainWindow::closeEvent(QCloseEvent *event)
 
65
    {
 
66
        if (maybeSave()) {
 
67
            writeSettings();
 
68
            event-&gt;accept();
 
69
        } else {
 
70
            event-&gt;ignore();
 
71
        }
 
72
    }
 
73
 
 
74
    void MainWindow::newFile()
 
75
    {
 
76
        MainWindow *other = new MainWindow;
 
77
        other-&gt;move(x() + 40, y() + 40);
 
78
        other-&gt;show();
 
79
    }
 
80
 
 
81
    void MainWindow::open()
 
82
    {
 
83
        QString fileName = QFileDialog::getOpenFileName(this);
 
84
        if (!fileName.isEmpty()) {
 
85
            MainWindow *existing = findMainWindow(fileName);
 
86
            if (existing) {
 
87
                existing-&gt;show();
 
88
                existing-&gt;raise();
 
89
                existing-&gt;activateWindow();
 
90
                return;
 
91
            }
 
92
 
 
93
            if (isUntitled &amp;&amp; textEdit-&gt;document()-&gt;isEmpty()
 
94
                    &amp;&amp; !isWindowModified()) {
 
95
                loadFile(fileName);
 
96
            } else {
 
97
                MainWindow *other = new MainWindow(fileName);
 
98
                if (other-&gt;isUntitled) {
 
99
                    delete other;
 
100
                    return;
 
101
                }
 
102
                other-&gt;move(x() + 40, y() + 40);
 
103
                other-&gt;show();
 
104
            }
 
105
        }
 
106
    }
 
107
 
 
108
    bool MainWindow::save()
 
109
    {
 
110
        if (isUntitled) {
 
111
            return saveAs();
 
112
        } else {
 
113
            return saveFile(curFile);
 
114
        }
 
115
    }
 
116
 
 
117
    bool MainWindow::saveAs()
 
118
    {
 
119
        QString fileName = QFileDialog::getSaveFileName(this, tr(&quot;Save As&quot;),
 
120
                                                        curFile);
 
121
        if (fileName.isEmpty())
 
122
            return false;
 
123
 
 
124
        return saveFile(fileName);
 
125
    }
 
126
 
 
127
    void MainWindow::about()
 
128
    {
 
129
       QMessageBox::about(this, tr(&quot;About SDI&quot;),
 
130
                tr(&quot;The &lt;b&gt;SDI&lt;/b&gt; example demonstrates how to write single &quot;
 
131
                   &quot;document interface applications using Qt.&quot;));
 
132
    }
 
133
 
 
134
    void MainWindow::documentWasModified()
 
135
    {
 
136
        setWindowModified(true);
 
137
    }
 
138
 
 
139
    void MainWindow::init()
 
140
    {
 
141
        setAttribute(Qt::WA_DeleteOnClose);
 
142
 
 
143
        isUntitled = true;
 
144
 
 
145
        textEdit = new QTextEdit;
 
146
        setCentralWidget(textEdit);
 
147
 
 
148
        createActions();
 
149
        createMenus();
 
150
        createToolBars();
 
151
        createStatusBar();
 
152
 
 
153
        readSettings();
 
154
 
 
155
        connect(textEdit-&gt;document(), SIGNAL(contentsChanged()),
 
156
                this, SLOT(documentWasModified()));
 
157
    }
 
158
 
 
159
    void MainWindow::createActions()
 
160
    {
 
161
        newAct = new QAction(QIcon(&quot;:/images/new.png&quot;), tr(&quot;&amp;New&quot;), this);
 
162
        newAct-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
 
163
        newAct-&gt;setStatusTip(tr(&quot;Create a new file&quot;));
 
164
        connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 
165
 
 
166
        openAct = new QAction(QIcon(&quot;:/images/open.png&quot;), tr(&quot;&amp;Open...&quot;), this);
 
167
        openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
 
168
        openAct-&gt;setStatusTip(tr(&quot;Open an existing file&quot;));
 
169
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
170
 
 
171
        saveAct = new QAction(QIcon(&quot;:/images/save.png&quot;), tr(&quot;&amp;Save&quot;), this);
 
172
        saveAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
 
173
        saveAct-&gt;setStatusTip(tr(&quot;Save the document to disk&quot;));
 
174
        connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
 
175
 
 
176
        saveAsAct = new QAction(tr(&quot;Save &amp;As...&quot;), this);
 
177
        saveAsAct-&gt;setStatusTip(tr(&quot;Save the document under a new name&quot;));
 
178
        connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
 
179
 
 
180
        closeAct = new QAction(tr(&quot;&amp;Close&quot;), this);
 
181
        closeAct-&gt;setShortcut(tr(&quot;Ctrl+W&quot;));
 
182
        closeAct-&gt;setStatusTip(tr(&quot;Close this window&quot;));
 
183
        connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
 
184
 
 
185
        exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
186
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
187
        exitAct-&gt;setStatusTip(tr(&quot;Exit the application&quot;));
 
188
        connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
 
189
 
 
190
        cutAct = new QAction(QIcon(&quot;:/images/cut.png&quot;), tr(&quot;Cu&amp;t&quot;), this);
 
191
        cutAct-&gt;setShortcut(tr(&quot;Ctrl+X&quot;));
 
192
        cutAct-&gt;setStatusTip(tr(&quot;Cut the current selection's contents to the &quot;
 
193
                                &quot;clipboard&quot;));
 
194
        connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
 
195
 
 
196
        copyAct = new QAction(QIcon(&quot;:/images/copy.png&quot;), tr(&quot;&amp;Copy&quot;), this);
 
197
        copyAct-&gt;setShortcut(tr(&quot;Ctrl+C&quot;));
 
198
        copyAct-&gt;setStatusTip(tr(&quot;Copy the current selection's contents to the &quot;
 
199
                                 &quot;clipboard&quot;));
 
200
        connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
 
201
 
 
202
        pasteAct = new QAction(QIcon(&quot;:/images/paste.png&quot;), tr(&quot;&amp;Paste&quot;), this);
 
203
        pasteAct-&gt;setShortcut(tr(&quot;Ctrl+V&quot;));
 
204
        pasteAct-&gt;setStatusTip(tr(&quot;Paste the clipboard's contents into the current &quot;
 
205
                                  &quot;selection&quot;));
 
206
        connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
 
207
 
 
208
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
209
        aboutAct-&gt;setStatusTip(tr(&quot;Show the application's About box&quot;));
 
210
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
211
 
 
212
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
213
        aboutQtAct-&gt;setStatusTip(tr(&quot;Show the Qt library's About box&quot;));
 
214
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
215
    }
 
216
 
 
217
    void MainWindow::createMenus()
 
218
    {
 
219
        fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
220
        fileMenu-&gt;addAction(newAct);
 
221
        fileMenu-&gt;addAction(openAct);
 
222
        fileMenu-&gt;addAction(saveAct);
 
223
        fileMenu-&gt;addAction(saveAsAct);
 
224
        fileMenu-&gt;addSeparator();
 
225
        fileMenu-&gt;addAction(closeAct);
 
226
        fileMenu-&gt;addAction(exitAct);
 
227
 
 
228
        editMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Edit&quot;));
 
229
        editMenu-&gt;addAction(cutAct);
 
230
        editMenu-&gt;addAction(copyAct);
 
231
        editMenu-&gt;addAction(pasteAct);
 
232
 
 
233
        menuBar()-&gt;addSeparator();
 
234
 
 
235
        helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
 
236
        helpMenu-&gt;addAction(aboutAct);
 
237
        helpMenu-&gt;addAction(aboutQtAct);
 
238
    }
 
239
 
 
240
    void MainWindow::createToolBars()
 
241
    {
 
242
        fileToolBar = addToolBar(tr(&quot;File&quot;));
 
243
        fileToolBar-&gt;addAction(newAct);
 
244
        fileToolBar-&gt;addAction(openAct);
 
245
        fileToolBar-&gt;addAction(saveAct);
 
246
 
 
247
        editToolBar = addToolBar(tr(&quot;Edit&quot;));
 
248
        editToolBar-&gt;addAction(cutAct);
 
249
        editToolBar-&gt;addAction(copyAct);
 
250
        editToolBar-&gt;addAction(pasteAct);
 
251
    }
 
252
 
 
253
    void MainWindow::createStatusBar()
 
254
    {
 
255
        statusBar()-&gt;showMessage(tr(&quot;Ready&quot;));
 
256
    }
 
257
 
 
258
    void MainWindow::readSettings()
 
259
    {
 
260
        QSettings settings(&quot;Trolltech&quot;, &quot;SDI Example&quot;);
 
261
        QPoint pos = settings.value(&quot;pos&quot;, QPoint(200, 200)).toPoint();
 
262
        QSize size = settings.value(&quot;size&quot;, QSize(400, 400)).toSize();
 
263
        move(pos);
 
264
        resize(size);
 
265
    }
 
266
 
 
267
    void MainWindow::writeSettings()
 
268
    {
 
269
        QSettings settings(&quot;Trolltech&quot;, &quot;SDI Example&quot;);
 
270
        settings.setValue(&quot;pos&quot;, pos());
 
271
        settings.setValue(&quot;size&quot;, size());
 
272
    }
 
273
 
 
274
    bool MainWindow::maybeSave()
 
275
    {
 
276
        if (textEdit-&gt;document()-&gt;isModified()) {
 
277
            int ret = QMessageBox::warning(this, tr(&quot;SDI&quot;),
 
278
                         tr(&quot;The document has been modified.\n&quot;
 
279
                            &quot;Do you want to save your changes?&quot;),
 
280
                         QMessageBox::Yes | QMessageBox::Default,
 
281
                         QMessageBox::No,
 
282
                         QMessageBox::Cancel | QMessageBox::Escape);
 
283
            if (ret == QMessageBox::Yes)
 
284
                return save();
 
285
            else if (ret == QMessageBox::Cancel)
 
286
                return false;
 
287
        }
 
288
        return true;
 
289
    }
 
290
 
 
291
    void MainWindow::loadFile(const QString &amp;fileName)
 
292
    {
 
293
 
 
294
        QFile file(fileName);
 
295
        if (!file.open(QFile::ReadOnly | QFile::Text)) {
 
296
            QMessageBox::warning(this, tr(&quot;SDI&quot;),
 
297
                                 tr(&quot;Cannot read file %1:\n%2.&quot;)
 
298
                                 .arg(fileName)
 
299
                                 .arg(file.errorString()));
 
300
            return;
 
301
        }
 
302
 
 
303
        QTextStream in(&amp;file);
 
304
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
305
        textEdit-&gt;setPlainText(in.readAll());
 
306
        QApplication::restoreOverrideCursor();
 
307
 
 
308
        setCurrentFile(fileName);
 
309
        statusBar()-&gt;showMessage(tr(&quot;File loaded&quot;), 2000);
 
310
    }
 
311
 
 
312
    bool MainWindow::saveFile(const QString &amp;fileName)
 
313
    {
 
314
        QFile file(fileName);
 
315
        if (!file.open(QFile::WriteOnly | QFile::Text)) {
 
316
            QMessageBox::warning(this, tr(&quot;SDI&quot;),
 
317
                                 tr(&quot;Cannot write file %1:\n%2.&quot;)
 
318
                                 .arg(fileName)
 
319
                                 .arg(file.errorString()));
 
320
            return false;
 
321
        }
 
322
 
 
323
        QTextStream out(&amp;file);
 
324
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
325
        out &lt;&lt; textEdit-&gt;toPlainText();
 
326
        QApplication::restoreOverrideCursor();
 
327
 
 
328
        setCurrentFile(fileName);
 
329
        statusBar()-&gt;showMessage(tr(&quot;File saved&quot;), 2000);
 
330
        return true;
 
331
    }
 
332
 
 
333
    void MainWindow::setCurrentFile(const QString &amp;fileName)
 
334
    {
 
335
        static int sequenceNumber = 1;
 
336
 
 
337
        isUntitled = fileName.isEmpty();
 
338
        if (isUntitled) {
 
339
            curFile = tr(&quot;document%1.txt&quot;).arg(sequenceNumber++);
 
340
        } else {
 
341
            curFile = QFileInfo(fileName).canonicalFilePath();
 
342
        }
 
343
 
 
344
        textEdit-&gt;document()-&gt;setModified(false);
 
345
        setWindowModified(false);
 
346
 
 
347
        setWindowTitle(tr(&quot;%1[*] - %2&quot;).arg(strippedName(curFile))
 
348
                                           .arg(tr(&quot;SDI&quot;)));
 
349
    }
 
350
 
 
351
    QString MainWindow::strippedName(const QString &amp;fullFileName)
 
352
    {
 
353
        return QFileInfo(fullFileName).fileName();
 
354
    }
 
355
 
 
356
    MainWindow *MainWindow::findMainWindow(const QString &amp;fileName)
 
357
    {
 
358
        QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
 
359
 
 
360
        foreach (QWidget *widget, qApp-&gt;topLevelWidgets()) {
 
361
            MainWindow *mainWin = qobject_cast&lt;MainWindow *&gt;(widget);
 
362
            if (mainWin &amp;&amp; mainWin-&gt;curFile == canonicalFilePath)
 
363
                return mainWin;
 
364
        }
 
365
        return 0;
 
366
    }</pre>
 
367
<p /><address><hr /><div align="center">
 
368
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
369
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
370
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
371
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
372
</tr></table></div></address></body>
 
373
</html>