1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
47
setAttribute(Qt::WA_DeleteOnClose);
51
void MdiChild::newFile()
53
static int sequenceNumber = 1;
56
curFile = tr("document%1.txt").arg(sequenceNumber++);
57
setWindowTitle(curFile + "[*]");
59
connect(document(), SIGNAL(contentsChanged()),
60
this, SLOT(documentWasModified()));
63
bool MdiChild::loadFile(const QString &fileName)
66
if (!file.open(QFile::ReadOnly | QFile::Text)) {
67
QMessageBox::warning(this, tr("MDI"),
68
tr("Cannot read file %1:\n%2.")
70
.arg(file.errorString()));
74
QTextStream in(&file);
75
QApplication::setOverrideCursor(Qt::WaitCursor);
76
setPlainText(in.readAll());
77
QApplication::restoreOverrideCursor();
79
setCurrentFile(fileName);
81
connect(document(), SIGNAL(contentsChanged()),
82
this, SLOT(documentWasModified()));
92
return saveFile(curFile);
96
bool MdiChild::saveAs()
98
QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
100
if (fileName.isEmpty())
103
return saveFile(fileName);
106
bool MdiChild::saveFile(const QString &fileName)
108
QFile file(fileName);
109
if (!file.open(QFile::WriteOnly | QFile::Text)) {
110
QMessageBox::warning(this, tr("MDI"),
111
tr("Cannot write file %1:\n%2.")
113
.arg(file.errorString()));
117
QTextStream out(&file);
118
QApplication::setOverrideCursor(Qt::WaitCursor);
119
out << toPlainText();
120
QApplication::restoreOverrideCursor();
122
setCurrentFile(fileName);
126
QString MdiChild::userFriendlyCurrentFile()
128
return strippedName(curFile);
131
void MdiChild::closeEvent(QCloseEvent *event)
140
void MdiChild::documentWasModified()
142
setWindowModified(document()->isModified());
145
bool MdiChild::maybeSave()
147
if (document()->isModified()) {
148
QMessageBox::StandardButton ret;
149
ret = QMessageBox::warning(this, tr("MDI"),
150
tr("'%1' has been modified.\n"
151
"Do you want to save your changes?")
152
.arg(userFriendlyCurrentFile()),
153
QMessageBox::Save | QMessageBox::Discard
154
| QMessageBox::Cancel);
155
if (ret == QMessageBox::Save)
157
else if (ret == QMessageBox::Cancel)
163
void MdiChild::setCurrentFile(const QString &fileName)
165
curFile = QFileInfo(fileName).canonicalFilePath();
167
document()->setModified(false);
168
setWindowModified(false);
169
setWindowTitle(userFriendlyCurrentFile() + "[*]");
172
QString MdiChild::strippedName(const QString &fullFileName)
174
return QFileInfo(fullFileName).fileName();