1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "mainwindow.h"
|
---|
44 |
|
---|
45 | MainWindow::MainWindow()
|
---|
46 | {
|
---|
47 | setAttribute(Qt::WA_DeleteOnClose);
|
---|
48 |
|
---|
49 | textEdit = new QTextEdit;
|
---|
50 | setCentralWidget(textEdit);
|
---|
51 |
|
---|
52 | createActions();
|
---|
53 | createMenus();
|
---|
54 | (void)statusBar();
|
---|
55 |
|
---|
56 | setWindowFilePath(QString());
|
---|
57 | resize(400, 300);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void MainWindow::newFile()
|
---|
61 | {
|
---|
62 | MainWindow *other = new MainWindow;
|
---|
63 | other->show();
|
---|
64 | }
|
---|
65 |
|
---|
66 | void MainWindow::open()
|
---|
67 | {
|
---|
68 | QString fileName = QFileDialog::getOpenFileName(this);
|
---|
69 | if (!fileName.isEmpty())
|
---|
70 | loadFile(fileName);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void MainWindow::save()
|
---|
74 | {
|
---|
75 | if (curFile.isEmpty())
|
---|
76 | saveAs();
|
---|
77 | else
|
---|
78 | saveFile(curFile);
|
---|
79 | }
|
---|
80 |
|
---|
81 | void MainWindow::saveAs()
|
---|
82 | {
|
---|
83 | QString fileName = QFileDialog::getSaveFileName(this);
|
---|
84 | if (fileName.isEmpty())
|
---|
85 | return;
|
---|
86 |
|
---|
87 | saveFile(fileName);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void MainWindow::openRecentFile()
|
---|
91 | {
|
---|
92 | QAction *action = qobject_cast<QAction *>(sender());
|
---|
93 | if (action)
|
---|
94 | loadFile(action->data().toString());
|
---|
95 | }
|
---|
96 |
|
---|
97 | void MainWindow::about()
|
---|
98 | {
|
---|
99 | QMessageBox::about(this, tr("About Recent Files"),
|
---|
100 | tr("The <b>Recent Files</b> example demonstrates how to provide a "
|
---|
101 | "recently used file menu in a Qt application."));
|
---|
102 | }
|
---|
103 |
|
---|
104 | void MainWindow::createActions()
|
---|
105 | {
|
---|
106 | newAct = new QAction(tr("&New"), this);
|
---|
107 | newAct->setShortcuts(QKeySequence::New);
|
---|
108 | newAct->setStatusTip(tr("Create a new file"));
|
---|
109 | connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
|
---|
110 |
|
---|
111 | openAct = new QAction(tr("&Open..."), this);
|
---|
112 | openAct->setShortcuts(QKeySequence::Open);
|
---|
113 | openAct->setStatusTip(tr("Open an existing file"));
|
---|
114 | connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
---|
115 |
|
---|
116 | saveAct = new QAction(tr("&Save"), this);
|
---|
117 | saveAct->setShortcuts(QKeySequence::Save);
|
---|
118 | saveAct->setStatusTip(tr("Save the document to disk"));
|
---|
119 | connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
|
---|
120 |
|
---|
121 | saveAsAct = new QAction(tr("Save &As..."), this);
|
---|
122 | saveAsAct->setShortcuts(QKeySequence::SaveAs);
|
---|
123 | saveAsAct->setStatusTip(tr("Save the document under a new name"));
|
---|
124 | connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
|
---|
125 |
|
---|
126 | for (int i = 0; i < MaxRecentFiles; ++i) {
|
---|
127 | recentFileActs[i] = new QAction(this);
|
---|
128 | recentFileActs[i]->setVisible(false);
|
---|
129 | connect(recentFileActs[i], SIGNAL(triggered()),
|
---|
130 | this, SLOT(openRecentFile()));
|
---|
131 | }
|
---|
132 |
|
---|
133 | exitAct = new QAction(tr("E&xit"), this);
|
---|
134 | exitAct->setShortcuts(QKeySequence::Quit);
|
---|
135 | exitAct->setStatusTip(tr("Exit the application"));
|
---|
136 | connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
|
---|
137 |
|
---|
138 | aboutAct = new QAction(tr("&About"), this);
|
---|
139 | aboutAct->setStatusTip(tr("Show the application's About box"));
|
---|
140 | connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
---|
141 |
|
---|
142 | aboutQtAct = new QAction(tr("About &Qt"), this);
|
---|
143 | aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
|
---|
144 | connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
---|
145 | }
|
---|
146 |
|
---|
147 | void MainWindow::createMenus()
|
---|
148 | {
|
---|
149 | fileMenu = menuBar()->addMenu(tr("&File"));
|
---|
150 | fileMenu->addAction(newAct);
|
---|
151 | fileMenu->addAction(openAct);
|
---|
152 | fileMenu->addAction(saveAct);
|
---|
153 | fileMenu->addAction(saveAsAct);
|
---|
154 | separatorAct = fileMenu->addSeparator();
|
---|
155 | for (int i = 0; i < MaxRecentFiles; ++i)
|
---|
156 | fileMenu->addAction(recentFileActs[i]);
|
---|
157 | fileMenu->addSeparator();
|
---|
158 | fileMenu->addAction(exitAct);
|
---|
159 | updateRecentFileActions();
|
---|
160 |
|
---|
161 | menuBar()->addSeparator();
|
---|
162 |
|
---|
163 | helpMenu = menuBar()->addMenu(tr("&Help"));
|
---|
164 | helpMenu->addAction(aboutAct);
|
---|
165 | helpMenu->addAction(aboutQtAct);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void MainWindow::loadFile(const QString &fileName)
|
---|
169 | {
|
---|
170 | QFile file(fileName);
|
---|
171 | if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
---|
172 | QMessageBox::warning(this, tr("Recent Files"),
|
---|
173 | tr("Cannot read file %1:\n%2.")
|
---|
174 | .arg(fileName)
|
---|
175 | .arg(file.errorString()));
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | QTextStream in(&file);
|
---|
180 | QApplication::setOverrideCursor(Qt::WaitCursor);
|
---|
181 | textEdit->setPlainText(in.readAll());
|
---|
182 | QApplication::restoreOverrideCursor();
|
---|
183 |
|
---|
184 | setCurrentFile(fileName);
|
---|
185 | statusBar()->showMessage(tr("File loaded"), 2000);
|
---|
186 | }
|
---|
187 |
|
---|
188 | void MainWindow::saveFile(const QString &fileName)
|
---|
189 | {
|
---|
190 | QFile file(fileName);
|
---|
191 | if (!file.open(QFile::WriteOnly | QFile::Text)) {
|
---|
192 | QMessageBox::warning(this, tr("Recent Files"),
|
---|
193 | tr("Cannot write file %1:\n%2.")
|
---|
194 | .arg(fileName)
|
---|
195 | .arg(file.errorString()));
|
---|
196 | return;
|
---|
197 | }
|
---|
198 |
|
---|
199 | QTextStream out(&file);
|
---|
200 | QApplication::setOverrideCursor(Qt::WaitCursor);
|
---|
201 | out << textEdit->toPlainText();
|
---|
202 | QApplication::restoreOverrideCursor();
|
---|
203 |
|
---|
204 | setCurrentFile(fileName);
|
---|
205 | statusBar()->showMessage(tr("File saved"), 2000);
|
---|
206 | }
|
---|
207 |
|
---|
208 | void MainWindow::setCurrentFile(const QString &fileName)
|
---|
209 | {
|
---|
210 | curFile = fileName;
|
---|
211 | setWindowFilePath(curFile);
|
---|
212 |
|
---|
213 | QSettings settings;
|
---|
214 | QStringList files = settings.value("recentFileList").toStringList();
|
---|
215 | files.removeAll(fileName);
|
---|
216 | files.prepend(fileName);
|
---|
217 | while (files.size() > MaxRecentFiles)
|
---|
218 | files.removeLast();
|
---|
219 |
|
---|
220 | settings.setValue("recentFileList", files);
|
---|
221 |
|
---|
222 | foreach (QWidget *widget, QApplication::topLevelWidgets()) {
|
---|
223 | MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
|
---|
224 | if (mainWin)
|
---|
225 | mainWin->updateRecentFileActions();
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | void MainWindow::updateRecentFileActions()
|
---|
230 | {
|
---|
231 | QSettings settings;
|
---|
232 | QStringList files = settings.value("recentFileList").toStringList();
|
---|
233 |
|
---|
234 | int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
|
---|
235 |
|
---|
236 | for (int i = 0; i < numRecentFiles; ++i) {
|
---|
237 | QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
|
---|
238 | recentFileActs[i]->setText(text);
|
---|
239 | recentFileActs[i]->setData(files[i]);
|
---|
240 | recentFileActs[i]->setVisible(true);
|
---|
241 | }
|
---|
242 | for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
|
---|
243 | recentFileActs[j]->setVisible(false);
|
---|
244 |
|
---|
245 | separatorAct->setVisible(numRecentFiles > 0);
|
---|
246 | }
|
---|
247 |
|
---|
248 | QString MainWindow::strippedName(const QString &fullFileName)
|
---|
249 | {
|
---|
250 | return QFileInfo(fullFileName).fileName();
|
---|
251 | }
|
---|