1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at qt-sales@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "mainwindow.h"
|
---|
45 |
|
---|
46 | MainWindow::MainWindow()
|
---|
47 | {
|
---|
48 | setAttribute(Qt::WA_DeleteOnClose);
|
---|
49 |
|
---|
50 | textEdit = new QTextEdit;
|
---|
51 | setCentralWidget(textEdit);
|
---|
52 |
|
---|
53 | createActions();
|
---|
54 | createMenus();
|
---|
55 | (void)statusBar();
|
---|
56 |
|
---|
57 | setWindowTitle(tr("Recent Files"));
|
---|
58 | resize(400, 300);
|
---|
59 | }
|
---|
60 |
|
---|
61 | void MainWindow::newFile()
|
---|
62 | {
|
---|
63 | MainWindow *other = new MainWindow;
|
---|
64 | other->show();
|
---|
65 | }
|
---|
66 |
|
---|
67 | void MainWindow::open()
|
---|
68 | {
|
---|
69 | QString fileName = QFileDialog::getOpenFileName(this);
|
---|
70 | if (!fileName.isEmpty())
|
---|
71 | loadFile(fileName);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void MainWindow::save()
|
---|
75 | {
|
---|
76 | if (curFile.isEmpty())
|
---|
77 | saveAs();
|
---|
78 | else
|
---|
79 | saveFile(curFile);
|
---|
80 | }
|
---|
81 |
|
---|
82 | void MainWindow::saveAs()
|
---|
83 | {
|
---|
84 | QString fileName = QFileDialog::getSaveFileName(this);
|
---|
85 | if (fileName.isEmpty())
|
---|
86 | return;
|
---|
87 |
|
---|
88 | saveFile(fileName);
|
---|
89 | }
|
---|
90 |
|
---|
91 | void MainWindow::openRecentFile()
|
---|
92 | {
|
---|
93 | QAction *action = qobject_cast<QAction *>(sender());
|
---|
94 | if (action)
|
---|
95 | loadFile(action->data().toString());
|
---|
96 | }
|
---|
97 |
|
---|
98 | void MainWindow::about()
|
---|
99 | {
|
---|
100 | QMessageBox::about(this, tr("About Recent Files"),
|
---|
101 | tr("The <b>Recent Files</b> example demonstrates how to provide a "
|
---|
102 | "recently used file menu in a Qt application."));
|
---|
103 | }
|
---|
104 |
|
---|
105 | void MainWindow::createActions()
|
---|
106 | {
|
---|
107 | newAct = new QAction(tr("&New"), this);
|
---|
108 | newAct->setShortcuts(QKeySequence::New);
|
---|
109 | newAct->setStatusTip(tr("Create a new file"));
|
---|
110 | connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
|
---|
111 |
|
---|
112 | openAct = new QAction(tr("&Open..."), this);
|
---|
113 | openAct->setShortcuts(QKeySequence::Open);
|
---|
114 | openAct->setStatusTip(tr("Open an existing file"));
|
---|
115 | connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
---|
116 |
|
---|
117 | saveAct = new QAction(tr("&Save"), this);
|
---|
118 | saveAct->setShortcuts(QKeySequence::Save);
|
---|
119 | saveAct->setStatusTip(tr("Save the document to disk"));
|
---|
120 | connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
|
---|
121 |
|
---|
122 | saveAsAct = new QAction(tr("Save &As..."), this);
|
---|
123 | saveAsAct->setShortcuts(QKeySequence::SaveAs);
|
---|
124 | saveAsAct->setStatusTip(tr("Save the document under a new name"));
|
---|
125 | connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
|
---|
126 |
|
---|
127 | for (int i = 0; i < MaxRecentFiles; ++i) {
|
---|
128 | recentFileActs[i] = new QAction(this);
|
---|
129 | recentFileActs[i]->setVisible(false);
|
---|
130 | connect(recentFileActs[i], SIGNAL(triggered()),
|
---|
131 | this, SLOT(openRecentFile()));
|
---|
132 | }
|
---|
133 |
|
---|
134 | exitAct = new QAction(tr("E&xit"), this);
|
---|
135 | exitAct->setShortcut(tr("Ctrl+Q"));
|
---|
136 | exitAct->setStatusTip(tr("Exit the application"));
|
---|
137 | connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
|
---|
138 |
|
---|
139 | aboutAct = new QAction(tr("&About"), this);
|
---|
140 | aboutAct->setStatusTip(tr("Show the application's About box"));
|
---|
141 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
---|
142 |
|
---|
143 | aboutQtAct = new QAction(tr("About &Qt"), this);
|
---|
144 | aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
|
---|
145 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
146 | }
|
---|
147 |
|
---|
148 | void MainWindow::createMenus()
|
---|
149 | {
|
---|
150 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
151 | fileMenu->addAction(newAct);
|
---|
152 | fileMenu->addAction(openAct);
|
---|
153 | fileMenu->addAction(saveAct);
|
---|
154 | fileMenu->addAction(saveAsAct);
|
---|
155 | separatorAct = fileMenu->addSeparator();
|
---|
156 | for (int i = 0; i < MaxRecentFiles; ++i)
|
---|
157 | fileMenu->addAction(recentFileActs[i]);
|
---|
158 | fileMenu->addSeparator();
|
---|
159 | fileMenu->addAction(exitAct);
|
---|
160 | updateRecentFileActions();
|
---|
161 |
|
---|
162 | menuBar()->addSeparator();
|
---|
163 |
|
---|
164 | helpMenu = menuBar()->addMenu(tr("&Help"));
|
---|
165 | helpMenu->addAction(aboutAct);
|
---|
166 | helpMenu->addAction(aboutQtAct);
|
---|
167 | }
|
---|
168 |
|
---|
169 | void MainWindow::loadFile(const QString &fileName)
|
---|
170 | {
|
---|
171 | QFile file(fileName);
|
---|
172 | if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
---|
173 | QMessageBox::warning(this, tr("Recent Files"),
|
---|
174 | tr("Cannot read file %1:\n%2.")
|
---|
175 | .arg(fileName)
|
---|
176 | .arg(file.errorString()));
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | QTextStream in(&file);
|
---|
181 | QApplication::setOverrideCursor(Qt::WaitCursor);
|
---|
182 | textEdit->setPlainText(in.readAll());
|
---|
183 | QApplication::restoreOverrideCursor();
|
---|
184 |
|
---|
185 | setCurrentFile(fileName);
|
---|
186 | statusBar()->showMessage(tr("File loaded"), 2000);
|
---|
187 | }
|
---|
188 |
|
---|
189 | void MainWindow::saveFile(const QString &fileName)
|
---|
190 | {
|
---|
191 | QFile file(fileName);
|
---|
192 | if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
---|
193 | QMessageBox::warning(this, tr("Recent Files"),
|
---|
194 | tr("Cannot write file %1:\n%2.")
|
---|
195 | .arg(fileName)
|
---|
196 | .arg(file.errorString()));
|
---|
197 | return;
|
---|
198 | }
|
---|
199 |
|
---|
200 | QTextStream out(&file);
|
---|
201 | QApplication::setOverrideCursor(Qt::WaitCursor);
|
---|
202 | out << textEdit->toPlainText();
|
---|
203 | QApplication::restoreOverrideCursor();
|
---|
204 |
|
---|
205 | setCurrentFile(fileName);
|
---|
206 | statusBar()->showMessage(tr("File saved"), 2000);
|
---|
207 | }
|
---|
208 |
|
---|
209 | void MainWindow::setCurrentFile(const QString &fileName)
|
---|
210 | {
|
---|
211 | curFile = fileName;
|
---|
212 | if (curFile.isEmpty())
|
---|
213 | setWindowTitle(tr("Recent Files"));
|
---|
214 | else
|
---|
215 | setWindowTitle(tr("%1 - %2").arg(strippedName(curFile))
|
---|
216 | .arg(tr("Recent Files")));
|
---|
217 |
|
---|
218 | QSettings settings("Trolltech", "Recent Files Example");
|
---|
219 | QStringList files = settings.value("recentFileList").toStringList();
|
---|
220 | files.removeAll(fileName);
|
---|
221 | files.prepend(fileName);
|
---|
222 | while (files.size() > MaxRecentFiles)
|
---|
223 | files.removeLast();
|
---|
224 |
|
---|
225 | settings.setValue("recentFileList", files);
|
---|
226 |
|
---|
227 | foreach (QWidget *widget, QApplication::topLevelWidgets()) {
|
---|
228 | MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
|
---|
229 | if (mainWin)
|
---|
230 | mainWin->updateRecentFileActions();
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | void MainWindow::updateRecentFileActions()
|
---|
235 | {
|
---|
236 | QSettings settings("Trolltech", "Recent Files Example");
|
---|
237 | QStringList files = settings.value("recentFileList").toStringList();
|
---|
238 |
|
---|
239 | int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
|
---|
240 |
|
---|
241 | for (int i = 0; i < numRecentFiles; ++i) {
|
---|
242 | QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
|
---|
243 | recentFileActs[i]->setText(text);
|
---|
244 | recentFileActs[i]->setData(files[i]);
|
---|
245 | recentFileActs[i]->setVisible(true);
|
---|
246 | }
|
---|
247 | for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
|
---|
248 | recentFileActs[j]->setVisible(false);
|
---|
249 |
|
---|
250 | separatorAct->setVisible(numRecentFiles > 0);
|
---|
251 | }
|
---|
252 |
|
---|
253 | QString MainWindow::strippedName(const QString &fullFileName)
|
---|
254 | {
|
---|
255 | return QFileInfo(fullFileName).fileName();
|
---|
256 | }
|
---|