1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include <QtWidgets> |
52 | |
53 | #include "mdichild.h" |
54 | |
55 | MdiChild::MdiChild() |
56 | { |
57 | setAttribute(Qt::WA_DeleteOnClose); |
58 | isUntitled = true; |
59 | } |
60 | |
61 | void MdiChild::newFile() |
62 | { |
63 | static int sequenceNumber = 1; |
64 | |
65 | isUntitled = true; |
66 | curFile = tr("document%1.txt" ).arg(sequenceNumber++); |
67 | setWindowTitle(curFile + "[*]" ); |
68 | |
69 | connect(document(), &QTextDocument::contentsChanged, |
70 | this, &MdiChild::documentWasModified); |
71 | } |
72 | |
73 | bool MdiChild::loadFile(const QString &fileName) |
74 | { |
75 | QFile file(fileName); |
76 | if (!file.open(QFile::ReadOnly | QFile::Text)) { |
77 | QMessageBox::warning(this, tr("MDI" ), |
78 | tr("Cannot read file %1:\n%2." ) |
79 | .arg(fileName) |
80 | .arg(file.errorString())); |
81 | return false; |
82 | } |
83 | |
84 | QTextStream in(&file); |
85 | QGuiApplication::setOverrideCursor(Qt::WaitCursor); |
86 | setPlainText(in.readAll()); |
87 | QGuiApplication::restoreOverrideCursor(); |
88 | |
89 | setCurrentFile(fileName); |
90 | |
91 | connect(document(), &QTextDocument::contentsChanged, |
92 | this, &MdiChild::documentWasModified); |
93 | |
94 | return true; |
95 | } |
96 | |
97 | bool MdiChild::save() |
98 | { |
99 | if (isUntitled) { |
100 | return saveAs(); |
101 | } else { |
102 | return saveFile(curFile); |
103 | } |
104 | } |
105 | |
106 | bool MdiChild::saveAs() |
107 | { |
108 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save As" ), |
109 | curFile); |
110 | if (fileName.isEmpty()) |
111 | return false; |
112 | |
113 | return saveFile(fileName); |
114 | } |
115 | |
116 | bool MdiChild::saveFile(const QString &fileName) |
117 | { |
118 | QString errorMessage; |
119 | |
120 | QGuiApplication::setOverrideCursor(Qt::WaitCursor); |
121 | QSaveFile file(fileName); |
122 | if (file.open(QFile::WriteOnly | QFile::Text)) { |
123 | QTextStream out(&file); |
124 | out << toPlainText(); |
125 | if (!file.commit()) { |
126 | errorMessage = tr("Cannot write file %1:\n%2." ) |
127 | .arg(QDir::toNativeSeparators(fileName), file.errorString()); |
128 | } |
129 | } else { |
130 | errorMessage = tr("Cannot open file %1 for writing:\n%2." ) |
131 | .arg(QDir::toNativeSeparators(fileName), file.errorString()); |
132 | } |
133 | QGuiApplication::restoreOverrideCursor(); |
134 | |
135 | if (!errorMessage.isEmpty()) { |
136 | QMessageBox::warning(this, tr("MDI" ), errorMessage); |
137 | return false; |
138 | } |
139 | |
140 | setCurrentFile(fileName); |
141 | return true; |
142 | } |
143 | |
144 | QString MdiChild::userFriendlyCurrentFile() |
145 | { |
146 | return strippedName(curFile); |
147 | } |
148 | |
149 | void MdiChild::closeEvent(QCloseEvent *event) |
150 | { |
151 | if (maybeSave()) { |
152 | event->accept(); |
153 | } else { |
154 | event->ignore(); |
155 | } |
156 | } |
157 | |
158 | void MdiChild::documentWasModified() |
159 | { |
160 | setWindowModified(document()->isModified()); |
161 | } |
162 | |
163 | bool MdiChild::maybeSave() |
164 | { |
165 | if (!document()->isModified()) |
166 | return true; |
167 | const QMessageBox::StandardButton ret |
168 | = QMessageBox::warning(this, tr("MDI" ), |
169 | tr("'%1' has been modified.\n" |
170 | "Do you want to save your changes?" ) |
171 | .arg(userFriendlyCurrentFile()), |
172 | QMessageBox::Save | QMessageBox::Discard |
173 | | QMessageBox::Cancel); |
174 | switch (ret) { |
175 | case QMessageBox::Save: |
176 | return save(); |
177 | case QMessageBox::Cancel: |
178 | return false; |
179 | default: |
180 | break; |
181 | } |
182 | return true; |
183 | } |
184 | |
185 | void MdiChild::setCurrentFile(const QString &fileName) |
186 | { |
187 | curFile = QFileInfo(fileName).canonicalFilePath(); |
188 | isUntitled = false; |
189 | document()->setModified(false); |
190 | setWindowModified(false); |
191 | setWindowTitle(userFriendlyCurrentFile() + "[*]" ); |
192 | } |
193 | |
194 | QString MdiChild::strippedName(const QString &fullFileName) |
195 | { |
196 | return QFileInfo(fullFileName).fileName(); |
197 | } |
198 | |