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#include "mdichild.h"
55
56MainWindow::MainWindow()
57 : mdiArea(new QMdiArea)
58{
59 mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
60 mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
61 setCentralWidget(mdiArea);
62 connect(sender: mdiArea, signal: &QMdiArea::subWindowActivated,
63 receiver: this, slot: &MainWindow::updateMenus);
64
65 createActions();
66 createStatusBar();
67 updateMenus();
68
69 readSettings();
70
71 setWindowTitle(tr(s: "MDI"));
72 setUnifiedTitleAndToolBarOnMac(true);
73}
74
75void MainWindow::closeEvent(QCloseEvent *event)
76{
77 mdiArea->closeAllSubWindows();
78 if (mdiArea->currentSubWindow()) {
79 event->ignore();
80 } else {
81 writeSettings();
82 event->accept();
83 }
84}
85
86void MainWindow::newFile()
87{
88 MdiChild *child = createMdiChild();
89 child->newFile();
90 child->show();
91}
92
93void MainWindow::open()
94{
95 const QString fileName = QFileDialog::getOpenFileName(parent: this);
96 if (!fileName.isEmpty())
97 openFile(fileName);
98}
99
100bool MainWindow::openFile(const QString &fileName)
101{
102 if (QMdiSubWindow *existing = findMdiChild(fileName)) {
103 mdiArea->setActiveSubWindow(existing);
104 return true;
105 }
106 const bool succeeded = loadFile(fileName);
107 if (succeeded)
108 statusBar()->showMessage(text: tr(s: "File loaded"), timeout: 2000);
109 return succeeded;
110}
111
112bool MainWindow::loadFile(const QString &fileName)
113{
114 MdiChild *child = createMdiChild();
115 const bool succeeded = child->loadFile(fileName);
116 if (succeeded)
117 child->show();
118 else
119 child->close();
120 MainWindow::prependToRecentFiles(fileName);
121 return succeeded;
122}
123
124static inline QString recentFilesKey() { return QStringLiteral("recentFileList"); }
125static inline QString fileKey() { return QStringLiteral("file"); }
126
127static QStringList readRecentFiles(QSettings &settings)
128{
129 QStringList result;
130 const int count = settings.beginReadArray(prefix: recentFilesKey());
131 for (int i = 0; i < count; ++i) {
132 settings.setArrayIndex(i);
133 result.append(t: settings.value(key: fileKey()).toString());
134 }
135 settings.endArray();
136 return result;
137}
138
139static void writeRecentFiles(const QStringList &files, QSettings &settings)
140{
141 const int count = files.size();
142 settings.beginWriteArray(prefix: recentFilesKey());
143 for (int i = 0; i < count; ++i) {
144 settings.setArrayIndex(i);
145 settings.setValue(key: fileKey(), value: files.at(i));
146 }
147 settings.endArray();
148}
149
150bool MainWindow::hasRecentFiles()
151{
152 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
153 const int count = settings.beginReadArray(prefix: recentFilesKey());
154 settings.endArray();
155 return count > 0;
156}
157
158void MainWindow::prependToRecentFiles(const QString &fileName)
159{
160 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
161
162 const QStringList oldRecentFiles = readRecentFiles(settings);
163 QStringList recentFiles = oldRecentFiles;
164 recentFiles.removeAll(t: fileName);
165 recentFiles.prepend(t: fileName);
166 if (oldRecentFiles != recentFiles)
167 writeRecentFiles(files: recentFiles, settings);
168
169 setRecentFilesVisible(!recentFiles.isEmpty());
170}
171
172void MainWindow::setRecentFilesVisible(bool visible)
173{
174 recentFileSubMenuAct->setVisible(visible);
175 recentFileSeparator->setVisible(visible);
176}
177
178void MainWindow::updateRecentFileActions()
179{
180 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
181
182 const QStringList recentFiles = readRecentFiles(settings);
183 const int count = qMin(a: int(MaxRecentFiles), b: recentFiles.size());
184 int i = 0;
185 for ( ; i < count; ++i) {
186 const QString fileName = QFileInfo(recentFiles.at(i)).fileName();
187 recentFileActs[i]->setText(tr(s: "&%1 %2").arg(a: i + 1).arg(a: fileName));
188 recentFileActs[i]->setData(recentFiles.at(i));
189 recentFileActs[i]->setVisible(true);
190 }
191 for ( ; i < MaxRecentFiles; ++i)
192 recentFileActs[i]->setVisible(false);
193}
194
195void MainWindow::openRecentFile()
196{
197 if (const QAction *action = qobject_cast<const QAction *>(object: sender()))
198 openFile(fileName: action->data().toString());
199}
200
201void MainWindow::save()
202{
203 if (activeMdiChild() && activeMdiChild()->save())
204 statusBar()->showMessage(text: tr(s: "File saved"), timeout: 2000);
205}
206
207void MainWindow::saveAs()
208{
209 MdiChild *child = activeMdiChild();
210 if (child && child->saveAs()) {
211 statusBar()->showMessage(text: tr(s: "File saved"), timeout: 2000);
212 MainWindow::prependToRecentFiles(fileName: child->currentFile());
213 }
214}
215
216#ifndef QT_NO_CLIPBOARD
217void MainWindow::cut()
218{
219 if (activeMdiChild())
220 activeMdiChild()->cut();
221}
222
223void MainWindow::copy()
224{
225 if (activeMdiChild())
226 activeMdiChild()->copy();
227}
228
229void MainWindow::paste()
230{
231 if (activeMdiChild())
232 activeMdiChild()->paste();
233}
234#endif
235
236void MainWindow::about()
237{
238 QMessageBox::about(parent: this, title: tr(s: "About MDI"),
239 text: tr(s: "The <b>MDI</b> example demonstrates how to write multiple "
240 "document interface applications using Qt."));
241}
242
243void MainWindow::updateMenus()
244{
245 bool hasMdiChild = (activeMdiChild() != nullptr);
246 saveAct->setEnabled(hasMdiChild);
247 saveAsAct->setEnabled(hasMdiChild);
248#ifndef QT_NO_CLIPBOARD
249 pasteAct->setEnabled(hasMdiChild);
250#endif
251 closeAct->setEnabled(hasMdiChild);
252 closeAllAct->setEnabled(hasMdiChild);
253 tileAct->setEnabled(hasMdiChild);
254 cascadeAct->setEnabled(hasMdiChild);
255 nextAct->setEnabled(hasMdiChild);
256 previousAct->setEnabled(hasMdiChild);
257 windowMenuSeparatorAct->setVisible(hasMdiChild);
258
259#ifndef QT_NO_CLIPBOARD
260 bool hasSelection = (activeMdiChild() &&
261 activeMdiChild()->textCursor().hasSelection());
262 cutAct->setEnabled(hasSelection);
263 copyAct->setEnabled(hasSelection);
264#endif
265}
266
267void MainWindow::updateWindowMenu()
268{
269 windowMenu->clear();
270 windowMenu->addAction(action: closeAct);
271 windowMenu->addAction(action: closeAllAct);
272 windowMenu->addSeparator();
273 windowMenu->addAction(action: tileAct);
274 windowMenu->addAction(action: cascadeAct);
275 windowMenu->addSeparator();
276 windowMenu->addAction(action: nextAct);
277 windowMenu->addAction(action: previousAct);
278 windowMenu->addAction(action: windowMenuSeparatorAct);
279
280 QList<QMdiSubWindow *> windows = mdiArea->subWindowList();
281 windowMenuSeparatorAct->setVisible(!windows.isEmpty());
282
283 for (int i = 0; i < windows.size(); ++i) {
284 QMdiSubWindow *mdiSubWindow = windows.at(i);
285 MdiChild *child = qobject_cast<MdiChild *>(object: mdiSubWindow->widget());
286
287 QString text;
288 if (i < 9) {
289 text = tr(s: "&%1 %2").arg(a: i + 1)
290 .arg(a: child->userFriendlyCurrentFile());
291 } else {
292 text = tr(s: "%1 %2").arg(a: i + 1)
293 .arg(a: child->userFriendlyCurrentFile());
294 }
295 QAction *action = windowMenu->addAction(text, object: mdiSubWindow, slot: [this, mdiSubWindow]() {
296 mdiArea->setActiveSubWindow(mdiSubWindow);
297 });
298 action->setCheckable(true);
299 action ->setChecked(child == activeMdiChild());
300 }
301}
302
303MdiChild *MainWindow::createMdiChild()
304{
305 MdiChild *child = new MdiChild;
306 mdiArea->addSubWindow(widget: child);
307
308#ifndef QT_NO_CLIPBOARD
309 connect(sender: child, signal: &QTextEdit::copyAvailable, receiver: cutAct, slot: &QAction::setEnabled);
310 connect(sender: child, signal: &QTextEdit::copyAvailable, receiver: copyAct, slot: &QAction::setEnabled);
311#endif
312
313 return child;
314}
315
316void MainWindow::createActions()
317{
318 QMenu *fileMenu = menuBar()->addMenu(title: tr(s: "&File"));
319 QToolBar *fileToolBar = addToolBar(title: tr(s: "File"));
320
321 const QIcon newIcon = QIcon::fromTheme(name: "document-new", fallback: QIcon(":/images/new.png"));
322 newAct = new QAction(newIcon, tr(s: "&New"), this);
323 newAct->setShortcuts(QKeySequence::New);
324 newAct->setStatusTip(tr(s: "Create a new file"));
325 connect(sender: newAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::newFile);
326 fileMenu->addAction(action: newAct);
327 fileToolBar->addAction(action: newAct);
328
329 const QIcon openIcon = QIcon::fromTheme(name: "document-open", fallback: QIcon(":/images/open.png"));
330 QAction *openAct = new QAction(openIcon, tr(s: "&Open..."), this);
331 openAct->setShortcuts(QKeySequence::Open);
332 openAct->setStatusTip(tr(s: "Open an existing file"));
333 connect(sender: openAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::open);
334 fileMenu->addAction(action: openAct);
335 fileToolBar->addAction(action: openAct);
336
337 const QIcon saveIcon = QIcon::fromTheme(name: "document-save", fallback: QIcon(":/images/save.png"));
338 saveAct = new QAction(saveIcon, tr(s: "&Save"), this);
339 saveAct->setShortcuts(QKeySequence::Save);
340 saveAct->setStatusTip(tr(s: "Save the document to disk"));
341 connect(sender: saveAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::save);
342 fileToolBar->addAction(action: saveAct);
343
344 const QIcon saveAsIcon = QIcon::fromTheme(name: "document-save-as");
345 saveAsAct = new QAction(saveAsIcon, tr(s: "Save &As..."), this);
346 saveAsAct->setShortcuts(QKeySequence::SaveAs);
347 saveAsAct->setStatusTip(tr(s: "Save the document under a new name"));
348 connect(sender: saveAsAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::saveAs);
349 fileMenu->addAction(action: saveAsAct);
350
351 fileMenu->addSeparator();
352
353 QMenu *recentMenu = fileMenu->addMenu(title: tr(s: "Recent..."));
354 connect(sender: recentMenu, signal: &QMenu::aboutToShow, receiver: this, slot: &MainWindow::updateRecentFileActions);
355 recentFileSubMenuAct = recentMenu->menuAction();
356
357 for (int i = 0; i < MaxRecentFiles; ++i) {
358 recentFileActs[i] = recentMenu->addAction(text: QString(), object: this, slot: &MainWindow::openRecentFile);
359 recentFileActs[i]->setVisible(false);
360 }
361
362 recentFileSeparator = fileMenu->addSeparator();
363
364 setRecentFilesVisible(MainWindow::hasRecentFiles());
365
366 fileMenu->addAction(text: tr(s: "Switch layout direction"), object: this, slot: &MainWindow::switchLayoutDirection);
367
368 fileMenu->addSeparator();
369
370//! [0]
371 const QIcon exitIcon = QIcon::fromTheme(name: "application-exit");
372 QAction *exitAct = fileMenu->addAction(actionIcon: exitIcon, text: tr(s: "E&xit"), qApp, slot: &QApplication::closeAllWindows);
373 exitAct->setShortcuts(QKeySequence::Quit);
374 exitAct->setStatusTip(tr(s: "Exit the application"));
375 fileMenu->addAction(action: exitAct);
376//! [0]
377
378#ifndef QT_NO_CLIPBOARD
379 QMenu *editMenu = menuBar()->addMenu(title: tr(s: "&Edit"));
380 QToolBar *editToolBar = addToolBar(title: tr(s: "Edit"));
381
382 const QIcon cutIcon = QIcon::fromTheme(name: "edit-cut", fallback: QIcon(":/images/cut.png"));
383 cutAct = new QAction(cutIcon, tr(s: "Cu&t"), this);
384 cutAct->setShortcuts(QKeySequence::Cut);
385 cutAct->setStatusTip(tr(s: "Cut the current selection's contents to the "
386 "clipboard"));
387 connect(sender: cutAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::cut);
388 editMenu->addAction(action: cutAct);
389 editToolBar->addAction(action: cutAct);
390
391 const QIcon copyIcon = QIcon::fromTheme(name: "edit-copy", fallback: QIcon(":/images/copy.png"));
392 copyAct = new QAction(copyIcon, tr(s: "&Copy"), this);
393 copyAct->setShortcuts(QKeySequence::Copy);
394 copyAct->setStatusTip(tr(s: "Copy the current selection's contents to the "
395 "clipboard"));
396 connect(sender: copyAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::copy);
397 editMenu->addAction(action: copyAct);
398 editToolBar->addAction(action: copyAct);
399
400 const QIcon pasteIcon = QIcon::fromTheme(name: "edit-paste", fallback: QIcon(":/images/paste.png"));
401 pasteAct = new QAction(pasteIcon, tr(s: "&Paste"), this);
402 pasteAct->setShortcuts(QKeySequence::Paste);
403 pasteAct->setStatusTip(tr(s: "Paste the clipboard's contents into the current "
404 "selection"));
405 connect(sender: pasteAct, signal: &QAction::triggered, receiver: this, slot: &MainWindow::paste);
406 editMenu->addAction(action: pasteAct);
407 editToolBar->addAction(action: pasteAct);
408#endif
409
410 windowMenu = menuBar()->addMenu(title: tr(s: "&Window"));
411 connect(sender: windowMenu, signal: &QMenu::aboutToShow, receiver: this, slot: &MainWindow::updateWindowMenu);
412
413 closeAct = new QAction(tr(s: "Cl&ose"), this);
414 closeAct->setStatusTip(tr(s: "Close the active window"));
415 connect(sender: closeAct, signal: &QAction::triggered,
416 receiver: mdiArea, slot: &QMdiArea::closeActiveSubWindow);
417
418 closeAllAct = new QAction(tr(s: "Close &All"), this);
419 closeAllAct->setStatusTip(tr(s: "Close all the windows"));
420 connect(sender: closeAllAct, signal: &QAction::triggered, receiver: mdiArea, slot: &QMdiArea::closeAllSubWindows);
421
422 tileAct = new QAction(tr(s: "&Tile"), this);
423 tileAct->setStatusTip(tr(s: "Tile the windows"));
424 connect(sender: tileAct, signal: &QAction::triggered, receiver: mdiArea, slot: &QMdiArea::tileSubWindows);
425
426 cascadeAct = new QAction(tr(s: "&Cascade"), this);
427 cascadeAct->setStatusTip(tr(s: "Cascade the windows"));
428 connect(sender: cascadeAct, signal: &QAction::triggered, receiver: mdiArea, slot: &QMdiArea::cascadeSubWindows);
429
430 nextAct = new QAction(tr(s: "Ne&xt"), this);
431 nextAct->setShortcuts(QKeySequence::NextChild);
432 nextAct->setStatusTip(tr(s: "Move the focus to the next window"));
433 connect(sender: nextAct, signal: &QAction::triggered, receiver: mdiArea, slot: &QMdiArea::activateNextSubWindow);
434
435 previousAct = new QAction(tr(s: "Pre&vious"), this);
436 previousAct->setShortcuts(QKeySequence::PreviousChild);
437 previousAct->setStatusTip(tr(s: "Move the focus to the previous "
438 "window"));
439 connect(sender: previousAct, signal: &QAction::triggered, receiver: mdiArea, slot: &QMdiArea::activatePreviousSubWindow);
440
441 windowMenuSeparatorAct = new QAction(this);
442 windowMenuSeparatorAct->setSeparator(true);
443
444 updateWindowMenu();
445
446 menuBar()->addSeparator();
447
448 QMenu *helpMenu = menuBar()->addMenu(title: tr(s: "&Help"));
449
450 QAction *aboutAct = helpMenu->addAction(text: tr(s: "&About"), object: this, slot: &MainWindow::about);
451 aboutAct->setStatusTip(tr(s: "Show the application's About box"));
452
453 QAction *aboutQtAct = helpMenu->addAction(text: tr(s: "About &Qt"), qApp, slot: &QApplication::aboutQt);
454 aboutQtAct->setStatusTip(tr(s: "Show the Qt library's About box"));
455}
456
457void MainWindow::createStatusBar()
458{
459 statusBar()->showMessage(text: tr(s: "Ready"));
460}
461
462void MainWindow::readSettings()
463{
464 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
465 const QByteArray geometry = settings.value(key: "geometry", defaultValue: QByteArray()).toByteArray();
466 if (geometry.isEmpty()) {
467 const QRect availableGeometry = screen()->availableGeometry();
468 resize(w: availableGeometry.width() / 3, h: availableGeometry.height() / 2);
469 move(ax: (availableGeometry.width() - width()) / 2,
470 ay: (availableGeometry.height() - height()) / 2);
471 } else {
472 restoreGeometry(geometry);
473 }
474}
475
476void MainWindow::writeSettings()
477{
478 QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
479 settings.setValue(key: "geometry", value: saveGeometry());
480}
481
482MdiChild *MainWindow::activeMdiChild() const
483{
484 if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow())
485 return qobject_cast<MdiChild *>(object: activeSubWindow->widget());
486 return nullptr;
487}
488
489QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName) const
490{
491 QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
492
493 const QList<QMdiSubWindow *> subWindows = mdiArea->subWindowList();
494 for (QMdiSubWindow *window : subWindows) {
495 MdiChild *mdiChild = qobject_cast<MdiChild *>(object: window->widget());
496 if (mdiChild->currentFile() == canonicalFilePath)
497 return window;
498 }
499 return nullptr;
500}
501
502void MainWindow::switchLayoutDirection()
503{
504 if (layoutDirection() == Qt::LeftToRight)
505 QGuiApplication::setLayoutDirection(Qt::RightToLeft);
506 else
507 QGuiApplication::setLayoutDirection(Qt::LeftToRight);
508}
509

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