1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
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.
16 **
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.
25 **
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.
29 **
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.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "mainwindow.h"
43 #include "colorswatch.h"
44 #include "toolbar.h"
45 
46 #include <QAction>
47 #include <QLayout>
48 #include <QMenu>
49 #include <QMenuBar>
50 #include <QStatusBar>
51 #include <QTextEdit>
52 #include <QFile>
53 #include <QDataStream>
54 #include <QFileDialog>
55 #include <QMessageBox>
56 #include <QSignalMapper>
57 #include <QApplication>
58 #include <QPainter>
59 #include <QMouseEvent>
60 #include <QLineEdit>
61 #include <QComboBox>
62 #include <QLabel>
63 #include <QPushButton>
64 #include <qdebug.h>
65 
66 static const char * const message =
67     "<p><b>Qt Main Window Demo</b></p>"
68 
69     "<p>This is a demonstration of the QMainWindow, QToolBar and "
70     "QDockWidget classes.</p>"
71 
72     "<p>The tool bar and dock widgets can be dragged around and rearranged "
73     "using the mouse or via the menu.</p>"
74 
75     "<p>Each dock widget contains a colored frame and a context "
76     "(right-click) menu.</p>"
77 
78 #ifdef Q_WS_MAC
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>"
81 #endif
82     ;
83 
Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures)84 Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures)
85 
86 MainWindow::MainWindow(const QMap<QString, QSize> &customSizeHints,
87                         QWidget *parent, Qt::WindowFlags flags)
88     : QMainWindow(parent, flags)
89 {
90     setObjectName("MainWindow");
91     setWindowTitle("Qt Main Window Demo");
92 
93     center = new QTextEdit(this);
94     center->setReadOnly(true);
95     center->setMinimumSize(400, 205);
96     setCentralWidget(center);
97 
98     setupToolBar();
99     setupMenuBar();
100     setupDockWidgets(customSizeHints);
101 
102     statusBar()->showMessage(tr("Status Bar"));
103 }
104 
actionTriggered(QAction * action)105 void MainWindow::actionTriggered(QAction *action)
106 {
107     qDebug("action '%s' triggered", action->text().toLocal8Bit().data());
108 }
109 
setupToolBar()110 void MainWindow::setupToolBar()
111 {
112     for (int i = 0; i < 3; ++i) {
113         ToolBar *tb = new ToolBar(QString::fromLatin1("Tool Bar %1").arg(i + 1), this);
114         toolBars.append(tb);
115         addToolBar(tb);
116     }
117 }
118 
setupMenuBar()119 void MainWindow::setupMenuBar()
120 {
121     QMenu *menu = menuBar()->addMenu(tr("&File"));
122 
123     QAction *action = menu->addAction(tr("Save layout..."));
124     connect(action, SIGNAL(triggered()), this, SLOT(saveLayout()));
125 
126     action = menu->addAction(tr("Load layout..."));
127     connect(action, SIGNAL(triggered()), this, SLOT(loadLayout()));
128 
129     action = menu->addAction(tr("Switch layout direction"));
130     connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection()));
131 
132     menu->addSeparator();
133 
134     menu->addAction(tr("&Quit"), this, SLOT(close()));
135 
136     mainWindowMenu = menuBar()->addMenu(tr("Main window"));
137 
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()));
142 
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()));
147 
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()));
152 
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()));
157 
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()));
162 
163     QMenu *toolBarMenu = menuBar()->addMenu(tr("Tool bars"));
164     for (int i = 0; i < toolBars.count(); ++i)
165         toolBarMenu->addMenu(toolBars.at(i)->menu);
166 
167     dockWidgetMenu = menuBar()->addMenu(tr("&Dock Widgets"));
168 }
169 
setDockOptions()170 void MainWindow::setDockOptions()
171 {
172     DockOptions opts;
173     QList<QAction*> actions = mainWindowMenu->actions();
174 
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;
185 
186     QMainWindow::setDockOptions(opts);
187 }
188 
saveLayout()189 void MainWindow::saveLayout()
190 {
191     QString fileName
192         = QFileDialog::getSaveFileName(this, tr("Save layout"));
193     if (fileName.isEmpty())
194         return;
195     QFile file(fileName);
196     if (!file.open(QFile::WriteOnly)) {
197         QString msg = tr("Failed to open %1\n%2")
198                         .arg(fileName)
199                         .arg(file.errorString());
200         QMessageBox::warning(this, tr("Error"), msg);
201         return;
202     }
203 
204     QByteArray geo_data = saveGeometry();
205     QByteArray layout_data = saveState();
206 
207     bool ok = file.putChar((uchar)geo_data.size());
208     if (ok)
209         ok = file.write(geo_data) == geo_data.size();
210     if (ok)
211         ok = file.write(layout_data) == layout_data.size();
212 
213     if (!ok) {
214         QString msg = tr("Error writing to %1\n%2")
215                         .arg(fileName)
216                         .arg(file.errorString());
217         QMessageBox::warning(this, tr("Error"), msg);
218         return;
219     }
220 }
221 
loadLayout()222 void MainWindow::loadLayout()
223 {
224     QString fileName
225         = QFileDialog::getOpenFileName(this, tr("Load layout"));
226     if (fileName.isEmpty())
227         return;
228     QFile file(fileName);
229     if (!file.open(QFile::ReadOnly)) {
230         QString msg = tr("Failed to open %1\n%2")
231                         .arg(fileName)
232                         .arg(file.errorString());
233         QMessageBox::warning(this, tr("Error"), msg);
234         return;
235     }
236 
237     uchar geo_size;
238     QByteArray geo_data;
239     QByteArray layout_data;
240 
241     bool ok = file.getChar((char*)&geo_size);
242     if (ok) {
243         geo_data = file.read(geo_size);
244         ok = geo_data.size() == geo_size;
245     }
246     if (ok) {
247         layout_data = file.readAll();
248         ok = layout_data.size() > 0;
249     }
250 
251     if (ok)
252         ok = restoreGeometry(geo_data);
253     if (ok)
254         ok = restoreState(layout_data);
255 
256     if (!ok) {
257         QString msg = tr("Error reading %1")
258                         .arg(fileName);
259         QMessageBox::warning(this, tr("Error"), msg);
260         return;
261     }
262 }
263 
addAction(QMenu * menu,const QString & text,QActionGroup * group,QSignalMapper * mapper,int id)264 QAction *addAction(QMenu *menu, const QString &text, QActionGroup *group, QSignalMapper *mapper,
265                     int id)
266 {
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);
274     return result;
275 }
276 
setupDockWidgets(const QMap<QString,QSize> & customSizeHints)277 void MainWindow::setupDockWidgets(const QMap<QString, QSize> &customSizeHints)
278 {
279     qRegisterMetaType<QDockWidget::DockWidgetFeatures>();
280 
281     mapper = new QSignalMapper(this);
282     connect(mapper, SIGNAL(mapped(int)), this, SLOT(setCorner(int)));
283 
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);
289 
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);
295 
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);
301 
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);
307 
308     dockWidgetMenu->addSeparator();
309 
310     static const struct Set {
311         const char * name;
312         uint flags;
313         Qt::DockWidgetArea area;
314     } sets [] = {
315 #ifndef Q_WS_MAC
316         { "Black", 0, Qt::LeftDockWidgetArea },
317 #else
318         { "Black", Qt::Drawer, Qt::LeftDockWidgetArea },
319 #endif
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 }
325     };
326     const int setCount = sizeof(sets) / sizeof(Set);
327 
328     for (int i = 0; i < setCount; ++i) {
329         ColorSwatch *swatch = new ColorSwatch(tr(sets[i].name), this, Qt::WindowFlags(sets[i].flags));
330         if (i%2)
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);
337 
338 #ifdef Q_WS_QWS
339             QPalette pal = palette();
340             pal.setBrush(backgroundRole(), QColor(0,0,0,0));
341             swatch->setPalette(pal);
342 #endif
343         }
344 
345         QString name = QString::fromLatin1(sets[i].name);
346         if (customSizeHints.contains(name))
347             swatch->setCustomSizeHint(customSizeHints.value(name));
348 
349         addDockWidget(sets[i].area, swatch);
350         dockWidgetMenu->addMenu(swatch->menu);
351     }
352 
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*)));
358 
359     dockWidgetMenu->addSeparator();
360     dockWidgetMenu->addAction(createDockWidgetAction);
361     dockWidgetMenu->addMenu(destroyDockWidgetMenu);
362 }
363 
setCorner(int id)364 void MainWindow::setCorner(int id)
365 {
366     switch (id) {
367         case 0:
368             QMainWindow::setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
369             break;
370         case 1:
371             QMainWindow::setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
372             break;
373         case 2:
374             QMainWindow::setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
375             break;
376         case 3:
377             QMainWindow::setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
378             break;
379         case 4:
380             QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
381             break;
382         case 5:
383             QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
384             break;
385         case 6:
386             QMainWindow::setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
387             break;
388         case 7:
389             QMainWindow::setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
390             break;
391     }
392 }
393 
showEvent(QShowEvent * event)394 void MainWindow::showEvent(QShowEvent *event)
395 {
396     QMainWindow::showEvent(event);
397 }
398 
switchLayoutDirection()399 void MainWindow::switchLayoutDirection()
400 {
401     if (layoutDirection() == Qt::LeftToRight)
402         qApp->setLayoutDirection(Qt::RightToLeft);
403     else
404         qApp->setLayoutDirection(Qt::LeftToRight);
405 }
406 
407 class CreateDockWidgetDialog : public QDialog
408 {
409 public:
410     CreateDockWidgetDialog(QWidget *parent = 0);
411 
412     QString objectName() const;
413     Qt::DockWidgetArea location() const;
414 
415 private:
416     QLineEdit *m_objectName;
417     QComboBox *m_location;
418 };
419 
CreateDockWidgetDialog(QWidget * parent)420 CreateDockWidgetDialog::CreateDockWidgetDialog(QWidget *parent)
421     : QDialog(parent)
422 {
423     QGridLayout *layout = new QGridLayout(this);
424 
425     layout->addWidget(new QLabel(tr("Object name:")), 0, 0);
426     m_objectName = new QLineEdit;
427     layout->addWidget(m_objectName, 0, 1);
428 
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);
438 
439     QHBoxLayout *buttonLayout = new QHBoxLayout;
440     layout->addLayout(buttonLayout, 2, 0, 1, 2);
441     buttonLayout->addStretch();
442 
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);
449 
450     okButton->setDefault(true);
451 }
452 
objectName() const453 QString CreateDockWidgetDialog::objectName() const
454 {
455     return m_objectName->text();
456 }
457 
location() const458 Qt::DockWidgetArea CreateDockWidgetDialog::location() const
459 {
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;
465         default:
466             break;
467     }
468     return Qt::NoDockWidgetArea;
469 }
470 
createDockWidget()471 void MainWindow::createDockWidget()
472 {
473     CreateDockWidgetDialog dialog(this);
474     int ret = dialog.exec();
475     if (ret == QDialog::Rejected)
476         return;
477 
478     QDockWidget *dw = new QDockWidget;
479     dw->setObjectName(dialog.objectName());
480     dw->setWindowTitle(dialog.objectName());
481     dw->setWidget(new QTextEdit);
482 
483     Qt::DockWidgetArea area = dialog.location();
484     switch (area) {
485         case Qt::LeftDockWidgetArea:
486         case Qt::RightDockWidgetArea:
487         case Qt::TopDockWidgetArea:
488         case Qt::BottomDockWidgetArea:
489             addDockWidget(area, dw);
490             break;
491         default:
492             if (!restoreDockWidget(dw)) {
493                 QMessageBox::warning(this, QString(), tr("Failed to restore dock widget"));
494                 delete dw;
495                 return;
496             }
497             break;
498     }
499 
500     extraDockWidgets.append(dw);
501     destroyDockWidgetMenu->setEnabled(true);
502     destroyDockWidgetMenu->addAction(new QAction(dialog.objectName(), this));
503 }
504 
destroyDockWidget(QAction * action)505 void MainWindow::destroyDockWidget(QAction *action)
506 {
507     int index = destroyDockWidgetMenu->actions().indexOf(action);
508     delete extraDockWidgets.takeAt(index);
509     destroyDockWidgetMenu->removeAction(action);
510     action->deleteLater();
511 
512     if (destroyDockWidgetMenu->isEmpty())
513         destroyDockWidgetMenu->setEnabled(false);
514 }
515