source: trunk/examples/mainwindows/mdi/mdichild.cpp@ 117

Last change on this file since 117 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "mdichild.h"
45
46MdiChild::MdiChild()
47{
48 setAttribute(Qt::WA_DeleteOnClose);
49 isUntitled = true;
50}
51
52void MdiChild::newFile()
53{
54 static int sequenceNumber = 1;
55
56 isUntitled = true;
57 curFile = tr("document%1.txt").arg(sequenceNumber++);
58 setWindowTitle(curFile + "[*]");
59
60 connect(document(), SIGNAL(contentsChanged()),
61 this, SLOT(documentWasModified()));
62}
63
64bool MdiChild::loadFile(const QString &fileName)
65{
66 QFile file(fileName);
67 if (!file.open(QFile::ReadOnly | QFile::Text)) {
68 QMessageBox::warning(this, tr("MDI"),
69 tr("Cannot read file %1:\n%2.")
70 .arg(fileName)
71 .arg(file.errorString()));
72 return false;
73 }
74
75 QTextStream in(&file);
76 QApplication::setOverrideCursor(Qt::WaitCursor);
77 setPlainText(in.readAll());
78 QApplication::restoreOverrideCursor();
79
80 setCurrentFile(fileName);
81
82 connect(document(), SIGNAL(contentsChanged()),
83 this, SLOT(documentWasModified()));
84
85 return true;
86}
87
88bool MdiChild::save()
89{
90 if (isUntitled) {
91 return saveAs();
92 } else {
93 return saveFile(curFile);
94 }
95}
96
97bool MdiChild::saveAs()
98{
99 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
100 curFile);
101 if (fileName.isEmpty())
102 return false;
103
104 return saveFile(fileName);
105}
106
107bool MdiChild::saveFile(const QString &fileName)
108{
109 QFile file(fileName);
110 if (!file.open(QFile::WriteOnly | QFile::Text)) {
111 QMessageBox::warning(this, tr("MDI"),
112 tr("Cannot write file %1:\n%2.")
113 .arg(fileName)
114 .arg(file.errorString()));
115 return false;
116 }
117
118 QTextStream out(&file);
119 QApplication::setOverrideCursor(Qt::WaitCursor);
120 out << toPlainText();
121 QApplication::restoreOverrideCursor();
122
123 setCurrentFile(fileName);
124 return true;
125}
126
127QString MdiChild::userFriendlyCurrentFile()
128{
129 return strippedName(curFile);
130}
131
132void MdiChild::closeEvent(QCloseEvent *event)
133{
134 if (maybeSave()) {
135 event->accept();
136 } else {
137 event->ignore();
138 }
139}
140
141void MdiChild::documentWasModified()
142{
143 setWindowModified(document()->isModified());
144}
145
146bool MdiChild::maybeSave()
147{
148 if (document()->isModified()) {
149 QMessageBox::StandardButton ret;
150 ret = QMessageBox::warning(this, tr("MDI"),
151 tr("'%1' has been modified.\n"
152 "Do you want to save your changes?")
153 .arg(userFriendlyCurrentFile()),
154 QMessageBox::Save | QMessageBox::Discard
155 | QMessageBox::Cancel);
156 if (ret == QMessageBox::Save)
157 return save();
158 else if (ret == QMessageBox::Cancel)
159 return false;
160 }
161 return true;
162}
163
164void MdiChild::setCurrentFile(const QString &fileName)
165{
166 curFile = QFileInfo(fileName).canonicalFilePath();
167 isUntitled = false;
168 document()->setModified(false);
169 setWindowModified(false);
170 setWindowTitle(userFriendlyCurrentFile() + "[*]");
171}
172
173QString MdiChild::strippedName(const QString &fullFileName)
174{
175 return QFileInfo(fullFileName).fileName();
176}
Note: See TracBrowser for help on using the repository browser.