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 "mainwindow.h"
54
55MainWindow::MainWindow()
56{
57 init();
58 setCurrentFile(QString());
59}
60
61MainWindow::MainWindow(const QString &fileName)
62{
63 init();
64 loadFile(fileName);
65}
66
67void MainWindow::closeEvent(QCloseEvent *event)
68{
69 if (maybeSave()) {
70 writeSettings();
71 event->accept();
72 } else {
73 event->ignore();
74 }
75}
76
77void MainWindow::newFile()
78{
79 MainWindow *other = new MainWindow;
80 other->tile(this);
81 other->show();
82}
83
84void MainWindow::open()
85{
86 const QString fileName = QFileDialog::getOpenFileName(this);
87 if (!fileName.isEmpty())
88 openFile(fileName);
89}
90
91void MainWindow::openFile(const QString &fileName)
92{
93 MainWindow *existing = findMainWindow(fileName);
94 if (existing) {
95 existing->show();
96 existing->raise();
97 existing->activateWindow();
98 return;
99 }
100
101 if (isUntitled && textEdit->document()->isEmpty() && !isWindowModified()) {
102 loadFile(fileName);
103 return;
104 }
105
106 MainWindow *other = new MainWindow(fileName);
107 if (other->isUntitled) {
108 delete other;
109 return;
110 }
111 other->tile(this);
112 other->show();
113}
114
115bool MainWindow::save()
116{
117 return isUntitled ? saveAs() : saveFile(curFile);
118}
119
120bool MainWindow::saveAs()
121{
122 QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),
123 curFile);
124 if (fileName.isEmpty())
125 return false;
126
127 return saveFile(fileName);
128}
129
130void MainWindow::about()
131{
132 QMessageBox::about(this, tr("About SDI"),
133 tr("The <b>SDI</b> example demonstrates how to write single "
134 "document interface applications using Qt."));
135}
136
137void MainWindow::documentWasModified()
138{
139 setWindowModified(true);
140}
141
142void MainWindow::init()
143{
144 setAttribute(Qt::WA_DeleteOnClose);
145
146 isUntitled = true;
147
148 textEdit = new QTextEdit;
149 setCentralWidget(textEdit);
150
151 createActions();
152 createStatusBar();
153
154 readSettings();
155
156 connect(textEdit->document(), &QTextDocument::contentsChanged,
157 this, &MainWindow::documentWasModified);
158
159 setUnifiedTitleAndToolBarOnMac(true);
160}
161
162void MainWindow::tile(const QMainWindow *previous)
163{
164 if (!previous)
165 return;
166 int topFrameWidth = previous->geometry().top() - previous->pos().y();
167 if (!topFrameWidth)
168 topFrameWidth = 40;
169 const QPoint pos = previous->pos() + 2 * QPoint(topFrameWidth, topFrameWidth);
170 if (screen()->availableGeometry().contains(rect().bottomRight() + pos))
171 move(pos);
172}
173
174//! [implicit tr context]
175void MainWindow::createActions()
176{
177 QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
178//! [implicit tr context]
179 QToolBar *fileToolBar = addToolBar(tr("File"));
180
181 const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(":/images/new.png"));
182 QAction *newAct = new QAction(newIcon, tr("&New"), this);
183 newAct->setShortcuts(QKeySequence::New);
184 newAct->setStatusTip(tr("Create a new file"));
185 connect(newAct, &QAction::triggered, this, &MainWindow::newFile);
186 fileMenu->addAction(newAct);
187 fileToolBar->addAction(newAct);
188
189 const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
190 QAction *openAct = new QAction(openIcon, tr("&Open..."), this);
191 openAct->setShortcuts(QKeySequence::Open);
192 openAct->setStatusTip(tr("Open an existing file"));
193 connect(openAct, &QAction::triggered, this, &MainWindow::open);
194 fileMenu->addAction(openAct);
195 fileToolBar->addAction(openAct);
196
197 const QIcon saveIcon = QIcon::fromTheme("document-save", QIcon(":/images/save.png"));
198 QAction *saveAct = new QAction(saveIcon, tr("&Save"), this);
199 saveAct->setShortcuts(QKeySequence::Save);
200 saveAct->setStatusTip(tr("Save the document to disk"));
201 connect(saveAct, &QAction::triggered, this, &MainWindow::save);
202 fileMenu->addAction(saveAct);
203 fileToolBar->addAction(saveAct);
204
205 const QIcon saveAsIcon = QIcon::fromTheme("document-save-as");
206 QAction *saveAsAct = fileMenu->addAction(saveAsIcon, tr("Save &As..."), this, &MainWindow::saveAs);
207 saveAsAct->setShortcuts(QKeySequence::SaveAs);
208 saveAsAct->setStatusTip(tr("Save the document under a new name"));
209
210 fileMenu->addSeparator();
211
212 QMenu *recentMenu = fileMenu->addMenu(tr("Recent..."));
213 connect(recentMenu, &QMenu::aboutToShow, this, &MainWindow::updateRecentFileActions);
214 recentFileSubMenuAct = recentMenu->menuAction();
215
216 for (int i = 0; i < MaxRecentFiles; ++i) {
217 recentFileActs[i] = recentMenu->addAction(QString(), this, &MainWindow::openRecentFile);
218 recentFileActs[i]->setVisible(false);
219 }
220
221 recentFileSeparator = fileMenu->addSeparator();
222
223 setRecentFilesVisible(MainWindow::hasRecentFiles());
224
225 QAction *closeAct = fileMenu->addAction(tr("&Close"), this, &QWidget::close);
226 closeAct->setShortcut(tr("Ctrl+W"));
227 closeAct->setStatusTip(tr("Close this window"));
228
229 const QIcon exitIcon = QIcon::fromTheme("application-exit");
230 QAction *exitAct = fileMenu->addAction(exitIcon, tr("E&xit"), qApp, &QApplication::quit);
231 exitAct->setShortcuts(QKeySequence::Quit);
232 exitAct->setStatusTip(tr("Exit the application"));
233
234 QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
235 QToolBar *editToolBar = addToolBar(tr("Edit"));
236
237#ifndef QT_NO_CLIPBOARD
238 const QIcon cutIcon = QIcon::fromTheme("edit-cut", QIcon(":/images/cut.png"));
239 QAction *cutAct = new QAction(cutIcon, tr("Cu&t"), this);
240 cutAct->setShortcuts(QKeySequence::Cut);
241 cutAct->setStatusTip(tr("Cut the current selection's contents to the "
242 "clipboard"));
243 connect(cutAct, &QAction::triggered, textEdit, &QTextEdit::cut);
244 editMenu->addAction(cutAct);
245 editToolBar->addAction(cutAct);
246
247 const QIcon copyIcon = QIcon::fromTheme("edit-copy", QIcon(":/images/copy.png"));
248 QAction *copyAct = new QAction(copyIcon, tr("&Copy"), this);
249 copyAct->setShortcuts(QKeySequence::Copy);
250 copyAct->setStatusTip(tr("Copy the current selection's contents to the "
251 "clipboard"));
252 connect(copyAct, &QAction::triggered, textEdit, &QTextEdit::copy);
253 editMenu->addAction(copyAct);
254 editToolBar->addAction(copyAct);
255
256 const QIcon pasteIcon = QIcon::fromTheme("edit-paste", QIcon(":/images/paste.png"));
257 QAction *pasteAct = new QAction(pasteIcon, tr("&Paste"), this);
258 pasteAct->setShortcuts(QKeySequence::Paste);
259 pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
260 "selection"));
261 connect(pasteAct, &QAction::triggered, textEdit, &QTextEdit::paste);
262 editMenu->addAction(pasteAct);
263 editToolBar->addAction(pasteAct);
264
265 menuBar()->addSeparator();
266#endif // !QT_NO_CLIPBOARD
267
268 QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
269 QAction *aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::about);
270 aboutAct->setStatusTip(tr("Show the application's About box"));
271
272 QAction *aboutQtAct = helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
273 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
274
275#ifndef QT_NO_CLIPBOARD
276 cutAct->setEnabled(false);
277 copyAct->setEnabled(false);
278 connect(textEdit, &QTextEdit::copyAvailable, cutAct, &QAction::setEnabled);
279 connect(textEdit, &QTextEdit::copyAvailable, copyAct, &QAction::setEnabled);
280#endif // !QT_NO_CLIPBOARD
281}
282
283void MainWindow::createStatusBar()
284{
285 statusBar()->showMessage(tr("Ready"));
286}
287
288void MainWindow::readSettings()
289{
290 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
291 const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
292 if (geometry.isEmpty()) {
293 const QRect availableGeometry = screen()->availableGeometry();
294 resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
295 move((availableGeometry.width() - width()) / 2,
296 (availableGeometry.height() - height()) / 2);
297 } else {
298 restoreGeometry(geometry);
299 }
300}
301
302void MainWindow::writeSettings()
303{
304 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
305 settings.setValue("geometry", saveGeometry());
306}
307
308bool MainWindow::maybeSave()
309{
310 if (!textEdit->document()->isModified())
311 return true;
312 const QMessageBox::StandardButton ret
313 = QMessageBox::warning(this, tr("SDI"),
314 tr("The document has been modified.\n"
315 "Do you want to save your changes?"),
316 QMessageBox::Save | QMessageBox::Discard
317 | QMessageBox::Cancel);
318 switch (ret) {
319 case QMessageBox::Save:
320 return save();
321 case QMessageBox::Cancel:
322 return false;
323 default:
324 break;
325 }
326 return true;
327}
328
329void MainWindow::loadFile(const QString &fileName)
330{
331
332 QFile file(fileName);
333 if (!file.open(QFile::ReadOnly | QFile::Text)) {
334 QMessageBox::warning(this, tr("SDI"),
335 tr("Cannot read file %1:\n%2.")
336 .arg(QDir::toNativeSeparators(fileName), file.errorString()));
337 return;
338 }
339
340 QTextStream in(&file);
341 QGuiApplication::setOverrideCursor(Qt::WaitCursor);
342 textEdit->setPlainText(in.readAll());
343 QGuiApplication::restoreOverrideCursor();
344
345 setCurrentFile(fileName);
346 statusBar()->showMessage(tr("File loaded"), 2000);
347}
348
349void MainWindow::setRecentFilesVisible(bool visible)
350{
351 recentFileSubMenuAct->setVisible(visible);
352 recentFileSeparator->setVisible(visible);
353}
354
355static inline QString recentFilesKey() { return QStringLiteral("recentFileList"); }
356static inline QString fileKey() { return QStringLiteral("file"); }
357
358static QStringList readRecentFiles(QSettings &settings)
359{
360 QStringList result;
361 const int count = settings.beginReadArray(recentFilesKey());
362 for (int i = 0; i < count; ++i) {
363 settings.setArrayIndex(i);
364 result.append(settings.value(fileKey()).toString());
365 }
366 settings.endArray();
367 return result;
368}
369
370static void writeRecentFiles(const QStringList &files, QSettings &settings)
371{
372 const int count = files.size();
373 settings.beginWriteArray(recentFilesKey());
374 for (int i = 0; i < count; ++i) {
375 settings.setArrayIndex(i);
376 settings.setValue(fileKey(), files.at(i));
377 }
378 settings.endArray();
379}
380
381bool MainWindow::hasRecentFiles()
382{
383 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
384 const int count = settings.beginReadArray(recentFilesKey());
385 settings.endArray();
386 return count > 0;
387}
388
389void MainWindow::prependToRecentFiles(const QString &fileName)
390{
391 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
392
393 const QStringList oldRecentFiles = readRecentFiles(settings);
394 QStringList recentFiles = oldRecentFiles;
395 recentFiles.removeAll(fileName);
396 recentFiles.prepend(fileName);
397 if (oldRecentFiles != recentFiles)
398 writeRecentFiles(recentFiles, settings);
399
400 setRecentFilesVisible(!recentFiles.isEmpty());
401}
402
403void MainWindow::updateRecentFileActions()
404{
405 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
406
407 const QStringList recentFiles = readRecentFiles(settings);
408 const int count = qMin(int(MaxRecentFiles), recentFiles.size());
409 int i = 0;
410 for ( ; i < count; ++i) {
411 const QString fileName = MainWindow::strippedName(recentFiles.at(i));
412 recentFileActs[i]->setText(tr("&%1 %2").arg(i + 1).arg(fileName));
413 recentFileActs[i]->setData(recentFiles.at(i));
414 recentFileActs[i]->setVisible(true);
415 }
416 for ( ; i < MaxRecentFiles; ++i)
417 recentFileActs[i]->setVisible(false);
418}
419
420void MainWindow::openRecentFile()
421{
422 if (const QAction *action = qobject_cast<const QAction *>(sender()))
423 openFile(action->data().toString());
424}
425
426bool MainWindow::saveFile(const QString &fileName)
427{
428 QString errorMessage;
429
430 QGuiApplication::setOverrideCursor(Qt::WaitCursor);
431 QSaveFile file(fileName);
432 if (file.open(QFile::WriteOnly | QFile::Text)) {
433 QTextStream out(&file);
434 out << textEdit->toPlainText();
435 if (!file.commit()) {
436 errorMessage = tr("Cannot write file %1:\n%2.")
437 .arg(QDir::toNativeSeparators(fileName), file.errorString());
438 }
439 } else {
440 errorMessage = tr("Cannot open file %1 for writing:\n%2.")
441 .arg(QDir::toNativeSeparators(fileName), file.errorString());
442 }
443 QGuiApplication::restoreOverrideCursor();
444
445 if (!errorMessage.isEmpty()) {
446 QMessageBox::warning(this, tr("SDI"), errorMessage);
447 return false;
448 }
449
450 setCurrentFile(fileName);
451 statusBar()->showMessage(tr("File saved"), 2000);
452 return true;
453}
454
455void MainWindow::setCurrentFile(const QString &fileName)
456{
457 static int sequenceNumber = 1;
458
459 isUntitled = fileName.isEmpty();
460 if (isUntitled) {
461 curFile = tr("document%1.txt").arg(sequenceNumber++);
462 } else {
463 curFile = QFileInfo(fileName).canonicalFilePath();
464 }
465
466 textEdit->document()->setModified(false);
467 setWindowModified(false);
468
469 if (!isUntitled && windowFilePath() != curFile)
470 MainWindow::prependToRecentFiles(curFile);
471
472 setWindowFilePath(curFile);
473}
474
475QString MainWindow::strippedName(const QString &fullFileName)
476{
477 return QFileInfo(fullFileName).fileName();
478}
479
480MainWindow *MainWindow::findMainWindow(const QString &fileName) const
481{
482 QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
483
484 const QList<QWidget *> topLevelWidgets = QApplication::topLevelWidgets();
485 for (QWidget *widget : topLevelWidgets) {
486 MainWindow *mainWin = qobject_cast<MainWindow *>(widget);
487 if (mainWin && mainWin->curFile == canonicalFilePath)
488 return mainWin;
489 }
490
491 return nullptr;
492}
493