forked from KDAB/KDDockWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockWidgetBase.cpp
945 lines (775 loc) · 24.8 KB
/
DockWidgetBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2019-2022 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
Author: Sérgio Martins <[email protected]>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <[email protected]> for commercial licensing options.
*/
#include "DockWidgetBase.h"
#include "private/DockWidgetBase_p.h"
#include "private/DockRegistry_p.h"
#include "private/FloatingWindow_p.h"
#include "private/Frame_p.h"
#include "private/LayoutSaver_p.h"
#include "private/Logging_p.h"
#include "private/MDILayoutWidget_p.h"
#include "private/SideBar_p.h"
#include "private/TitleBar_p.h"
#include "private/Utils_p.h"
#include "private/WindowBeingDragged_p.h"
#include "private/Position_p.h"
#include "Config.h"
#include "FrameworkWidgetFactory.h"
#include <QEvent>
#include <QCloseEvent>
#include <QTimer>
#include <QScopedValueRollback>
/**
* @file
* @brief The DockWidget base-class that's shared between QtWidgets and QtQuick stack.
*
* @author Sérgio Martins \<[email protected]\>
*/
using namespace KDDockWidgets;
DockWidgetBase::DockWidgetBase(const QString &name, Options options,
LayoutSaverOptions layoutSaverOptions)
: QWidgetAdapter(nullptr, Qt::Tool)
, d(new Private(name, options, layoutSaverOptions, this))
{
d->init();
DockRegistry::self()->registerDockWidget(this);
if (name.isEmpty())
qWarning() << Q_FUNC_INFO << "Name can't be null";
setAttribute(Qt::WA_PendingMoveEvent, false);
}
DockWidgetBase::~DockWidgetBase()
{
DockRegistry::self()->unregisterDockWidget(this);
delete d;
}
void DockWidgetBase::addDockWidgetAsTab(DockWidgetBase *other, InitialOption option)
{
if (other == this) {
qWarning() << Q_FUNC_INFO << "Refusing to add dock widget into itself" << other;
return;
}
if (!other) {
qWarning() << Q_FUNC_INFO << "dock widget is null";
return;
}
if (!DockRegistry::self()->affinitiesMatch(other->affinities(), d->affinities)) {
qWarning() << Q_FUNC_INFO << "Refusing to dock widget with incompatible affinity."
<< other->affinities() << affinities();
return;
}
if ((other->options() & DockWidgetBase::Option_NotDockable) || (options() & DockWidgetBase::Option_NotDockable)) {
qWarning() << Q_FUNC_INFO << "Refusing to dock non-dockable widget" << other;
return;
}
if (isPersistentCentralDockWidget()) {
qWarning() << Q_FUNC_INFO << "Not supported with MainWindowOption_HasCentralWidget."
<< "MainWindowOption_HasCentralWidget can only have 1 widget in the center."
<< "Use MainWindowOption_HasCentralFrame instead, which is similar but supports tabbing.";
return;
}
Frame *frame = d->frame();
if (frame) {
if (frame->containsDockWidget(other)) {
qWarning() << Q_FUNC_INFO << "Already contains" << other;
return;
}
} else {
if (isWindow()) {
// Doesn't have a frame yet
d->morphIntoFloatingWindow();
frame = d->frame();
} else {
// Doesn't happen
qWarning() << Q_FUNC_INFO << "null frame";
return;
}
}
other->setParent(nullptr);
frame->addWidget(other, option);
}
void DockWidgetBase::addDockWidgetToContainingWindow(DockWidgetBase *other,
Location location,
DockWidgetBase *relativeTo,
InitialOption initialOption)
{
if (auto mainWindow = qobject_cast<MainWindowBase *>(window())) {
// It's inside a main window. Simply use the main window API.
mainWindow->addDockWidget(other, location, relativeTo, initialOption);
return;
}
if (!DockRegistry::self()->affinitiesMatch(other->affinities(), d->affinities)) {
qWarning() << Q_FUNC_INFO << "Refusing to dock widget with incompatible affinity."
<< other->affinities() << affinities();
return;
}
if ((other->options() & DockWidgetBase::Option_NotDockable) || (options() & DockWidgetBase::Option_NotDockable)) {
qWarning() << Q_FUNC_INFO << "Refusing to dock non-dockable widget" << other;
return;
}
if (isWindow())
d->morphIntoFloatingWindow();
if (auto fw = floatingWindow()) {
fw->addDockWidget(other, location, relativeTo, initialOption);
} else {
qWarning() << Q_FUNC_INFO << "Couldn't find floating nested window";
}
}
void DockWidgetBase::setWidget(QWidgetOrQuick *w)
{
if (w == d->widget)
return;
if (d->widget) {
// Unparent the old widget, we're giving back ownership
d->widget->setParent(nullptr);
}
d->widget = w;
if (w)
setSizePolicy(w->sizePolicy());
Q_EMIT widgetChanged(w);
}
QWidgetOrQuick *DockWidgetBase::widget() const
{
return d->widget;
}
bool DockWidgetBase::isFloating() const
{
if (isWindow())
return true;
auto fw = floatingWindow();
return fw && fw->hasSingleDockWidget();
}
bool DockWidgetBase::setFloating(bool floats)
{
const bool alreadyFloating = isFloating();
if ((floats && alreadyFloating) || (!floats && !alreadyFloating))
return true; // Nothing to do
if (!floats && (Config::self().internalFlags() & Config::InternalFlag_DontShowWhenUnfloatingHiddenWindow) && !isVisible()) {
// Mimics behaviour of QDockWidget, which you might need during porting.
// Not something we suggest though. For KDDW, setFloating(false) means dock, and that implies showing.
return false;
}
if (floats && isPersistentCentralDockWidget())
return false;
if (floats) {
d->saveTabIndex();
if (isTabbed()) {
auto frame = d->frame();
if (!frame) {
qWarning() << "DockWidget::setFloating: Tabbed but no frame exists"
<< this;
Q_ASSERT(false);
return false;
}
frame->detachTab(this);
} else {
d->frame()->titleBar()->makeWindow();
}
auto lastGeo = d->lastPositions().lastFloatingGeometry();
if (lastGeo.isValid()) {
if (auto fw = floatingWindow())
fw->setSuggestedGeometry(lastGeo, SuggestedGeometryHint_PreserveCenter);
}
return true;
} else {
d->saveLastFloatingGeometry();
return d->restoreToPreviousPosition();
}
}
QAction *DockWidgetBase::toggleAction() const
{
return d->toggleAction;
}
QAction *DockWidgetBase::floatAction() const
{
return d->floatAction;
}
QString DockWidgetBase::uniqueName() const
{
return d->name;
}
QString DockWidgetBase::title() const
{
return d->title;
}
void DockWidgetBase::setTitle(const QString &title)
{
if (title != d->title) {
d->title = title;
d->updateTitle();
Q_EMIT titleChanged(title);
}
}
QRect DockWidgetBase::frameGeometry() const
{
if (Frame *f = d->frame())
return f->QWidgetAdapter::geometry();
// Means the dock widget isn't visible. Just fallback to its own geometry
return QWidgetAdapter::geometry();
}
DockWidgetBase::Options DockWidgetBase::options() const
{
return d->options;
}
DockWidgetBase::LayoutSaverOptions DockWidgetBase::layoutSaverOptions() const
{
return d->layoutSaverOptions;
}
void DockWidgetBase::setOptions(Options options)
{
if ((d->options & Option_NotDockable) != (options & Option_NotDockable)) {
qWarning() << Q_FUNC_INFO << "Option_NotDockable not allowed to change. Pass via ctor only.";
return;
}
if (options != d->options) {
d->options = options;
Q_EMIT optionsChanged(options);
if (auto tb = titleBar())
tb->updateButtons();
}
}
bool DockWidgetBase::isTabbed() const
{
if (Frame *frame = d->frame()) {
return frame->alwaysShowsTabs() || frame->dockWidgetCount() > 1;
} else {
if (!isFloating())
qWarning() << "DockWidget::isTabbed() Couldn't find any tab widget.";
return false;
}
}
bool DockWidgetBase::isCurrentTab() const
{
if (Frame *frame = d->frame()) {
return frame->currentIndex() == frame->indexOfDockWidget(const_cast<DockWidgetBase *>(this));
} else {
return true;
}
}
void DockWidgetBase::setAsCurrentTab()
{
if (Frame *frame = d->frame())
frame->setCurrentDockWidget(this);
}
int DockWidgetBase::tabIndex() const
{
if (Frame *frame = d->frame())
return frame->indexOfDockWidget(this);
return 0;
}
void DockWidgetBase::setIcon(const QIcon &icon, IconPlaces places)
{
if (places & IconPlace::TitleBar)
d->titleBarIcon = icon;
if (places & IconPlace::TabBar)
d->tabBarIcon = icon;
if (places & IconPlace::ToggleAction)
d->toggleAction->setIcon(icon);
Q_EMIT iconChanged();
}
QIcon DockWidgetBase::icon(IconPlace place) const
{
if (place == IconPlace::TitleBar)
return d->titleBarIcon;
if (place == IconPlace::TabBar)
return d->tabBarIcon;
if (place == IconPlace::ToggleAction)
return d->toggleAction->icon();
return {};
}
void DockWidgetBase::forceClose()
{
d->forceClose();
}
TitleBar *DockWidgetBase::titleBar() const
{
if (Frame *f = d->frame())
return f->actualTitleBar();
return nullptr;
}
bool DockWidgetBase::isOpen() const
{
return d->toggleAction->isChecked();
}
QStringList DockWidgetBase::affinities() const
{
return d->affinities;
}
void DockWidgetBase::show()
{
if (isWindow() && (d->m_lastPositions.wasFloating() || !d->m_lastPositions.isValid())) {
// Create the FloatingWindow already, instead of waiting for the show event.
// This reduces flickering on some platforms
d->morphIntoFloatingWindow();
} else {
QWidgetOrQuick::show();
}
}
void DockWidgetBase::raise()
{
if (!isOpen())
return;
setAsCurrentTab();
if (auto fw = floatingWindow()) {
fw->raise();
fw->activateWindow();
} else if (Frame *frame = d->frame()) {
if (frame->isMDI())
frame->raise();
}
}
bool DockWidgetBase::isMainWindow() const
{
return qobject_cast<MainWindowBase *>(widget());
}
bool DockWidgetBase::isInMainWindow() const
{
return d->mainWindow() != nullptr;
}
MainWindowBase *DockWidgetBase::mainWindow() const
{
return d->mainWindow();
}
bool DockWidgetBase::isFocused() const
{
auto f = d->frame();
return f && f->isFocused() && isCurrentTab();
}
void DockWidgetBase::setAffinityName(const QString &affinity)
{
setAffinities({ affinity });
}
void DockWidgetBase::setAffinities(const QStringList &affinityNames)
{
QStringList affinities = affinityNames;
affinities.removeAll(QString());
if (d->affinities == affinities)
return;
if (!d->affinities.isEmpty()) {
qWarning() << Q_FUNC_INFO
<< "Affinity is already set, refusing to change."
<< "Submit a feature request with a good justification.";
return;
}
d->affinities = affinities;
}
void DockWidgetBase::moveToSideBar()
{
if (MainWindowBase *m = mainWindow())
m->moveToSideBar(this);
}
bool DockWidgetBase::isOverlayed() const
{
if (MainWindowBase *m = mainWindow())
return m->overlayedDockWidget() == this;
return false;
}
SideBarLocation DockWidgetBase::sideBarLocation() const
{
return DockRegistry::self()->sideBarLocationForDockWidget(this);
}
bool DockWidgetBase::isInSideBar() const
{
return sideBarLocation() != SideBarLocation::None;
}
bool DockWidgetBase::hasPreviousDockedLocation() const
{
return d->m_lastPositions.isValid();
}
QSize DockWidgetBase::lastOverlayedSize() const
{
return d->m_lastOverlayedSize;
}
DockWidgetBase *DockWidgetBase::byName(const QString &uniqueName)
{
return DockRegistry::self()->dockByName(uniqueName);
}
bool DockWidgetBase::skipsRestore() const
{
return d->layoutSaverOptions & LayoutSaverOption::Skip;
}
void DockWidgetBase::setFloatingGeometry(QRect geometry)
{
if (isOpen() && isFloating()) {
window()->setGeometry(geometry);
} else {
d->m_lastPositions.setLastFloatingGeometry(geometry);
}
}
FloatingWindow *DockWidgetBase::Private::morphIntoFloatingWindow()
{
if (auto fw = floatingWindow())
return fw; // Nothing to do
if (q->isWindow()) {
QRect geo = m_lastPositions.lastFloatingGeometry();
if (geo.isNull()) {
geo = q->geometry();
if (!q->testAttribute(Qt::WA_PendingMoveEvent)) { // If user already moved it, we don't
// interfere
const QPoint center = defaultCenterPosForFloating();
if (!center.isNull())
geo.moveCenter(center);
}
}
auto frame = Config::self().frameworkWidgetFactory()->createFrame();
frame->addWidget(q);
geo.setSize(geo.size().boundedTo(frame->maxSizeHint()));
FloatingWindow::ensureRectIsOnScreen(geo);
auto floatingWindow =
Config::self().frameworkWidgetFactory()->createFloatingWindow(frame, nullptr, geo);
floatingWindow->show();
return floatingWindow;
} else {
return nullptr;
}
}
void DockWidgetBase::Private::maybeMorphIntoFloatingWindow()
{
if (q->isWindow() && q->isVisible())
morphIntoFloatingWindow();
}
MDILayoutWidget *DockWidgetBase::Private::mdiLayout() const
{
if (auto mw = mainWindow())
return mw->mdiLayoutWidget();
return nullptr;
}
DockWidgetBase::Private *DockWidgetBase::dptr() const
{
return d;
}
QPoint DockWidgetBase::Private::defaultCenterPosForFloating()
{
MainWindowBase::List mainWindows = DockRegistry::self()->mainwindows();
// We don't care about multiple mainwindows yet. Or, let's just say that the first one is more main than the others
MainWindowBase *mw = mainWindows.isEmpty() ? nullptr : mainWindows.constFirst();
if (!mw || !q->isFloating())
return {};
return mw->geometry().center();
}
bool DockWidgetBase::Private::eventFilter(QObject *watched, QEvent *event)
{
const bool isWindowActivate = event->type() == QEvent::WindowActivate;
const bool isWindowDeactivate = event->type() == QEvent::WindowDeactivate;
if ((isWindowActivate || isWindowDeactivate) && watched == q->window())
Q_EMIT q->windowActiveAboutToChange(isWindowActivate);
return QObject::eventFilter(watched, event);
}
void DockWidgetBase::Private::updateTitle()
{
if (q->isFloating())
q->window()->setWindowTitle(title);
toggleAction->setText(title);
}
void DockWidgetBase::Private::toggle(bool enabled)
{
if (SideBar *sb = sideBar()) {
// The widget is in the sidebar, let's toggle its overlayed state
sb->toggleOverlay(q);
} else {
// The most common case. The dock widget is not in the sidebar. just close or open it.
if (enabled) {
show();
} else {
q->close();
}
}
}
void DockWidgetBase::Private::updateToggleAction()
{
QScopedValueRollback<bool> recursionGuard(m_updatingToggleAction, true); // Guard against recursiveness
m_updatingToggleAction = true;
if ((q->isVisible() || frame()) && !toggleAction->isChecked()) {
toggleAction->setChecked(true);
} else if ((!q->isVisible() && !frame()) && toggleAction->isChecked()) {
toggleAction->setChecked(false);
}
}
void DockWidgetBase::Private::updateFloatAction()
{
QScopedValueRollback<bool> recursionGuard(m_updatingFloatAction, true); // Guard against recursiveness
if (q->isFloating()) {
floatAction->setEnabled(m_lastPositions.isValid());
floatAction->setChecked(true);
floatAction->setToolTip(tr("Dock"));
} else {
floatAction->setEnabled(true);
floatAction->setChecked(false);
floatAction->setToolTip(tr("Detach"));
}
}
void DockWidgetBase::Private::onDockWidgetShown()
{
updateToggleAction();
updateFloatAction();
}
void DockWidgetBase::Private::onDockWidgetHidden()
{
updateToggleAction();
updateFloatAction();
}
void DockWidgetBase::Private::close()
{
if (!m_processingToggleAction && !q->isOpen()) {
return;
}
if (m_isPersistentCentralDockWidget)
return;
// If it's overlayed and we're closing, we need to close the overlay
if (SideBar *sb = DockRegistry::self()->sideBarForDockWidget(q)) {
auto mainWindow = sb->mainWindow();
if (mainWindow->overlayedDockWidget() == q) {
mainWindow->clearSideBarOverlay(/* deleteFrame=*/false);
}
}
if (!m_isForceClosing && q->isFloating()
&& q->isVisible()) { // only user-closing is interesting to save the geometry
// We check for isVisible so we don't save geometry if you call close() on an already closed
// dock widget
m_lastPositions.setLastFloatingGeometry(q->window()->geometry());
}
saveTabIndex();
// Do some cleaning. Widget is hidden, but we must hide the tab containing it.
if (Frame *frame = this->frame()) {
q->setParent(nullptr);
frame->removeWidget(q);
if (SideBar *sb = DockRegistry::self()->sideBarForDockWidget(q)) {
sb->removeDockWidget(q);
}
}
if (!m_isMovingToSideBar && (options & DockWidgetBase::Option_DeleteOnClose)) {
Q_EMIT q->aboutToDeleteOnClose();
q->deleteLater();
}
}
bool DockWidgetBase::Private::restoreToPreviousPosition()
{
if (!m_lastPositions.isValid())
return false;
Layouting::Item *item = m_lastPositions.lastItem();
LayoutWidget *layout = DockRegistry::self()->layoutForItem(item);
Q_ASSERT(layout);
layout->restorePlaceholder(q, item, m_lastPositions.lastTabIndex());
return true;
}
void DockWidgetBase::Private::maybeRestoreToPreviousPosition()
{
// This is called when we get a QEvent::Show. Let's see if we have to restore it to a previous position.
if (!m_lastPositions.isValid())
return;
Layouting::Item *layoutItem = m_lastPositions.lastItem();
if (!layoutItem)
return; // nothing to do, no last position
if (m_lastPositions.wasFloating())
return; // Nothing to do, it was floating before, now it'll just get visible
Frame *frame = this->frame();
if (frame && frame->QWidgetAdapter::parentWidget() == DockRegistry::self()->layoutForItem(layoutItem)) {
// There's a frame already. Means the DockWidget was hidden instead of closed.
// Nothing to do, the dock widget will simply be shown
return;
}
// Now we deal with the case where the DockWidget was close()ed. In this case it doesn't have a parent.
if (q->parentWidget()) {
// The QEvent::Show is due to it being made floating. Nothing to restore.
return;
}
// Finally, restore it
restoreToPreviousPosition();
}
int DockWidgetBase::Private::currentTabIndex() const
{
Frame *frame = this->frame();
return frame ? frame->indexOfDockWidget(q) : 0;
}
void DockWidgetBase::Private::saveTabIndex()
{
m_lastPositions.saveTabIndex(currentTabIndex(), q->isFloating());
}
void DockWidgetBase::Private::show()
{
// Only show for now
q->show();
}
void DockWidgetBase::onParentChanged()
{
#ifdef KDDOCKWIDGETS_QTWIDGETS
Q_EMIT parentChanged();
#else
Q_EMIT QQuickItem::parentChanged(parentItem());
#endif
d->updateToggleAction();
d->updateFloatAction();
Q_EMIT actualTitleBarChanged();
}
void DockWidgetBase::onShown(bool spontaneous)
{
d->onDockWidgetShown();
Q_EMIT shown();
if (Frame *f = d->frame()) {
if (!spontaneous) {
f->onDockWidgetShown(this);
}
}
d->maybeRestoreToPreviousPosition();
// Transform into a FloatingWindow if this will be a regular floating dock widget.
QTimer::singleShot(0, d, &DockWidgetBase::Private::maybeMorphIntoFloatingWindow);
}
void DockWidgetBase::onHidden(bool spontaneous)
{
d->onDockWidgetHidden();
Q_EMIT hidden();
if (Frame *f = d->frame()) {
if (!spontaneous) {
f->onDockWidgetHidden(this);
}
}
}
bool DockWidgetBase::onResize(QSize newSize)
{
if (isOverlayed()) {
if (auto frame = d->frame()) {
d->m_lastOverlayedSize = frame->QWidgetAdapter::size();
} else {
qWarning() << Q_FUNC_INFO << "Overlayed dock widget without frame shouldn't happen";
}
}
return QWidgetAdapter::onResize(newSize);
}
void DockWidgetBase::onCloseEvent(QCloseEvent *e)
{
e->accept(); // By default we accept, means DockWidget closes
if (d->widget)
qApp->sendEvent(d->widget, e); // Give a chance for the widget to ignore
if (e->isAccepted())
d->close();
}
DockWidgetBase *DockWidgetBase::deserialize(const LayoutSaver::DockWidget::Ptr &saved)
{
auto dr = DockRegistry::self();
DockWidgetBase *dw = dr->dockByName(saved->uniqueName, DockRegistry::DockByNameFlag::CreateIfNotFound);
if (dw) {
if (QWidgetOrQuick *w = dw->widget())
w->setVisible(true);
dw->setProperty("kddockwidget_was_restored", true);
if (dw->affinities() != saved->affinities) {
qWarning() << Q_FUNC_INFO << "Affinity name changed from" << dw->affinities()
<< "; to" << saved->affinities;
dw->d->affinities = saved->affinities;
}
}
return dw;
}
void DockWidgetBase::setUserType(int userType)
{
d->m_userType = userType;
}
int DockWidgetBase::userType() const
{
return d->m_userType;
}
void DockWidgetBase::setMDIPosition(QPoint pos)
{
if (MDILayoutWidget *layout = d->mdiLayout())
layout->moveDockWidget(this, pos);
}
void DockWidgetBase::setMDISize(QSize size)
{
if (MDILayoutWidget *layout = d->mdiLayout())
layout->resizeDockWidget(this, size);
}
void DockWidgetBase::setMDIZ(int z)
{
#ifdef KDDOCKWIDGETS_QTQUICK
if (Frame *frame = d->frame()) {
if (!frame->isMDI())
return;
frame->setZ(z);
}
#else
Q_UNUSED(z);
qWarning() << Q_FUNC_INFO << "Not implemented for QtQuick";
#endif
}
bool DockWidgetBase::isPersistentCentralDockWidget() const
{
return d->m_isPersistentCentralDockWidget;
}
LayoutSaver::DockWidget::Ptr DockWidgetBase::Private::serialize() const
{
auto ptr = LayoutSaver::DockWidget::dockWidgetForName(q->uniqueName());
ptr->affinities = q->affinities();
return ptr;
}
void DockWidgetBase::Private::forceClose()
{
QScopedValueRollback<bool> rollback(m_isForceClosing, true);
close();
}
DockWidgetBase::Private::Private(const QString &dockName, DockWidgetBase::Options options_,
LayoutSaverOptions layoutSaverOptions_, DockWidgetBase *qq)
: name(dockName)
, title(dockName)
, q(qq)
, options(options_)
, layoutSaverOptions(layoutSaverOptions_)
, toggleAction(new QAction(q))
, floatAction(new QAction(q))
{
q->connect(toggleAction, &QAction::toggled, q, [this](bool enabled) {
if (!m_updatingToggleAction) { // guard against recursiveness
toggleAction->blockSignals(true); // and don't emit spurious toggle. Like when a dock
// widget is inserted into a tab widget it might get
// hide events, ignore those. The Dock Widget is open.
m_processingToggleAction = true;
toggle(enabled);
toggleAction->blockSignals(false);
m_processingToggleAction = false;
}
});
q->connect(floatAction, &QAction::toggled, q, [this](bool checked) {
if (!m_updatingFloatAction) { // guard against recursiveness
q->setFloating(checked);
}
Q_EMIT q->isFloatingChanged(checked);
// When floating, we remove from the sidebar
if (checked && q->isOpen()) {
if (SideBar *sb = DockRegistry::self()->sideBarForDockWidget(q)) {
sb->mainWindow()->clearSideBarOverlay(/* deleteFrame=*/false);
sb->removeDockWidget(q);
}
}
});
toggleAction->setCheckable(true);
floatAction->setCheckable(true);
qApp->installEventFilter(this);
}
void DockWidgetBase::Private::addPlaceholderItem(Layouting::Item *item)
{
Q_ASSERT(item);
m_lastPositions.addPosition(item);
}
LastPositions &DockWidgetBase::Private::lastPositions()
{
return m_lastPositions;
}
Frame *DockWidgetBase::Private::frame() const
{
QWidgetOrQuick *p = q->parentWidget();
while (p) {
if (auto frame = qobject_cast<Frame *>(p))
return frame;
p = p->parentWidget();
}
return nullptr;
}
void DockWidgetBase::Private::saveLastFloatingGeometry()
{
if (q->isFloating() && q->isVisible()) {
// It's getting docked, save last floating position
lastPositions().setLastFloatingGeometry(q->window()->geometry());
}
}