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
textEdit = new QTextEdit;
36
setCentralWidget(textEdit);
45
connect(textEdit->document(), SIGNAL(contentsChanged()),
46
this, SLOT(documentWasModified()));
48
setWindowTitle(tr("Application"));
51
void MainWindow::closeEvent(QCloseEvent *event)
61
void MainWindow::newFile()
69
void MainWindow::open()
72
QString fileName = QFileDialog::getOpenFileName(this);
73
if (!fileName.isEmpty())
78
bool MainWindow::save()
80
if (curFile.isEmpty()) {
83
return saveFile(curFile);
87
bool MainWindow::saveAs()
89
QString fileName = QFileDialog::getSaveFileName(this);
90
if (fileName.isEmpty())
93
return saveFile(fileName);
96
void MainWindow::about()
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."));
104
void MainWindow::documentWasModified()
106
setWindowModified(true);
109
void MainWindow::createActions()
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()));
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()));
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()));
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()));
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()));
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 "
139
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
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 "
145
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
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 "
151
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
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()));
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()));
162
void MainWindow::createMenus()
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);
172
editMenu = menuBar()->addMenu(tr("&Edit"));
173
editMenu->addAction(cutAct);
174
editMenu->addAction(copyAct);
175
editMenu->addAction(pasteAct);
177
menuBar()->addSeparator();
179
helpMenu = menuBar()->addMenu(tr("&Help"));
180
helpMenu->addAction(aboutAct);
181
helpMenu->addAction(aboutQtAct);
184
void MainWindow::createToolBars()
186
fileToolBar = addToolBar(tr("File"));
187
fileToolBar->addAction(newAct);
188
fileToolBar->addAction(openAct);
189
fileToolBar->addAction(saveAct);
191
editToolBar = addToolBar(tr("Edit"));
192
editToolBar->addAction(cutAct);
193
editToolBar->addAction(copyAct);
194
editToolBar->addAction(pasteAct);
197
void MainWindow::createStatusBar()
199
statusBar()->showMessage(tr("Ready"));
202
void MainWindow::readSettings()
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();
211
void MainWindow::writeSettings()
213
QSettings settings("Trolltech", "Application Example");
214
settings.setValue("pos", pos());
215
settings.setValue("size", size());
218
bool MainWindow::maybeSave()
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,
226
QMessageBox::Cancel | QMessageBox::Escape);
227
if (ret == QMessageBox::Yes)
229
else if (ret == QMessageBox::Cancel)
235
void MainWindow::loadFile(const QString &fileName)
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.")
242
.arg(file.errorString()));
246
QTextStream in(&file);
247
QApplication::setOverrideCursor(Qt::WaitCursor);
248
textEdit->setPlainText(in.readAll());
249
QApplication::restoreOverrideCursor();
251
setCurrentFile(fileName);
252
statusBar()->showMessage(tr("File loaded"), 2000);
255
bool MainWindow::saveFile(const QString &fileName)
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.")
262
.arg(file.errorString()));
266
QTextStream out(&file);
267
QApplication::setOverrideCursor(Qt::WaitCursor);
268
out << textEdit->toPlainText();
269
QApplication::restoreOverrideCursor();
271
setCurrentFile(fileName);
272
statusBar()->showMessage(tr("File saved"), 2000);
276
void MainWindow::setCurrentFile(const QString &fileName)
279
textEdit->document()->setModified(false);
280
setWindowModified(false);
282
if (curFile.isEmpty())
283
setWindowTitle(tr("Application"));
285
setWindowTitle(tr("%1[*] - %2").arg(strippedName(curFile))
286
.arg(tr("Application")));
289
QString MainWindow::strippedName(const QString &fullFileName)
291
return QFileInfo(fullFileName).fileName();