source: trunk/examples/mainwindows/mdi/mainwindow.cpp@ 296

Last change on this file since 296 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "mainwindow.h"
45#include "mdichild.h"
46
47MainWindow::MainWindow()
48{
49 mdiArea = new QMdiArea;
50 mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
51 mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
52 setCentralWidget(mdiArea);
53 connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow *)),
54 this, SLOT(updateMenus()));
55 windowMapper = new QSignalMapper(this);
56 connect(windowMapper, SIGNAL(mapped(QWidget *)),
57 this, SLOT(setActiveSubWindow(QWidget *)));
58
59 createActions();
60 createMenus();
61 createToolBars();
62 createStatusBar();
63 updateMenus();
64
65 readSettings();
66
67 setWindowTitle(tr("MDI"));
68 setUnifiedTitleAndToolBarOnMac(true);
69}
70
71void MainWindow::closeEvent(QCloseEvent *event)
72{
73 mdiArea->closeAllSubWindows();
74 if (activeMdiChild()) {
75 event->ignore();
76 } else {
77 writeSettings();
78 event->accept();
79 }
80}
81
82void MainWindow::newFile()
83{
84 MdiChild *child = createMdiChild();
85 child->newFile();
86 child->show();
87}
88
89void MainWindow::open()
90{
91 QString fileName = QFileDialog::getOpenFileName(this);
92 if (!fileName.isEmpty()) {
93 QMdiSubWindow *existing = findMdiChild(fileName);
94 if (existing) {
95 mdiArea->setActiveSubWindow(existing);
96 return;
97 }
98
99 MdiChild *child = createMdiChild();
100 if (child->loadFile(fileName)) {
101 statusBar()->showMessage(tr("File loaded"), 2000);
102 child->show();
103 } else {
104 child->close();
105 }
106 }
107}
108
109void MainWindow::save()
110{
111 if (activeMdiChild() && activeMdiChild()->save())
112 statusBar()->showMessage(tr("File saved"), 2000);
113}
114
115void MainWindow::saveAs()
116{
117 if (activeMdiChild() && activeMdiChild()->saveAs())
118 statusBar()->showMessage(tr("File saved"), 2000);
119}
120
121void MainWindow::cut()
122{
123 if (activeMdiChild())
124 activeMdiChild()->cut();
125}
126
127void MainWindow::copy()
128{
129 if (activeMdiChild())
130 activeMdiChild()->copy();
131}
132
133void MainWindow::paste()
134{
135 if (activeMdiChild())
136 activeMdiChild()->paste();
137}
138
139void MainWindow::about()
140{
141 QMessageBox::about(this, tr("About MDI"),
142 tr("The <b>MDI</b> example demonstrates how to write multiple "
143 "document interface applications using Qt."));
144}
145
146void MainWindow::updateMenus()
147{
148 bool hasMdiChild = (activeMdiChild() != 0);
149 saveAct->setEnabled(hasMdiChild);
150 saveAsAct->setEnabled(hasMdiChild);
151 pasteAct->setEnabled(hasMdiChild);
152 closeAct->setEnabled(hasMdiChild);
153 closeAllAct->setEnabled(hasMdiChild);
154 tileAct->setEnabled(hasMdiChild);
155 cascadeAct->setEnabled(hasMdiChild);
156 nextAct->setEnabled(hasMdiChild);
157 previousAct->setEnabled(hasMdiChild);
158 separatorAct->setVisible(hasMdiChild);
159
160 bool hasSelection = (activeMdiChild() &&
161 activeMdiChild()->textCursor().hasSelection());
162 cutAct->setEnabled(hasSelection);
163 copyAct->setEnabled(hasSelection);
164}
165
166void MainWindow::updateWindowMenu()
167{
168 windowMenu->clear();
169 windowMenu->addAction(closeAct);
170 windowMenu->addAction(closeAllAct);
171 windowMenu->addSeparator();
172 windowMenu->addAction(tileAct);
173 windowMenu->addAction(cascadeAct);