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
55MdiChild::MdiChild()
56{
57 setAttribute(Qt::WA_DeleteOnClose);
58 isUntitled = true;
59}
60
61void MdiChild::newFile()
62{
63 static int sequenceNumber = 1;
64
65 isUntitled = true;
66 curFile = tr(s: "document%1.txt").arg(a: sequenceNumber++);
67 setWindowTitle(curFile + "[*]");
68
69 connect(sender: document(), signal: &QTextDocument::contentsChanged,
70 receiver: this, slot: &MdiChild::documentWasModified);
71}
72
73bool MdiChild::loadFile(const QString &fileName)
74{
75 QFile file(fileName);
76 if (!file.open(flags: QFile::ReadOnly | QFile::Text)) {
77 QMessageBox::warning(parent: this, title: tr(s: "MDI"),
78 text: tr(s: "Cannot read file %1:\n%2.")
79 .arg(a: fileName)
80 .arg(a: 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(sender: document(), signal: &QTextDocument::contentsChanged,
92 receiver: this, slot: &MdiChild::documentWasModified);
93
94 return true;
95}
96
97bool MdiChild::save()
98{
99 if (isUntitled) {
100 return saveAs();
101 } else {
102 return saveFile(fileName: curFile);
103 }
104}
105
106bool MdiChild::saveAs()
107{
108 QString fileName = QFileDialog::getSaveFileName(parent: this, caption: tr(s: "Save As"),
109 dir: curFile);
110 if (fileName.isEmpty())
111 return false;
112
113 return saveFile(fileName);
114}
115
116bool MdiChild::saveFile(const QString &fileName)
117{
118 QString errorMessage;
119
120 QGuiApplication::setOverrideCursor(Qt::WaitCursor);
121 QSaveFile file(fileName);
122 if (file.open(flags: QFile::WriteOnly | QFile::Text)) {
123 QTextStream out(&file);
124 out << toPlainText();
125 if (!file.commit()) {
126 errorMessage = tr(s: "Cannot write file %1:\n%2.")
127 .arg(args: QDir::toNativeSeparators(pathName: fileName), args: file.errorString());
128 }
129 } else {
130 errorMessage = tr(s: "Cannot open file %1 for writing:\n%2.")
131 .arg(args: QDir::toNativeSeparators(pathName: fileName), args: file.errorString());
132 }
133 QGuiApplication::restoreOverrideCursor();
134
135 if (!errorMessage.isEmpty()) {
136 QMessageBox::warning(parent: this, title: tr(s: "MDI"), text: errorMessage);
137 return false;
138 }
139
140 setCurrentFile(fileName);
141 return true;
142}
143
144QString MdiChild::userFriendlyCurrentFile()
145{
146 return strippedName(fullFileName: curFile);
147}
148
149void MdiChild::closeEvent(QCloseEvent *event)
150{
151 if (maybeSave()) {
152 event->accept();
153 } else {
154 event->ignore();
155 }
156}
157
158void MdiChild::documentWasModified()
159{
160 setWindowModified(document()->isModified());
161}
162
163bool MdiChild::maybeSave()
164{
165 if (!document()->isModified())
166 return true;
167 const QMessageBox::StandardButton ret
168 = QMessageBox::warning(parent: this, title: tr(s: "MDI"),
169 text: tr(s: "'%1' has been modified.\n"
170 "Do you want to save your changes?")
171 .arg(a: userFriendlyCurrentFile()),
172 buttons: 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
185void 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
194QString MdiChild::strippedName(const QString &fullFileName)
195{
196 return QFileInfo(fullFileName).fileName();
197}
198

source code of qtbase/examples/widgets/mainwindows/mdi/mdichild.cpp