|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 setWindowFilePath(QString()); |
|
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->setShortcuts(QKeySequence::Quit); |
|
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 setWindowFilePath(curFile); |
|
213 |
|
214 QSettings settings; |
|
215 QStringList files = settings.value("recentFileList").toStringList(); |
|
216 files.removeAll(fileName); |
|
217 files.prepend(fileName); |
|
218 while (files.size() > MaxRecentFiles) |
|
219 files.removeLast(); |
|
220 |
|
221 settings.setValue("recentFileList", files); |
|
222 |
|
223 foreach (QWidget *widget, QApplication::topLevelWidgets()) { |
|
224 MainWindow *mainWin = qobject_cast<MainWindow *>(widget); |
|
225 if (mainWin) |
|
226 mainWin->updateRecentFileActions(); |
|
227 } |
|
228 } |
|
229 |
|
230 void MainWindow::updateRecentFileActions() |
|
231 { |
|
232 QSettings settings; |
|
233 QStringList files = settings.value("recentFileList").toStringList(); |
|
234 |
|
235 int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles); |
|
236 |
|
237 for (int i = 0; i < numRecentFiles; ++i) { |
|
238 QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i])); |
|
239 recentFileActs[i]->setText(text); |
|
240 recentFileActs[i]->setData(files[i]); |
|
241 recentFileActs[i]->setVisible(true); |
|
242 } |
|
243 for (int j = numRecentFiles; j < MaxRecentFiles; ++j) |
|
244 recentFileActs[j]->setVisible(false); |
|
245 |
|
246 separatorAct->setVisible(numRecentFiles > 0); |
|
247 } |
|
248 |
|
249 QString MainWindow::strippedName(const QString &fullFileName) |
|
250 { |
|
251 return QFileInfo(fullFileName).fileName(); |
|
252 } |