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

« back to all changes in this revision

Viewing changes to examples/mainwindows/sdi/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
    init();
 
36
    setCurrentFile("");
 
37
}
 
38
 
 
39
MainWindow::MainWindow(const QString &fileName)
 
40
{
 
41
    init();
 
42
    loadFile(fileName);
 
43
}
 
44
 
 
45
void MainWindow::closeEvent(QCloseEvent *event)
 
46
{
 
47
    if (maybeSave()) {
 
48
        writeSettings();
 
49
        event->accept();
 
50
    } else {
 
51
        event->ignore();
 
52
    }
 
53
}
 
54
 
 
55
void MainWindow::newFile()
 
56
{
 
57
    MainWindow *other = new MainWindow;
 
58
    other->move(x() + 40, y() + 40);
 
59
    other->show();
 
60
}
 
61
 
 
62
void MainWindow::open()
 
63
{
 
64
    QString fileName = QFileDialog::getOpenFileName(this);
 
65
    if (!fileName.isEmpty()) {
 
66
        MainWindow *existing = findMainWindow(fileName);
 
67
        if (existing) {
 
68
            existing->show();
 
69
            existing->raise();
 
70
            existing->activateWindow();
 
71
            return;
 
72
        }
 
73
 
 
74
        if (isUntitled && textEdit->document()->isEmpty()
 
75
                && !isWindowModified()) {
 
76
            loadFile(fileName);
 
77
        } else {
 
78
            MainWindow *other = new MainWindow(fileName);
 
79
            if (other->isUntitled) {
 
80
                delete other;
 
81
                return;
 
82
            }
 
83
            other->move(x() + 40, y() + 40);
 
84
            other->show();
 
85
        }
 
86
    }
 
87
}
 
88
 
 
89
bool MainWindow::save()
 
90
{
 
91
    if (isUntitled) {
 
92
        return saveAs();
 
93
    } else {
 
94
        return saveFile(curFile);
 
95
    }
 
96
}
 
97
 
 
98
bool MainWindow::saveAs()
 
99
{
 
100
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
 
101
                                                    curFile);
 
102
    if (fileName.isEmpty())
 
103
        return false;
 
104
 
 
105
    return saveFile(fileName);
 
106
}
 
107
 
 
108
void MainWindow::about()
 
109
{
 
110
   QMessageBox::about(this, tr("About SDI"),
 
111
            tr("The <b>SDI</b> example demonstrates how to write single "
 
112
               "document interface applications using Qt."));
 
113
}
 
114
 
 
115
void MainWindow::documentWasModified()
 
116
{
 
117
    setWindowModified(true);
 
118
}
 
119
 
 
120
void MainWindow::init()
 
121
{
 
122
    setAttribute(Qt::WA_DeleteOnClose);
 
123
 
 
124
    isUntitled = true;
 
125
 
 
126
    textEdit = new QTextEdit;
 
127
    setCentralWidget(textEdit);
 
128
 
 
129
    createActions();
 
130
    createMenus();
 
131
    createToolBars();
 
132
    createStatusBar();
 
133
 
 
134
    readSettings();
 
135
 
 
136
    connect(textEdit->document(), SIGNAL(contentsChanged()),
 
137
            this, SLOT(documentWasModified()));
 
138
}
 
139
 
 
140
void MainWindow::createActions()
 
141
{
 
142
    newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
 
143
    newAct->setShortcut(tr("Ctrl+N"));
 
144
    newAct->setStatusTip(tr("Create a new file"));
 
145
    connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
 
146
 
 
147
    openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
 
148
    openAct->setShortcut(tr("Ctrl+O"));
 
149
    openAct->setStatusTip(tr("Open an existing file"));
 
150
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
151
 
 
152
    saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
 
153
    saveAct->setShortcut(tr("Ctrl+S"));
 
154
    saveAct->setStatusTip(tr("Save the document to disk"));
 
155
    connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
 
156
 
 
157
    saveAsAct = new QAction(tr("Save &As..."), this);
 
158
    saveAsAct->setStatusTip(tr("Save the document under a new name"));
 
159
    connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
 
160
 
 
161
    closeAct = new QAction(tr("&Close"), this);
 
162
    closeAct->setShortcut(tr("Ctrl+W"));
 
163
    closeAct->setStatusTip(tr("Close this window"));
 
164
    connect(closeAct, SIGNAL(triggered()), this, SLOT(close()));
 
165
 
 
166
    exitAct = new QAction(tr("E&xit"), this);
 
167
    exitAct->setShortcut(tr("Ctrl+Q"));
 
168
    exitAct->setStatusTip(tr("Exit the application"));
 
169
    connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
 
170
 
 
171
    cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
 
172
    cutAct->setShortcut(tr("Ctrl+X"));
 
173
    cutAct->setStatusTip(tr("Cut the current selection's contents to the "
 
174
                            "clipboard"));
 
175
    connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
 
176
 
 
177
    copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
 
178
    copyAct->setShortcut(tr("Ctrl+C"));
 
179
    copyAct->setStatusTip(tr("Copy the current selection's contents to the "
 
180
                             "clipboard"));
 
181
    connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
 
182
 
 
183
    pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
 
184
    pasteAct->setShortcut(tr("Ctrl+V"));
 
185
    pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
 
186
                              "selection"));
 
187
    connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
 
188
 
 
189
    aboutAct = new QAction(tr("&About"), this);
 
190
    aboutAct->setStatusTip(tr("Show the application's About box"));
 
191
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
192
 
 
193
    aboutQtAct = new QAction(tr("About &Qt"), this);
 
194
    aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
 
195
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
196
}
 
197
 
 
198
void MainWindow::createMenus()
 
199
{
 
200
    fileMenu = menuBar()->addMenu(tr("&File"));
 
201
    fileMenu->addAction(newAct);
 
202
    fileMenu->addAction(openAct);
 
203
    fileMenu->addAction(saveAct);
 
204
    fileMenu->addAction(saveAsAct);
 
205
    fileMenu->addSeparator();
 
206
    fileMenu->addAction(closeAct);
 
207
    fileMenu->addAction(exitAct);
 
208
 
 
209
    editMenu = menuBar()->addMenu(tr("&Edit"));
 
210
    editMenu->addAction(cutAct);
 
211
    editMenu->addAction(copyAct);
 
212
    editMenu->addAction(pasteAct);
 
213
 
 
214
    menuBar()->addSeparator();
 
215
 
 
216
    helpMenu = menuBar()->addMenu(tr("&Help"));
 
217
    helpMenu->addAction(aboutAct);
 
218
    helpMenu->addAction(aboutQtAct);
 
219
}
 
220
 
 
221
void MainWindow::createToolBars()
 
222
{
 
223
    fileToolBar = addToolBar(tr("File"));
 
224
    fileToolBar->addAction(newAct);
 
225
    fileToolBar->addAction(openAct);
 
226
    fileToolBar->addAction(saveAct);
 
227
 
 
228
    editToolBar = addToolBar(tr("Edit"));
 
229
    editToolBar->addAction(cutAct);
 
230
    editToolBar->addAction(copyAct);
 
231
    editToolBar->addAction(pasteAct);
 
232
}
 
233
 
 
234
void MainWindow::createStatusBar()
 
235
{
 
236
    statusBar()->showMessage(tr("Ready"));
 
237
}
 
238
 
 
239
void MainWindow::readSettings()
 
240
{
 
241
    QSettings settings("Trolltech", "SDI Example");
 
242
    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
 
243
    QSize size = settings.value("size", QSize(400, 400)).toSize();
 
244
    move(pos);
 
245
    resize(size);
 
246
}
 
247
 
 
248
void MainWindow::writeSettings()
 
249
{
 
250
    QSettings settings("Trolltech", "SDI Example");
 
251
    settings.setValue("pos", pos());
 
252
    settings.setValue("size", size());
 
253
}
 
254
 
 
255
bool MainWindow::maybeSave()
 
256
{
 
257
    if (textEdit->document()->isModified()) {
 
258
        int ret = QMessageBox::warning(this, tr("SDI"),
 
259
                     tr("The document has been modified.\n"
 
260
                        "Do you want to save your changes?"),
 
261
                     QMessageBox::Yes | QMessageBox::Default,
 
262
                     QMessageBox::No,
 
263
                     QMessageBox::Cancel | QMessageBox::Escape);
 
264
        if (ret == QMessageBox::Yes)
 
265
            return save();
 
266
        else if (ret == QMessageBox::Cancel)
 
267
            return false;
 
268
    }
 
269
    return true;
 
270
}
 
271
 
 
272
void MainWindow::loadFile(const QString &fileName)
 
273
{
 
274
 
 
275
    QFile file(fileName);
 
276
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
 
277
        QMessageBox::warning(this, tr("SDI"),
 
278
                             tr("Cannot read file %1:\n%2.")
 
279
                             .arg(fileName)
 
280
                             .arg(file.errorString()));
 
281
        return;
 
282
    }
 
283
 
 
284
    QTextStream in(&file);
 
285
    QApplication::setOverrideCursor(Qt::WaitCursor);
 
286
    textEdit->setPlainText(in.readAll());
 
287
    QApplication::restoreOverrideCursor();
 
288
 
 
289
    setCurrentFile(fileName);
 
290
    statusBar()->showMessage(tr("File loaded"), 2000);
 
291
}
 
292
 
 
293
bool MainWindow::saveFile(const QString &fileName)
 
294
{
 
295
    QFile file(fileName);
 
296
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
 
297
        QMessageBox::warning(this, tr("SDI"),
 
298
                             tr("Cannot write file %1:\n%2.")
 
299
                             .arg(fileName)
 
300
                             .arg(file.errorString()));
 
301
        return false;
 
302
    }
 
303
 
 
304
    QTextStream out(&file);
 
305
    QApplication::setOverrideCursor(Qt::WaitCursor);
 
306
    out << textEdit->toPlainText();
 
307
    QApplication::restoreOverrideCursor();
 
308
 
 
309
    setCurrentFile(fileName);
 
310
    statusBar()->showMessage(tr("File saved"), 2000);
 
311
    return true;
 
312
}
 
313
 
 
314
void MainWindow::setCurrentFile(const QString &fileName)
 
315
{
 
316
    static int sequenceNumber = 1;
 
317
 
 
318
    isUntitled = fileName.isEmpty();
 
319
    if (isUntitled) {
 
320
        curFile = tr("document%1.txt").arg(sequenceNumber++);
 
321
    } else {
 
322
        curFile = QFileInfo(fileName).canonicalFilePath();
 
323
    }
 
324
 
 
325
    textEdit->document()->setModified(false);
 
326
    setWindowModified(false);
 
327
 
 
328
    setWindowTitle(tr("%1[*] - %2").arg(strippedName(curFile))
 
329
                                       .arg(tr("SDI")));
 
330
}
 
331
 
 
332
QString MainWindow::strippedName(const QString &fullFileName)
 
333
{
 
334
    return QFileInfo(fullFileName).fileName();
 
335
}
 
336
 
 
337
MainWindow *MainWindow::findMainWindow(const QString &fileName)
 
338
{
 
339
    QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
 
340
 
 
341
    foreach (QWidget *widget, qApp->topLevelWidgets()) {
 
342
        MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
 
343
        if (mainWin && mainWin->curFile == canonicalFilePath)
 
344
            return mainWin;
 
345
    }
 
346
    return 0;
 
347
}