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

« back to all changes in this revision

Viewing changes to doc/html/mainwindows-recentfiles-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/recentfiles/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/recentfiles/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
        setAttribute(Qt::WA_DeleteOnClose);
 
55
 
 
56
        textEdit = new QTextEdit;
 
57
        setCentralWidget(textEdit);
 
58
 
 
59
        createActions();
 
60
        createMenus();
 
61
        (void)statusBar();
 
62
 
 
63
        setWindowTitle(tr(&quot;Recent Files&quot;));
 
64
        resize(400, 300);
 
65
    }
 
66
 
 
67
    void MainWindow::newFile()
 
68
    {
 
69
        MainWindow *other = new MainWindow;
 
70
        other-&gt;show();
 
71
    }
 
72
 
 
73
    void MainWindow::open()
 
74
    {
 
75
        QString fileName = QFileDialog::getOpenFileName(this);
 
76
        if (!fileName.isEmpty())
 
77
            loadFile(fileName);
 
78
    }
 
79
 
 
80
    void MainWindow::save()
 
81
    {
 
82
        if (curFile.isEmpty())
 
83
            saveAs();
 
84
        else
 
85
            saveFile(curFile);
 
86
    }
 
87
 
 
88
    void MainWindow::saveAs()
 
89
    {
 
90
        QString fileName = QFileDialog::getSaveFileName(this);
 
91
        if (fileName.isEmpty())
 
92
            return;
 
93
 
 
94
        if (QFile::exists(fileName)) {
 
95
            int ret = QMessageBox::warning(this, tr(&quot;Recent Files&quot;),
 
96
                         tr(&quot;File %1 already exists.\n&quot;
 
97
                            &quot;Do you want to overwrite it?&quot;)
 
98
                         .arg(QDir::convertSeparators(fileName)),
 
99
                         QMessageBox::Yes | QMessageBox::Default,
 
100
                         QMessageBox::No | QMessageBox::Escape);
 
101
            if (ret == QMessageBox::No)
 
102
                return;
 
103
        }
 
104
        saveFile(fileName);
 
105
    }
 
106
 
 
107
    void MainWindow::openRecentFile()
 
108
    {
 
109
        QAction *action = qobject_cast&lt;QAction *&gt;(sender());
 
110
        if (action)
 
111
            loadFile(action-&gt;data().toString());
 
112
    }
 
113
 
 
114
    void MainWindow::about()
 
115
    {
 
116
       QMessageBox::about(this, tr(&quot;About Recent Files&quot;),
 
117
                tr(&quot;The &lt;b&gt;Recent Files&lt;/b&gt; example demonstrates how to provide a &quot;
 
118
                   &quot;recently used file menu in a Qt application.&quot;));
 
119
    }
 
120
 
 
121
    void MainWindow::createActions()
 
122
    {
 
123
        newAct = new QAction(tr(&quot;&amp;New&quot;), this);
 
124
        newAct-&gt;setShortcut(tr(&quot;Ctrl+N&quot;));
 
125
        newAct-&gt;setStatusTip(tr(&quot;Create a new file&quot;));
 
126
        connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 
127
 
 
128
        openAct = new QAction(tr(&quot;&amp;Open...&quot;), this);
 
129
        openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
 
130
        openAct-&gt;setStatusTip(tr(&quot;Open an existing file&quot;));
 
131
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
132
 
 
133
        saveAct = new QAction(tr(&quot;&amp;Save&quot;), this);
 
134
        saveAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
 
135
        saveAct-&gt;setStatusTip(tr(&quot;Save the document to disk&quot;));
 
136
        connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
 
137
 
 
138
        saveAsAct = new QAction(tr(&quot;Save &amp;As...&quot;), this);
 
139
        saveAsAct-&gt;setStatusTip(tr(&quot;Save the document under a new name&quot;));
 
140
        connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
 
141
 
 
142
        for (int i = 0; i &lt; MaxRecentFiles; ++i) {
 
143
            recentFileActs[i] = new QAction(this);
 
144
            recentFileActs[i]-&gt;setVisible(false);
 
145
            connect(recentFileActs[i], SIGNAL(triggered()),
 
146
                    this, SLOT(openRecentFile()));
 
147
        }
 
148
 
 
149
        exitAct = new QAction(tr(&quot;&amp;Close&quot;), this);
 
150
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+W&quot;));
 
151
        exitAct-&gt;setStatusTip(tr(&quot;Close this window&quot;));
 
152
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
153
 
 
154
        exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
155
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
156
        exitAct-&gt;setStatusTip(tr(&quot;Exit the application&quot;));
 
157
        connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
 
158
 
 
159
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
160
        aboutAct-&gt;setStatusTip(tr(&quot;Show the application's About box&quot;));
 
161
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
162
 
 
163
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
164
        aboutQtAct-&gt;setStatusTip(tr(&quot;Show the Qt library's About box&quot;));
 
165
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
166
    }
 
167
 
 
168
    void MainWindow::createMenus()
 
169
    {
 
170
        fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
171
        fileMenu-&gt;addAction(newAct);
 
172
        fileMenu-&gt;addAction(openAct);
 
173
        fileMenu-&gt;addAction(saveAct);
 
174
        fileMenu-&gt;addAction(saveAsAct);
 
175
        separatorAct = fileMenu-&gt;addSeparator();
 
176
        for (int i = 0; i &lt; MaxRecentFiles; ++i)
 
177
            fileMenu-&gt;addAction(recentFileActs[i]);
 
178
        fileMenu-&gt;addSeparator();
 
179
        fileMenu-&gt;addAction(exitAct);
 
180
        updateRecentFileActions();
 
181
 
 
182
        menuBar()-&gt;addSeparator();
 
183
 
 
184
        helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
 
185
        helpMenu-&gt;addAction(aboutAct);
 
186
        helpMenu-&gt;addAction(aboutQtAct);
 
187
    }
 
188
 
 
189
    void MainWindow::loadFile(const QString &amp;fileName)
 
190
    {
 
191
        QFile file(fileName);
 
192
        if (!file.open(QFile::ReadOnly | QFile::Text)) {
 
193
            QMessageBox::warning(this, tr(&quot;Recent Files&quot;),
 
194
                                 tr(&quot;Cannot read file %1:\n%2.&quot;)
 
195
                                 .arg(fileName)
 
196
                                 .arg(file.errorString()));
 
197
            return;
 
198
        }
 
199
 
 
200
        QTextStream in(&amp;file);
 
201
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
202
        textEdit-&gt;setPlainText(in.readAll());
 
203
        QApplication::restoreOverrideCursor();
 
204
 
 
205
        setCurrentFile(fileName);
 
206
        statusBar()-&gt;showMessage(tr(&quot;File loaded&quot;), 2000);
 
207
    }
 
208
 
 
209
    void MainWindow::saveFile(const QString &amp;fileName)
 
210
    {
 
211
        QFile file(fileName);
 
212
        if (!file.open(QFile::WriteOnly | QFile::Text)) {
 
213
            QMessageBox::warning(this, tr(&quot;Recent Files&quot;),
 
214
                                 tr(&quot;Cannot write file %1:\n%2.&quot;)
 
215
                                 .arg(fileName)
 
216
                                 .arg(file.errorString()));
 
217
            return;
 
218
        }
 
219
 
 
220
        QTextStream out(&amp;file);
 
221
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
222
        out &lt;&lt; textEdit-&gt;toPlainText();
 
223
        QApplication::restoreOverrideCursor();
 
224
 
 
225
        setCurrentFile(fileName);
 
226
        statusBar()-&gt;showMessage(tr(&quot;File saved&quot;), 2000);
 
227
    }
 
228
 
 
229
    void MainWindow::setCurrentFile(const QString &amp;fileName)
 
230
    {
 
231
        curFile = fileName;
 
232
        if (curFile.isEmpty())
 
233
            setWindowTitle(tr(&quot;Recent Files&quot;));
 
234
        else
 
235
            setWindowTitle(tr(&quot;%1 - %2&quot;).arg(strippedName(curFile))
 
236
                                        .arg(tr(&quot;Recent Files&quot;)));
 
237
 
 
238
        QSettings settings(&quot;Trolltech&quot;, &quot;Recent Files Example&quot;);
 
239
        QStringList files = settings.value(&quot;recentFileList&quot;).toStringList();
 
240
        files.removeAll(fileName);
 
241
        files.prepend(fileName);
 
242
        while (files.size() &gt; MaxRecentFiles)
 
243
            files.removeLast();
 
244
 
 
245
        settings.setValue(&quot;recentFileList&quot;, files);
 
246
 
 
247
        foreach (QWidget *widget, QApplication::topLevelWidgets()) {
 
248
            MainWindow *mainWin = qobject_cast&lt;MainWindow *&gt;(widget);
 
249
            if (mainWin)
 
250
                mainWin-&gt;updateRecentFileActions();
 
251
        }
 
252
    }
 
253
 
 
254
    void MainWindow::updateRecentFileActions()
 
255
    {
 
256
        QSettings settings(&quot;Trolltech&quot;, &quot;Recent Files Example&quot;);
 
257
        QStringList files = settings.value(&quot;recentFileList&quot;).toStringList();
 
258
 
 
259
        int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
 
260
 
 
261
        for (int i = 0; i &lt; numRecentFiles; ++i) {
 
262
            QString text = tr(&quot;&amp;%1 %2&quot;).arg(i + 1).arg(strippedName(files[i]));
 
263
            recentFileActs[i]-&gt;setText(text);
 
264
            recentFileActs[i]-&gt;setData(files[i]);
 
265
            recentFileActs[i]-&gt;setVisible(true);
 
266
        }
 
267
        for (int j = numRecentFiles; j &lt; MaxRecentFiles; ++j)
 
268
            recentFileActs[j]-&gt;setVisible(false);
 
269
 
 
270
        separatorAct-&gt;setVisible(numRecentFiles &gt; 0);
 
271
    }
 
272
 
 
273
    QString MainWindow::strippedName(const QString &amp;fullFileName)
 
274
    {
 
275
        return QFileInfo(fullFileName).fileName();
 
276
    }</pre>
 
277
<p /><address><hr /><div align="center">
 
278
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
279
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
280
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
281
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
282
</tr></table></div></address></body>
 
283
</html>