8#if QT_CONFIG(dockwidget)
9#include "qdockwidget.h"
15#include <qapplication.h>
20#if QT_CONFIG(statusbar)
21#include <qstatusbar.h>
29#include <private/qwidget_p.h>
31#include "qtoolbar_p.h"
34#include <QtGui/qpa/qplatformwindow.h>
35#include <QtGui/qpa/qplatformwindow_p.h>
39using namespace Qt::StringLiterals;
43 Q_DECLARE_PUBLIC(QMainWindow)
62 return mainWindow ? mainWindow->d_func()->layout.data() :
static_cast<QMainWindowLayout *>(
nullptr);
68 return QMainWindowPrivate::mainWindowLayout(mainWindow);
75 layout =
new QMainWindowLayout(q,
nullptr);
77 const int metric = q->style()->pixelMetric(QStyle::PM_ToolBarIconSize,
nullptr, q);
78 iconSize = QSize(metric, metric);
79 q->setAttribute(Qt::WA_Hover);
80 q->setAcceptDrops(
true);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
239
240
241
242
243
244
245
246
247
248
251
252
253
254
255
256
257
258
259
260
262#if QT_CONFIG(dockwidget)
264
265
266
267
268
269
270
271
275
276
277
278
279
280
281QMainWindow::QMainWindow(QWidget *parent, Qt::WindowFlags flags)
282 : QWidget(*(
new QMainWindowPrivate()), parent, flags | Qt::Window)
289
290
291QMainWindow::~QMainWindow()
295
296
297
298
299
300
303
304
305
306
307
308
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
349void QMainWindow::setDockOptions(DockOptions opt)
352 d->layout->setDockOptions(opt);
355QMainWindow::DockOptions QMainWindow::dockOptions()
const
357 Q_D(
const QMainWindow);
358 return d->layout->dockOptions;
361QSize QMainWindow::iconSize()
const
362{
return d_func()->iconSize; }
364void QMainWindow::setIconSize(
const QSize &iconSize)
369 const int metric = style()->pixelMetric(QStyle::PM_ToolBarIconSize,
nullptr,
this);
370 sz = QSize(metric, metric);
372 if (d->iconSize != sz) {
374 emit iconSizeChanged(d->iconSize);
376 d->explicitIconSize = iconSize.isValid();
380
381
382
383
384
385
386
387
389Qt::ToolButtonStyle QMainWindow::toolButtonStyle()
const
390{
return d_func()->toolButtonStyle; }
392void QMainWindow::setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
395 if (d->toolButtonStyle == toolButtonStyle)
397 d->toolButtonStyle = toolButtonStyle;
398 emit toolButtonStyleChanged(d->toolButtonStyle);
401#if QT_CONFIG(menubar)
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417QMenuBar *QMainWindow::menuBar()
const
419 QMenuBar *menuBar = qobject_cast<QMenuBar *>(layout()->menuBar());
421 QMainWindow *self =
const_cast<QMainWindow *>(
this);
422 menuBar =
new QMenuBar(self);
423 self->setMenuBar(menuBar);
429
430
431
432
433
434
435
436void QMainWindow::setMenuBar(QMenuBar *menuBar)
438 QLayout *topLayout = layout();
440 if (QWidget *existingMenuBar = topLayout->menuBar(); existingMenuBar && existingMenuBar != menuBar) {
442 QMenuBar *oldMenuBar = qobject_cast<QMenuBar *>(existingMenuBar);
443 if (oldMenuBar && menuBar) {
445 QWidget *cornerWidget = oldMenuBar->cornerWidget(Qt::TopLeftCorner);
447 menuBar->setCornerWidget(cornerWidget, Qt::TopLeftCorner);
449 cornerWidget = oldMenuBar->cornerWidget(Qt::TopRightCorner);
451 menuBar->setCornerWidget(cornerWidget, Qt::TopRightCorner);
454 existingMenuBar->hide();
455 existingMenuBar->setParent(
nullptr);
456 existingMenuBar->deleteLater();
458 topLayout->setMenuBar(menuBar);
462
463
464
465
466
467QWidget *QMainWindow::menuWidget()
const
469 QWidget *menuBar = d_func()->layout->menuBar();
474
475
476
477
478
479
480
481void QMainWindow::setMenuWidget(QWidget *menuBar)
484 if (d->layout->menuBar() && d->layout->menuBar() != menuBar) {
485 d->layout->menuBar()->hide();
486 d->layout->menuBar()->deleteLater();
488 d->layout->setMenuBar(menuBar);
492#if QT_CONFIG(statusbar)
494
495
496
497
498
499QStatusBar *QMainWindow::statusBar()
const
501 QStatusBar *statusbar = d_func()->layout->statusBar();
503 QMainWindow *self =
const_cast<QMainWindow *>(
this);
504 statusbar =
new QStatusBar(self);
505 statusbar->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
506 self->setStatusBar(statusbar);
512
513
514
515
516
517
518
519
520void QMainWindow::setStatusBar(QStatusBar *statusbar)
523 if (d->layout->statusBar() && d->layout->statusBar() != statusbar) {
524 d->layout->statusBar()->hide();
525 d->layout->statusBar()->deleteLater();
527 d->layout->setStatusBar(statusbar);
532
533
534
535
536
537QWidget *QMainWindow::centralWidget()
const
538{
return d_func()->layout->centralWidget(); }
541
542
543
544
545
546
547
548void QMainWindow::setCentralWidget(QWidget *widget)
551 if (d->layout->centralWidget() && d->layout->centralWidget() != widget) {
552 d->layout->centralWidget()->hide();
553 d->layout->centralWidget()->deleteLater();
555 d->layout->setCentralWidget(widget);
559
560
561
562
563
564
565QWidget *QMainWindow::takeCentralWidget()
568 QWidget *oldcentralwidget = d->layout->centralWidget();
569 if (oldcentralwidget) {
570 oldcentralwidget->setParent(
nullptr);
571 d->layout->setCentralWidget(
nullptr);
573 return oldcentralwidget;
576#if QT_CONFIG(dockwidget)
578
579
580
581
582
583void QMainWindow::setCorner(Qt::Corner corner, Qt::DockWidgetArea area)
587 case Qt::TopLeftCorner:
588 valid = (area == Qt::TopDockWidgetArea || area == Qt::LeftDockWidgetArea);
590 case Qt::TopRightCorner:
591 valid = (area == Qt::TopDockWidgetArea || area == Qt::RightDockWidgetArea);
593 case Qt::BottomLeftCorner:
594 valid = (area == Qt::BottomDockWidgetArea || area == Qt::LeftDockWidgetArea);
596 case Qt::BottomRightCorner:
597 valid = (area == Qt::BottomDockWidgetArea || area == Qt::RightDockWidgetArea);
600 if (Q_UNLIKELY(!valid))
601 qWarning(
"QMainWindow::setCorner(): 'area' is not valid for 'corner'");
603 d_func()->layout->setCorner(corner, area);
607
608
609
610
611
612Qt::DockWidgetArea QMainWindow::corner(Qt::Corner corner)
const
613{
return d_func()->layout->corner(corner); }
616#if QT_CONFIG(toolbar)
618static bool checkToolBarArea(Qt::ToolBarArea area,
const char *where)
621 case Qt::LeftToolBarArea:
622 case Qt::RightToolBarArea:
623 case Qt::TopToolBarArea:
624 case Qt::BottomToolBarArea:
629 qWarning(
"%s: invalid 'area' argument", where);
634
635
636
637void QMainWindow::addToolBarBreak(Qt::ToolBarArea area)
639 if (!checkToolBarArea(area,
"QMainWindow::addToolBarBreak"))
641 d_func()->layout->addToolBarBreak(area);
645
646
647void QMainWindow::insertToolBarBreak(QToolBar *before)
648{ d_func()->layout->insertToolBarBreak(before); }
651
652
654void QMainWindow::removeToolBarBreak(QToolBar *before)
657 d->layout->removeToolBarBreak(before);
661
662
663
664
665
666
667
668void QMainWindow::addToolBar(Qt::ToolBarArea area, QToolBar *toolbar)
670 if (!checkToolBarArea(area,
"QMainWindow::addToolBar"))
675 disconnect(
this, SIGNAL(iconSizeChanged(QSize)),
676 toolbar, SLOT(_q_updateIconSize(QSize)));
677 disconnect(
this, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
678 toolbar, SLOT(_q_updateToolButtonStyle(Qt::ToolButtonStyle)));
680 if (toolbar->d_func()->state && toolbar->d_func()->state->dragging) {
682#if QT_CONFIG(dockwidget)
683 bool animated = isAnimated();
686 toolbar->d_func()->endDrag();
687#if QT_CONFIG(dockwidget)
688 setAnimated(animated);
692 d->layout->removeToolBar(toolbar);
694 toolbar->d_func()->_q_updateIconSize(d->iconSize);
695 toolbar->d_func()->_q_updateToolButtonStyle(d->toolButtonStyle);
696 connect(
this, SIGNAL(iconSizeChanged(QSize)),
697 toolbar, SLOT(_q_updateIconSize(QSize)));
698 connect(
this, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
699 toolbar, SLOT(_q_updateToolButtonStyle(Qt::ToolButtonStyle)));
701 d->layout->addToolBar(area, toolbar);
705
706
707void QMainWindow::addToolBar(QToolBar *toolbar)
708{ addToolBar(Qt::TopToolBarArea, toolbar); }
711
712
713
714
715
716
717
718QToolBar *QMainWindow::addToolBar(
const QString &title)
720 QToolBar *toolBar =
new QToolBar(
this);
721 toolBar->setWindowTitle(title);
727
728
729
730
731
732
733
734void QMainWindow::insertToolBar(QToolBar *before, QToolBar *toolbar)
738 d->layout->removeToolBar(toolbar);
740 toolbar->d_func()->_q_updateIconSize(d->iconSize);
741 toolbar->d_func()->_q_updateToolButtonStyle(d->toolButtonStyle);
742 connect(
this, SIGNAL(iconSizeChanged(QSize)),
743 toolbar, SLOT(_q_updateIconSize(QSize)));
744 connect(
this, SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
745 toolbar, SLOT(_q_updateToolButtonStyle(Qt::ToolButtonStyle)));
747 d->layout->insertToolBar(before, toolbar);
751
752
753
754void QMainWindow::removeToolBar(QToolBar *toolbar)
757 d_func()->layout->removeToolBar(toolbar);
763
764
765
766
767
768
769Qt::ToolBarArea QMainWindow::toolBarArea(
const QToolBar *toolbar)
const
770{
return d_func()->layout->toolBarArea(toolbar); }
773
774
775
776
777
778
779bool QMainWindow::toolBarBreak(QToolBar *toolbar)
const
781 return d_func()->layout->toolBarBreak(toolbar);
786#if QT_CONFIG(dockwidget)
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
808bool QMainWindow::isAnimated()
const
810 Q_D(
const QMainWindow);
811 return d->layout->dockOptions & AnimatedDocks;
814void QMainWindow::setAnimated(
bool enabled)
818 DockOptions opts = d->layout->dockOptions;
819 opts.setFlag(AnimatedDocks, enabled);
821 d->layout->setDockOptions(opts);
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
844bool QMainWindow::isDockNestingEnabled()
const
846 Q_D(
const QMainWindow);
847 return d->layout->dockOptions & AllowNestedDocks;
850void QMainWindow::setDockNestingEnabled(
bool enabled)
854 DockOptions opts = d->layout->dockOptions;
855 opts.setFlag(AllowNestedDocks, enabled);
857 d->layout->setDockOptions(opts);
863
864
865
866
867
868
869
870
871
872
873
874
876bool QMainWindow::verticalTabsEnabled()
const
878 return d_func()->layout->verticalTabsEnabled();
881void QMainWindow::setVerticalTabsEnabled(
bool enabled)
883 d_func()->layout->setVerticalTabsEnabled(enabled);
887static bool checkDockWidgetArea(Qt::DockWidgetArea area,
const char *where)
890 case Qt::LeftDockWidgetArea:
891 case Qt::RightDockWidgetArea:
892 case Qt::TopDockWidgetArea:
893 case Qt::BottomDockWidgetArea:
898 qWarning(
"%s: invalid 'area' argument", where);
904
905
906
907
908
909
910
911
912bool QMainWindow::documentMode()
const
914 return d_func()->layout->documentMode();
917void QMainWindow::setDocumentMode(
bool enabled)
919 d_func()->layout->setDocumentMode(enabled);
923#if QT_CONFIG(tabwidget)
925
926
927
928
929
930
931
932
933QTabWidget::TabShape QMainWindow::tabShape()
const
935 return d_func()->layout->tabShape();
938void QMainWindow::setTabShape(QTabWidget::TabShape tabShape)
940 d_func()->layout->setTabShape(tabShape);
944
945
946
947
948
949
950
951
952
953QTabWidget::TabPosition QMainWindow::tabPosition(Qt::DockWidgetArea area)
const
955 if (!checkDockWidgetArea(area,
"QMainWindow::tabPosition"))
956 return QTabWidget::South;
957 return d_func()->layout->tabPosition(area);
961
962
963
964
965
966
967
968
969
970
971void QMainWindow::setTabPosition(Qt::DockWidgetAreas areas, QTabWidget::TabPosition tabPosition)
973 d_func()->layout->setTabPosition(areas, tabPosition);
978
979
980void QMainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget)
982 if (!checkDockWidgetArea(area,
"QMainWindow::addDockWidget"))
985 Qt::Orientation orientation = Qt::Vertical;
987 case Qt::TopDockWidgetArea:
988 case Qt::BottomDockWidgetArea:
989 orientation = Qt::Horizontal;
994 const Qt::DockWidgetArea oldArea = dockWidgetArea(dockwidget);
995 d_func()->layout->removeWidget(dockwidget);
996 addDockWidget(area, dockwidget, orientation);
998 emit dockwidget->dockLocationChanged(area);
1002
1003
1004
1005
1006
1007
1009bool QMainWindow::restoreDockWidget(QDockWidget *dockwidget)
1011 return d_func()->layout->restoreDockWidget(dockwidget);
1015
1016
1017
1018void QMainWindow::addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget,
1019 Qt::Orientation orientation)
1021 if (!checkDockWidgetArea(area,
"QMainWindow::addDockWidget"))
1025 d_func()->layout->addDockWidget(area, dockwidget, orientation);
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049void QMainWindow::splitDockWidget(QDockWidget *after, QDockWidget *dockwidget,
1050 Qt::Orientation orientation)
1052 d_func()->layout->splitDockWidget(after, dockwidget, orientation);
1055#if QT_CONFIG(tabbar)
1057
1058
1059
1060
1061
1062
1063
1064void QMainWindow::tabifyDockWidget(QDockWidget *first, QDockWidget *second)
1066 d_func()->layout->tabifyDockWidget(first, second);
1071
1072
1073
1074
1075
1076
1077
1079QList<QDockWidget*> QMainWindow::tabifiedDockWidgets(QDockWidget *dockwidget)
const
1081 Q_D(
const QMainWindow);
1082 return d->layout ? d->layout->tabifiedDockWidgets(dockwidget) : QList<QDockWidget *>();
1088
1089
1090
1091void QMainWindow::removeDockWidget(QDockWidget *dockwidget)
1094 d_func()->layout->removeWidget(dockwidget);
1100
1101
1102
1103
1104
1105
1106Qt::DockWidgetArea QMainWindow::dockWidgetArea(QDockWidget *dockwidget)
const
1107{
return d_func()->layout->dockWidgetArea(dockwidget); }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129void QMainWindow::resizeDocks(
const QList<QDockWidget *> &docks,
1130 const QList<
int> &sizes, Qt::Orientation orientation)
1132 d_func()->layout->layoutState.dockAreaLayout.resizeDocks(docks, sizes, orientation);
1133 d_func()->layout->invalidate();
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160QByteArray QMainWindow::saveState(
int version)
const
1163 QDataStream stream(&data, QIODevice::WriteOnly);
1164 stream.setVersion(QDataStream::Qt_5_0);
1165 stream << QMainWindowLayout::VersionMarker;
1167 d_func()->layout->saveState(stream);
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187bool QMainWindow::restoreState(
const QByteArray &state,
int version)
1189 if (state.isEmpty())
1191 QByteArray sd = state;
1192 QDataStream stream(&sd, QIODevice::ReadOnly);
1193 stream.setVersion(QDataStream::Qt_5_0);
1197 if (stream.status() != QDataStream::Ok || marker != QMainWindowLayout::VersionMarker || v != version)
1199 bool restored = d_func()->layout->restoreState(stream);
1204bool QMainWindow::event(QEvent *event)
1208#if QT_CONFIG(dockwidget)
1209 if (d->layout && d->layout->windowEvent(event))
1213 switch (event->type()) {
1215#if QT_CONFIG(toolbar)
1216 case QEvent::ToolBarChange: {
1217 Q_ASSERT(d->layout);
1218 d->layout->toggleToolBarsVisible();
1223#if QT_CONFIG(statustip)
1224 case QEvent::StatusTip:
1225#if QT_CONFIG(statusbar)
1226 Q_ASSERT(d->layout);
1227 if (QStatusBar *sb = d->layout->statusBar())
1228 sb->showMessage(
static_cast<QStatusTipEvent*>(event)->tip());
1231 static_cast<QStatusTipEvent*>(event)->ignore();
1235 case QEvent::StyleChange:
1236#if QT_CONFIG(dockwidget)
1237 Q_ASSERT(d->layout);
1238 d->layout->layoutState.dockAreaLayout.styleChangedEvent();
1240 if (!d->explicitIconSize)
1241 setIconSize(QSize());
1243#if QT_CONFIG(draganddrop)
1244 case QEvent::DragEnter:
1246 if (!d->layout->draggingWidget)
1250 case QEvent::DragMove: {
1251 if (!d->layout->draggingWidget)
1253 auto dragMoveEvent =
static_cast<QDragMoveEvent *>(event);
1254 d->layout->hover(d->layout->draggingWidget,
1255 mapToGlobal(dragMoveEvent->position()).toPoint());
1259 case QEvent::DragLeave:
1260 if (!d->layout->draggingWidget)
1262 d->layout->hover(d->layout->draggingWidget, pos() - QPoint(-1, -1));
1269 return QWidget::event(event);
1272#if QT_CONFIG(toolbar)
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286void QMainWindow::setUnifiedTitleAndToolBarOnMac(
bool enabled)
1293 d->useUnifiedToolBar = enabled;
1299 setAttribute(Qt::WA_NativeWindow);
1300 setAttribute(Qt::WA_TranslucentBackground, enabled);
1303 using namespace QNativeInterface::Private;
1304 if (
auto *platformWindow =
dynamic_cast<QCocoaWindow*>(window()->windowHandle()->handle()))
1305 platformWindow->setContentBorderEnabled(enabled);
1313bool QMainWindow::unifiedTitleAndToolBarOnMac()
const
1316 return d_func()->useUnifiedToolBar;
1324
1325
1326bool QMainWindow::isSeparator(
const QPoint &pos)
const
1328#if QT_CONFIG(dockwidget)
1329 Q_D(
const QMainWindow);
1330 return !d->layout->layoutState.dockAreaLayout.findSeparator(pos).isEmpty();
1337#ifndef QT_NO_CONTEXTMENU
1339
1340
1341void QMainWindow::contextMenuEvent(QContextMenuEvent *event)
1346 QWidget *child = childAt(event->pos());
1347 while (child && child !=
this) {
1348#if QT_CONFIG(menubar)
1349 if (QMenuBar *mb = qobject_cast<QMenuBar *>(child)) {
1350 if (mb->parentWidget() !=
this)
1355#if QT_CONFIG(dockwidget)
1356 if (QDockWidget *dw = qobject_cast<QDockWidget *>(child)) {
1357 if (dw->parentWidget() !=
this)
1360 && dw->widget()->geometry().contains(child->mapFrom(
this, event->pos()))) {
1367#if QT_CONFIG(toolbar)
1368 if (QToolBar *tb = qobject_cast<QToolBar *>(child)) {
1369 if (tb->parentWidget() !=
this)
1374 child = child->parentWidget();
1380 QMenu *popup = createPopupMenu();
1382 if (!popup->isEmpty()) {
1383 popup->setAttribute(Qt::WA_DeleteOnClose);
1384 popup->popup(event->globalPos());
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410QMenu *QMainWindow::createPopupMenu()
1413 QMenu *menu =
nullptr;
1414#if QT_CONFIG(dockwidget)
1415 QList<QDockWidget *> dockwidgets = findChildren<QDockWidget *>();
1416 if (dockwidgets.size()) {
1417 menu =
new QMenu(
this);
1418 for (
int i = 0; i < dockwidgets.size(); ++i) {
1419 QDockWidget *dockWidget = dockwidgets.at(i);
1421 if (dockWidget->parentWidget() ==
this) {
1422 if (d->layout->layoutState.dockAreaLayout.indexOf(dockWidget).isEmpty())
1424 }
else if (QDockWidgetGroupWindow *dwgw =
1425 qobject_cast<QDockWidgetGroupWindow *>(dockWidget->parentWidget())) {
1426 if (dwgw->parentWidget() !=
this)
1428 if (dwgw->layoutInfo()->indexOf(dockWidget).isEmpty())
1433 menu->addAction(dockwidgets.at(i)->toggleViewAction());
1435 menu->addSeparator();
1438#if QT_CONFIG(toolbar)
1439 QList<QToolBar *> toolbars = findChildren<QToolBar *>();
1440 if (toolbars.size()) {
1442 menu =
new QMenu(
this);
1443 for (
int i = 0; i < toolbars.size(); ++i) {
1444 QToolBar *toolBar = toolbars.at(i);
1445 if (toolBar->parentWidget() ==
this
1446 && (!d->layout->layoutState.toolBarAreaLayout.indexOf(toolBar).isEmpty())) {
1447 menu->addAction(toolbars.at(i)->toggleViewAction());
1459#include "moc_qmainwindow.cpp"
Qt::ToolButtonStyle toolButtonStyle
static QMainWindowLayout * mainWindowLayout(const QMainWindow *mainWindow)
QMainWindowLayout * qt_mainwindow_layout(const QMainWindow *mainWindow)