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()
39
MainWindow::MainWindow(const QString &fileName)
45
void MainWindow::closeEvent(QCloseEvent *event)
55
void MainWindow::newFile()
57
MainWindow *other = new MainWindow;
58
other->move(x() + 40, y() + 40);
62
void MainWindow::open()
64
QString fileName = QFileDialog::getOpenFileName(this);
65
if (!fileName.isEmpty()) {
66
MainWindow *existing = findMainWindow(fileName);
70
existing->activateWindow();
74
if (isUntitled && textEdit->document()->isEmpty()
75
&& !isWindowModified()) {
78
MainWindow *other = new MainWindow(fileName);
79
if (other->isUntitled) {
83
other->move(x() + 40, y() + 40);
89
bool MainWindow::save()
94
return saveFile(curFile);
98
bool MainWindow::saveAs()
100
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
102
if (fileName.isEmpty())
105
return saveFile(fileName);
108
void MainWindow::about()
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."));
115
void MainWindow::documentWasModified()
117
setWindowModified(true);
120
void MainWindow::init()
122
setAttribute(Qt::WA_DeleteOnClose);
126
textEdit = new QTextEdit;
127
setCentralWidget(textEdit);
136
connect(textEdit->document(), SIGNAL(contentsChanged()),
137
this, SLOT(documentWasModified()));
140
void MainWindow::createActions()
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()));
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()));
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()));
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()));
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()));
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()));
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 "
175
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));
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 "
181
connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));
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 "
187
connect(pasteAct, SIGNAL(triggered()), textEdit, SLOT(paste()));
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()));
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()));
198
void MainWindow::createMenus()
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);
209
editMenu = menuBar()->addMenu(tr("&Edit"));
210
editMenu->addAction(cutAct);
211
editMenu->addAction(copyAct);
212
editMenu->addAction(pasteAct);
214
menuBar()->addSeparator();
216
helpMenu = menuBar()->addMenu(tr("&Help"));
217
helpMenu->addAction(aboutAct);
218
helpMenu->addAction(aboutQtAct);
221
void MainWindow::createToolBars()
223
fileToolBar = addToolBar(tr("File"));
224
fileToolBar->addAction(newAct);
225
fileToolBar->addAction(openAct);
226
fileToolBar->addAction(saveAct);
228
editToolBar = addToolBar(tr("Edit"));
229
editToolBar->addAction(cutAct);
230
editToolBar->addAction(copyAct);
231
editToolBar->addAction(pasteAct);
234
void MainWindow::createStatusBar()
236
statusBar()->showMessage(tr("Ready"));
239
void MainWindow::readSettings()
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();
248
void MainWindow::writeSettings()
250
QSettings settings("Trolltech", "SDI Example");
251
settings.setValue("pos", pos());
252
settings.setValue("size", size());
255
bool MainWindow::maybeSave()
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,
263
QMessageBox::Cancel | QMessageBox::Escape);
264
if (ret == QMessageBox::Yes)
266
else if (ret == QMessageBox::Cancel)
272
void MainWindow::loadFile(const QString &fileName)
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.")
280
.arg(file.errorString()));
284
QTextStream in(&file);
285
QApplication::setOverrideCursor(Qt::WaitCursor);
286
textEdit->setPlainText(in.readAll());
287
QApplication::restoreOverrideCursor();
289
setCurrentFile(fileName);
290
statusBar()->showMessage(tr("File loaded"), 2000);
293
bool MainWindow::saveFile(const QString &fileName)
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.")
300
.arg(file.errorString()));
304
QTextStream out(&file);
305
QApplication::setOverrideCursor(Qt::WaitCursor);
306
out << textEdit->toPlainText();
307
QApplication::restoreOverrideCursor();
309
setCurrentFile(fileName);
310
statusBar()->showMessage(tr("File saved"), 2000);
314
void MainWindow::setCurrentFile(const QString &fileName)
316
static int sequenceNumber = 1;
318
isUntitled = fileName.isEmpty();
320
curFile = tr("document%1.txt").arg(sequenceNumber++);
322
curFile = QFileInfo(fileName).canonicalFilePath();
325
textEdit->document()->setModified(false);
326
setWindowModified(false);
328
setWindowTitle(tr("%1[*] - %2").arg(strippedName(curFile))
332
QString MainWindow::strippedName(const QString &fullFileName)
334
return QFileInfo(fullFileName).fileName();
337
MainWindow *MainWindow::findMainWindow(const QString &fileName)
339
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
341
foreach (QWidget *widget, qApp->topLevelWidgets()) {
342
MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
343
if (mainWin && mainWin->curFile == canonicalFilePath)