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

« back to all changes in this revision

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