1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "mainwindow.h"
33
MainWindow::MainWindow()
35
setAttribute(Qt::WA_DeleteOnClose);
37
textEdit = new QTextEdit;
38
setCentralWidget(textEdit);
44
setWindowTitle(tr("Recent Files"));
48
void MainWindow::newFile()
50
MainWindow *other = new MainWindow;
54
void MainWindow::open()
56
QString fileName = QFileDialog::getOpenFileName(this);
57
if (!fileName.isEmpty())
61
void MainWindow::save()
63
if (curFile.isEmpty())
69
void MainWindow::saveAs()
71
QString fileName = QFileDialog::getSaveFileName(this);
72
if (fileName.isEmpty())
75
if (QFile::exists(fileName)) {
76
int ret = QMessageBox::warning(this, tr("Recent Files"),
77
tr("File %1 already exists.\n"
78
"Do you want to overwrite it?")
79
.arg(QDir::convertSeparators(fileName)),
80
QMessageBox::Yes | QMessageBox::Default,
81
QMessageBox::No | QMessageBox::Escape);
82
if (ret == QMessageBox::No)
88
void MainWindow::openRecentFile()
90
QAction *action = qobject_cast<QAction *>(sender());
92
loadFile(action->data().toString());
95
void MainWindow::about()
97
QMessageBox::about(this, tr("About Recent Files"),
98
tr("The <b>Recent Files</b> example demonstrates how to provide a "
99
"recently used file menu in a Qt application."));
102
void MainWindow::createActions()
104
newAct = new QAction(tr("&New"), this);
105
newAct->setShortcut(tr("Ctrl+N"));
106
newAct->setStatusTip(tr("Create a new file"));
107
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
109
openAct = new QAction(tr("&Open..."), this);
110
openAct->setShortcut(tr("Ctrl+O"));
111
openAct->setStatusTip(tr("Open an existing file"));
112
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
114
saveAct = new QAction(tr("&Save"), this);
115
saveAct->setShortcut(tr("Ctrl+S"));
116
saveAct->setStatusTip(tr("Save the document to disk"));
117
connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
119
saveAsAct = new QAction(tr("Save &As..."), this);
120
saveAsAct->setStatusTip(tr("Save the document under a new name"));
121
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
123
for (int i = 0; i < MaxRecentFiles; ++i) {
124
recentFileActs[i] = new QAction(this);
125
recentFileActs[i]->setVisible(false);
126
connect(recentFileActs[i], SIGNAL(triggered()),
127
this, SLOT(openRecentFile()));
130
exitAct = new QAction(tr("&Close"), this);
131
exitAct->setShortcut(tr("Ctrl+W"));
132
exitAct->setStatusTip(tr("Close this window"));
133
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
135
exitAct = new QAction(tr("E&xit"), this);
136
exitAct->setShortcut(tr("Ctrl+Q"));
137
exitAct->setStatusTip(tr("Exit the application"));
138
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
140
aboutAct = new QAction(tr("&About"), this);
141
aboutAct->setStatusTip(tr("Show the application's About box"));
142
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
144
aboutQtAct = new QAction(tr("About &Qt"), this);
145
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
146
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
149
void MainWindow::createMenus()
151
fileMenu = menuBar()->addMenu(tr("&File"));
152
fileMenu->addAction(newAct);
153
fileMenu->addAction(openAct);
154
fileMenu->addAction(saveAct);
155
fileMenu->addAction(saveAsAct);
156
separatorAct = fileMenu->addSeparator();
157
for (int i = 0; i < MaxRecentFiles; ++i)
158
fileMenu->addAction(recentFileActs[i]);
159
fileMenu->addSeparator();
160
fileMenu->addAction(exitAct);
161
updateRecentFileActions();
163
menuBar()->addSeparator();
165
helpMenu = menuBar()->addMenu(tr("&Help"));
166
helpMenu->addAction(aboutAct);
167
helpMenu->addAction(aboutQtAct);
170
void MainWindow::loadFile(const QString &fileName)
172
QFile file(fileName);
173
if (!file.open(QFile::ReadOnly | QFile::Text)) {
174
QMessageBox::warning(this, tr("Recent Files"),
175
tr("Cannot read file %1:\n%2.")
177
.arg(file.errorString()));
181
QTextStream in(&file);
182
QApplication::setOverrideCursor(Qt::WaitCursor);
183
textEdit->setPlainText(in.readAll());
184
QApplication::restoreOverrideCursor();
186
setCurrentFile(fileName);
187
statusBar()->showMessage(tr("File loaded"), 2000);
190
void MainWindow::saveFile(const QString &fileName)
192
QFile file(fileName);
193
if (!file.open(QFile::WriteOnly | QFile::Text)) {
194
QMessageBox::warning(this, tr("Recent Files"),
195
tr("Cannot write file %1:\n%2.")
197
.arg(file.errorString()));
201
QTextStream out(&file);
202
QApplication::setOverrideCursor(Qt::WaitCursor);
203
out << textEdit->toPlainText();
204
QApplication::restoreOverrideCursor();
206
setCurrentFile(fileName);
207
statusBar()->showMessage(tr("File saved"), 2000);
210
void MainWindow::setCurrentFile(const QString &fileName)
213
if (curFile.isEmpty())
214
setWindowTitle(tr("Recent Files"));
216
setWindowTitle(tr("%1 - %2").arg(strippedName(curFile))
217
.arg(tr("Recent Files")));
219
QSettings settings("Trolltech", "Recent Files Example");
220
QStringList files = settings.value("recentFileList").toStringList();
221
files.removeAll(fileName);
222
files.prepend(fileName);
223
while (files.size() > MaxRecentFiles)
226
settings.setValue("recentFileList", files);
228
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
229
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
231
mainWin->updateRecentFileActions();
235
void MainWindow::updateRecentFileActions()
237
QSettings settings("Trolltech", "Recent Files Example");
238
QStringList files = settings.value("recentFileList").toStringList();
240
int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
242
for (int i = 0; i < numRecentFiles; ++i) {
243
QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
244
recentFileActs[i]->setText(text);
245
recentFileActs[i]->setData(files[i]);
246
recentFileActs[i]->setVisible(true);
248
for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
249
recentFileActs[j]->setVisible(false);
251
separatorAct->setVisible(numRecentFiles > 0);
254
QString MainWindow::strippedName(const QString &fullFileName)
256
return QFileInfo(fullFileName).fileName();