1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the demonstration applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "mainwindow.h"
43 #include "colorswatch.h"
53 #include <QDataStream>
54 #include <QFileDialog>
55 #include <QMessageBox>
56 #include <QSignalMapper>
57 #include <QApplication>
59 #include <QMouseEvent>
63 #include <QPushButton>
66 static const char * const message =
67 "<p><b>Qt Main Window Demo</b></p>"
69 "<p>This is a demonstration of the QMainWindow, QToolBar and "
70 "QDockWidget classes.</p>"
72 "<p>The tool bar and dock widgets can be dragged around and rearranged "
73 "using the mouse or via the menu.</p>"
75 "<p>Each dock widget contains a colored frame and a context "
76 "(right-click) menu.</p>"
79 "<p>On Mac OS X, the \"Black\" dock widget has been created as a "
80 "<em>Drawer</em>, which is a special kind of QDockWidget.</p>"
84 Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures)
86 MainWindow::MainWindow(const QMap<QString, QSize> &customSizeHints,
87 QWidget *parent, Qt::WindowFlags flags)
88 : QMainWindow(parent, flags)
90 setObjectName("MainWindow");
91 setWindowTitle("Qt Main Window Demo");
93 center = new QTextEdit(this);
94 center->setReadOnly(true);
95 center->setMinimumSize(400, 205);
96 setCentralWidget(center);
100 setupDockWidgets(customSizeHints);
102 statusBar()->showMessage(tr("Status Bar"));
105 void MainWindow::actionTriggered(QAction *action)
107 qDebug("action '%s' triggered", action->text().toLocal8Bit().data());
110 void MainWindow::setupToolBar()
112 for (int i = 0; i < 3; ++i) {
113 ToolBar *tb = new ToolBar(QString::fromLatin1("Tool Bar %1").arg(i + 1), this);
119 void MainWindow::setupMenuBar()
121 QMenu *menu = menuBar()->addMenu(tr("&File"));
123 QAction *action = menu->addAction(tr("Save layout..."));
124 connect(action, SIGNAL(triggered()), this, SLOT(saveLayout()));
126 action = menu->addAction(tr("Load layout..."));
127 connect(action, SIGNAL(triggered()), this, SLOT(loadLayout()));
129 action = menu->addAction(tr("Switch layout direction"));
130 connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection()));
132 menu->addSeparator();
134 menu->addAction(tr("&Quit"), this, SLOT(close()));
136 mainWindowMenu = menuBar()->addMenu(tr("Main window"));
138 action = mainWindowMenu->addAction(tr("Animated docks"));
139 action->setCheckable(true);
140 action->setChecked(dockOptions() & AnimatedDocks);
141 connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions()));
143 action = mainWindowMenu->addAction(tr("Allow nested docks"));
144 action->setCheckable(true);
145 action->setChecked(dockOptions() & AllowNestedDocks);
146 connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions()));
148 action = mainWindowMenu->addAction(tr("Allow tabbed docks"));
149 action->setCheckable(true);
150 action->setChecked(dockOptions() & AllowTabbedDocks);
151 connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions()));
153 action = mainWindowMenu->addAction(tr("Force tabbed docks"));
154 action->setCheckable(true);
155 action->setChecked(dockOptions() & ForceTabbedDocks);
156 connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions()));
158 action = mainWindowMenu->addAction(tr("Vertical tabs"));
159 action->setCheckable(true);
160 action->setChecked(dockOptions() & VerticalTabs);
161 connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions()));
163 QMenu *toolBarMenu = menuBar()->addMenu(tr("Tool bars"));
164 for (int i = 0; i < toolBars.count(); ++i)
165 toolBarMenu->addMenu(toolBars.at(i)->menu);
167 dockWidgetMenu = menuBar()->addMenu(tr("&Dock Widgets"));
170 void MainWindow::setDockOptions()
173 QList<QAction*> actions = mainWindowMenu->actions();
175 if (actions.at(0)->isChecked())
176 opts |= AnimatedDocks;
177 if (actions.at(1)->isChecked())
178 opts |= AllowNestedDocks;
179 if (actions.at(2)->isChecked())
180 opts |= AllowTabbedDocks;
181 if (actions.at(3)->isChecked())
182 opts |= ForceTabbedDocks;
183 if (actions.at(4)->isChecked())
184 opts |= VerticalTabs;
186 QMainWindow::setDockOptions(opts);
189 void MainWindow::saveLayout()
192 = QFileDialog::getSaveFileName(this, tr("Save layout"));
193 if (fileName.isEmpty())
195 QFile file(fileName);
196 if (!file.open(QFile::WriteOnly)) {
197 QString msg = tr("Failed to open %1\n%2")
199 .arg(file.errorString());
200 QMessageBox::warning(this, tr("Error"), msg);
204 QByteArray geo_data = saveGeometry();
205 QByteArray layout_data = saveState();
207 bool ok = file.putChar((uchar)geo_data.size());
209 ok = file.write(geo_data) == geo_data.size();
211 ok = file.write(layout_data) == layout_data.size();
214 QString msg = tr("Error writing to %1\n%2")
216 .arg(file.errorString());
217 QMessageBox::warning(this, tr("Error"), msg);
222 void MainWindow::loadLayout()
225 = QFileDialog::getOpenFileName(this, tr("Load layout"));
226 if (fileName.isEmpty())
228 QFile file(fileName);
229 if (!file.open(QFile::ReadOnly)) {
230 QString msg = tr("Failed to open %1\n%2")
232 .arg(file.errorString());
233 QMessageBox::warning(this, tr("Error"), msg);
239 QByteArray layout_data;
241 bool ok = file.getChar((char*)&geo_size);
243 geo_data = file.read(geo_size);
244 ok = geo_data.size() == geo_size;
247 layout_data = file.readAll();
248 ok = layout_data.size() > 0;
252 ok = restoreGeometry(geo_data);
254 ok = restoreState(layout_data);
257 QString msg = tr("Error reading %1")
259 QMessageBox::warning(this, tr("Error"), msg);
264 QAction *addAction(QMenu *menu, const QString &text, QActionGroup *group, QSignalMapper *mapper,
267 bool first = group->actions().isEmpty();
268 QAction *result = menu->addAction(text);
269 result->setCheckable(true);
270 result->setChecked(first);
271 group->addAction(result);
272 QObject::connect(result, SIGNAL(triggered()), mapper, SLOT(map()));
273 mapper->setMapping(result, id);
277 void MainWindow::setupDockWidgets(const QMap<QString, QSize> &customSizeHints)
279 qRegisterMetaType<QDockWidget::DockWidgetFeatures>();
281 mapper = new QSignalMapper(this);
282 connect(mapper, SIGNAL(mapped(int)), this, SLOT(setCorner(int)));
284 QMenu *corner_menu = dockWidgetMenu->addMenu(tr("Top left corner"));
285 QActionGroup *group = new QActionGroup(this);
286 group->setExclusive(true);
287 ::addAction(corner_menu, tr("Top dock area"), group, mapper, 0);
288 ::addAction(corner_menu, tr("Left dock area"), group, mapper, 1);
290 corner_menu = dockWidgetMenu->addMenu(tr("Top right corner"));
291 group = new QActionGroup(this);
292 group->setExclusive(true);
293 ::addAction(corner_menu, tr("Top dock area"), group, mapper, 2);
294 ::addAction(corner_menu, tr("Right dock area"), group, mapper, 3);
296 corner_menu = dockWidgetMenu->addMenu(tr("Bottom left corner"));
297 group = new QActionGroup(this);
298 group->setExclusive(true);
299 ::addAction(corner_menu, tr("Bottom dock area"), group, mapper, 4);
300 ::addAction(corner_menu, tr("Left dock area"), group, mapper, 5);
302 corner_menu = dockWidgetMenu->addMenu(tr("Bottom right corner"));
303 group = new QActionGroup(this);
304 group->setExclusive(true);
305 ::addAction(corner_menu, tr("Bottom dock area"), group, mapper, 6);
306 ::addAction(corner_menu, tr("Right dock area"), group, mapper, 7);
308 dockWidgetMenu->addSeparator();
310 static const struct Set {
313 Qt::DockWidgetArea area;
316 { "Black", 0, Qt::LeftDockWidgetArea },
318 { "Black", Qt::Drawer, Qt::LeftDockWidgetArea },
320 { "White", 0, Qt::RightDockWidgetArea },
321 { "Red", 0, Qt::TopDockWidgetArea },
322 { "Green", 0, Qt::TopDockWidgetArea },
323 { "Blue", 0, Qt::BottomDockWidgetArea },
324 { "Yellow", 0, Qt::BottomDockWidgetArea }
326 const int setCount = sizeof(sets) / sizeof(Set);
328 for (int i = 0; i < setCount; ++i) {
329 ColorSwatch *swatch = new ColorSwatch(tr(sets[i].name), this, Qt::WindowFlags(sets[i].flags));
331 swatch->setWindowIcon(QIcon(QPixmap(":/res/qt.png")));
332 if (qstrcmp(sets[i].name, "Blue") == 0) {
333 BlueTitleBar *titlebar = new BlueTitleBar(swatch);
334 swatch->setTitleBarWidget(titlebar);
335 connect(swatch, SIGNAL(topLevelChanged(bool)), titlebar, SLOT(updateMask()));
336 connect(swatch, SIGNAL(featuresChanged(QDockWidget::DockWidgetFeatures)), titlebar, SLOT(updateMask()), Qt::QueuedConnection);
339 QPalette pal = palette();
340 pal.setBrush(backgroundRole(), QColor(0,0,0,0));
341 swatch->setPalette(pal);
345 QString name = QString::fromLatin1(sets[i].name);
346 if (customSizeHints.contains(name))
347 swatch->setCustomSizeHint(customSizeHints.value(name));
349 addDockWidget(sets[i].area, swatch);
350 dockWidgetMenu->addMenu(swatch->menu);
353 createDockWidgetAction = new QAction(tr("Add dock widget..."), this);
354 connect(createDockWidgetAction, SIGNAL(triggered()), this, SLOT(createDockWidget()));
355 destroyDockWidgetMenu = new QMenu(tr("Destroy dock widget"), this);
356 destroyDockWidgetMenu->setEnabled(false);
357 connect(destroyDockWidgetMenu, SIGNAL(triggered(QAction*)), this, SLOT(destroyDockWidget(QAction*)));
359 dockWidgetMenu->addSeparator();
360 dockWidgetMenu->addAction(createDockWidgetAction);
361 dockWidgetMenu->addMenu(destroyDockWidgetMenu);
364 void MainWindow::setCorner(int id)
368 QMainWindow::setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
371 QMainWindow::setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
374 QMainWindow::setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
377 QMainWindow::setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
380 QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
383 QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
386 QMainWindow::setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
389 QMainWindow::setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
394 void MainWindow::showEvent(QShowEvent *event)
396 QMainWindow::showEvent(event);
399 void MainWindow::switchLayoutDirection()
401 if (layoutDirection() == Qt::LeftToRight)
402 qApp->setLayoutDirection(Qt::RightToLeft);
404 qApp->setLayoutDirection(Qt::LeftToRight);
407 class CreateDockWidgetDialog : public QDialog
410 CreateDockWidgetDialog(QWidget *parent = 0);
412 QString objectName() const;
413 Qt::DockWidgetArea location() const;
416 QLineEdit *m_objectName;
417 QComboBox *m_location;
420 CreateDockWidgetDialog::CreateDockWidgetDialog(QWidget *parent)
423 QGridLayout *layout = new QGridLayout(this);
425 layout->addWidget(new QLabel(tr("Object name:")), 0, 0);
426 m_objectName = new QLineEdit;
427 layout->addWidget(m_objectName, 0, 1);
429 layout->addWidget(new QLabel(tr("Location:")), 1, 0);
430 m_location = new QComboBox;
431 m_location->setEditable(false);
432 m_location->addItem(tr("Top"));
433 m_location->addItem(tr("Left"));
434 m_location->addItem(tr("Right"));
435 m_location->addItem(tr("Bottom"));
436 m_location->addItem(tr("Restore"));
437 layout->addWidget(m_location, 1, 1);
439 QHBoxLayout *buttonLayout = new QHBoxLayout;
440 layout->addLayout(buttonLayout, 2, 0, 1, 2);
441 buttonLayout->addStretch();
443 QPushButton *cancelButton = new QPushButton(tr("Cancel"));
444 connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
445 buttonLayout->addWidget(cancelButton);
446 QPushButton *okButton = new QPushButton(tr("Ok"));
447 connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
448 buttonLayout->addWidget(okButton);
450 okButton->setDefault(true);
453 QString CreateDockWidgetDialog::objectName() const
455 return m_objectName->text();
458 Qt::DockWidgetArea CreateDockWidgetDialog::location() const
460 switch (m_location->currentIndex()) {
461 case 0: return Qt::TopDockWidgetArea;
462 case 1: return Qt::LeftDockWidgetArea;
463 case 2: return Qt::RightDockWidgetArea;
464 case 3: return Qt::BottomDockWidgetArea;
468 return Qt::NoDockWidgetArea;
471 void MainWindow::createDockWidget()
473 CreateDockWidgetDialog dialog(this);
474 int ret = dialog.exec();
475 if (ret == QDialog::Rejected)
478 QDockWidget *dw = new QDockWidget;
479 dw->setObjectName(dialog.objectName());
480 dw->setWindowTitle(dialog.objectName());
481 dw->setWidget(new QTextEdit);
483 Qt::DockWidgetArea area = dialog.location();
485 case Qt::LeftDockWidgetArea:
486 case Qt::RightDockWidgetArea:
487 case Qt::TopDockWidgetArea:
488 case Qt::BottomDockWidgetArea:
489 addDockWidget(area, dw);
492 if (!restoreDockWidget(dw)) {
493 QMessageBox::warning(this, QString(), tr("Failed to restore dock widget"));
500 extraDockWidgets.append(dw);
501 destroyDockWidgetMenu->setEnabled(true);
502 destroyDockWidgetMenu->addAction(new QAction(dialog.objectName(), this));
505 void MainWindow::destroyDockWidget(QAction *action)
507 int index = destroyDockWidgetMenu->actions().indexOf(action);
508 delete extraDockWidgets.takeAt(index);
509 destroyDockWidgetMenu->removeAction(action);
510 action->deleteLater();
512 if (destroyDockWidgetMenu->isEmpty())
513 destroyDockWidgetMenu->setEnabled(false);