Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdockarealayout.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#include "QtWidgets/qapplication.h"
7#include "QtWidgets/qwidget.h"
8#if QT_CONFIG(tabbar)
9#include "QtWidgets/qtabbar.h"
10#endif
11#include "QtWidgets/qstyle.h"
12#include "QtWidgets/qapplication.h"
13#include "QtCore/qvariant.h"
15#include "qdockwidget.h"
16#include "qmainwindow.h"
18#include "qmenu_p.h"
19#include "qdockwidget_p.h"
20#include <private/qlayoutengine_p.h>
21
22#if QT_CONFIG(toolbar)
23#include "qtoolbar.h"
24#include "qtoolbarlayout_p.h"
25#endif
26
27#include <qpainter.h>
28#include <qstyleoption.h>
29#include <optional>
30
32
33Q_LOGGING_CATEGORY(lcQpaDockWidgets, "qt.widgets.dockwidgets");
34
35// qmainwindow.cpp
36extern QMainWindowLayout *qt_mainwindow_layout(const QMainWindow *window);
37
38enum class StateFlag
39{
40 Visible = 0x01,
41 Floating = 0x02,
43 Closed = 0x08,
44};
46
47using StateMarkers = QMainWindowLayoutState::StateMarkers;
48
49/******************************************************************************
50** QPlaceHolderItem
51*/
52
53QPlaceHolderItem::QPlaceHolderItem(QWidget *w)
54{
55 objectName = w->objectName();
56 hidden = w->isHidden();
57 window = w->isWindow();
58 if (window)
59 topLevelRect = w->geometry();
60}
61
62/******************************************************************************
63** QDockAreaLayoutItem
64*/
65
66QDockAreaLayoutItem::QDockAreaLayoutItem(QLayoutItem *_widgetItem)
67 : widgetItem(_widgetItem), subinfo(nullptr), placeHolderItem(nullptr), pos(0), size(-1), flags(NoFlags)
68{
69}
70
71QDockAreaLayoutItem::QDockAreaLayoutItem(QDockAreaLayoutInfo *_subinfo)
72 : widgetItem(nullptr), subinfo(_subinfo), placeHolderItem(nullptr), pos(0), size(-1), flags(NoFlags)
73{
74}
75
76QDockAreaLayoutItem::QDockAreaLayoutItem(QPlaceHolderItem *_placeHolderItem)
77 : widgetItem(nullptr), subinfo(nullptr), placeHolderItem(_placeHolderItem), pos(0), size(-1), flags(NoFlags)
78{
79}
80
81QDockAreaLayoutItem::QDockAreaLayoutItem(const QDockAreaLayoutItem &other)
82 : widgetItem(other.widgetItem), subinfo(nullptr), placeHolderItem(nullptr), pos(other.pos),
83 size(other.size), flags(other.flags)
84{
85 if (other.subinfo != nullptr)
86 subinfo = new QDockAreaLayoutInfo(*other.subinfo);
87 else if (other.placeHolderItem != nullptr)
88 placeHolderItem = new QPlaceHolderItem(*other.placeHolderItem);
89}
90
91QDockAreaLayoutItem::~QDockAreaLayoutItem()
92{
93 delete subinfo;
94 delete placeHolderItem;
95}
96
97bool QDockAreaLayoutItem::skip() const
98{
99 if (placeHolderItem != nullptr)
100 return true;
101
102 if (flags & GapItem)
103 return false;
104
105 if (widgetItem != nullptr)
106 return widgetItem->isEmpty();
107
108 if (subinfo != nullptr) {
109 for (int i = 0; i < subinfo->item_list.size(); ++i) {
110 if (!subinfo->item_list.at(i).skip())
111 return false;
112 }
113 }
114
115 return true;
116}
117
118QSize QDockAreaLayoutItem::minimumSize() const
119{
120 if (widgetItem)
121 return widgetItem->minimumSize().grownBy(widgetItem->widget()->contentsMargins());
122 if (subinfo != nullptr)
123 return subinfo->minimumSize();
124 return QSize(0, 0);
125}
126
127QSize QDockAreaLayoutItem::maximumSize() const
128{
129 if (widgetItem)
130 return widgetItem->maximumSize().grownBy(widgetItem->widget()->contentsMargins());
131 if (subinfo != nullptr)
132 return subinfo->maximumSize();
133 return QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
134}
135
136bool QDockAreaLayoutItem::hasFixedSize(Qt::Orientation o) const
137{
138 return perp(o, minimumSize()) == perp(o, maximumSize());
139}
140
141bool QDockAreaLayoutItem::expansive(Qt::Orientation o) const
142{
143 if ((flags & GapItem) || placeHolderItem != nullptr)
144 return false;
145 if (widgetItem != nullptr)
146 return ((widgetItem->expandingDirections() & o) == o);
147 if (subinfo != nullptr)
148 return subinfo->expansive(o);
149 return false;
150}
151
152QSize QDockAreaLayoutItem::sizeHint() const
153{
154 if (placeHolderItem != nullptr)
155 return QSize(0, 0);
156 if (widgetItem)
157 return widgetItem->sizeHint().grownBy(widgetItem->widget()->contentsMargins());
158 if (subinfo != nullptr)
159 return subinfo->sizeHint();
160 return QSize(-1, -1);
161}
162
163QDockAreaLayoutItem
164 &QDockAreaLayoutItem::operator = (const QDockAreaLayoutItem &other)
165{
166 if (this == &other)
167 return *this;
168
169 widgetItem = other.widgetItem;
170 delete subinfo;
171 if (other.subinfo == nullptr)
172 subinfo = nullptr;
173 else
174 subinfo = new QDockAreaLayoutInfo(*other.subinfo);
175
176 delete placeHolderItem;
177 if (other.placeHolderItem == nullptr)
178 placeHolderItem = nullptr;
179 else
180 placeHolderItem = new QPlaceHolderItem(*other.placeHolderItem);
181
182 pos = other.pos;
183 size = other.size;
184 flags = other.flags;
185
186 return *this;
187}
188
189#ifndef QT_NO_DEBUG_STREAM
190QDebug operator<<(QDebug dbg, const QDockAreaLayoutItem *item)
191{
192 QDebugStateSaver saver(dbg);
193 dbg.nospace();
194 return item ? dbg << *item : dbg << "QDockAreaLayoutItem(0x0)";
195}
196
197QDebug operator<<(QDebug dbg, const QDockAreaLayoutItem &item)
198{
199 QDebugStateSaver saver(dbg);
200 dbg.nospace();
201 dbg << "QDockAreaLayoutItem(" << static_cast<const void *>(&item) << "->";
202 if (item.widgetItem) {
203 QWidget *widget = item.widgetItem->widget();
204 if (auto *dockWidget = qobject_cast<QDockWidget *>(widget)) {
205 dbg << "widgetItem(" << dockWidget << ")";
206 } else if (auto *groupWindow = qobject_cast<QDockWidgetGroupWindow *>(widget)) {
207 dbg << "widgetItem(" << groupWindow << "->(" << groupWindow->dockWidgets() << "))";
208 } else {
209 dbg << "widgetItem(" << widget << ")";
210 }
211 } else if (item.subinfo) {
212 dbg << "subInfo(" << item.subinfo << "->(" << item.subinfo->item_list << ")";
213 } else if (item.placeHolderItem) {
214 dbg << "placeHolderItem(" << item.placeHolderItem
215 << "objectName:" << item.placeHolderItem->objectName << "; "
216 << "hidden:" << item.placeHolderItem->hidden << "; "
217 << "window:" << item.placeHolderItem->window << "; "
218 << "topLevelRect:" << item.placeHolderItem->topLevelRect << ")";
219 }
220 dbg << ")";
221 return dbg;
222}
223
224QDebug operator<<(QDebug dbg, StateFlags flags)
225{
226 QDebugStateSaver saver(dbg);
227 dbg.nospace().noquote();
228 QStringList f;
229 if (flags.testFlag(StateFlag::Visible))
230 f << QLatin1StringView("Visible");
231 if (flags.testFlag(StateFlag::Floating))
232 f << QLatin1StringView("Floating");
233 if (flags.testFlag(StateFlag::FloatingTabs))
234 f << QLatin1StringView("FloatingTabs");
235 if (flags.testFlag(StateFlag::Closed))
236 f << QLatin1StringView("Closed");
237 dbg << "StateFlags(" << f.join(u'|') << ")";
238 return dbg;
239}
240#endif // QT_NO_DEBUG_STREAM
241
242/******************************************************************************
243** QDockAreaLayoutInfo
244*/
245
246#if QT_CONFIG(tabbar)
247static quintptr tabId(const QDockAreaLayoutItem &item)
248{
249 if (item.widgetItem == nullptr)
250 return 0;
251 return reinterpret_cast<quintptr>(item.widgetItem->widget());
252}
253#endif
254
255static const int zero = 0;
256
257QDockAreaLayoutInfo::QDockAreaLayoutInfo()
258 : sep(&zero), dockPos(QInternal::LeftDock), o(Qt::Horizontal), mainWindow(nullptr)
259#if QT_CONFIG(tabbar)
260 , tabbed(false), tabBar(nullptr), tabBarShape(QTabBar::RoundedSouth)
261#endif
262{
263}
264
265QDockAreaLayoutInfo::QDockAreaLayoutInfo(const int *_sep, QInternal::DockPosition _dockPos,
266 Qt::Orientation _o, int tbshape,
267 QMainWindow *window)
268 : sep(_sep), dockPos(_dockPos), o(_o), mainWindow(window)
269#if QT_CONFIG(tabbar)
270 , tabbed(false), tabBar(nullptr), tabBarShape(static_cast<QTabBar::Shape>(tbshape))
271#endif
272{
273#if !QT_CONFIG(tabbar)
274 Q_UNUSED(tbshape);
275#endif
276}
277
278QSize QDockAreaLayoutInfo::size() const
279{
280 return isEmpty() ? QSize(0, 0) : rect.size();
281}
282
283void QDockAreaLayoutInfo::clear()
284{
285 item_list.clear();
286 rect = QRect();
287#if QT_CONFIG(tabbar)
288 tabbed = false;
289 tabBar = nullptr;
290#endif
291}
292
293bool QDockAreaLayoutInfo::isEmpty() const
294{
295 return next(-1) == -1;
296}
297
298bool QDockAreaLayoutInfo::onlyHasPlaceholders() const
299{
300 for (const QDockAreaLayoutItem &item : item_list) {
301 if (!item.placeHolderItem)
302 return false;
303 }
304
305 return true;
306}
307
308QSize QDockAreaLayoutInfo::minimumSize() const
309{
310 if (isEmpty())
311 return QSize(0, 0);
312
313 int a = 0, b = 0;
314 bool first = true;
315 for (int i = 0; i < item_list.size(); ++i) {
316 const QDockAreaLayoutItem &item = item_list.at(i);
317 if (item.skip())
318 continue;
319
320 QSize min_size = item.minimumSize();
321#if QT_CONFIG(tabbar)
322 if (tabbed) {
323 a = qMax(a, pick(o, min_size));
324 } else
325#endif
326 {
327 if (!first)
328 a += *sep;
329 a += pick(o, min_size);
330 }
331 b = qMax(b, perp(o, min_size));
332
333 first = false;
334 }
335
336 QSize result;
337 rpick(o, result) = a;
338 rperp(o, result) = b;
339
340#if QT_CONFIG(tabbar)
341 QSize tbm = tabBarMinimumSize();
342 if (!tbm.isNull()) {
343 switch (tabBarShape) {
344 case QTabBar::RoundedNorth:
345 case QTabBar::RoundedSouth:
346 case QTabBar::TriangularNorth:
347 case QTabBar::TriangularSouth:
348 result.rheight() += tbm.height();
349 result.rwidth() = qMax(tbm.width(), result.width());
350 break;
351 case QTabBar::RoundedEast:
352 case QTabBar::RoundedWest:
353 case QTabBar::TriangularEast:
354 case QTabBar::TriangularWest:
355 result.rheight() = qMax(tbm.height(), result.height());
356 result.rwidth() += tbm.width();
357 break;
358 default:
359 break;
360 }
361 }
362#endif // QT_CONFIG(tabbar)
363
364 return result;
365}
366
367QSize QDockAreaLayoutInfo::maximumSize() const
368{
369 if (isEmpty())
370 return QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
371
372 int a = 0, b = QWIDGETSIZE_MAX;
373#if QT_CONFIG(tabbar)
374 if (tabbed)
375 a = QWIDGETSIZE_MAX;
376#endif
377
378 int min_perp = 0;
379
380 bool first = true;
381 for (int i = 0; i < item_list.size(); ++i) {
382 const QDockAreaLayoutItem &item = item_list.at(i);
383 if (item.skip())
384 continue;
385
386 QSize max_size = item.maximumSize();
387 min_perp = qMax(min_perp, perp(o, item.minimumSize()));
388
389#if QT_CONFIG(tabbar)
390 if (tabbed) {
391 a = qMin(a, pick(o, max_size));
392 } else
393#endif
394 {
395 if (!first)
396 a += *sep;
397 a += pick(o, max_size);
398 }
399 b = qMin(b, perp(o, max_size));
400
401 a = qMin(a, int(QWIDGETSIZE_MAX));
402 b = qMin(b, int(QWIDGETSIZE_MAX));
403
404 first = false;
405 }
406
407 b = qMax(b, min_perp);
408
409 QSize result;
410 rpick(o, result) = a;
411 rperp(o, result) = b;
412
413#if QT_CONFIG(tabbar)
414 QSize tbh = tabBarSizeHint();
415 if (!tbh.isNull()) {
416 switch (tabBarShape) {
417 case QTabBar::RoundedNorth:
418 case QTabBar::RoundedSouth:
419 result.rheight() += tbh.height();
420 break;
421 case QTabBar::RoundedEast:
422 case QTabBar::RoundedWest:
423 result.rwidth() += tbh.width();
424 break;
425 default:
426 break;
427 }
428 }
429#endif // QT_CONFIG(tabbar)
430
431 return result;
432}
433
434QSize QDockAreaLayoutInfo::sizeHint() const
435{
436 if (isEmpty())
437 return QSize(0, 0);
438
439 int a = 0, b = 0;
440 int min_perp = 0;
441 int max_perp = QWIDGETSIZE_MAX;
442 const QDockAreaLayoutItem *previous = nullptr;
443 for (int i = 0; i < item_list.size(); ++i) {
444 const QDockAreaLayoutItem &item = item_list.at(i);
445 if (item.skip())
446 continue;
447
448 bool gap = item.flags & QDockAreaLayoutItem::GapItem;
449
450 QSize size_hint = item.sizeHint();
451 min_perp = qMax(min_perp, perp(o, item.minimumSize()));
452 max_perp = qMin(max_perp, perp(o, item.maximumSize()));
453
454#if QT_CONFIG(tabbar)
455 if (tabbed) {
456 a = qMax(a, gap ? item.size : pick(o, size_hint));
457 } else
458#endif
459 {
460 if (previous && !gap && !(previous->flags & QDockAreaLayoutItem::GapItem)
461 && !previous->hasFixedSize(o)) {
462 a += *sep;
463 }
464 a += gap ? item.size : pick(o, size_hint);
465 }
466 b = qMax(b, perp(o, size_hint));
467
468 previous = &item;
469 }
470
471 max_perp = qMax(max_perp, min_perp);
472 b = qMax(b, min_perp);
473 b = qMin(b, max_perp);
474
475 QSize result;
476 rpick(o, result) = a;
477 rperp(o, result) = b;
478
479#if QT_CONFIG(tabbar)
480 if (tabbed) {
481 QSize tbh = tabBarSizeHint();
482 switch (tabBarShape) {
483 case QTabBar::RoundedNorth:
484 case QTabBar::RoundedSouth:
485 case QTabBar::TriangularNorth:
486 case QTabBar::TriangularSouth:
487 result.rheight() += tbh.height();
488 result.rwidth() = qMax(tbh.width(), result.width());
489 break;
490 case QTabBar::RoundedEast:
491 case QTabBar::RoundedWest:
492 case QTabBar::TriangularEast:
493 case QTabBar::TriangularWest:
494 result.rheight() = qMax(tbh.height(), result.height());
495 result.rwidth() += tbh.width();
496 break;
497 default:
498 break;
499 }
500 }
501#endif // QT_CONFIG(tabbar)
502
503 return result;
504}
505
506bool QDockAreaLayoutInfo::expansive(Qt::Orientation o) const
507{
508 for (int i = 0; i < item_list.size(); ++i) {
509 if (item_list.at(i).expansive(o))
510 return true;
511 }
512 return false;
513}
514
515/* QDockAreaLayoutInfo::maximumSize() doesn't return the real max size. For example,
516 if the layout is empty, it returns QWIDGETSIZE_MAX. This is so that empty dock areas
517 don't constrain the size of the QMainWindow, but sometimes we really need to know the
518 maximum size. Also, these functions take into account widgets that want to keep their
519 size (f.ex. when they are hidden and then shown, they should not change size).
520*/
521
522static int realMinSize(const QDockAreaLayoutInfo &info)
523{
524 int result = 0;
525 bool first = true;
526 for (int i = 0; i < info.item_list.size(); ++i) {
527 const QDockAreaLayoutItem &item = info.item_list.at(i);
528 if (item.skip())
529 continue;
530
531 int min = 0;
532 if ((item.flags & QDockAreaLayoutItem::KeepSize) && item.size != -1)
533 min = item.size;
534 else
535 min = pick(info.o, item.minimumSize());
536
537 if (!first)
538 result += *info.sep;
539 result += min;
540
541 first = false;
542 }
543
544 return result;
545}
546
547static int realMaxSize(const QDockAreaLayoutInfo &info)
548{
549 int result = 0;
550 bool first = true;
551 for (int i = 0; i < info.item_list.size(); ++i) {
552 const QDockAreaLayoutItem &item = info.item_list.at(i);
553 if (item.skip())
554 continue;
555
556 int max = 0;
557 if ((item.flags & QDockAreaLayoutItem::KeepSize) && item.size != -1)
558 max = item.size;
559 else
560 max = pick(info.o, item.maximumSize());
561
562 if (!first)
563 result += *info.sep;
564 result += max;
565
566 if (result >= QWIDGETSIZE_MAX)
567 return QWIDGETSIZE_MAX;
568
569 first = false;
570 }
571
572 return result;
573}
574
575void QDockAreaLayoutInfo::fitItems()
576{
577#if QT_CONFIG(tabbar)
578 if (tabbed) {
579 return;
580 }
581#endif
582
583 QList<QLayoutStruct> layout_struct_list(item_list.size() * 2);
584 int j = 0;
585
586 int size = pick(o, rect.size());
587 int min_size = realMinSize(*this);
588 int max_size = realMaxSize(*this);
589 int last_index = -1;
590
591 const QDockAreaLayoutItem *previous = nullptr;
592 for (int i = 0; i < item_list.size(); ++i) {
593 QDockAreaLayoutItem &item = item_list[i];
594 if (item.skip())
595 continue;
596
597 bool gap = item.flags & QDockAreaLayoutItem::GapItem;
598 if (previous && !gap) {
599 if (!(previous->flags & QDockAreaLayoutItem::GapItem)) {
600 QLayoutStruct &ls = layout_struct_list[j++];
601 ls.init();
602 ls.minimumSize = ls.maximumSize = ls.sizeHint = previous->hasFixedSize(o) ? 0 : *sep;
603 ls.empty = false;
604 }
605 }
606
607 if (item.flags & QDockAreaLayoutItem::KeepSize) {
608 // Check if the item can keep its size, without violating size constraints
609 // of other items.
610
611 if (size < min_size) {
612 // There is too little space to keep this widget's size
613 item.flags &= ~QDockAreaLayoutItem::KeepSize;
614 min_size -= item.size;
615 min_size += pick(o, item.minimumSize());
616 min_size = qMax(0, min_size);
617 } else if (size > max_size) {
618 // There is too much space to keep this widget's size
619 item.flags &= ~QDockAreaLayoutItem::KeepSize;
620 max_size -= item.size;
621 max_size += pick(o, item.maximumSize());
622 max_size = qMin<int>(QWIDGETSIZE_MAX, max_size);
623 }
624 }
625
626 last_index = j;
627 QLayoutStruct &ls = layout_struct_list[j++];
628 ls.init();
629 ls.empty = false;
630 if (item.flags & QDockAreaLayoutItem::KeepSize) {
631 ls.minimumSize = ls.maximumSize = ls.sizeHint = item.size;
632 ls.expansive = false;
633 ls.stretch = 0;
634 } else {
635 ls.maximumSize = pick(o, item.maximumSize());
636 ls.expansive = item.expansive(o);
637 ls.minimumSize = pick(o, item.minimumSize());
638 ls.sizeHint = item.size == -1 ? pick(o, item.sizeHint()) : item.size;
639 ls.stretch = ls.expansive ? ls.sizeHint : 0;
640 }
641
642 item.flags &= ~QDockAreaLayoutItem::KeepSize;
643 previous = &item;
644 }
645 layout_struct_list.resize(j);
646
647 // If there is more space than the widgets can take (due to maximum size constraints),
648 // we detect it here and stretch the last widget to take up the rest of the space.
649 if (size > max_size && last_index != -1) {
650 layout_struct_list[last_index].maximumSize = QWIDGETSIZE_MAX;
651 layout_struct_list[last_index].expansive = true;
652 }
653
654 qGeomCalc(layout_struct_list, 0, j, pick(o, rect.topLeft()), size, 0);
655
656 j = 0;
657 bool prev_gap = false;
658 bool first = true;
659 for (int i = 0; i < item_list.size(); ++i) {
660 QDockAreaLayoutItem &item = item_list[i];
661 if (item.skip())
662 continue;
663
664 bool gap = item.flags & QDockAreaLayoutItem::GapItem;
665 if (!first && !gap && !prev_gap)
666 ++j;
667
668 const QLayoutStruct &ls = layout_struct_list.at(j++);
669 item.size = ls.size;
670 item.pos = ls.pos;
671
672 if (item.subinfo != nullptr) {
673 item.subinfo->rect = itemRect(i);
674 item.subinfo->fitItems();
675 }
676
677 prev_gap = gap;
678 first = false;
679 }
680}
681
683dockPosHelper(const QRect &rect, const QPoint &_pos,
684 Qt::Orientation o, bool nestingEnabled,
685 QDockAreaLayoutInfo::TabMode tabMode)
686{
687 if (tabMode == QDockAreaLayoutInfo::ForceTabs)
688 return std::nullopt;
689
690 QPoint pos = _pos - rect.topLeft();
691
692 int x = pos.x();
693 int y = pos.y();
694 int w = rect.width();
695 int h = rect.height();
696
697 if (tabMode != QDockAreaLayoutInfo::NoTabs) {
698 // is it in the center?
699 if (nestingEnabled) {
700 /* 2/3
701 +--------------+
702 | |
703 | CCCCCCCC |
704 2/3 | CCCCCCCC |
705 | CCCCCCCC |
706 | |
707 +--------------+ */
708
709 QRect center(w/6, h/6, 2*w/3, 2*h/3);
710 if (center.contains(pos))
711 return std::nullopt;
712 } else if (o == Qt::Horizontal) {
713 /* 2/3
714 +--------------+
715 | CCCCCCCC |
716 | CCCCCCCC |
717 | CCCCCCCC |
718 | CCCCCCCC |
719 | CCCCCCCC |
720 +--------------+ */
721
722 if (x > w/6 && x < w*5/6)
723 return std::nullopt;
724 } else {
725 /*
726 +--------------+
727 | |
728 2/3 |CCCCCCCCCCCCCC|
729 |CCCCCCCCCCCCCC|
730 | |
731 +--------------+ */
732 if (y > h/6 && y < 5*h/6)
733 return std::nullopt;
734 }
735 }
736
737 // not in the center. which edge?
738 if (nestingEnabled) {
739 if (o == Qt::Horizontal) {
740 /* 1/3 1/3 1/3
741 +------------+ (we've already ruled out the center)
742 |LLLLTTTTRRRR|
743 |LLLLTTTTRRRR|
744 |LLLLBBBBRRRR|
745 |LLLLBBBBRRRR|
746 +------------+ */
747
748 if (x < w/3)
749 return QInternal::LeftDock;
750 if (x > 2*w/3)
751 return QInternal::RightDock;
752 if (y < h/2)
753 return QInternal::TopDock;
754 return QInternal::BottomDock;
755 } else {
756 /* +------------+ (we've already ruled out the center)
757 1/3 |TTTTTTTTTTTT|
758 |LLLLLLRRRRRR|
759 1/3 |LLLLLLRRRRRR|
760 1/3 |BBBBBBBBBBBB|
761 +------------+ */
762
763 if (y < h/3)
764 return QInternal::TopDock;
765 if (y > 2*h/3)
766 return QInternal::BottomDock;
767 if (x < w/2)
768 return QInternal::LeftDock;
769 return QInternal::RightDock;
770 }
771 } else {
772 if (o == Qt::Horizontal) {
773 return x < w/2
774 ? QInternal::LeftDock
775 : QInternal::RightDock;
776 } else {
777 return y < h/2
778 ? QInternal::TopDock
779 : QInternal::BottomDock;
780 }
781 }
782}
783
784QList<int> QDockAreaLayoutInfo::gapIndex(const QPoint& _pos,
785 bool nestingEnabled, TabMode tabMode) const
786{
787 QList<int> result;
788 QRect item_rect;
789 int item_index = 0;
790
791#if QT_CONFIG(tabbar)
792 if (tabbed) {
793 item_rect = tabContentRect();
794 } else
795#endif
796 {
797 int pos = pick(o, _pos);
798
799 int last = -1;
800 for (int i = 0; i < item_list.size(); ++i) {
801 const QDockAreaLayoutItem &item = item_list.at(i);
802 if (item.skip())
803 continue;
804
805 last = i;
806
807 if (item.pos + item.size < pos)
808 continue;
809
810 if (item.subinfo != nullptr
811#if QT_CONFIG(tabbar)
812 && !item.subinfo->tabbed
813#endif
814 ) {
815 result = item.subinfo->gapIndex(_pos, nestingEnabled,
816 tabMode);
817 result.prepend(i);
818 return result;
819 }
820
821 item_rect = itemRect(i);
822 item_index = i;
823 break;
824 }
825
826 if (item_rect.isNull()) {
827 result.append(last + 1);
828 return result;
829 }
830 }
831
832 Q_ASSERT(!item_rect.isNull());
833
834 const auto dock_pos
835 = dockPosHelper(item_rect, _pos, o, nestingEnabled, tabMode);
836
837 if (dock_pos) {
838 switch (*dock_pos) {
839 case QInternal::LeftDock:
840 if (o == Qt::Horizontal)
841 result << item_index;
842 else
843 result << item_index << 0; // this subinfo doesn't exist yet, but insertGap()
844 // handles this by inserting it
845 break;
846 case QInternal::RightDock:
847 if (o == Qt::Horizontal)
848 result << item_index + 1;
849 else
850 result << item_index << 1;
851 break;
852 case QInternal::TopDock:
853 if (o == Qt::Horizontal)
854 result << item_index << 0;
855 else
856 result << item_index;
857 break;
858 case QInternal::BottomDock:
859 if (o == Qt::Horizontal)
860 result << item_index << 1;
861 else
862 result << item_index + 1;
863 break;
864 case QInternal::DockCount:
865 Q_UNREACHABLE();
866 break;
867 }
868 } else {
869 result << (-item_index - 1) << 0; // negative item_index means "on top of"
870 // -item_index - 1, insertGap()
871 // will insert a tabbed subinfo
872 }
873
874 return result;
875}
876
877static inline int shrink(QLayoutStruct &ls, int delta)
878{
879 if (ls.empty)
880 return 0;
881 int old_size = ls.size;
882 ls.size = qMax(ls.size - delta, ls.minimumSize);
883 return old_size - ls.size;
884}
885
886static inline int grow(QLayoutStruct &ls, int delta)
887{
888 if (ls.empty)
889 return 0;
890 int old_size = ls.size;
891 ls.size = qMin(ls.size + delta, ls.maximumSize);
892 return ls.size - old_size;
893}
894
895static int separatorMoveHelper(QList<QLayoutStruct> &list, int index, int delta, int sep)
896{
897 // adjust sizes
898 int pos = -1;
899 for (int i = 0; i < list.size(); ++i) {
900 const QLayoutStruct &ls = list.at(i);
901 if (!ls.empty) {
902 pos = ls.pos;
903 break;
904 }
905 }
906 if (pos == -1)
907 return 0;
908
909 if (delta > 0) {
910 int growlimit = 0;
911 for (int i = 0; i<=index; ++i) {
912 const QLayoutStruct &ls = list.at(i);
913 if (ls.empty)
914 continue;
915 if (ls.maximumSize == QLAYOUTSIZE_MAX) {
916 growlimit = QLAYOUTSIZE_MAX;
917 break;
918 }
919 growlimit += ls.maximumSize - ls.size;
920 }
921 if (delta > growlimit)
922 delta = growlimit;
923
924 int d = 0;
925 for (int i = index + 1; d < delta && i < list.size(); ++i)
926 d += shrink(list[i], delta - d);
927 delta = d;
928 d = 0;
929 for (int i = index; d < delta && i >= 0; --i)
930 d += grow(list[i], delta - d);
931 } else if (delta < 0) {
932 int growlimit = 0;
933 for (int i = index + 1; i < list.size(); ++i) {
934 const QLayoutStruct &ls = list.at(i);
935 if (ls.empty)
936 continue;
937 if (ls.maximumSize == QLAYOUTSIZE_MAX) {
938 growlimit = QLAYOUTSIZE_MAX;
939 break;
940 }
941 growlimit += ls.maximumSize - ls.size;
942 }
943 if (-delta > growlimit)
944 delta = -growlimit;
945
946 int d = 0;
947 for (int i = index; d < -delta && i >= 0; --i)
948 d += shrink(list[i], -delta - d);
949 delta = -d;
950 d = 0;
951 for (int i = index + 1; d < -delta && i < list.size(); ++i)
952 d += grow(list[i], -delta - d);
953 }
954
955 // adjust positions
956 bool first = true;
957 for (int i = 0; i < list.size(); ++i) {
958 QLayoutStruct &ls = list[i];
959 if (ls.empty) {
960 ls.pos = pos + (first ? 0 : sep);
961 continue;
962 }
963 if (!first)
964 pos += sep;
965 ls.pos = pos;
966 pos += ls.size;
967 first = false;
968 }
969
970 return delta;
971}
972
973int QDockAreaLayoutInfo::separatorMove(int index, int delta)
974{
975#if QT_CONFIG(tabbar)
976 Q_ASSERT(!tabbed);
977#endif
978
979 QList<QLayoutStruct> list(item_list.size());
980 for (int i = 0; i < list.size(); ++i) {
981 const QDockAreaLayoutItem &item = item_list.at(i);
982 QLayoutStruct &ls = list[i];
983 Q_ASSERT(!(item.flags & QDockAreaLayoutItem::GapItem));
984 if (item.skip()) {
985 ls.empty = true;
986 } else {
987 const int separatorSpace = item.hasFixedSize(o) ? 0 : *sep;
988 ls.empty = false;
989 ls.pos = item.pos;
990 ls.size = item.size + separatorSpace;
991 ls.minimumSize = pick(o, item.minimumSize()) + separatorSpace;
992 ls.maximumSize = pick(o, item.maximumSize()) + separatorSpace;
993
994 }
995 }
996
997 //the separator space has been added to the size, so we pass 0 as a parameter
998 delta = separatorMoveHelper(list, index, delta, 0 /*separator*/);
999
1000 for (int i = 0; i < list.size(); ++i) {
1001 QDockAreaLayoutItem &item = item_list[i];
1002 if (item.skip())
1003 continue;
1004 QLayoutStruct &ls = list[i];
1005 const int separatorSpace = item.hasFixedSize(o) ? 0 : *sep;
1006 item.size = ls.size - separatorSpace;
1007 item.pos = ls.pos;
1008 if (item.subinfo != nullptr) {
1009 item.subinfo->rect = itemRect(i);
1010 item.subinfo->fitItems();
1011 }
1012 }
1013
1014 return delta;
1015}
1016
1017void QDockAreaLayoutInfo::unnest(int index)
1018{
1019 QDockAreaLayoutItem &item = item_list[index];
1020 if (item.subinfo == nullptr)
1021 return;
1022 if (item.subinfo->item_list.size() > 1)
1023 return;
1024
1025 if (item.subinfo->item_list.size() == 0) {
1026 item_list.removeAt(index);
1027 } else if (item.subinfo->item_list.size() == 1) {
1028 QDockAreaLayoutItem &child = item.subinfo->item_list.first();
1029 if (child.widgetItem != nullptr) {
1030 item.widgetItem = child.widgetItem;
1031 delete item.subinfo;
1032 item.subinfo = nullptr;
1033 } else if (child.subinfo != nullptr) {
1034 QDockAreaLayoutInfo *tmp = item.subinfo;
1035 item.subinfo = child.subinfo;
1036 child.subinfo = nullptr;
1037 tmp->item_list.clear();
1038 delete tmp;
1039 }
1040 }
1041}
1042
1043void QDockAreaLayoutInfo::remove(const QList<int> &path)
1044{
1045 Q_ASSERT(!path.isEmpty());
1046
1047 if (path.size() > 1) {
1048 const int index = path.first();
1049 QDockAreaLayoutItem &item = item_list[index];
1050 Q_ASSERT(item.subinfo != nullptr);
1051 item.subinfo->remove(path.mid(1));
1052 unnest(index);
1053 } else {
1054 int index = path.first();
1055 item_list.removeAt(index);
1056 }
1057}
1058
1059void QDockAreaLayoutInfo::remove(QWidget *widget)
1060{
1061 const QList<int> path = indexOf(widget);
1062 if (path.isEmpty())
1063 return;
1064 remove(path);
1065}
1066
1067QLayoutItem *QDockAreaLayoutInfo::plug(const QList<int> &path)
1068{
1069 Q_ASSERT(!path.isEmpty());
1070
1071 int index = path.first();
1072 if (index < 0)
1073 index = -index - 1;
1074
1075 if (path.size() > 1) {
1076 QDockAreaLayoutItem &item = item_list[index];
1077 Q_ASSERT(item.subinfo != nullptr);
1078 return item.subinfo->plug(path.mid(1));
1079 }
1080
1081 QDockAreaLayoutItem &item = item_list[index];
1082
1083 Q_ASSERT(item.widgetItem != nullptr);
1084 Q_ASSERT(item.flags & QDockAreaLayoutItem::GapItem);
1085 item.flags &= ~QDockAreaLayoutItem::GapItem;
1086 return item.widgetItem;
1087}
1088
1089QLayoutItem *QDockAreaLayoutInfo::unplug(const QList<int> &path)
1090{
1091 Q_ASSERT(!path.isEmpty());
1092
1093 const int index = path.first();
1094 if (path.size() > 1) {
1095 QDockAreaLayoutItem &item = item_list[index];
1096 Q_ASSERT(item.subinfo != nullptr);
1097 return item.subinfo->unplug(path.mid(1));
1098 }
1099
1100 QDockAreaLayoutItem &item = item_list[index];
1101 int prev = this->prev(index);
1102 int next = this->next(index);
1103
1104 Q_ASSERT(!(item.flags & QDockAreaLayoutItem::GapItem));
1105 item.flags |= QDockAreaLayoutItem::GapItem;
1106
1107#if QT_CONFIG(tabbar)
1108 if (tabbed) {
1109 } else
1110#endif
1111 {
1112 if (prev != -1 && !(item_list.at(prev).flags & QDockAreaLayoutItem::GapItem)) {
1113 item.pos -= *sep;
1114 item.size += *sep;
1115 }
1116 if (next != -1 && !(item_list.at(next).flags & QDockAreaLayoutItem::GapItem))
1117 item.size += *sep;
1118 }
1119
1120 return item.widgetItem;
1121}
1122
1123#if QT_CONFIG(tabbar)
1124
1125quintptr QDockAreaLayoutInfo::currentTabId() const
1126{
1127 if (!tabbed || tabBar == nullptr)
1128 return 0;
1129
1130 int index = tabBar->currentIndex();
1131 if (index == -1)
1132 return 0;
1133
1134 return qvariant_cast<quintptr>(tabBar->tabData(index));
1135}
1136
1137void QDockAreaLayoutInfo::setCurrentTab(QWidget *widget)
1138{
1139 setCurrentTabId(reinterpret_cast<quintptr>(widget));
1140}
1141
1142void QDockAreaLayoutInfo::setCurrentTabId(quintptr id)
1143{
1144 if (!tabbed || tabBar == nullptr)
1145 return;
1146
1147 for (int i = 0; i < tabBar->count(); ++i) {
1148 if (qvariant_cast<quintptr>(tabBar->tabData(i)) == id) {
1149 tabBar->setCurrentIndex(i);
1150 return;
1151 }
1152 }
1153}
1154
1155#endif // QT_CONFIG(tabbar)
1156
1158{
1159 int titleHeight = 0;
1160
1161 QDockWidgetLayout *layout
1162 = qobject_cast<QDockWidgetLayout*>(widget->layout());
1163 if (layout && layout->nativeWindowDeco())
1164 titleHeight = layout->titleHeight();
1165
1166 QRect result = widget->geometry();
1167 result.adjust(0, -titleHeight, 0, 0);
1168 return result;
1169}
1170
1171bool QDockAreaLayoutInfo::hasGapItem(const QList<int> &path) const
1172{
1173 // empty path has no gap item
1174 if (path.isEmpty())
1175 return false;
1176
1177 // Index -1 isn't a gap
1178 // Index out of range points at a position to be created. That isn't a gap either.
1179 const int index = path.constFirst();
1180 if (index < 0 || index >= item_list.count())
1181 return false;
1182
1183 return item_list[index].flags & QDockAreaLayoutItem::GapItem;
1184}
1185
1186bool QDockAreaLayoutInfo::insertGap(const QList<int> &path, QLayoutItem *dockWidgetItem)
1187{
1188 Q_ASSERT(!path.isEmpty());
1189
1190 bool insert_tabbed = false;
1191 int index = path.first();
1192 if (index < 0) {
1193 insert_tabbed = true;
1194 index = -index - 1;
1195 }
1196
1197 if (path.size() > 1) {
1198 QDockAreaLayoutItem &item = item_list[index];
1199
1200 if (item.subinfo == nullptr
1201#if QT_CONFIG(tabbar)
1202 || (item.subinfo->tabbed && !insert_tabbed)
1203#endif
1204 ) {
1205
1206 // this is not yet a nested layout - make it
1207
1208 QDockAreaLayoutInfo *subinfo = item.subinfo;
1209 QLayoutItem *widgetItem = item.widgetItem;
1210 QPlaceHolderItem *placeHolderItem = item.placeHolderItem;
1211 QRect r = subinfo == nullptr ? widgetItem ? dockedGeometry(widgetItem->widget()) : placeHolderItem->topLevelRect : subinfo->rect;
1212
1213 Qt::Orientation opposite = o == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal;
1214#if !QT_CONFIG(tabbar)
1215 const int tabBarShape = 0;
1216#endif
1217 QDockAreaLayoutInfo *new_info
1218 = new QDockAreaLayoutInfo(sep, dockPos, opposite, tabBarShape, mainWindow);
1219
1220 //item become a new top-level
1221 item.subinfo = new_info;
1222 item.widgetItem = nullptr;
1223 item.placeHolderItem = nullptr;
1224
1225 QDockAreaLayoutItem new_item
1226 = widgetItem == nullptr
1227 ? QDockAreaLayoutItem(subinfo)
1228 : widgetItem ? QDockAreaLayoutItem(widgetItem) : QDockAreaLayoutItem(placeHolderItem);
1229 new_item.size = pick(opposite, r.size());
1230 new_item.pos = pick(opposite, r.topLeft());
1231 new_info->item_list.append(new_item);
1232#if QT_CONFIG(tabbar)
1233 if (insert_tabbed) {
1234 new_info->tabbed = true;
1235 }
1236#endif
1237 }
1238
1239 return item.subinfo->insertGap(path.mid(1), dockWidgetItem);
1240 }
1241
1242 // create the gap item
1243 QDockAreaLayoutItem gap_item;
1244 gap_item.flags |= QDockAreaLayoutItem::GapItem;
1245 gap_item.widgetItem = dockWidgetItem; // so minimumSize(), maximumSize() and
1246 // sizeHint() will work
1247#if QT_CONFIG(tabbar)
1248 if (!tabbed)
1249#endif
1250 {
1251 int prev = this->prev(index);
1252 int next = this->next(index - 1);
1253 // find out how much space we have in the layout
1254 int space = 0;
1255 if (isEmpty()) {
1256 // I am an empty dock area, therefore I am a top-level dock area.
1257 switch (dockPos) {
1258 case QInternal::LeftDock:
1259 case QInternal::RightDock:
1260 if (o == Qt::Vertical) {
1261 // the "size" is the height of the dock area (remember we are empty)
1262 space = pick(Qt::Vertical, rect.size());
1263 } else {
1264 space = pick(Qt::Horizontal, dockWidgetItem->widget()->size());
1265 }
1266 break;
1267 case QInternal::TopDock:
1268 case QInternal::BottomDock:
1269 default:
1270 if (o == Qt::Horizontal) {
1271 // the "size" is width of the dock area
1272 space = pick(Qt::Horizontal, rect.size());
1273 } else {
1274 space = pick(Qt::Vertical, dockWidgetItem->widget()->size());
1275 }
1276 break;
1277 }
1278 } else {
1279 for (int i = 0; i < item_list.size(); ++i) {
1280 const QDockAreaLayoutItem &item = item_list.at(i);
1281 if (item.skip())
1282 continue;
1283 Q_ASSERT_X(!(item.flags & QDockAreaLayoutItem::GapItem),
1284 "QDockAreaLayoutInfo::insertGap", "inserting two gaps after each other");
1285 space += item.size - pick(o, item.minimumSize());
1286 qCDebug(lcQpaDockWidgets) << "Item space:" << item.flags << this;
1287 }
1288 }
1289
1290 // find the actual size of the gap
1291 int gap_size = 0;
1292 int sep_size = 0;
1293 if (isEmpty()) {
1294 gap_size = space;
1295 sep_size = 0;
1296 } else {
1297 QRect r = dockedGeometry(dockWidgetItem->widget());
1298 gap_size = pick(o, r.size());
1299 if (prev != -1 && !(item_list.at(prev).flags & QDockAreaLayoutItem::GapItem))
1300 sep_size += *sep;
1301 if (next != -1 && !(item_list.at(next).flags & QDockAreaLayoutItem::GapItem))
1302 sep_size += *sep;
1303 }
1304 if (gap_size + sep_size > space)
1305 gap_size = pick(o, gap_item.minimumSize());
1306 gap_item.size = gap_size + sep_size;
1307 }
1308
1309 // finally, insert the gap
1310 item_list.insert(index, gap_item);
1311 qCDebug(lcQpaDockWidgets) << "Insert gap after:" << index << this;
1312
1313 return true;
1314}
1315
1316QDockAreaLayoutInfo *QDockAreaLayoutInfo::info(QWidget *widget)
1317{
1318 for (int i = 0; i < item_list.size(); ++i) {
1319 const QDockAreaLayoutItem &item = item_list.at(i);
1320 if (item.skip())
1321 continue;
1322
1323#if QT_CONFIG(tabbar)
1324 if (tabbed && widget == tabBar)
1325 return this;
1326#endif
1327
1328 if (item.widgetItem != nullptr && item.widgetItem->widget() == widget)
1329 return this;
1330
1331 if (item.subinfo != nullptr) {
1332 if (QDockAreaLayoutInfo *result = item.subinfo->info(widget))
1333 return result;
1334 }
1335 }
1336
1337 return nullptr;
1338}
1339
1340QDockAreaLayoutInfo *QDockAreaLayoutInfo::info(const QList<int> &path)
1341{
1342 int index = path.first();
1343 if (index < 0)
1344 index = -index - 1;
1345 if (index >= item_list.size())
1346 return this;
1347 if (path.size() == 1 || item_list[index].subinfo == nullptr)
1348 return this;
1349 return item_list[index].subinfo->info(path.mid(1));
1350}
1351
1352QRect QDockAreaLayoutInfo::itemRect(int index, bool isGap) const
1353{
1354 const QDockAreaLayoutItem &item = item_list.at(index);
1355
1356 if (item.skip())
1357 return QRect();
1358
1359 if (isGap && !(item.flags & QDockAreaLayoutItem::GapItem))
1360 return QRect();
1361
1362 QRect result;
1363
1364#if QT_CONFIG(tabbar)
1365 if (tabbed) {
1366 if (isGap || tabId(item) == currentTabId())
1367 result = tabContentRect();
1368 } else
1369#endif
1370 {
1371 int pos = item.pos;
1372 int size = item.size;
1373
1374 if (isGap) {
1375 int prev = this->prev(index);
1376 int next = this->next(index);
1377 if (prev != -1 && !(item_list.at(prev).flags & QDockAreaLayoutItem::GapItem)) {
1378 pos += *sep;
1379 size -= *sep;
1380 }
1381 if (next != -1 && !(item_list.at(next).flags & QDockAreaLayoutItem::GapItem))
1382 size -= *sep;
1383 }
1384
1385 QPoint p;
1386 rpick(o, p) = pos;
1387 rperp(o, p) = perp(o, rect.topLeft());
1388 QSize s;
1389 rpick(o, s) = size;
1390 rperp(o, s) = perp(o, rect.size());
1391 result = QRect(p, s);
1392 }
1393
1394 return result;
1395}
1396
1397QRect QDockAreaLayoutInfo::itemRect(const QList<int> &path) const
1398{
1399 Q_ASSERT(!path.isEmpty());
1400
1401 const int index = path.first();
1402 if (path.size() > 1) {
1403 const QDockAreaLayoutItem &item = item_list.at(index);
1404 Q_ASSERT(item.subinfo != nullptr);
1405 return item.subinfo->itemRect(path.mid(1));
1406 }
1407
1408 return itemRect(index);
1409}
1410
1411QRect QDockAreaLayoutInfo::separatorRect() const
1412{
1413 if (isEmpty())
1414 return QRect();
1415 const QRect &r = rect;
1416 switch (dockPos) {
1417 case QInternal::LeftDock:
1418 return QRect(r.right() + 1, r.top(), *sep, r.height());
1419 case QInternal::RightDock:
1420 return QRect(r.left() - *sep, r.top(), *sep, r.height());
1421 case QInternal::TopDock:
1422 return QRect(r.left(), r.bottom() + 1, r.width(), *sep);
1423 case QInternal::BottomDock:
1424 return QRect(r.left(), r.top() - *sep, r.width(), *sep);
1425 case QInternal::DockCount:
1426 Q_UNREACHABLE();
1427 break;
1428 }
1429 return QRect();
1430}
1431
1432QRect QDockAreaLayoutInfo::separatorRect(int index) const
1433{
1434#if QT_CONFIG(tabbar)
1435 if (tabbed)
1436 return QRect();
1437#endif
1438
1439 const QDockAreaLayoutItem &item = item_list.at(index);
1440 if (item.skip())
1441 return QRect();
1442
1443 QPoint pos = rect.topLeft();
1444 rpick(o, pos) = item.pos + item.size;
1445 QSize s = rect.size();
1446 rpick(o, s) = *sep;
1447
1448 return QRect(pos, s);
1449}
1450
1451QRect QDockAreaLayoutInfo::separatorRect(const QList<int> &path) const
1452{
1453 Q_ASSERT(!path.isEmpty());
1454
1455 const int index = path.first();
1456 if (path.size() > 1) {
1457 const QDockAreaLayoutItem &item = item_list.at(index);
1458 Q_ASSERT(item.subinfo != nullptr);
1459 return item.subinfo->separatorRect(path.mid(1));
1460 }
1461 return separatorRect(index);
1462}
1463
1464QList<int> QDockAreaLayoutInfo::findSeparator(const QPoint &_pos) const
1465{
1466#if QT_CONFIG(tabbar)
1467 if (tabbed)
1468 return QList<int>();
1469#endif
1470
1471 int pos = pick(o, _pos);
1472
1473 for (int i = 0; i < item_list.size(); ++i) {
1474 const QDockAreaLayoutItem &item = item_list.at(i);
1475 if (item.skip() || (item.flags & QDockAreaLayoutItem::GapItem))
1476 continue;
1477
1478 if (item.pos + item.size > pos) {
1479 if (item.subinfo != nullptr) {
1480 QList<int> result = item.subinfo->findSeparator(_pos);
1481 if (!result.isEmpty()) {
1482 result.prepend(i);
1483 return result;
1484 } else {
1485 return QList<int>();
1486 }
1487 }
1488 }
1489
1490 int next = this->next(i);
1491 if (next == -1 || (item_list.at(next).flags & QDockAreaLayoutItem::GapItem))
1492 continue;
1493
1494 QRect sepRect = separatorRect(i);
1495 if (!sepRect.isNull() && *sep == 1)
1496 sepRect.adjust(-2, -2, 2, 2);
1497 //we also make sure we don't find a separator that's not there
1498 if (sepRect.contains(_pos) && !item.hasFixedSize(o)) {
1499 return QList<int>() << i;
1500 }
1501
1502 }
1503
1504 return QList<int>();
1505}
1506
1507QList<int> QDockAreaLayoutInfo::indexOfPlaceHolder(const QString &objectName) const
1508{
1509 for (int i = 0; i < item_list.size(); ++i) {
1510 const QDockAreaLayoutItem &item = item_list.at(i);
1511
1512 if (item.subinfo != nullptr) {
1513 QList<int> result = item.subinfo->indexOfPlaceHolder(objectName);
1514 if (!result.isEmpty()) {
1515 result.prepend(i);
1516 return result;
1517 }
1518 continue;
1519 }
1520
1521 if (item.placeHolderItem != nullptr && item.placeHolderItem->objectName == objectName) {
1522 QList<int> result;
1523 result << i;
1524 return result;
1525 }
1526 }
1527
1528 return QList<int>();
1529}
1530
1531QList<int> QDockAreaLayoutInfo::indexOf(const QWidget *widget) const
1532{
1533 for (int i = 0; i < item_list.size(); ++i) {
1534 const QDockAreaLayoutItem &item = item_list.at(i);
1535
1536 if (item.placeHolderItem != nullptr)
1537 continue;
1538
1539 if (item.subinfo != nullptr) {
1540 QList<int> result = item.subinfo->indexOf(widget);
1541 if (!result.isEmpty()) {
1542 result.prepend(i);
1543 return result;
1544 }
1545 continue;
1546 }
1547
1548 if (!(item.flags & QDockAreaLayoutItem::GapItem) && item.widgetItem && item.widgetItem->widget() == widget) {
1549 QList<int> result;
1550 result << i;
1551 return result;
1552 }
1553 }
1554
1555 return QList<int>();
1556}
1557
1558std::unique_ptr<QLayoutItem> QDockAreaLayoutInfo::takeWidgetItem(QWidget *widget)
1559{
1560 std::unique_ptr<QLayoutItem> widgetItem;
1561 if (const auto path = indexOf(widget); !path.isEmpty())
1562 widgetItem.reset(item(path).widgetItem);
1563 return widgetItem;
1564}
1565
1566QMainWindowLayout *QDockAreaLayoutInfo::mainWindowLayout() const
1567{
1568 QMainWindowLayout *result = qt_mainwindow_layout(mainWindow);
1569 Q_ASSERT(result != nullptr);
1570 return result;
1571}
1572
1573bool QDockAreaLayoutInfo::hasFixedSize() const
1574{
1575 return perp(o, minimumSize()) == perp(o, maximumSize());
1576}
1577
1578/*! \internal
1579 Applies the layout and returns the activated QDockWidget or nullptr.
1580 */
1581QDockWidget *QDockAreaLayoutInfo::apply(QWidgetAnimator::AnimationRule rule)
1582{
1583 QWidgetAnimator &widgetAnimator = mainWindowLayout()->widgetAnimator;
1584
1585#if QT_CONFIG(tabbar)
1586 if (tabbed) {
1587 QRect tab_rect;
1588 QSize tbh = tabBarSizeHint();
1589
1590 if (!tbh.isNull()) {
1591 switch (tabBarShape) {
1592 case QTabBar::RoundedNorth:
1593 case QTabBar::TriangularNorth:
1594 tab_rect = QRect(rect.left(), rect.top(), rect.width(), tbh.height());
1595 break;
1596 case QTabBar::RoundedSouth:
1597 case QTabBar::TriangularSouth:
1598 tab_rect = QRect(rect.left(), rect.bottom() - tbh.height() + 1,
1599 rect.width(), tbh.height());
1600 break;
1601 case QTabBar::RoundedEast:
1602 case QTabBar::TriangularEast:
1603 tab_rect = QRect(rect.right() - tbh.width() + 1, rect.top(),
1604 tbh.width(), rect.height());
1605 break;
1606 case QTabBar::RoundedWest:
1607 case QTabBar::TriangularWest:
1608 tab_rect = QRect(rect.left(), rect.top(),
1609 tbh.width(), rect.height());
1610 break;
1611 default:
1612 break;
1613 }
1614 }
1615
1616 widgetAnimator.animate(tabBar, tab_rect, rule);
1617 }
1618#endif // QT_CONFIG(tabbar)
1619
1620 QDockWidget *activated = nullptr;
1621
1622 for (int i = 0; i < item_list.size(); ++i) {
1623 QDockAreaLayoutItem &item = item_list[i];
1624
1625 if (item.flags & QDockAreaLayoutItem::GapItem)
1626 continue;
1627
1628 if (item.subinfo != nullptr) {
1629 item.subinfo->apply(rule);
1630 continue;
1631 }
1632
1633 if (item.skip())
1634 continue;
1635
1636 Q_ASSERT(item.widgetItem);
1637 QRect r = itemRect(i);
1638 QWidget *w = item.widgetItem->widget();
1639
1640 QRect geo = w->geometry();
1641 widgetAnimator.animate(w, r, rule);
1642 if (!w->isHidden() && w->window()->isVisible()) {
1643 QDockWidget *dw = qobject_cast<QDockWidget*>(w);
1644 if (!r.isValid() && geo.right() >= 0 && geo.bottom() >= 0) {
1645 dw->lower();
1646 emit dw->visibilityChanged(false);
1647 } else if (r.isValid()
1648 && (geo.right() < 0 || geo.bottom() < 0)) {
1649 emit dw->visibilityChanged(true);
1650 activated = dw;
1651 }
1652 }
1653 }
1654#if QT_CONFIG(tabbar)
1655 if (*sep == 1)
1656 updateSeparatorWidgets();
1657#endif // QT_CONFIG(tabbar)
1658
1659 return activated;
1660}
1661
1662static void paintSep(QPainter *p, QWidget *w, const QRect &r, Qt::Orientation o, bool mouse_over)
1663{
1664 QStyleOption opt(0);
1665 opt.state = QStyle::State_None;
1666 if (w->isEnabled())
1667 opt.state |= QStyle::State_Enabled;
1668 if (o != Qt::Horizontal)
1669 opt.state |= QStyle::State_Horizontal;
1670 if (mouse_over)
1671 opt.state |= QStyle::State_MouseOver;
1672 opt.rect = r;
1673 opt.palette = w->palette();
1674
1675 w->style()->drawPrimitive(QStyle::PE_IndicatorDockWidgetResizeHandle, &opt, p, w);
1676}
1677
1678QRegion QDockAreaLayoutInfo::separatorRegion() const
1679{
1680 QRegion result;
1681
1682 if (isEmpty())
1683 return result;
1684#if QT_CONFIG(tabbar)
1685 if (tabbed)
1686 return result;
1687#endif
1688
1689 for (int i = 0; i < item_list.size(); ++i) {
1690 const QDockAreaLayoutItem &item = item_list.at(i);
1691
1692 if (item.skip())
1693 continue;
1694
1695 int next = this->next(i);
1696
1697 if (item.subinfo)
1698 result |= item.subinfo->separatorRegion();
1699
1700 if (next == -1)
1701 break;
1702 result |= separatorRect(i);
1703 }
1704
1705 return result;
1706}
1707
1708void QDockAreaLayoutInfo::paintSeparators(QPainter *p, QWidget *widget,
1709 const QRegion &clip,
1710 const QPoint &mouse) const
1711{
1712 if (isEmpty())
1713 return;
1714#if QT_CONFIG(tabbar)
1715 if (tabbed)
1716 return;
1717#endif
1718
1719 for (int i = 0; i < item_list.size(); ++i) {
1720 const QDockAreaLayoutItem &item = item_list.at(i);
1721
1722 if (item.skip())
1723 continue;
1724
1725 int next = this->next(i);
1726 if ((item.flags & QDockAreaLayoutItem::GapItem)
1727 || (next != -1 && (item_list.at(next).flags & QDockAreaLayoutItem::GapItem)))
1728 continue;
1729
1730 if (item.subinfo) {
1731 if (clip.contains(item.subinfo->rect))
1732 item.subinfo->paintSeparators(p, widget, clip, mouse);
1733 }
1734
1735 if (next == -1)
1736 break;
1737 QRect r = separatorRect(i);
1738 if (clip.contains(r) && !item.hasFixedSize(o))
1739 paintSep(p, widget, r, o, r.contains(mouse));
1740 }
1741}
1742
1743int QDockAreaLayoutInfo::next(int index) const
1744{
1745 for (int i = index + 1; i < item_list.size(); ++i) {
1746 if (!item_list.at(i).skip())
1747 return i;
1748 }
1749 return -1;
1750}
1751
1752int QDockAreaLayoutInfo::prev(int index) const
1753{
1754 for (int i = index - 1; i >= 0; --i) {
1755 if (!item_list.at(i).skip())
1756 return i;
1757 }
1758 return -1;
1759}
1760
1761#if QT_CONFIG(tabbar)
1762void QDockAreaLayoutInfo::tab(int index, QLayoutItem *dockWidgetItem)
1763{
1764 if (tabbed) {
1765 item_list.append(QDockAreaLayoutItem(dockWidgetItem));
1766 updateTabBar();
1767 setCurrentTab(dockWidgetItem->widget());
1768 } else {
1769 QDockAreaLayoutInfo *new_info
1770 = new QDockAreaLayoutInfo(sep, dockPos, o, tabBarShape, mainWindow);
1771 item_list[index].subinfo = new_info;
1772 new_info->item_list.append(QDockAreaLayoutItem(item_list.at(index).widgetItem));
1773 item_list[index].widgetItem = nullptr;
1774 new_info->item_list.append(QDockAreaLayoutItem(dockWidgetItem));
1775 new_info->tabbed = true;
1776 new_info->updateTabBar();
1777 new_info->setCurrentTab(dockWidgetItem->widget());
1778 }
1779}
1780#endif // QT_CONFIG(tabbar)
1781
1782void QDockAreaLayoutInfo::split(int index, Qt::Orientation orientation,
1783 QLayoutItem *dockWidgetItem)
1784{
1785 if (orientation == o) {
1786 item_list.insert(index + 1, QDockAreaLayoutItem(dockWidgetItem));
1787 } else {
1788#if !QT_CONFIG(tabbar)
1789 const int tabBarShape = 0;
1790#endif
1791 QDockAreaLayoutInfo *new_info
1792 = new QDockAreaLayoutInfo(sep, dockPos, orientation, tabBarShape, mainWindow);
1793 item_list[index].subinfo = new_info;
1794 new_info->item_list.append(QDockAreaLayoutItem(item_list.at(index).widgetItem));
1795 item_list[index].widgetItem = nullptr;
1796 new_info->item_list.append(QDockAreaLayoutItem(dockWidgetItem));
1797 }
1798}
1799
1800QDockAreaLayoutItem &QDockAreaLayoutInfo::item(const QList<int> &path)
1801{
1802 Q_ASSERT(!path.isEmpty());
1803 const int index = path.first();
1804 if (path.size() > 1) {
1805 const QDockAreaLayoutItem &item = item_list[index];
1806 Q_ASSERT(item.subinfo != nullptr);
1807 return item.subinfo->item(path.mid(1));
1808 }
1809 return item_list[index];
1810}
1811
1812QLayoutItem *QDockAreaLayoutInfo::itemAt(int *x, int index) const
1813{
1814 for (int i = 0; i < item_list.size(); ++i) {
1815 const QDockAreaLayoutItem &item = item_list.at(i);
1816 if (item.placeHolderItem != nullptr)
1817 continue;
1818 if (item.subinfo) {
1819 if (QLayoutItem *ret = item.subinfo->itemAt(x, index))
1820 return ret;
1821 } else if (item.widgetItem) {
1822 if ((*x)++ == index)
1823 return item.widgetItem;
1824 }
1825 }
1826 return nullptr;
1827}
1828
1829QLayoutItem *QDockAreaLayoutInfo::takeAt(int *x, int index)
1830{
1831 for (int i = 0; i < item_list.size(); ++i) {
1832 QDockAreaLayoutItem &item = item_list[i];
1833 if (item.placeHolderItem != nullptr)
1834 continue;
1835 else if (item.subinfo) {
1836 if (QLayoutItem *ret = item.subinfo->takeAt(x, index)) {
1837 unnest(i);
1838 return ret;
1839 }
1840 } else if (item.widgetItem) {
1841 if ((*x)++ == index) {
1842 QWidget *widget = item.widgetItem->widget();
1843 if (widget->isWidgetType())
1844 item.placeHolderItem = new QPlaceHolderItem(widget);
1845 else
1846 qCDebug(lcQpaDockWidgets) << widget << "is in destruction. No gap created.";
1847 QLayoutItem *ret = item.widgetItem;
1848 item.widgetItem = nullptr;
1849 if (item.size != -1)
1850 item.flags |= QDockAreaLayoutItem::KeepSize;
1851 return ret;
1852 }
1853 }
1854 }
1855 return nullptr;
1856}
1857
1858// Add a dock widget or dock widget group window to the item list
1859void QDockAreaLayoutInfo::add(QWidget *widget)
1860{
1861 // Do not add twice
1862 if (!indexOf(widget).isEmpty())
1863 return;
1864
1865 if (auto *dockWidget = qobject_cast<QDockWidget *>(widget)) {
1866 item_list.append(QDockAreaLayoutItem(new QDockWidgetItem(dockWidget)));
1867 return;
1868 }
1869
1870 if (auto *groupWindow = qobject_cast<QDockWidgetGroupWindow *>(widget)) {
1871 item_list.append(QDockAreaLayoutItem(new QDockWidgetGroupWindowItem(groupWindow)));
1872 return;
1873 }
1874
1875 qFatal("Coding error. Add supports only QDockWidget and QDockWidgetGroupWindow");
1876}
1877
1878void QDockAreaLayoutInfo::deleteAllLayoutItems()
1879{
1880 for (int i = 0; i < item_list.size(); ++i) {
1881 QDockAreaLayoutItem &item= item_list[i];
1882 if (item.subinfo) {
1883 item.subinfo->deleteAllLayoutItems();
1884 } else {
1885 delete item.widgetItem;
1886 item.widgetItem = nullptr;
1887 }
1888 }
1889}
1890
1891void QDockAreaLayoutInfo::saveState(QDataStream &stream) const
1892{
1893 qsizetype cnt = 0;
1894 for (qsizetype i = 0; i < item_list.size(); ++i) {
1895 const QDockAreaLayoutItem &item = item_list.at(i);
1896 if (item.widgetItem && qobject_cast<QDockWidgetGroupWindow *>(item.widgetItem->widget())) {
1897 continue;
1898 }
1899 ++cnt;
1900 }
1901
1902#if QT_CONFIG(tabbar)
1903 if (tabbed) {
1904 stream << StateMarkers::Tab;
1905
1906 // write the index in item_list of the widget that's currently on top.
1907 quintptr id = currentTabId();
1908 int index = -1;
1909 for (int i = 0; i < item_list.size(); ++i) {
1910 if (tabId(item_list.at(i)) == id) {
1911 index = i;
1912 break;
1913 }
1914 }
1915 stream << index;
1916 } else
1917#endif // QT_CONFIG(tabbar)
1918 {
1919 stream << StateMarkers::Sequence;
1920 }
1921
1922 stream << static_cast<uchar>(o) << static_cast<int>(cnt);
1923
1924 for (int i = 0; i < item_list.size(); ++i) {
1925 const QDockAreaLayoutItem &item = item_list.at(i);
1926 if (item.widgetItem != nullptr) {
1927 QWidget *w = item.widgetItem->widget();
1928 if (qobject_cast<QDockWidgetGroupWindow *>(w)) {
1929 qCDebug(lcQpaDockWidgets) << "Ignoring dock widget group window" << w;
1930 continue;
1931 }
1932 stream << StateMarkers::Widget;
1933 QString name = w->objectName();
1934 if (Q_UNLIKELY(name.isEmpty())) {
1935 qWarning() << "QMainWindow::saveState(): 'objectName' not set for"
1936 << w << qUtf16Printable(w->windowTitle());
1937 }
1938 stream << name;
1939
1940 StateFlags flags;
1941 if (!w->isHidden())
1942 flags.setFlag(StateFlag::Visible);
1943 if (w->isWindow())
1944 flags.setFlag(StateFlag::Floating);
1945 if (qobject_cast<QDockWidgetGroupWindow *>(w->parent()))
1946 flags.setFlag(StateFlag::FloatingTabs);
1947 if (auto *dw = qobject_cast<QDockWidget *>(w)) {
1948 const auto priv = static_cast<QDockWidgetPrivate *>(QObjectPrivate::get(dw));
1949 flags.setFlag(StateFlag::Closed, priv->closed);
1950 }
1951 stream << static_cast<uchar>(flags.toInt());
1952
1953 qCDebug(lcQpaDockWidgets) << "Saving state for dock widget:" << w->objectName() << flags;
1954 if (w->isWindow()) {
1955 const QRect geometry = w->geometry();
1956 stream << geometry.x() << geometry.y() << geometry.width() << geometry.height();
1957 } else {
1958 stream << item.pos << item.size << pick(o, item.minimumSize())
1959 << pick(o, item.maximumSize());
1960 }
1961 } else if (item.placeHolderItem != nullptr) {
1962 stream << StateMarkers::Widget;
1963 stream << item.placeHolderItem->objectName;
1964 StateFlags flags;
1965 if (!item.placeHolderItem->hidden)
1966 flags.setFlag(StateFlag::Visible);
1967 if (item.placeHolderItem->window)
1968 flags.setFlag(StateFlag::Floating);
1969 qCDebug(lcQpaDockWidgets) << "Saving state for placeholder item:" << item << flags;
1970 stream << static_cast<uchar>(flags.toInt());
1971 if (item.placeHolderItem->window) {
1972 QRect r = item.placeHolderItem->topLevelRect;
1973 stream << r.x() << r.y() << r.width() << r.height();
1974 } else {
1975 stream << item.pos << item.size << (int)0 << (int)0;
1976 }
1977 } else if (item.subinfo != nullptr) {
1978 stream << StateMarkers::Sequence;
1979 stream << item.pos << item.size << pick(o, item.minimumSize()) << pick(o, item.maximumSize());
1980 item.subinfo->saveState(stream);
1981 }
1982 }
1983}
1984
1985static Qt::DockWidgetArea toDockWidgetArea(QInternal::DockPosition pos)
1986{
1987 switch (pos) {
1988 case QInternal::LeftDock: return Qt::LeftDockWidgetArea;
1989 case QInternal::RightDock: return Qt::RightDockWidgetArea;
1990 case QInternal::TopDock: return Qt::TopDockWidgetArea;
1991 case QInternal::BottomDock: return Qt::BottomDockWidgetArea;
1992 default: break;
1993 }
1994 return Qt::NoDockWidgetArea;
1995}
1996
1997bool QDockAreaLayoutInfo::restoreState(QDataStream &stream, QList<QDockWidget*> &widgets,
1998 QInternal::CallMode callMode, int depth)
1999{
2000 // A sane layout nests at most one level per dock widget; a stream nested
2001 // deeper than this is corrupt or hostile.
2002 constexpr int maxDepth = 100;
2003 if (depth >= maxDepth)
2004 return false;
2005
2006 const bool testing = callMode == QInternal::Testing;
2007 const QLatin1StringView debugCallMode = testing ? QLatin1StringView("(testing)")
2008 : QLatin1StringView("(restoring)");
2009 QMainWindowLayoutState::StateMarkers marker;
2010 stream >> marker;
2011 if (marker != StateMarkers::Tab && marker != StateMarkers::Sequence)
2012 return false;
2013
2014#if QT_CONFIG(tabbar)
2015 tabbed = marker == StateMarkers::Tab;
2016
2017 int index = -1;
2018 if (tabbed)
2019 stream >> index;
2020#endif
2021
2022 uchar orientation;
2023 stream >> orientation;
2024 o = static_cast<Qt::Orientation>(orientation);
2025
2026 int cnt;
2027 stream >> cnt;
2028
2029 for (int i = 0; i < cnt; ++i) {
2030 QMainWindowLayoutState::StateMarkers nextMarker;
2031 stream >> nextMarker;
2032 if (nextMarker == StateMarkers::Widget) {
2033 QString name;
2034 uchar f;
2035 stream >> name >> f;
2036 StateFlags flags(f);
2037 if (name.isEmpty()) {
2038 int dummy;
2039 stream >> dummy >> dummy >> dummy >> dummy;
2040 continue;
2041 }
2042
2043 QDockWidget *widget = nullptr;
2044 for (qsizetype j = 0; j < widgets.size(); ++j) {
2045 if (widgets.at(j)->objectName() == name) {
2046 widget = widgets.takeAt(j);
2047 break;
2048 }
2049 }
2050
2051 if (widget == nullptr) {
2052 QPlaceHolderItem *placeHolder = new QPlaceHolderItem;
2053 QDockAreaLayoutItem item(placeHolder);
2054
2055 placeHolder->window = flags.testFlag(StateFlag::Floating);
2056 placeHolder->hidden = !flags.testFlag(StateFlag::Visible);
2057 if (placeHolder->window) {
2058 int x, y, w, h;
2059 stream >> x >> y >> w >> h;
2060 placeHolder->topLevelRect = QRect(x, y, w, h);
2061 } else {
2062 int dummy;
2063 stream >> item.pos >> item.size >> dummy >> dummy;
2064 }
2065 if (item.size != -1)
2066 item.flags |= QDockAreaLayoutItem::KeepSize;
2067 placeHolder->objectName = std::move(name);
2068 qCDebug(lcQpaDockWidgets) << "Restoring placeholder" << item << debugCallMode << flags;
2069 if (!testing)
2070 item_list.append(item);
2071 } else {
2072 if (flags.testFlag(StateFlag::FloatingTabs))
2073 qCDebug(lcQpaDockWidgets) << "Found floating tab:" << name << flags << debugCallMode;
2074 QDockAreaLayoutItem item(new QDockWidgetItem(widget));
2075 if (flags.testFlag(StateFlag::Floating)) {
2076 if (!testing) {
2077 widget->hide();
2078 widget->setFloating(true);
2079 }
2080
2081 int x, y, w, h;
2082 stream >> x >> y >> w >> h;
2083
2084 if (!testing) {
2085 widget->setGeometry(QDockAreaLayout::constrainedRect(QRect(x, y, w, h), widget));
2086 widget->setVisible(flags.testFlag(StateFlag::Visible));
2087 item_list.append(item);
2088 }
2089 } else {
2090 int dummy;
2091 stream >> item.pos >> item.size >> dummy >> dummy;
2092 if (!testing) {
2093 item_list.append(item);
2094 if (flags.testFlag(StateFlag::Closed)) {
2095 widget->close();
2096 } else {
2097 widget->setFloating(false);
2098 widget->setVisible(flags.testFlag(StateFlag::Visible));
2099 }
2100 emit widget->dockLocationChanged(toDockWidgetArea(dockPos));
2101 }
2102 }
2103 qCDebug(lcQpaDockWidgets) << "Restoring QDockWidget" << item << flags << debugCallMode;
2104 if (testing) {
2105 //was it is not really added to the layout, we need to delete the object here
2106 delete item.widgetItem;
2107 item.widgetItem = nullptr;
2108 }
2109 }
2110 } else if (nextMarker == StateMarkers::Sequence) {
2111 int dummy;
2112#if !QT_CONFIG(tabbar)
2113 const int tabBarShape = 0;
2114#endif
2115 QDockAreaLayoutItem item(new QDockAreaLayoutInfo(sep, dockPos, o,
2116 tabBarShape, mainWindow));
2117 stream >> item.pos >> item.size >> dummy >> dummy;
2118 qCDebug(lcQpaDockWidgets) << "Restoring separator item" << item << debugCallMode;
2119 //we need to make sure the element is in the list so the dock widget can eventually be docked correctly
2120 if (!testing)
2121 item_list.append(item);
2122
2123 //here we need to make sure we change the item in the item_list
2124 QDockAreaLayoutItem &lastItem = testing ? item : item_list.last();
2125
2126 if (!lastItem.subinfo->restoreState(stream, widgets, callMode, depth + 1))
2127 return false;
2128
2129 } else {
2130 return false;
2131 }
2132 }
2133
2134#if QT_CONFIG(tabbar)
2135 if (!testing && tabbed && index >= 0 && index < item_list.size()) {
2136 updateTabBar();
2137 setCurrentTabId(tabId(item_list.at(index)));
2138 }
2139 if (!testing && *sep == 1)
2140 updateSeparatorWidgets();
2141#endif
2142
2143 return true;
2144}
2145
2146#if QT_CONFIG(tabbar)
2147
2148static void raiseSeparatorWidget(QWidget *separatorWidget)
2149{
2150 Q_ASSERT(separatorWidget);
2151
2152#if QT_CONFIG(toolbar)
2153 // Raise the separator widget, but make sure it doesn't go above
2154 // an expanded toolbar, as that would break mouse event hit testing.
2155 Q_ASSERT(separatorWidget->parent());
2156 const auto toolBars = separatorWidget->parent()->findChildren<QToolBar*>(Qt::FindDirectChildrenOnly);
2157 for (auto *toolBar : toolBars) {
2158 if (auto *toolBarLayout = qobject_cast<QToolBarLayout*>(toolBar->layout())) {
2159 if (toolBarLayout->expanded) {
2160 separatorWidget->stackUnder(toolBar);
2161 return;
2162 }
2163 }
2164 }
2165#endif
2166
2167 separatorWidget->raise();
2168}
2169
2170
2171void QDockAreaLayoutInfo::updateSeparatorWidgets() const
2172{
2173 if (tabbed) {
2174 separatorWidgets.clear();
2175 return;
2176 }
2177
2178 int j = 0;
2179 for (int i = 0; i < item_list.size(); ++i) {
2180 const QDockAreaLayoutItem &item = item_list.at(i);
2181
2182 if (item.skip())
2183 continue;
2184
2185 int next = this->next(i);
2186 if ((item.flags & QDockAreaLayoutItem::GapItem)
2187 || (next != -1 && (item_list.at(next).flags & QDockAreaLayoutItem::GapItem)))
2188 continue;
2189
2190 if (item.subinfo) {
2191 item.subinfo->updateSeparatorWidgets();
2192 }
2193
2194 if (next == -1)
2195 break;
2196
2197 QWidget *sepWidget;
2198 if (j < separatorWidgets.size()) {
2199 sepWidget = separatorWidgets.at(j);
2200 if (!sepWidget) {
2201 qWarning("QDockAreaLayoutInfo::updateSeparatorWidgets: null separator widget");
2202 sepWidget = mainWindowLayout()->getSeparatorWidget();
2203 separatorWidgets[j] = sepWidget;
2204 }
2205 } else {
2206 sepWidget = mainWindowLayout()->getSeparatorWidget();
2207 separatorWidgets.append(sepWidget);
2208 }
2209 j++;
2210
2211 Q_ASSERT(sepWidget);
2212 raiseSeparatorWidget(sepWidget);
2213
2214 QRect sepRect = separatorRect(i).adjusted(-2, -2, 2, 2);
2215 sepWidget->setGeometry(sepRect);
2216 sepWidget->setMask( QRegion(separatorRect(i).translated( - sepRect.topLeft())));
2217 sepWidget->show();
2218 }
2219
2220 for (int k = j; k < separatorWidgets.size(); ++k) {
2221 separatorWidgets[k]->hide();
2222 }
2223 separatorWidgets.resize(j);
2224}
2225
2226/*! \internal
2227 reparent all the widgets contained in this layout portion to the
2228 specified parent. This is used to reparent dock widgets and tabbars
2229 to the floating window or the main window
2230 */
2231void QDockAreaLayoutInfo::reparentWidgets(QWidget *parent)
2232{
2233 if (tabBar)
2234 tabBar->setParent(parent);
2235
2236 for (int i = 0; i < item_list.size(); ++i) {
2237 const QDockAreaLayoutItem &item = item_list.at(i);
2238 if (item.flags & QDockAreaLayoutItem::GapItem)
2239 continue;
2240 if (item.subinfo)
2241 item.subinfo->reparentWidgets(parent);
2242 if (item.widgetItem) {
2243 QWidget *w = item.widgetItem->widget();
2244 if (qobject_cast<QDockWidgetGroupWindow *>(w))
2245 continue;
2246 if (w->parent() != parent) {
2247 bool hidden = w->isHidden();
2248 w->setParent(parent, w->windowFlags());
2249 if (!hidden)
2250 w->show();
2251 }
2252 }
2253 }
2254}
2255
2256//returns whether the tabbar is visible or not
2257bool QDockAreaLayoutInfo::updateTabBar() const
2258{
2259 if (!tabbed)
2260 return false;
2261
2262 QDockAreaLayoutInfo *that = const_cast<QDockAreaLayoutInfo*>(this);
2263
2264 if (that->tabBar == nullptr) {
2265 that->tabBar = mainWindowLayout()->getTabBar();
2266 that->tabBar->setShape(static_cast<QTabBar::Shape>(tabBarShape));
2267 that->tabBar->setDrawBase(true);
2268 }
2269
2270 const QSignalBlocker blocker(tabBar);
2271 bool gap = false;
2272
2273 const quintptr oldCurrentId = currentTabId();
2274
2275 int tab_idx = 0;
2276 for (int i = 0; i < item_list.size(); ++i) {
2277 const QDockAreaLayoutItem &item = item_list.at(i);
2278 if (item.skip())
2279 continue;
2280 if (item.flags & QDockAreaLayoutItem::GapItem) {
2281 gap = true;
2282 continue;
2283 }
2284 if (item.widgetItem == nullptr)
2285 continue;
2286
2287 QDockWidget *dw = qobject_cast<QDockWidget*>(item.widgetItem->widget());
2288 QString title = dw->d_func()->fixedWindowTitle;
2289 quintptr id = tabId(item);
2290 const QIcon windowIcon = dw->testAttribute(Qt::WA_SetWindowIcon) ? dw->windowIcon()
2291 : QIcon();
2292 if (tab_idx == tabBar->count()) {
2293 tabBar->insertTab(tab_idx, windowIcon, title);
2294#if QT_CONFIG(tooltip)
2295 tabBar->setTabToolTip(tab_idx, title);
2296#endif
2297 tabBar->setTabData(tab_idx, id);
2298 } else if (qvariant_cast<quintptr>(tabBar->tabData(tab_idx)) != id) {
2299 if (tab_idx + 1 < tabBar->count()
2300 && qvariant_cast<quintptr>(tabBar->tabData(tab_idx + 1)) == id)
2301 tabBar->removeTab(tab_idx);
2302 else {
2303 tabBar->insertTab(tab_idx, windowIcon, title);
2304#if QT_CONFIG(tooltip)
2305 tabBar->setTabToolTip(tab_idx, title);
2306#endif
2307 tabBar->setTabData(tab_idx, id);
2308 }
2309 }
2310
2311 if (title != tabBar->tabText(tab_idx)) {
2312 tabBar->setTabText(tab_idx, title);
2313#if QT_CONFIG(tooltip)
2314 tabBar->setTabToolTip(tab_idx, title);
2315#endif
2316 }
2317
2318 ++tab_idx;
2319 }
2320
2321 while (tab_idx < tabBar->count()) {
2322 tabBar->removeTab(tab_idx);
2323 }
2324
2325 if (oldCurrentId > 0 && currentTabId() != oldCurrentId)
2326 that->setCurrentTabId(oldCurrentId);
2327
2328 if (QDockWidgetGroupWindow *dwgw = qobject_cast<QDockWidgetGroupWindow *>(tabBar->parent()))
2329 dwgw->adjustFlags();
2330
2331 //returns if the tabbar is visible or not
2332 return ( (gap ? 1 : 0) + tabBar->count()) > 1;
2333}
2334
2335void QDockAreaLayoutInfo::setTabBarShape(int shape)
2336{
2337 if (shape == tabBarShape)
2338 return;
2339 tabBarShape = shape;
2340 if (tabBar != nullptr)
2341 tabBar->setShape(static_cast<QTabBar::Shape>(shape));
2342
2343 for (int i = 0; i < item_list.size(); ++i) {
2344 QDockAreaLayoutItem &item = item_list[i];
2345 if (item.subinfo != nullptr)
2346 item.subinfo->setTabBarShape(shape);
2347 }
2348}
2349
2350QSize QDockAreaLayoutInfo::tabBarMinimumSize() const
2351{
2352 if (!updateTabBar())
2353 return QSize(0, 0);
2354
2355 return tabBar->minimumSizeHint();
2356}
2357
2358QSize QDockAreaLayoutInfo::tabBarSizeHint() const
2359{
2360 if (!updateTabBar())
2361 return QSize(0, 0);
2362
2363 return tabBar->sizeHint();
2364}
2365
2366QSet<QTabBar*> QDockAreaLayoutInfo::usedTabBars() const
2367{
2368 QSet<QTabBar*> result;
2369
2370 if (tabbed) {
2371 updateTabBar();
2372 result.insert(tabBar);
2373 }
2374
2375 for (int i = 0; i < item_list.size(); ++i) {
2376 const QDockAreaLayoutItem &item = item_list.at(i);
2377 if (item.subinfo != nullptr)
2378 result += item.subinfo->usedTabBars();
2379 }
2380
2381 return result;
2382}
2383
2384// returns a set of all used separator widgets for this dockarelayout info
2385// and all subinfos
2386QSet<QWidget*> QDockAreaLayoutInfo::usedSeparatorWidgets() const
2387{
2388 QSet<QWidget*> result;
2389 const int numSeparatorWidgets = separatorWidgets.size();
2390 result.reserve(numSeparatorWidgets);
2391
2392 for (int i = 0; i < numSeparatorWidgets; ++i)
2393 result << separatorWidgets.at(i);
2394
2395 for (int i = 0; i < item_list.size(); ++i) {
2396 const QDockAreaLayoutItem &item = item_list.at(i);
2397 if (item.subinfo != nullptr)
2398 result += item.subinfo->usedSeparatorWidgets();
2399 }
2400
2401 return result;
2402}
2403
2404QRect QDockAreaLayoutInfo::tabContentRect() const
2405{
2406 if (!tabbed)
2407 return QRect();
2408
2409 QRect result = rect;
2410 QSize tbh = tabBarSizeHint();
2411
2412 if (!tbh.isNull()) {
2413 switch (tabBarShape) {
2414 case QTabBar::RoundedNorth:
2415 case QTabBar::TriangularNorth:
2416 result.adjust(0, tbh.height(), 0, 0);
2417 break;
2418 case QTabBar::RoundedSouth:
2419 case QTabBar::TriangularSouth:
2420 result.adjust(0, 0, 0, -tbh.height());
2421 break;
2422 case QTabBar::RoundedEast:
2423 case QTabBar::TriangularEast:
2424 result.adjust(0, 0, -tbh.width(), 0);
2425 break;
2426 case QTabBar::RoundedWest:
2427 case QTabBar::TriangularWest:
2428 result.adjust(tbh.width(), 0, 0, 0);
2429 break;
2430 default:
2431 break;
2432 }
2433 }
2434
2435 return result;
2436}
2437
2438int QDockAreaLayoutInfo::tabIndexToListIndex(int tabIndex) const
2439{
2440 Q_ASSERT(tabbed && tabBar);
2441 quintptr data = qvariant_cast<quintptr>(tabBar->tabData(tabIndex));
2442 for (int i = 0; i < item_list.size(); ++i) {
2443 if (tabId(item_list.at(i)) == data)
2444 return i;
2445 }
2446 return -1;
2447}
2448
2449void QDockAreaLayoutInfo::moveTab(int from, int to)
2450{
2451 item_list.move(tabIndexToListIndex(from), tabIndexToListIndex(to));
2452}
2453#endif // QT_CONFIG(tabbar)
2454
2455/******************************************************************************
2456** QDockAreaLayout
2457*/
2458
2459QDockAreaLayout::QDockAreaLayout(QMainWindow *win) : fallbackToSizeHints(true)
2460{
2461 mainWindow = win;
2462 sep = win->style()->pixelMetric(QStyle::PM_DockWidgetSeparatorExtent, nullptr, win);
2463#if QT_CONFIG(tabbar)
2464 const int tabShape = QTabBar::RoundedSouth;
2465#else
2466 const int tabShape = 0;
2467#endif
2468 docks[QInternal::LeftDock]
2469 = QDockAreaLayoutInfo(&sep, QInternal::LeftDock, Qt::Vertical, tabShape, win);
2470 docks[QInternal::RightDock]
2471 = QDockAreaLayoutInfo(&sep, QInternal::RightDock, Qt::Vertical, tabShape, win);
2472 docks[QInternal::TopDock]
2473 = QDockAreaLayoutInfo(&sep, QInternal::TopDock, Qt::Horizontal, tabShape, win);
2474 docks[QInternal::BottomDock]
2475 = QDockAreaLayoutInfo(&sep, QInternal::BottomDock, Qt::Horizontal, tabShape, win);
2476 centralWidgetItem = nullptr;
2477
2478
2479 corners[Qt::TopLeftCorner] = Qt::TopDockWidgetArea;
2480 corners[Qt::TopRightCorner] = Qt::TopDockWidgetArea;
2481 corners[Qt::BottomLeftCorner] = Qt::BottomDockWidgetArea;
2482 corners[Qt::BottomRightCorner] = Qt::BottomDockWidgetArea;
2483}
2484
2485bool QDockAreaLayout::isValid() const
2486{
2487 return rect.isValid();
2488}
2489
2490void QDockAreaLayout::saveState(QDataStream &stream) const
2491{
2492 stream << StateMarkers::DockWidget;
2493 int cnt = 0;
2494 for (const auto &dock : docks) {
2495 if (!dock.item_list.isEmpty())
2496 ++cnt;
2497 }
2498 stream << cnt;
2499 for (std::size_t i = 0; i < docks.size(); ++i) {
2500 if (docks[i].item_list.isEmpty())
2501 continue;
2502 stream << int(i) << docks[i].rect.size();
2503 docks[i].saveState(stream);
2504 }
2505
2506 stream << centralWidgetRect.size();
2507
2508 for (const auto &corner : corners)
2509 stream << static_cast<int>(corner);
2510}
2511
2512bool QDockAreaLayout::restoreState(QDataStream &stream, const QList<QDockWidget*> &_dockwidgets, QInternal::CallMode callMode)
2513{
2514 QList<QDockWidget*> dockwidgets = _dockwidgets;
2515 const bool testing = callMode == QInternal::Testing;
2516 int cnt;
2517 stream >> cnt;
2518 static const int dockCount = int(docks.size());
2519 if (cnt < 0 || cnt > dockCount) {
2520 qWarning() << "Invalid data stream. Dock count" << cnt << "is out of bound. Maximum:" << dockCount;
2521 stream.setStatus(QDataStream::Status::ReadCorruptData);
2522 return false;
2523 }
2524 for (int i = 0; i < cnt; ++i) {
2525 int pos;
2526 stream >> pos;
2527 if (pos < 0 || pos >= dockCount) {
2528 qWarning() << "Invalid data stream. Dock position" << pos << "is out of bound. Maximum:" << dockCount - 1;
2529 stream.setStatus(QDataStream::Status::ReadCorruptData);
2530 return false;
2531 }
2532
2533 QSize size;
2534 stream >> size;
2535 if (!testing) {
2536 docks[pos].rect = QRect(QPoint(0, 0), size);
2537 }
2538 if (!docks[pos].restoreState(stream, dockwidgets, callMode)) {
2539 stream.setStatus(QDataStream::ReadCorruptData);
2540 return false;
2541 }
2542 }
2543
2544 QSize size;
2545 stream >> size;
2546 centralWidgetRect = QRect(QPoint(0, 0), size);
2547
2548 bool ok = stream.status() == QDataStream::Ok;
2549
2550 if (ok) {
2551 QDockAreaLayoutCorners cornerData;
2552 for (auto &val : cornerData)
2553 stream >> val;
2554
2555 if (stream.status() == QDataStream::Ok)
2556 corners = std::move(cornerData);
2557
2558 if (!testing)
2559 fallbackToSizeHints = false;
2560 }
2561
2562 return ok;
2563}
2564
2565QList<int> QDockAreaLayout::indexOfPlaceHolder(const QString &objectName) const
2566{
2567 for (std::size_t i = 0; i < docks.size(); ++i) {
2568 QList<int> result = docks[i].indexOfPlaceHolder(objectName);
2569 if (!result.isEmpty()) {
2570 result.prepend(int(i));
2571 return result;
2572 }
2573 }
2574 return QList<int>();
2575}
2576
2577QList<int> QDockAreaLayout::indexOf(const QWidget *dockWidget) const
2578{
2579 for (std::size_t i = 0; i < docks.size(); ++i) {
2580 QList<int> result = docks[i].indexOf(dockWidget);
2581 if (!result.isEmpty()) {
2582 result.prepend(int(i));
2583 return result;
2584 }
2585 }
2586 return QList<int>();
2587}
2588
2589QList<int> QDockAreaLayout::gapIndex(const QPoint &pos, bool disallowTabs) const
2590{
2591 QMainWindow::DockOptions opts = mainWindow->dockOptions();
2592 bool nestingEnabled = opts & QMainWindow::AllowNestedDocks;
2593 QDockAreaLayoutInfo::TabMode tabMode = QDockAreaLayoutInfo::NoTabs;
2594#if QT_CONFIG(tabbar)
2595 if (!disallowTabs) {
2596 if (opts & QMainWindow::AllowTabbedDocks || opts & QMainWindow::VerticalTabs)
2597 tabMode = QDockAreaLayoutInfo::AllowTabs;
2598 if (opts & QMainWindow::ForceTabbedDocks)
2599 tabMode = QDockAreaLayoutInfo::ForceTabs;
2600
2601 if (tabMode == QDockAreaLayoutInfo::ForceTabs)
2602 nestingEnabled = false;
2603 }
2604#endif
2605
2606
2607 for (std::size_t i = 0; i < docks.size(); ++i) {
2608 const QDockAreaLayoutInfo &info = docks[i];
2609
2610 if (!info.isEmpty() && info.rect.contains(pos)) {
2611 QList<int> result = info.gapIndex(pos, nestingEnabled, tabMode);
2612 if (!result.isEmpty())
2613 result.prepend(int(i));
2614 return result;
2615 }
2616 }
2617
2618 for (std::size_t i = 0; i < docks.size(); ++i) {
2619 const QDockAreaLayoutInfo &info = docks[i];
2620
2621 if (info.isEmpty()) {
2622 const QRect r = gapRect(static_cast<QInternal::DockPosition>(i));
2623 if (r.contains(pos)) {
2624 if (opts & QMainWindow::ForceTabbedDocks && !info.item_list.isEmpty()) {
2625 //in case of ForceTabbedDocks, we pass -1 in order to force the gap to be tabbed
2626 //it mustn't be completely empty otherwise it won't work
2627 return {int(i), -1, 0};
2628 } else {
2629 return {int(i), 0};
2630 }
2631 }
2632 }
2633 }
2634
2635 return QList<int>();
2636}
2637
2638QRect QDockAreaLayout::gapRect(QInternal::DockPosition dockPos) const
2639{
2640 Q_ASSERT_X(mainWindow, "QDockAreaLayout::gapRect", "Called without valid mainWindow pointer.");
2641
2642 // Determine gap size depending on MainWindow size (QTBUG-101657)
2643 const QSize gapSize = (mainWindow->size()/2).boundedTo(QSize(EmptyDropAreaSize, EmptyDropAreaSize));
2644
2645 // Warn if main window is too small to create proper docks.
2646 // Do not fail because this can be triggered by a user making MainWindow too small
2647 if (mainWindow->height() < (2 * sep)) {
2648 qCWarning(lcQpaDockWidgets,
2649 "QDockAreaLayout::gapRect: Main window height %i is too small. Docking will not be possible.",
2650 mainWindow->height());
2651
2652 }
2653 if (mainWindow->width() < (2 * sep)) {
2654 qCWarning(lcQpaDockWidgets,
2655 "QDockAreaLayout::gapRect: Main window width %i is too small. Docking will not be possible.",
2656 mainWindow->width());
2657 }
2658
2659 // Calculate rectangle of requested dock
2660 switch (dockPos) {
2661 case QInternal::LeftDock:
2662 return QRect(rect.left(), rect.top(), gapSize.width(), rect.height());
2663 case QInternal::RightDock:
2664 return QRect(rect.right() - gapSize.width(), rect.top(), gapSize.width(), rect.height());
2665 case QInternal::TopDock:
2666 return QRect(rect.left(), rect.top(), rect.width(), gapSize.height());
2667 case QInternal::BottomDock:
2668 return QRect(rect.left(), rect.bottom() - gapSize.height(), rect.width(), gapSize.height());
2669 case QInternal::DockCount:
2670 break;
2671 }
2672 return QRect();
2673}
2674
2675QList<int> QDockAreaLayout::findSeparator(const QPoint &pos) const
2676{
2677 QList<int> result;
2678 for (std::size_t i = 0; i < docks.size(); ++i) {
2679 const QDockAreaLayoutInfo &info = docks[i];
2680 if (info.isEmpty())
2681 continue;
2682 QRect rect = info.separatorRect();
2683 if (!rect.isNull() && sep == 1)
2684 rect.adjust(-2, -2, 2, 2);
2685 if (rect.contains(pos) && !info.hasFixedSize()) {
2686 result << int(i);
2687 break;
2688 } else if (info.rect.contains(pos)) {
2689 result = info.findSeparator(pos);
2690 if (!result.isEmpty()) {
2691 result.prepend(int(i));
2692 break;
2693 }
2694 }
2695 }
2696
2697 return result;
2698}
2699
2700QDockAreaLayoutInfo *QDockAreaLayout::info(QWidget *widget)
2701{
2702 for (auto &dock : docks) {
2703 if (QDockAreaLayoutInfo *result = dock.info(widget))
2704 return result;
2705 }
2706
2707 return nullptr;
2708}
2709
2710QDockAreaLayoutInfo *QDockAreaLayout::info(const QList<int> &path)
2711{
2712 Q_ASSERT(!path.isEmpty());
2713 const int index = path.first();
2714 Q_ASSERT(index >= 0 && index < int(docks.size()));
2715
2716 if (path.size() == 1)
2717 return &docks[index];
2718
2719 return docks[index].info(path.mid(1));
2720}
2721
2722const QDockAreaLayoutInfo *QDockAreaLayout::info(const QList<int> &path) const
2723{
2724 return const_cast<QDockAreaLayout*>(this)->info(path);
2725}
2726
2727QDockAreaLayoutItem &QDockAreaLayout::item(const QList<int> &path)
2728{
2729 Q_ASSERT(!path.isEmpty());
2730 const int index = path.first();
2731 Q_ASSERT(index >= 0 && index < int(docks.size()));
2732 return docks[index].item(path.mid(1));
2733}
2734
2735QRect QDockAreaLayout::itemRect(const QList<int> &path) const
2736{
2737 Q_ASSERT(!path.isEmpty());
2738 const int index = path.first();
2739 Q_ASSERT(index >= 0 && index < int(docks.size()));
2740 return docks[index].itemRect(path.mid(1));
2741}
2742
2743QRect QDockAreaLayout::separatorRect(const QList<int> &path) const
2744{
2745 Q_ASSERT(!path.isEmpty());
2746
2747 const int index = path.first();
2748 Q_ASSERT(index >= 0 && index < int(docks.size()));
2749
2750 const auto &dock = docks[index];
2751 if (path.size() == 1)
2752 return dock.separatorRect();
2753 else
2754 return dock.separatorRect(path.mid(1));
2755}
2756
2757bool QDockAreaLayout::insertGap(const QList<int> &path, QLayoutItem *dockWidgetItem)
2758{
2759 Q_ASSERT(!path.isEmpty());
2760 const int index = path.first();
2761 Q_ASSERT(index >= 0 && index < int(docks.size()));
2762 return docks[index].insertGap(path.mid(1), dockWidgetItem);
2763}
2764
2765QLayoutItem *QDockAreaLayout::plug(const QList<int> &path)
2766{
2767#if QT_CONFIG(tabbar)
2768 Q_ASSERT(!path.isEmpty());
2769 const int index = path.first();
2770 Q_ASSERT(index >= 0 && index < int(docks.size()));
2771 QLayoutItem *item = docks[index].plug(path.mid(1));
2772 docks[index].reparentWidgets(mainWindow);
2773 return item;
2774#else
2775 return nullptr;
2776#endif
2777}
2778
2779QLayoutItem *QDockAreaLayout::unplug(const QList<int> &path)
2780{
2781 Q_ASSERT(!path.isEmpty());
2782 const int index = path.first();
2783 Q_ASSERT(index >= 0 && index < int(docks.size()));
2784 return docks[index].unplug(path.mid(1));
2785}
2786
2787void QDockAreaLayout::remove(const QList<int> &path)
2788{
2789 Q_ASSERT(!path.isEmpty());
2790 const int index = path.first();
2791 Q_ASSERT(index >= 0 && index < int(docks.size()));
2792 docks[index].remove(path.mid(1));
2793}
2794
2795void QDockAreaLayout::removePlaceHolder(const QString &name)
2796{
2797 QList<int> index = indexOfPlaceHolder(name);
2798 if (!index.isEmpty())
2799 remove(index);
2800 const auto groups =
2801 mainWindow->findChildren<QDockWidgetGroupWindow *>(Qt::FindDirectChildrenOnly);
2802 for (QDockWidgetGroupWindow *dwgw : groups) {
2803 index = dwgw->layoutInfo()->indexOfPlaceHolder(name);
2804 if (!index.isEmpty()) {
2805 dwgw->layoutInfo()->remove(index);
2806 dwgw->destroyOrHideIfEmpty();
2807 }
2808 }
2809}
2810
2811static inline int qMax(int i1, int i2, int i3) { return qMax(i1, qMax(i2, i3)); }
2812
2813void QDockAreaLayout::getGrid(QList<QLayoutStruct> *_ver_struct_list,
2814 QList<QLayoutStruct> *_hor_struct_list)
2815{
2816 QSize center_hint(0, 0);
2817 QSize center_min(0, 0);
2818 QSize center_max(0, 0);
2819 const bool have_central = centralWidgetItem != nullptr && !centralWidgetItem->isEmpty();
2820 if (have_central) {
2821 center_hint = centralWidgetRect.size();
2822 if (!center_hint.isValid())
2823 center_hint = centralWidgetItem->sizeHint();
2824 center_min = centralWidgetItem->minimumSize();
2825 center_max = centralWidgetItem->maximumSize();
2826 }
2827
2828 QRect center_rect = rect;
2829 if (!docks[QInternal::LeftDock].isEmpty())
2830 center_rect.setLeft(rect.left() + docks[QInternal::LeftDock].rect.width() + sep);
2831 if (!docks[QInternal::TopDock].isEmpty())
2832 center_rect.setTop(rect.top() + docks[QInternal::TopDock].rect.height() + sep);
2833 if (!docks[QInternal::RightDock].isEmpty())
2834 center_rect.setRight(rect.right() - docks[QInternal::RightDock].rect.width() - sep);
2835 if (!docks[QInternal::BottomDock].isEmpty())
2836 center_rect.setBottom(rect.bottom() - docks[QInternal::BottomDock].rect.height() - sep);
2837
2838 QSize left_hint = docks[QInternal::LeftDock].size();
2839 if (left_hint.isNull() || fallbackToSizeHints)
2840 left_hint = docks[QInternal::LeftDock].sizeHint();
2841 QSize left_min = docks[QInternal::LeftDock].minimumSize();
2842 QSize left_max = docks[QInternal::LeftDock].maximumSize();
2843 left_hint = left_hint.boundedTo(left_max).expandedTo(left_min);
2844
2845 QSize right_hint = docks[QInternal::RightDock].size();
2846 if (right_hint.isNull() || fallbackToSizeHints)
2847 right_hint = docks[QInternal::RightDock].sizeHint();
2848 QSize right_min = docks[QInternal::RightDock].minimumSize();
2849 QSize right_max = docks[QInternal::RightDock].maximumSize();
2850 right_hint = right_hint.boundedTo(right_max).expandedTo(right_min);
2851
2852 QSize top_hint = docks[QInternal::TopDock].size();
2853 if (top_hint.isNull() || fallbackToSizeHints)
2854 top_hint = docks[QInternal::TopDock].sizeHint();
2855 QSize top_min = docks[QInternal::TopDock].minimumSize();
2856 QSize top_max = docks[QInternal::TopDock].maximumSize();
2857 top_hint = top_hint.boundedTo(top_max).expandedTo(top_min);
2858
2859 QSize bottom_hint = docks[QInternal::BottomDock].size();
2860 if (bottom_hint.isNull() || fallbackToSizeHints)
2861 bottom_hint = docks[QInternal::BottomDock].sizeHint();
2862 QSize bottom_min = docks[QInternal::BottomDock].minimumSize();
2863 QSize bottom_max = docks[QInternal::BottomDock].maximumSize();
2864 bottom_hint = bottom_hint.boundedTo(bottom_max).expandedTo(bottom_min);
2865
2866 if (_ver_struct_list != nullptr) {
2867 QList<QLayoutStruct> &ver_struct_list = *_ver_struct_list;
2868 ver_struct_list.resize(3);
2869
2870 // top --------------------------------------------------
2871 ver_struct_list[0].init();
2872 ver_struct_list[0].stretch = 0;
2873 ver_struct_list[0].sizeHint = top_hint.height();
2874 ver_struct_list[0].minimumSize = top_min.height();
2875 ver_struct_list[0].maximumSize = top_max.height();
2876 ver_struct_list[0].expansive = false;
2877 ver_struct_list[0].empty = docks[QInternal::TopDock].isEmpty();
2878 ver_struct_list[0].pos = docks[QInternal::TopDock].rect.top();
2879 ver_struct_list[0].size = docks[QInternal::TopDock].rect.height();
2880
2881 // center --------------------------------------------------
2882 ver_struct_list[1].init();
2883 ver_struct_list[1].stretch = center_hint.height();
2884
2885 bool tl_significant = corners[Qt::TopLeftCorner] == Qt::TopDockWidgetArea
2886 || docks[QInternal::TopDock].isEmpty();
2887 bool bl_significant = corners[Qt::BottomLeftCorner] == Qt::BottomDockWidgetArea
2888 || docks[QInternal::BottomDock].isEmpty();
2889 bool tr_significant = corners[Qt::TopRightCorner] == Qt::TopDockWidgetArea
2890 || docks[QInternal::TopDock].isEmpty();
2891 bool br_significant = corners[Qt::BottomRightCorner] == Qt::BottomDockWidgetArea
2892 || docks[QInternal::BottomDock].isEmpty();
2893
2894 int left = (tl_significant && bl_significant) ? left_hint.height() : 0;
2895 int right = (tr_significant && br_significant) ? right_hint.height() : 0;
2896 ver_struct_list[1].sizeHint = qMax(left, center_hint.height(), right);
2897
2898 left = (tl_significant && bl_significant) ? left_min.height() : 0;
2899 right = (tr_significant && br_significant) ? right_min.height() : 0;
2900 ver_struct_list[1].minimumSize = qMax(left, center_min.height(), right);
2901 ver_struct_list[1].maximumSize = center_max.height();
2902 ver_struct_list[1].expansive = have_central;
2903 ver_struct_list[1].empty = docks[QInternal::LeftDock].isEmpty()
2904 && !have_central
2905 && docks[QInternal::RightDock].isEmpty();
2906 ver_struct_list[1].pos = center_rect.top();
2907 ver_struct_list[1].size = center_rect.height();
2908
2909 // bottom --------------------------------------------------
2910 ver_struct_list[2].init();
2911 ver_struct_list[2].stretch = 0;
2912 ver_struct_list[2].sizeHint = bottom_hint.height();
2913 ver_struct_list[2].minimumSize = bottom_min.height();
2914 ver_struct_list[2].maximumSize = bottom_max.height();
2915 ver_struct_list[2].expansive = false;
2916 ver_struct_list[2].empty = docks[QInternal::BottomDock].isEmpty();
2917 ver_struct_list[2].pos = docks[QInternal::BottomDock].rect.top();
2918 ver_struct_list[2].size = docks[QInternal::BottomDock].rect.height();
2919
2920 for (int i = 0; i < 3; ++i) {
2921 ver_struct_list[i].sizeHint
2922 = qMax(ver_struct_list[i].sizeHint, ver_struct_list[i].minimumSize);
2923 }
2924 if (have_central && ver_struct_list[0].empty && ver_struct_list[2].empty)
2925 ver_struct_list[1].maximumSize = QWIDGETSIZE_MAX;
2926 }
2927
2928 if (_hor_struct_list != nullptr) {
2929 QList<QLayoutStruct> &hor_struct_list = *_hor_struct_list;
2930 hor_struct_list.resize(3);
2931
2932 // left --------------------------------------------------
2933 hor_struct_list[0].init();
2934 hor_struct_list[0].stretch = 0;
2935 hor_struct_list[0].sizeHint = left_hint.width();
2936 hor_struct_list[0].minimumSize = left_min.width();
2937 hor_struct_list[0].maximumSize = left_max.width();
2938 hor_struct_list[0].expansive = false;
2939 hor_struct_list[0].empty = docks[QInternal::LeftDock].isEmpty();
2940 hor_struct_list[0].pos = docks[QInternal::LeftDock].rect.left();
2941 hor_struct_list[0].size = docks[QInternal::LeftDock].rect.width();
2942
2943 // center --------------------------------------------------
2944 hor_struct_list[1].init();
2945 hor_struct_list[1].stretch = center_hint.width();
2946
2947 bool tl_significant = corners[Qt::TopLeftCorner] == Qt::LeftDockWidgetArea
2948 || docks[QInternal::LeftDock].isEmpty();
2949 bool tr_significant = corners[Qt::TopRightCorner] == Qt::RightDockWidgetArea
2950 || docks[QInternal::RightDock].isEmpty();
2951 bool bl_significant = corners[Qt::BottomLeftCorner] == Qt::LeftDockWidgetArea
2952 || docks[QInternal::LeftDock].isEmpty();
2953 bool br_significant = corners[Qt::BottomRightCorner] == Qt::RightDockWidgetArea
2954 || docks[QInternal::RightDock].isEmpty();
2955
2956 int top = (tl_significant && tr_significant) ? top_hint.width() : 0;
2957 int bottom = (bl_significant && br_significant) ? bottom_hint.width() : 0;
2958 hor_struct_list[1].sizeHint = qMax(top, center_hint.width(), bottom);
2959
2960 top = (tl_significant && tr_significant) ? top_min.width() : 0;
2961 bottom = (bl_significant && br_significant) ? bottom_min.width() : 0;
2962 hor_struct_list[1].minimumSize = qMax(top, center_min.width(), bottom);
2963
2964 hor_struct_list[1].maximumSize = center_max.width();
2965 hor_struct_list[1].expansive = have_central;
2966 hor_struct_list[1].empty = !have_central;
2967 hor_struct_list[1].pos = center_rect.left();
2968 hor_struct_list[1].size = center_rect.width();
2969
2970 // right --------------------------------------------------
2971 hor_struct_list[2].init();
2972 hor_struct_list[2].stretch = 0;
2973 hor_struct_list[2].sizeHint = right_hint.width();
2974 hor_struct_list[2].minimumSize = right_min.width();
2975 hor_struct_list[2].maximumSize = right_max.width();
2976 hor_struct_list[2].expansive = false;
2977 hor_struct_list[2].empty = docks[QInternal::RightDock].isEmpty();
2978 hor_struct_list[2].pos = docks[QInternal::RightDock].rect.left();
2979 hor_struct_list[2].size = docks[QInternal::RightDock].rect.width();
2980
2981 for (int i = 0; i < 3; ++i) {
2982 hor_struct_list[i].sizeHint
2983 = qMax(hor_struct_list[i].sizeHint, hor_struct_list[i].minimumSize);
2984 }
2985 if (have_central && hor_struct_list[0].empty && hor_struct_list[2].empty)
2986 hor_struct_list[1].maximumSize = QWIDGETSIZE_MAX;
2987
2988 }
2989}
2990
2991void QDockAreaLayout::setGrid(QList<QLayoutStruct> *ver_struct_list,
2992 QList<QLayoutStruct> *hor_struct_list)
2993{
2994
2995 // top ---------------------------------------------------
2996
2997 if (!docks[QInternal::TopDock].isEmpty()) {
2998 QRect r = docks[QInternal::TopDock].rect;
2999 if (hor_struct_list != nullptr) {
3000 r.setLeft(corners[Qt::TopLeftCorner] == Qt::TopDockWidgetArea
3001 || docks[QInternal::LeftDock].isEmpty()
3002 ? rect.left() : hor_struct_list->at(1).pos);
3003 r.setRight(corners[Qt::TopRightCorner] == Qt::TopDockWidgetArea
3004 || docks[QInternal::RightDock].isEmpty()
3005 ? rect.right() : hor_struct_list->at(2).pos - sep - 1);
3006 }
3007 if (ver_struct_list != nullptr) {
3008 r.setTop(rect.top());
3009 r.setBottom(ver_struct_list->at(1).pos - sep - 1);
3010 }
3011 docks[QInternal::TopDock].rect = r;
3012 docks[QInternal::TopDock].fitItems();
3013 }
3014
3015 // bottom ---------------------------------------------------
3016
3017 if (!docks[QInternal::BottomDock].isEmpty()) {
3018 QRect r = docks[QInternal::BottomDock].rect;
3019 if (hor_struct_list != nullptr) {
3020 r.setLeft(corners[Qt::BottomLeftCorner] == Qt::BottomDockWidgetArea
3021 || docks[QInternal::LeftDock].isEmpty()
3022 ? rect.left() : hor_struct_list->at(1).pos);
3023 r.setRight(corners[Qt::BottomRightCorner] == Qt::BottomDockWidgetArea
3024 || docks[QInternal::RightDock].isEmpty()
3025 ? rect.right() : hor_struct_list->at(2).pos - sep - 1);
3026 }
3027 if (ver_struct_list != nullptr) {
3028 r.setTop(ver_struct_list->at(2).pos);
3029 r.setBottom(rect.bottom());
3030 }
3031 docks[QInternal::BottomDock].rect = r;
3032 docks[QInternal::BottomDock].fitItems();
3033 }
3034
3035 // left ---------------------------------------------------
3036
3037 if (!docks[QInternal::LeftDock].isEmpty()) {
3038 QRect r = docks[QInternal::LeftDock].rect;
3039 if (hor_struct_list != nullptr) {
3040 r.setLeft(rect.left());
3041 r.setRight(hor_struct_list->at(1).pos - sep - 1);
3042 }
3043 if (ver_struct_list != nullptr) {
3044 r.setTop(corners[Qt::TopLeftCorner] == Qt::LeftDockWidgetArea
3045 || docks[QInternal::TopDock].isEmpty()
3046 ? rect.top() : ver_struct_list->at(1).pos);
3047 r.setBottom(corners[Qt::BottomLeftCorner] == Qt::LeftDockWidgetArea
3048 || docks[QInternal::BottomDock].isEmpty()
3049 ? rect.bottom() : ver_struct_list->at(2).pos - sep - 1);
3050 }
3051 docks[QInternal::LeftDock].rect = r;
3052 docks[QInternal::LeftDock].fitItems();
3053 }
3054
3055 // right ---------------------------------------------------
3056
3057 if (!docks[QInternal::RightDock].isEmpty()) {
3058 QRect r = docks[QInternal::RightDock].rect;
3059 if (hor_struct_list != nullptr) {
3060 r.setLeft(hor_struct_list->at(2).pos);
3061 r.setRight(rect.right());
3062 }
3063 if (ver_struct_list != nullptr) {
3064 r.setTop(corners[Qt::TopRightCorner] == Qt::RightDockWidgetArea
3065 || docks[QInternal::TopDock].isEmpty()
3066 ? rect.top() : ver_struct_list->at(1).pos);
3067 r.setBottom(corners[Qt::BottomRightCorner] == Qt::RightDockWidgetArea
3068 || docks[QInternal::BottomDock].isEmpty()
3069 ? rect.bottom() : ver_struct_list->at(2).pos - sep - 1);
3070 }
3071 docks[QInternal::RightDock].rect = r;
3072 docks[QInternal::RightDock].fitItems();
3073 }
3074
3075 // center ---------------------------------------------------
3076
3077 if (hor_struct_list != nullptr) {
3078 centralWidgetRect.setLeft(hor_struct_list->at(1).pos);
3079 centralWidgetRect.setWidth(hor_struct_list->at(1).size);
3080 }
3081 if (ver_struct_list != nullptr) {
3082 centralWidgetRect.setTop(ver_struct_list->at(1).pos);
3083 centralWidgetRect.setHeight(ver_struct_list->at(1).size);
3084 }
3085}
3086
3087void QDockAreaLayout::fitLayout()
3088{
3089 QList<QLayoutStruct> ver_struct_list(3);
3090 QList<QLayoutStruct> hor_struct_list(3);
3091 getGrid(&ver_struct_list, &hor_struct_list);
3092
3093 qGeomCalc(ver_struct_list, 0, 3, rect.top(), rect.height(), sep);
3094 qGeomCalc(hor_struct_list, 0, 3, rect.left(), rect.width(), sep);
3095
3096 setGrid(&ver_struct_list, &hor_struct_list);
3097}
3098
3099void QDockAreaLayout::clear()
3100{
3101 for (auto &dock : docks)
3102 dock.clear();
3103
3104 rect = QRect();
3105 centralWidgetRect = QRect();
3106}
3107
3108template<typename SizePMF, typename CenterPMF>
3109QSize QDockAreaLayout::size_helper(SizePMF sizeFn, CenterPMF centerFn) const
3110{
3111 int left_sep = 0;
3112 int right_sep = 0;
3113 int top_sep = 0;
3114 int bottom_sep = 0;
3115
3116 if (centralWidgetItem != nullptr) {
3117 left_sep = docks[QInternal::LeftDock].isEmpty() ? 0 : sep;
3118 right_sep = docks[QInternal::RightDock].isEmpty() ? 0 : sep;
3119 top_sep = docks[QInternal::TopDock].isEmpty() ? 0 : sep;
3120 bottom_sep = docks[QInternal::BottomDock].isEmpty() ? 0 : sep;
3121 }
3122
3123 const QSize left = (docks[QInternal::LeftDock].*sizeFn)() + QSize(left_sep, 0);
3124 const QSize right = (docks[QInternal::RightDock].*sizeFn)() + QSize(right_sep, 0);
3125 const QSize top = (docks[QInternal::TopDock].*sizeFn)() + QSize(0, top_sep);
3126 const QSize bottom = (docks[QInternal::BottomDock].*sizeFn)() + QSize(0, bottom_sep);
3127 const QSize center = centralWidgetItem == nullptr
3128 ? QSize(0, 0) : (centralWidgetItem->*centerFn)();
3129
3130 int row1 = top.width();
3131 int row2 = left.width() + center.width() + right.width();
3132 int row3 = bottom.width();
3133 int col1 = left.height();
3134 int col2 = top.height() + center.height() + bottom.height();
3135 int col3 = right.height();
3136
3137 if (corners[Qt::TopLeftCorner] == Qt::LeftDockWidgetArea)
3138 row1 += left.width();
3139 else
3140 col1 += top.height();
3141
3142 if (corners[Qt::TopRightCorner] == Qt::RightDockWidgetArea)
3143 row1 += right.width();
3144 else
3145 col3 += top.height();
3146
3147 if (corners[Qt::BottomLeftCorner] == Qt::LeftDockWidgetArea)
3148 row3 += left.width();
3149 else
3150 col1 += bottom.height();
3151
3152 if (corners[Qt::BottomRightCorner] == Qt::RightDockWidgetArea)
3153 row3 += right.width();
3154 else
3155 col3 += bottom.height();
3156
3157 return QSize(qMax(row1, row2, row3), qMax(col1, col2, col3));
3158}
3159
3160QSize QDockAreaLayout::sizeHint() const
3161{
3162 return size_helper(&QDockAreaLayoutInfo::sizeHint, &QLayoutItem::sizeHint);
3163}
3164
3165QSize QDockAreaLayout::minimumSize() const
3166{
3167 return size_helper(&QDockAreaLayoutInfo::minimumSize, &QLayoutItem::minimumSize);
3168}
3169
3170/*!
3171 \internal
3172
3173 Returns the smallest size that doesn't change the size of any of the dock areas.
3174*/
3175QSize QDockAreaLayout::minimumStableSize() const
3176{
3177 return size_helper(&QDockAreaLayoutInfo::size, &QLayoutItem::minimumSize);
3178}
3179
3180/*! \internal
3181 Try to fit the given rectangle \a rect on the screen which contains
3182 the window \a widget.
3183 Used to compute the geometry of a dragged a dock widget that should
3184 be shown with \a rect, but needs to be visible on the screen
3185 */
3186QRect QDockAreaLayout::constrainedRect(QRect rect, QWidget* widget)
3187{
3188 QScreen *screen = nullptr;
3189 if (QGuiApplication::primaryScreen()->virtualSiblings().size() > 1)
3190 screen = QGuiApplication::screenAt(rect.topLeft());
3191 if (!screen)
3192 screen = widget->screen();
3193
3194 const QRect screenRect = screen->geometry();
3195 if (screenRect.isValid()) {
3196 rect.setWidth(qMin(rect.width(), screenRect.width()));
3197 rect.setHeight(qMin(rect.height(), screenRect.height()));
3198 rect.moveLeft(qMax(rect.left(), screenRect.left()));
3199 rect.moveTop(qMax(rect.top(), screenRect.top()));
3200 rect.moveRight(qMin(rect.right(), screenRect.right()));
3201 rect.moveBottom(qMin(rect.bottom(), screenRect.bottom()));
3202 }
3203
3204 return rect;
3205}
3206
3207bool QDockAreaLayout::restoreDockWidget(QDockWidget *dockWidget)
3208{
3209 QDockAreaLayoutItem *item = nullptr;
3210 const auto groups =
3211 mainWindow->findChildren<QDockWidgetGroupWindow *>(Qt::FindDirectChildrenOnly);
3212 for (QDockWidgetGroupWindow *dwgw : groups) {
3213 QList<int> index = dwgw->layoutInfo()->indexOfPlaceHolder(dockWidget->objectName());
3214 if (!index.isEmpty()) {
3215 dockWidget->setParent(dwgw);
3216 item = const_cast<QDockAreaLayoutItem *>(&dwgw->layoutInfo()->item(index));
3217 break;
3218 }
3219 }
3220 if (!item) {
3221 QList<int> index = indexOfPlaceHolder(dockWidget->objectName());
3222 if (index.isEmpty())
3223 return false;
3224 item = const_cast<QDockAreaLayoutItem *>(&this->item(index));
3225 }
3226
3227 QPlaceHolderItem *placeHolder = item->placeHolderItem;
3228 Q_ASSERT(placeHolder != nullptr);
3229
3230 item->widgetItem = new QDockWidgetItem(dockWidget);
3231
3232 if (placeHolder->window) {
3233 const QRect r = constrainedRect(placeHolder->topLevelRect, dockWidget);
3234 dockWidget->d_func()->setWindowState(QDockWidgetPrivate::WindowState::Floating, r);
3235 }
3236 dockWidget->setVisible(!placeHolder->hidden);
3237
3238 item->placeHolderItem = nullptr;
3239 delete placeHolder;
3240
3241 return true;
3242}
3243
3244void QDockAreaLayout::addDockWidget(QInternal::DockPosition pos, QDockWidget *dockWidget,
3245 Qt::Orientation orientation)
3246{
3247 QLayoutItem *dockWidgetItem = new QDockWidgetItem(dockWidget);
3248 Q_ASSERT(size_t(pos) < docks.size());
3249 QDockAreaLayoutInfo &info = docks[pos];
3250 if (orientation == info.o || info.item_list.size() <= 1) {
3251 // empty dock areas, or dock areas containing exactly one widget can have their orientation
3252 // switched.
3253 info.o = orientation;
3254
3255 QDockAreaLayoutItem new_item(dockWidgetItem);
3256 info.item_list.append(new_item);
3257#if QT_CONFIG(tabbar)
3258 if (info.tabbed && !new_item.skip()) {
3259 info.updateTabBar();
3260 info.setCurrentTabId(tabId(new_item));
3261 }
3262#endif
3263 } else {
3264#if QT_CONFIG(tabbar)
3265 int tbshape = info.tabBarShape;
3266#else
3267 int tbshape = 0;
3268#endif
3269 QDockAreaLayoutInfo new_info(&sep, pos, orientation, tbshape, mainWindow);
3270 new_info.item_list.append(QDockAreaLayoutItem(new QDockAreaLayoutInfo(info)));
3271 new_info.item_list.append(QDockAreaLayoutItem(dockWidgetItem));
3272 info = new_info;
3273 }
3274
3275 removePlaceHolder(dockWidget->objectName());
3276}
3277
3278#if QT_CONFIG(tabbar)
3279void QDockAreaLayout::tabifyDockWidget(QDockWidget *first, QDockWidget *second)
3280{
3281 const QList<int> path = indexOf(first);
3282 if (path.isEmpty())
3283 return;
3284
3285 QDockAreaLayoutInfo *info = this->info(path);
3286 Q_ASSERT(info != nullptr);
3287 info->tab(path.last(), new QDockWidgetItem(second));
3288
3289 removePlaceHolder(second->objectName());
3290}
3291#endif // QT_CONFIG(tabbar)
3292
3293void QDockAreaLayout::resizeDocks(const QList<QDockWidget *> &docks,
3294 const QList<int> &sizes, Qt::Orientation o)
3295{
3296 if (Q_UNLIKELY(docks.size() != sizes.size())) {
3297 qWarning("QMainWidget::resizeDocks: size of the lists are not the same");
3298 return;
3299 }
3300 const int count = docks.size();
3301 fallbackToSizeHints = false;
3302 for (int i = 0; i < count; ++i) {
3303 QList<int> path = indexOf(docks[i]);
3304 if (Q_UNLIKELY(path.isEmpty())) {
3305 qWarning("QMainWidget::resizeDocks: one QDockWidget is not part of the layout");
3306 continue;
3307 }
3308 int size = sizes[i];
3309 if (Q_UNLIKELY(size <= 0)) {
3310 qWarning("QMainWidget::resizeDocks: all sizes need to be larger than 0");
3311 size = 1;
3312 }
3313
3314 while (path.size() > 1) {
3315 QDockAreaLayoutInfo *info = this->info(path);
3316#if QT_CONFIG(tabbar)
3317 if (!info->tabbed && info->o == o) {
3318 info->item_list[path.constLast()].size = size;
3319 int totalSize = 0;
3320 for (const QDockAreaLayoutItem &item : std::as_const(info->item_list)) {
3321 if (!item.skip()) {
3322 if (totalSize != 0)
3323 totalSize += sep;
3324 totalSize += item.size == -1 ? pick(o, item.sizeHint()) : item.size;
3325 }
3326 }
3327 size = totalSize;
3328 }
3329#endif // QT_CONFIG(tabbar)
3330 path.removeLast();
3331 }
3332
3333 const int dockNum = path.constFirst();
3334 Q_ASSERT(std::size_t(dockNum) < this->docks.size());
3335 QRect &r = this->docks[dockNum].rect;
3336 QSize s = r.size();
3337 rpick(o, s) = size;
3338 r.setSize(s);
3339 }
3340}
3341
3342void QDockAreaLayout::splitDockWidget(QDockWidget *after,
3343 QDockWidget *dockWidget,
3344 Qt::Orientation orientation)
3345{
3346 const QList<int> path = indexOf(after);
3347 if (path.isEmpty())
3348 return;
3349
3350 QDockAreaLayoutInfo *info = this->info(path);
3351 Q_ASSERT(info != nullptr);
3352 info->split(path.last(), orientation, new QDockWidgetItem(dockWidget));
3353
3354 removePlaceHolder(dockWidget->objectName());
3355}
3356
3357void QDockAreaLayout::apply(QWidgetAnimator::AnimationRule rule)
3358{
3359 QWidgetAnimator &widgetAnimator = qt_mainwindow_layout(mainWindow)->widgetAnimator;
3360
3361 for (auto &dock : docks)
3362 dock.apply(rule);
3363 if (centralWidgetItem != nullptr && !centralWidgetItem->isEmpty()) {
3364 widgetAnimator.animate(centralWidgetItem->widget(), centralWidgetRect,
3365 rule);
3366 }
3367#if QT_CONFIG(tabbar)
3368 if (sep == 1)
3369 updateSeparatorWidgets();
3370#endif // QT_CONFIG(tabbar)
3371}
3372
3373void QDockAreaLayout::paintSeparators(QPainter *p, QWidget *widget,
3374 const QRegion &clip,
3375 const QPoint &mouse) const
3376{
3377 for (auto &dock : docks) {
3378 if (dock.isEmpty())
3379 continue;
3380 const QRect r = dock.separatorRect();
3381 if (clip.contains(r) && !dock.hasFixedSize()) {
3382 Qt::Orientation opposite = dock.o == Qt::Horizontal
3383 ? Qt::Vertical : Qt::Horizontal;
3384 paintSep(p, widget, r, opposite, r.contains(mouse));
3385 }
3386 if (clip.contains(dock.rect))
3387 dock.paintSeparators(p, widget, clip, mouse);
3388 }
3389}
3390
3391QRegion QDockAreaLayout::separatorRegion() const
3392{
3393 QRegion result;
3394
3395 for (auto &dock : docks) {
3396 if (dock.isEmpty())
3397 continue;
3398 result |= dock.separatorRect();
3399 result |= dock.separatorRegion();
3400 }
3401
3402 return result;
3403}
3404
3405int QDockAreaLayout::separatorMove(const QList<int> &separator, const QPoint &origin,
3406 const QPoint &dest)
3407{
3408 int delta = 0;
3409 const auto dockPosition = static_cast<QInternal::DockPosition>(separator.last());
3410 const bool isHorizontal =
3411 dockPosition == QInternal::LeftDock || dockPosition == QInternal::RightDock;
3412 const bool isLeftOrTop = dockPosition == QInternal::LeftDock || dockPosition == QInternal::TopDock;
3413 const bool separatorIsWithinDock = separator.size() > 1;
3414
3415 if (separatorIsWithinDock) {
3416 // The dock area contains more than one dock widget and therefore an internal separator,
3417 // which is being moved. The move changes the sizes of the dock widgets docked in the dock area.
3418 // The dock area's geometry remains unchanged.
3419 QDockAreaLayoutInfo *info = this->info(separator);
3420 delta = pick(info->o, dest - origin);
3421 if (delta != 0)
3422 delta = info->separatorMove(dockPosition, delta);
3423 info->apply(QWidgetAnimator::AnimationRule::Stop);
3424 return delta;
3425 }
3426
3427 // The dock area's external separator is moved. The move changes the size of the contained
3428 // dock widgets, as well as the size of neighbouring dock areas and their docked dock widgets.
3429 // If the move shrinks the contained dock widgets to their minimum size,
3430 // the entire dock area will attempted to be moved, retaining the minumum size.
3431 // If the contained dock widgets have to be shrunk below minimum size, they will be collapsed.
3432
3433 QList<QLayoutStruct> list;
3434
3435 if (isHorizontal)
3436 getGrid(nullptr, &list);
3437 else
3438 getGrid(&list, nullptr);
3439
3440 const int sep_index = isLeftOrTop ? 0 : 1;
3441 const Qt::Orientation o = isHorizontal ? Qt::Horizontal : Qt::Vertical;
3442
3443 delta = pick(o, dest - origin);
3444 delta = separatorMoveHelper(list, sep_index, delta, sep);
3445
3446 fallbackToSizeHints = false;
3447
3448 if (isHorizontal)
3449 setGrid(nullptr, &list);
3450 else
3451 setGrid(&list, nullptr);
3452
3453 apply(QWidgetAnimator::AnimationRule::Stop);
3454
3455 return delta;
3456}
3457
3458int QDockAreaLayoutInfo::separatorMove(const QList<int> &separator, const QPoint &origin,
3459 const QPoint &dest)
3460{
3461 int delta = 0;
3462 int index = separator.last();
3463 QDockAreaLayoutInfo *info = this->info(separator);
3464 delta = pick(info->o, dest - origin);
3465 if (delta != 0)
3466 delta = info->separatorMove(index, delta);
3467 info->apply(QWidgetAnimator::AnimationRule::Stop);
3468 return delta;
3469}
3470
3471#if QT_CONFIG(tabbar)
3472// Sets the correct positions for the separator widgets
3473// Allocates new separator widgets with getSeparatorWidget
3474void QDockAreaLayout::updateSeparatorWidgets() const
3475{
3476 int j = 0;
3477
3478 for (auto &dock : docks) {
3479 if (dock.isEmpty())
3480 continue;
3481
3482 QWidget *sepWidget;
3483 if (j < separatorWidgets.size()) {
3484 sepWidget = separatorWidgets.at(j);
3485 if (!sepWidget) {
3486 qWarning("QDockAreaLayout::updateSeparatorWidgets: null separator widget");
3487 sepWidget = qt_mainwindow_layout(mainWindow)->getSeparatorWidget();
3488 separatorWidgets[j] = sepWidget;
3489 }
3490 } else {
3491 sepWidget = qt_mainwindow_layout(mainWindow)->getSeparatorWidget();
3492 separatorWidgets.append(sepWidget);
3493 }
3494 j++;
3495
3496 Q_ASSERT(sepWidget);
3497 raiseSeparatorWidget(sepWidget);
3498
3499 const QRect sr = dock.separatorRect();
3500 const QRect sepRect = sr.adjusted(-2, -2, 2, 2);
3501 sepWidget->setGeometry(sepRect);
3502 sepWidget->setMask( QRegion(sr.translated( - sepRect.topLeft())));
3503 sepWidget->show();
3504 }
3505 for (int i = j; i < separatorWidgets.size(); ++i)
3506 separatorWidgets.at(i)->hide();
3507
3508 separatorWidgets.resize(j);
3509}
3510#endif // QT_CONFIG(tabbar)
3511
3512QLayoutItem *QDockAreaLayout::itemAt(int *x, int index) const
3513{
3514 Q_ASSERT(x != nullptr);
3515
3516 for (const auto &dock : docks) {
3517 if (QLayoutItem *ret = dock.itemAt(x, index))
3518 return ret;
3519 }
3520
3521 if (centralWidgetItem && (*x)++ == index)
3522 return centralWidgetItem;
3523
3524 return nullptr;
3525}
3526
3527QLayoutItem *QDockAreaLayout::takeAt(int *x, int index)
3528{
3529 Q_ASSERT(x != nullptr);
3530
3531 for (auto &dock : docks) {
3532 if (QLayoutItem *ret = dock.takeAt(x, index))
3533 return ret;
3534 }
3535
3536 if (centralWidgetItem && (*x)++ == index) {
3537 QLayoutItem *ret = centralWidgetItem;
3538 centralWidgetItem = nullptr;
3539 return ret;
3540 }
3541
3542 return nullptr;
3543}
3544
3545void QDockAreaLayout::deleteAllLayoutItems()
3546{
3547 for (auto &dock : docks)
3548 dock.deleteAllLayoutItems();
3549}
3550
3551#if QT_CONFIG(tabbar)
3552QSet<QTabBar*> QDockAreaLayout::usedTabBars() const
3553{
3554 QSet<QTabBar*> result;
3555 for (const auto &dock : docks)
3556 result += dock.usedTabBars();
3557 return result;
3558}
3559
3560// Returns the set of all used separator widgets
3561QSet<QWidget*> QDockAreaLayout::usedSeparatorWidgets() const
3562{
3563 QSet<QWidget*> result;
3564 const int numSeparators = separatorWidgets.size();
3565 result.reserve(numSeparators);
3566 for (int i = 0; i < numSeparators; ++i)
3567 result << separatorWidgets.at(i);
3568 for (const auto &dock : docks)
3569 result += dock.usedSeparatorWidgets();
3570 return result;
3571}
3572#endif
3573
3574QRect QDockAreaLayout::gapRect(const QList<int> &path) const
3575{
3576 const QDockAreaLayoutInfo *info = this->info(path);
3577 if (info == nullptr)
3578 return QRect();
3579 int index = path.last();
3580 if (index < 0 || index >= info->item_list.size())
3581 return QRect();
3582 return info->itemRect(index, true);
3583}
3584
3585void QDockAreaLayout::keepSize(QDockWidget *w)
3586{
3587 QList<int> path = indexOf(w);
3588 if (path.isEmpty())
3589 return;
3590 QDockAreaLayoutItem &item = this->item(path);
3591 if (item.size != -1)
3592 item.flags |= QDockAreaLayoutItem::KeepSize;
3593}
3594
3595void QDockAreaLayout::styleChangedEvent()
3596{
3597 sep = mainWindow->style()->pixelMetric(QStyle::PM_DockWidgetSeparatorExtent, nullptr, mainWindow);
3598 if (isValid())
3599 fitLayout();
3600}
3601
3602QT_END_NAMESPACE
friend class QWidget
Definition qpainter.h:432
static int grow(QLayoutStruct &ls, int delta)
static QRect dockedGeometry(QWidget *widget)
static Qt::DockWidgetArea toDockWidgetArea(QInternal::DockPosition pos)
static int realMinSize(const QDockAreaLayoutInfo &info)
static int qMax(int i1, int i2, int i3)
static int shrink(QLayoutStruct &ls, int delta)
static std::optional< QInternal::DockPosition > dockPosHelper(const QRect &rect, const QPoint &_pos, Qt::Orientation o, bool nestingEnabled, QDockAreaLayoutInfo::TabMode tabMode)
Q_DECLARE_FLAGS(StateFlags, StateFlag)
static const int zero
static int separatorMoveHelper(QList< QLayoutStruct > &list, int index, int delta, int sep)
static void paintSep(QPainter *p, QWidget *w, const QRect &r, Qt::Orientation o, bool mouse_over)
static int realMaxSize(const QDockAreaLayoutInfo &info)
QMainWindowLayout * qt_mainwindow_layout(const QMainWindow *window)
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")