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

« back to all changes in this revision

Viewing changes to doc/html/mainwindows-mdi-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/mdi/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/mdi/mainwindow.cpp</small></small></h1>
 
20
<pre>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2005-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
    #include &quot;mdichild.h&quot;
 
52
 
 
53
    MainWindow::MainWindow()
 
54
    {
 
55
        workspace = new QWorkspace;
 
56
        setCentralWidget(workspace);
 
57
        connect(workspace, SIGNAL(windowActivated(QWidget *)),
 
58
                this, SLOT(updateMenus()));
 
59
        windowMapper = new QSignalMapper(this);
 
60
        connect(windowMapper, SIGNAL(mapped(QWidget *)),
 
61
                workspace, SLOT(setActiveWindow(QWidget *)));
 
62
 
 
63
        createActions();
 
64
        createMenus();
 
65
        createToolBars();
 
66
        createStatusBar();
 
67
        updateMenus();
 
68
 
 
69
        readSettings();
 
70
 
 
71
        setWindowTitle(tr(&quot;MDI&quot;));
 
72
    }
 
73
 
 
74
    void MainWindow::closeEvent(QCloseEvent *event)
 
75
    {
 
76
        workspace-&gt;closeAllWindows();
 
77
        if (activeMdiChild()) {
 
78
            event-&gt;ignore();
 
79
        } else {
 
80
            writeSettings();
 
81
            event-&gt;accept();
 
82
        }
 
83
    }
 
84
 
 
85
    void MainWindow::newFile()
 
86
    {
 
87
        MdiChild *child = createMdiChild();
 
88
        child-&gt;newFile();
 
89
        child-&gt;show();
 
90
    }
 
91
 
 
92
    void MainWindow::open()
 
93
    {
 
94
        QString fileName = QFileDialog::getOpenFileName(this);
 
95
        if (!fileName.isEmpty()) {
 
96
            MdiChild *existing = findMdiChild(fileName);
 
97
            if (existing) {
 
98
                workspace-&gt;setActiveWindow(existing);
 
99
                return;
 
100
            }
 
101
 
 
102
            MdiChild *child = createMdiChild();
 
103
            if (child-&gt;loadFile(fileName)) {
 
104
                statusBar()-&gt;showMessage(tr(&quot;File loaded&quot;), 2000);
 
105
                child-&gt;show();
 
106
            } else {
 
107
                child-&gt;close();
 
108
            }
 
109
        }
 
110
    }
 
111
 
 
112
    void MainWindow::save()
 
113
    {
 
114
        if (activeMdiChild()-&gt;save())
 
115
            statusBar()-&gt;showMessage(tr(&quot;File saved&quot;), 2000);
 
116
    }
 
117
 
 
118
    void MainWindow::saveAs()
 
119
    {
 
120
        if (activeMdiChild()-&gt;saveAs())
 
121
            statusBar()-&gt;showMessage(tr(&quot;File saved&quot;), 2000);
 
122
    }
 
123
 
 
124
    void MainWindow::cut()
 
125
    {
 
126
        activeMdiChild()-&gt;cut();
 
127
    }
 
128
 
 
129
    void MainWindow::copy()
 
130
    {
 
131
        activeMdiChild()-&gt;copy();
 
132
    }
 
133
 
 
134
    void MainWindow::paste()
 
135
    {
 
136
        activeMdiChild()-&gt;paste();
 
137
    }
 
138
 
 
139
    void MainWindow::about()
 
140
    {
 
141
       QMessageBox::about(this, tr(&quot;About MDI&quot;),
 
142
                tr(&quot;The &lt;b&gt;MDI&lt;/b&gt; example demonstrates how to write multiple &quot;
 
143
                   &quot;document interface applications using Qt.&quot;));
 
144
    }
 
145
 
 
146
    void MainWindow::updateMenus()
 
147
    {
 
148
        bool hasMdiChild = (activeMdiChild() != 0);
 
149
        saveAct-&gt;setEnabled(hasMdiChild);
 
150
        saveAsAct-&gt;setEnabled(hasMdiChild);
 
151
        pasteAct-&gt;setEnabled(hasMdiChild);
 
152
        closeAct-&gt;setEnabled(hasMdiChild);
 
153
        closeAllAct-&gt;setEnabled(hasMdiChild);
 
154
        tileAct-&gt;setEnabled(hasMdiChild);
 
155
        cascadeAct-&gt;setEnabled(hasMdiChild);
 
156
        nextAct-&gt;setEnabled(hasMdiChild);
 
157
        previousAct-&gt;setEnabled(hasMdiChild);
 
158
        separatorAct-&gt;setVisible(hasMdiChild);
 
159
 
 
160
        bool hasSelection = (activeMdiChild() &amp;&amp;
 
161
                             activeMdiChild()-&gt;textCursor().hasSelection());
 
162
        cutAct-&gt;setEnabled(hasSelection);
 
163
        copyAct-&gt;setEnabled(hasSelection);
 
164
    }
 
165
 
 
166
    void MainWindow::updateWindowMenu()
 
167
    {
 
168
        windowMenu-&gt;clear();
 
169
        windowMenu-&gt;addAction(closeAct);
 
170
        windowMenu-&gt;addAction(closeAllAct);
 
171
        windowMenu-&gt;addSeparator();
 
172
        windowMenu-&gt;addAction(tileAct);
 
173
        windowMenu-&gt;addAction(cascadeAct);
 
174
        windowMenu-&gt;addSeparator();
 
175
        windowMenu-&gt;addAction(nextAct);
 
176
        windowMenu-&gt;addAction(previousAct);
 
177
        windowMenu-&gt;addAction(separatorAct);
 
178
 
 
179
        QList&lt;QWidget *&gt; windows = workspace-&gt;windowList();
 
180
        separatorAct-&gt;setVisible(!windows.isEmpty());
 
181
 
 
182
        for (int i = 0; i &lt; windows.size(); ++i) {
 
183
            MdiChild *child = qobject_cast&lt;MdiChild *&gt;(windows.at(i));
 
184
 
 
185
            QString text;
 
186
            if (i &lt; 9) {
 
187
                text = tr(&quot;&amp;%1. %2&quot;).arg(i + 1)
 
188
                                    .arg(child-&gt;userFriendlyCurrentFile());
 
189
            } else {
 
190
                text = tr(&quot;%1. %2&quot;).arg(i + 1)
 
191
                                   .arg(child-&gt;userFriendlyCurrentFile());
 
192
            }
 
193
            QAction *action  = windowMenu-&gt;addAction(text);
 
194
            action-&gt;setCheckable(true);
 
195
            action -&gt;setChecked(child == activeMdiChild());
 
196
            connect(action, SIGNAL(triggered()), windowMapper, SLOT(map()));
 
197
            windowMapper-&gt;setMapping(action, child);
 
198
        }
 
199
    }
 
200
 
 
201
    MdiChild *MainWindow::createMdiChild()
 
202
    {
 
203
        MdiChild *child = new MdiChild;
 
204
        workspace-&gt;addWindow(child);
 
205
 
 
206
        connect(child, SIGNAL(copyAvailable(bool)),
 
207
                cutAct, SLOT(setEnabled(bool)));
 
208
        connect(child, SIGNAL(copyAvailable(bool)),
 
209
                copyAct, SLOT(setEnabled(bool)));
 
210
 
 
211
        return child;
 
212
    }
 
213
 
 
214
    void MainWindow::createActions()
 
215
    {
 
216
        newAct = new QAction(QIcon(&quot;:/images/new.png&quot;), tr(&quot;&amp;New&quot;), this);
 
217
        newAct-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
 
218
        newAct-&gt;setStatusTip(tr(&quot;Create a new file&quot;));
 
219
        connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 
220
 
 
221
        openAct = new QAction(QIcon(&quot;:/images/open.png&quot;), tr(&quot;&amp;Open...&quot;), this);
 
222
        openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
 
223
        openAct-&gt;setStatusTip(tr(&quot;Open an existing file&quot;));
 
224
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
225
 
 
226
        saveAct = new QAction(QIcon(&quot;:/images/save.png&quot;), tr(&quot;&amp;Save&quot;), this);
 
227
        saveAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
 
228
        saveAct-&gt;setStatusTip(tr(&quot;Save the document to disk&quot;));
 
229
        connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
 
230
 
 
231
        saveAsAct = new QAction(tr(&quot;Save &amp;As...&quot;), this);
 
232
        saveAsAct-&gt;setStatusTip(tr(&quot;Save the document under a new name&quot;));
 
233
        connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
 
234
 
 
235
        exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
236
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
237
        exitAct-&gt;setStatusTip(tr(&quot;Exit the application&quot;));
 
238
        connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
 
239
 
 
240
        cutAct = new QAction(QIcon(&quot;:/images/cut.png&quot;), tr(&quot;Cu&amp;t&quot;), this);
 
241
        cutAct-&gt;setShortcut(tr(&quot;Ctrl+X&quot;));
 
242
        cutAct-&gt;setStatusTip(tr(&quot;Cut the current selection's contents to the &quot;
 
243
                                &quot;clipboard&quot;));
 
244
        connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
 
245
 
 
246
        copyAct = new QAction(QIcon(&quot;:/images/copy.png&quot;), tr(&quot;&amp;Copy&quot;), this);
 
247
        copyAct-&gt;setShortcut(tr(&quot;Ctrl+C&quot;));
 
248
        copyAct-&gt;setStatusTip(tr(&quot;Copy the current selection's contents to the &quot;
 
249
                                 &quot;clipboard&quot;));
 
250
        connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
 
251
 
 
252
        pasteAct = new QAction(QIcon(&quot;:/images/paste.png&quot;), tr(&quot;&amp;Paste&quot;), this);
 
253
        pasteAct-&gt;setShortcut(tr(&quot;Ctrl+V&quot;));
 
254
        pasteAct-&gt;setStatusTip(tr(&quot;Paste the clipboard's contents into the current &quot;
 
255
                                  &quot;selection&quot;));
 
256
        connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
 
257
 
 
258
        closeAct = new QAction(tr(&quot;Cl&amp;ose&quot;), this);
 
259
        closeAct-&gt;setShortcut(tr(&quot;Ctrl+F4&quot;));
 
260
        closeAct-&gt;setStatusTip(tr(&quot;Close the active window&quot;));
 
261
        connect(closeAct, SIGNAL(triggered()),
 
262
                workspace, SLOT(closeActiveWindow()));
 
263
 
 
264
        closeAllAct = new QAction(tr(&quot;Close &amp;All&quot;), this);
 
265
        closeAllAct-&gt;setStatusTip(tr(&quot;Close all the windows&quot;));
 
266
        connect(closeAllAct, SIGNAL(triggered()),
 
267
                workspace, SLOT(closeAllWindows()));
 
268
 
 
269
        tileAct = new QAction(tr(&quot;&amp;Tile&quot;), this);
 
270
        tileAct-&gt;setStatusTip(tr(&quot;Tile the windows&quot;));
 
271
        connect(tileAct, SIGNAL(triggered()), workspace, SLOT(tile()));
 
272
 
 
273
        cascadeAct = new QAction(tr(&quot;&amp;Cascade&quot;), this);
 
274
        cascadeAct-&gt;setStatusTip(tr(&quot;Cascade the windows&quot;));
 
275
        connect(cascadeAct, SIGNAL(triggered()), workspace, SLOT(cascade()));
 
276
 
 
277
        nextAct = new QAction(tr(&quot;Ne&amp;xt&quot;), this);
 
278
        nextAct-&gt;setShortcut(tr(&quot;Ctrl+F6&quot;));
 
279
        nextAct-&gt;setStatusTip(tr(&quot;Move the focus to the next window&quot;));
 
280
        connect(nextAct, SIGNAL(triggered()),
 
281
                workspace, SLOT(activateNextWindow()));
 
282
 
 
283
        previousAct = new QAction(tr(&quot;Pre&amp;vious&quot;), this);
 
284
        previousAct-&gt;setShortcut(tr(&quot;Ctrl+Shift+F6&quot;));
 
285
        previousAct-&gt;setStatusTip(tr(&quot;Move the focus to the previous &quot;
 
286
                                     &quot;window&quot;));
 
287
        connect(previousAct, SIGNAL(triggered()),
 
288
                workspace, SLOT(activatePreviousWindow()));
 
289
 
 
290
        separatorAct = new QAction(this);
 
291
        separatorAct-&gt;setSeparator(true);
 
292
 
 
293
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
294
        aboutAct-&gt;setStatusTip(tr(&quot;Show the application's About box&quot;));
 
295
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
296
 
 
297
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
298
        aboutQtAct-&gt;setStatusTip(tr(&quot;Show the Qt library's About box&quot;));
 
299
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
300
    }
 
301
 
 
302
    void MainWindow::createMenus()
 
303
    {
 
304
        fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
305
        fileMenu-&gt;addAction(newAct);
 
306
        fileMenu-&gt;addAction(openAct);
 
307
        fileMenu-&gt;addAction(saveAct);
 
308
        fileMenu-&gt;addAction(saveAsAct);
 
309
        fileMenu-&gt;addSeparator();
 
310
        fileMenu-&gt;addAction(exitAct);
 
311
 
 
312
        editMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Edit&quot;));
 
313
        editMenu-&gt;addAction(cutAct);
 
314
        editMenu-&gt;addAction(copyAct);
 
315
        editMenu-&gt;addAction(pasteAct);
 
316
 
 
317
        windowMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Window&quot;));
 
318
        connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu()));
 
319
 
 
320
        menuBar()-&gt;addSeparator();
 
321
 
 
322
        helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
 
323
        helpMenu-&gt;addAction(aboutAct);
 
324
        helpMenu-&gt;addAction(aboutQtAct);
 
325
    }
 
326
 
 
327
    void MainWindow::createToolBars()
 
328
    {
 
329
        fileToolBar = addToolBar(tr(&quot;File&quot;));
 
330
        fileToolBar-&gt;addAction(newAct);
 
331
        fileToolBar-&gt;addAction(openAct);
 
332
        fileToolBar-&gt;addAction(saveAct);
 
333
 
 
334
        editToolBar = addToolBar(tr(&quot;Edit&quot;));
 
335
        editToolBar-&gt;addAction(cutAct);
 
336
        editToolBar-&gt;addAction(copyAct);
 
337
        editToolBar-&gt;addAction(pasteAct);
 
338
    }
 
339
 
 
340
    void MainWindow::createStatusBar()
 
341
    {
 
342
        statusBar()-&gt;showMessage(tr(&quot;Ready&quot;));
 
343
    }
 
344
 
 
345
    void MainWindow::readSettings()
 
346
    {
 
347
        QSettings settings(&quot;Trolltech&quot;, &quot;MDI Example&quot;);
 
348
        QPoint pos = settings.value(&quot;pos&quot;, QPoint(200, 200)).toPoint();
 
349
        QSize size = settings.value(&quot;size&quot;, QSize(400, 400)).toSize();
 
350
        move(pos);
 
351
        resize(size);
 
352
    }
 
353
 
 
354
    void MainWindow::writeSettings()
 
355
    {
 
356
        QSettings settings(&quot;Trolltech&quot;, &quot;MDI Example&quot;);
 
357
        settings.setValue(&quot;pos&quot;, pos());
 
358
        settings.setValue(&quot;size&quot;, size());
 
359
    }
 
360
 
 
361
    MdiChild *MainWindow::activeMdiChild()
 
362
    {
 
363
        return qobject_cast&lt;MdiChild *&gt;(workspace-&gt;activeWindow());
 
364
    }
 
365
 
 
366
    MdiChild *MainWindow::findMdiChild(const QString &amp;fileName)
 
367
    {
 
368
        QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
 
369
 
 
370
        foreach (QWidget *window, workspace-&gt;windowList()) {
 
371
            MdiChild *mdiChild = qobject_cast&lt;MdiChild *&gt;(window);
 
372
            if (mdiChild-&gt;currentFile() == canonicalFilePath)
 
373
                return mdiChild;
 
374
        }
 
375
        return 0;
 
376
    }</pre>
 
377
<p /><address><hr /><div align="center">
 
378
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
379
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
380
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
381
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
382
</tr></table></div></address></body>
 
383
</html>