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

Last change on this file since 117 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);
174 windowMenu->addSeparator();
175 windowMenu->addAction(nextAct);
176 windowMenu->addAction(previousAct);
177 windowMenu->addAction(separatorAct);
178
179 QList<QMdiSubWindow *> windows = mdiArea->subWindowList();
180 separatorAct->setVisible(!windows.isEmpty());
181
182 for (int i = 0; i < windows.size(); ++i) {
183 MdiChild *child = qobject_cast<MdiChild *>(windows.at(i)->widget());
184
185 QString text;
186 if (i < 9) {
187 text = tr("&%1 %2").arg(i + 1)
188 .arg(child->userFriendlyCurrentFile());
189 } else {
190 text = tr("%1 %2").arg(i + 1)
191 .arg(child->userFriendlyCurrentFile());
192 }
193 QAction *action = windowMenu->addAction(text);
194 action->setCheckable(true);
195 action ->setChecked(child == activeMdiChild());
196 connect(action, SIGNAL(triggered()), windowMapper, SLOT(map()));
197 windowMapper->setMapping(action, windows.at(i));
198 }
199}
200
201MdiChild *MainWindow::createMdiChild()
202{
203 MdiChild *child = new MdiChild;
204 mdiArea->addSubWindow(child);
205
206 connect(child, SIGNAL(copyAvailable(bool)),
207 cutAct, SLOT(setEnabled(bool)));
208 connect(child, SIGNAL(copyAvailable(bool)),
209 copyAct, SLOT(setEnabled(bool)));
210
211 return child;
212}
213
214void MainWindow::createActions()
215{
216 newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
217 newAct->setShortcuts(QKeySequence::New);
218 newAct->setStatusTip(tr("Create a new file"));
219 connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
220
221 openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
222 openAct->setShortcuts(QKeySequence::Open);
223 openAct->setStatusTip(tr("Open an existing file"));
224 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
225
226 saveAct = new QAction(QIcon(":/images/save.png"), tr("&Save"), this);
227 saveAct->setShortcuts(QKeySequence::Save);
228 saveAct->setStatusTip(tr("Save the document to disk"));
229 connect(saveAct, SIGNAL(triggered()), this, SLOT(save()));
230
231 saveAsAct = new QAction(tr("Save &As..."), this);
232 saveAsAct->setShortcuts(QKeySequence::SaveAs);
233 saveAsAct->setStatusTip(tr("Save the document under a new name"));
234 connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAs()));
235
236//! [0]
237 exitAct = new QAction(tr("E&xit"), this);
238 exitAct->setShortcut(tr("Ctrl+Q"));
239 exitAct->setStatusTip(tr("Exit the application"));
240 connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
241//! [0]
242
243 cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);
244 cutAct->setShortcuts(QKeySequence::Cut);
245 cutAct->setStatusTip(tr("Cut the current selection's contents to the "
246 "clipboard"));
247 connect(cutAct, SIGNAL(triggered()), this, SLOT(cut()));
248
249 copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
250 copyAct->setShortcuts(QKeySequence::Copy);
251 copyAct->setStatusTip(tr("Copy the current selection's contents to the "
252 "clipboard"));
253 connect(copyAct, SIGNAL(triggered()), this, SLOT(copy()));
254
255 pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
256 pasteAct->setShortcuts(QKeySequence::Paste);
257 pasteAct->setStatusTip(tr("Paste the clipboard's contents into the current "
258 "selection"));
259 connect(pasteAct, SIGNAL(triggered()), this, SLOT(paste()));
260
261 closeAct = new QAction(tr("Cl&ose"), this);
262 closeAct->setShortcut(tr("Ctrl+F4"));
263 closeAct->setStatusTip(tr("Close the active window"));
264 connect(closeAct, SIGNAL(triggered()),
265 mdiArea, SLOT(closeActiveSubWindow()));
266
267 closeAllAct = new QAction(tr("Close &All"), this);
268 closeAllAct->setStatusTip(tr("Close all the windows"));
269 connect(closeAllAct, SIGNAL(triggered()),
270 mdiArea, SLOT(closeAllSubWindows()));
271
272 tileAct = new QAction(tr("&Tile"), this);
273 tileAct->setStatusTip(tr("Tile the windows"));
274 connect(tileAct, SIGNAL(triggered()), mdiArea, SLOT(tileSubWindows()));
275
276 cascadeAct = new QAction(tr("&Cascade"), this);
277 cascadeAct->setStatusTip(tr("Cascade the windows"));
278 connect(cascadeAct, SIGNAL(triggered()), mdiArea, SLOT(cascadeSubWindows()));
279
280 nextAct = new QAction(tr("Ne&xt"), this);
281 nextAct->setShortcuts(QKeySequence::NextChild);
282 nextAct->setStatusTip(tr("Move the focus to the next window"));
283 connect(nextAct, SIGNAL(triggered()),
284 mdiArea, SLOT(activateNextSubWindow()));
285
286 previousAct = new QAction(tr("Pre&vious"), this);
287 previousAct->setShortcuts(QKeySequence::PreviousChild);
288 previousAct->setStatusTip(tr("Move the focus to the previous "
289 "window"));
290 connect(previousAct, SIGNAL(triggered()),
291 mdiArea, SLOT(activatePreviousSubWindow()));
292
293 separatorAct = new QAction(this);
294 separatorAct->setSeparator(true);
295
296 aboutAct = new QAction(tr("&About"), this);
297 aboutAct->setStatusTip(tr("Show the application's About box"));
298 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
299
300 aboutQtAct = new QAction(tr("About &Qt"), this);
301 aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
302 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
303}
304
305void MainWindow::createMenus()
306{
307 fileMenu = menuBar()->addMenu(tr("&File"));
308 fileMenu->addAction(newAct);
309 fileMenu->addAction(openAct);
310 fileMenu->addAction(saveAct);
311 fileMenu->addAction(saveAsAct);
312 fileMenu->addSeparator();
313 QAction *action = fileMenu->addAction(tr("Switch layout direction"));
314 connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection()));
315 fileMenu->addAction(exitAct);
316
317 editMenu = menuBar()->addMenu(tr("&Edit"));
318 editMenu->addAction(cutAct);
319 editMenu->addAction(copyAct);
320 editMenu->addAction(pasteAct);
321
322 windowMenu = menuBar()->addMenu(tr("&Window"));
323 updateWindowMenu();
324 connect(windowMenu, SIGNAL(aboutToShow()), this, SLOT(updateWindowMenu()));
325
326 menuBar()->addSeparator();
327
328 helpMenu = menuBar()->addMenu(tr("&Help"));
329 helpMenu->addAction(aboutAct);
330 helpMenu->addAction(aboutQtAct);
331}
332
333void MainWindow::createToolBars()
334{
335 fileToolBar = addToolBar(tr("File"));
336 fileToolBar->addAction(newAct);
337 fileToolBar->addAction(openAct);
338 fileToolBar->addAction(saveAct);
339
340 editToolBar = addToolBar(tr("Edit"));
341 editToolBar->addAction(cutAct);
342 editToolBar->addAction(copyAct);
343 editToolBar->addAction(pasteAct);
344}
345
346void MainWindow::createStatusBar()
347{
348 statusBar()->showMessage(tr("Ready"));
349}
350
351void MainWindow::readSettings()
352{
353 QSettings settings("Trolltech", "MDI Example");
354 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
355 QSize size = settings.value("size", QSize(400, 400)).toSize();
356 move(pos);
357 resize(size);
358}
359
360void MainWindow::writeSettings()
361{
362 QSettings settings("Trolltech", "MDI Example");
363 settings.setValue("pos", pos());
364 settings.setValue("size", size());
365}
366
367MdiChild *MainWindow::activeMdiChild()
368{
369 if (QMdiSubWindow *activeSubWindow = mdiArea->activeSubWindow())
370 return qobject_cast<MdiChild *>(activeSubWindow->widget());
371 return 0;
372}
373
374QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName)
375{
376 QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
377
378 foreach (QMdiSubWindow *window, mdiArea->subWindowList()) {
379 MdiChild *mdiChild = qobject_cast<MdiChild *>(window->widget());
380 if (mdiChild->currentFile() == canonicalFilePath)
381 return window;
382 }
383 return 0;
384}
385
386void MainWindow::switchLayoutDirection()
387{
388 if (layoutDirection() == Qt::LeftToRight)
389 qApp->setLayoutDirection(Qt::RightToLeft);
390 else
391 qApp->setLayoutDirection(Qt::LeftToRight);
392}
393
394void MainWindow::setActiveSubWindow(QWidget *window)
395{
396 if (!window)
397 return;
398 mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow *>(window));
399}
Note: See TracBrowser for help on using the repository browser.