|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "mdichild.h" |
|
45 |
|
46 MdiChild::MdiChild() |
|
47 { |
|
48 setAttribute(Qt::WA_DeleteOnClose); |
|
49 isUntitled = true; |
|
50 } |
|
51 |
|
52 void 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 |
|
64 bool 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 |
|
88 bool MdiChild::save() |
|
89 { |
|
90 if (isUntitled) { |
|
91 return saveAs(); |
|
92 } else { |
|
93 return saveFile(curFile); |
|
94 } |
|
95 } |
|
96 |
|
97 bool 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 |
|
107 bool 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 |
|
127 QString MdiChild::userFriendlyCurrentFile() |
|
128 { |
|
129 return strippedName(curFile); |
|
130 } |
|
131 |
|
132 void MdiChild::closeEvent(QCloseEvent *event) |
|
133 { |
|
134 if (maybeSave()) { |
|
135 event->accept(); |
|
136 } else { |
|
137 event->ignore(); |
|
138 } |
|
139 } |
|
140 |
|
141 void MdiChild::documentWasModified() |
|
142 { |
|
143 setWindowModified(document()->isModified()); |
|
144 } |
|
145 |
|
146 bool 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 |
|
164 void 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 |
|
173 QString MdiChild::strippedName(const QString &fullFileName) |
|
174 { |
|
175 return QFileInfo(fullFileName).fileName(); |
|
176 } |