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

« back to all changes in this revision

Viewing changes to examples/mainwindows/recentfiles/mainwindow.cpp

  • 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
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "mainwindow.h"
 
32
 
 
33
MainWindow::MainWindow()
 
34
{
 
35
    setAttribute(Qt::WA_DeleteOnClose);
 
36
 
 
37
    textEdit = new QTextEdit;
 
38
    setCentralWidget(textEdit);
 
39
 
 
40
    createActions();
 
41
    createMenus();
 
42
    (void)statusBar();
 
43
 
 
44
    setWindowTitle(tr("Recent Files"));
 
45
    resize(400, 300);
 
46
}
 
47
 
 
48
void MainWindow::newFile()
 
49
{
 
50
    MainWindow *other = new MainWindow;
 
51
    other->show();
 
52
}
 
53
 
 
54
void MainWindow::open()
 
55
{
 
56
    QString fileName = QFileDialog::getOpenFileName(this);
 
57
    if (!fileName.isEmpty())
 
58
        loadFile(fileName);
 
59
}
 
60
 
 
61
void MainWindow::save()
 
62
{
 
63
    if (curFile.isEmpty())
 
64
        saveAs();
 
65
    else
 
66
        saveFile(curFile);
 
67
}
 
68
 
 
69
void MainWindow::saveAs()
 
70
{
 
71
    QString fileName = QFileDialog::getSaveFileName(this);
 
72
    if (fileName.isEmpty())
 
73
        return;
 
74
 
 
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)
 
83
            return;
 
84
    }
 
85
    saveFile(fileName);
 
86
}
 
87
 
 
88
void MainWindow::openRecentFile()
 
89
{
 
90
    QAction *action = qobject_cast<QAction *>(sender());
 
91
    if (action)
 
92
        loadFile(action->data().toString());
 
93
}
 
94
 
 
95
void MainWindow::about()
 
96
{
 
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."));
 
100
}
 
101
 
 
102
void MainWindow::createActions()
 
103
{
 
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()));
 
108
 
 
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()));
 
113
 
 
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()));
 
118
 
 
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()));
 
122
 
 
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()));
 
128
    }
 
129
 
 
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()));
 
134
 
 
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()));
 
139
 
 
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()));
 
143
 
 
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()));
 
147
}
 
148
 
 
149
void MainWindow::createMenus()
 
150
{
 
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();
 
162
 
 
163
    menuBar()->addSeparator();
 
164
 
 
165
    helpMenu = menuBar()->addMenu(tr("&Help"));
 
166
    helpMenu->addAction(aboutAct);
 
167
    helpMenu->addAction(aboutQtAct);
 
168
}
 
169
 
 
170
void MainWindow::loadFile(const QString &fileName)
 
171
{
 
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.")
 
176
                             .arg(fileName)
 
177
                             .arg(file.errorString()));
 
178
        return;
 
179
    }
 
180
 
 
181
    QTextStream in(&file);
 
182
    QApplication::setOverrideCursor(Qt::WaitCursor);
 
183
    textEdit->setPlainText(in.readAll());
 
184
    QApplication::restoreOverrideCursor();
 
185
 
 
186
    setCurrentFile(fileName);
 
187
    statusBar()->showMessage(tr("File loaded"), 2000);
 
188
}
 
189
 
 
190
void MainWindow::saveFile(const QString &fileName)
 
191
{
 
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.")
 
196
                             .arg(fileName)
 
197
                             .arg(file.errorString()));
 
198
        return;
 
199
    }
 
200
 
 
201
    QTextStream out(&file);
 
202
    QApplication::setOverrideCursor(Qt::WaitCursor);
 
203
    out << textEdit->toPlainText();
 
204
    QApplication::restoreOverrideCursor();
 
205
 
 
206
    setCurrentFile(fileName);
 
207
    statusBar()->showMessage(tr("File saved"), 2000);
 
208
}
 
209
 
 
210
void MainWindow::setCurrentFile(const QString &fileName)
 
211
{
 
212
    curFile = fileName;
 
213
    if (curFile.isEmpty())
 
214
        setWindowTitle(tr("Recent Files"));
 
215
    else
 
216
        setWindowTitle(tr("%1 - %2").arg(strippedName(curFile))
 
217
                                    .arg(tr("Recent Files")));
 
218
 
 
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)
 
224
        files.removeLast();
 
225
 
 
226
    settings.setValue("recentFileList", files);
 
227
 
 
228
    foreach (QWidget *widget, QApplication::topLevelWidgets()) {
 
229
        MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
 
230
        if (mainWin)
 
231
            mainWin->updateRecentFileActions();
 
232
    }
 
233
}
 
234
 
 
235
void MainWindow::updateRecentFileActions()
 
236
{
 
237
    QSettings settings("Trolltech", "Recent Files Example");
 
238
    QStringList files = settings.value("recentFileList").toStringList();
 
239
 
 
240
    int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);
 
241
 
 
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);
 
247
    }
 
248
    for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
 
249
        recentFileActs[j]->setVisible(false);
 
250
 
 
251
    separatorAct->setVisible(numRecentFiles > 0);
 
252
}
 
253
 
 
254
QString MainWindow::strippedName(const QString &fullFileName)
 
255
{
 
256
    return QFileInfo(fullFileName).fileName();
 
257
}