forked from mfreiholz/Qt-Advanced-Docking-System
-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathAutoHideDockContainer.cpp
583 lines (490 loc) · 15.7 KB
/
AutoHideDockContainer.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
/*******************************************************************************
** Qt Advanced Docking System
** Copyright (C) 2017 Uwe Kindler
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
//============================================================================
/// \file AutoHideDockContainer.cpp
/// \author Syarif Fakhri
/// \date 05.09.2022
/// \brief Implementation of CAutoHideDockContainer class
//============================================================================
//============================================================================
// INCLUDES
//============================================================================
#include "AutoHideDockContainer.h"
#include <QXmlStreamWriter>
#include <QBoxLayout>
#include <QPainter>
#include <QSplitter>
#include <QPointer>
#include <QApplication>
#include <QCursor>
#include "DockManager.h"
#include "DockWidgetTab.h"
#include "DockAreaWidget.h"
#include "DockingStateReader.h"
#include "ResizeHandle.h"
#include "DockComponentsFactory.h"
#include "AutoHideSideBar.h"
#include "AutoHideTab.h"
#include <iostream>
namespace ads
{
static const int ResizeMargin = 30;
//============================================================================
bool static isHorizontalArea(SideBarLocation Area)
{
switch (Area)
{
case SideBarLocation::SideBarTop:
case SideBarLocation::SideBarBottom: return true;
case SideBarLocation::SideBarLeft:
case SideBarLocation::SideBarRight: return false;
default:
return true;
}
return true;
}
//============================================================================
Qt::Edge static edgeFromSideTabBarArea(SideBarLocation Area)
{
switch (Area)
{
case SideBarLocation::SideBarTop: return Qt::BottomEdge;
case SideBarLocation::SideBarBottom: return Qt::TopEdge;
case SideBarLocation::SideBarLeft: return Qt::RightEdge;
case SideBarLocation::SideBarRight: return Qt::LeftEdge;
default:
return Qt::LeftEdge;
}
return Qt::LeftEdge;
}
//============================================================================
int resizeHandleLayoutPosition(SideBarLocation Area)
{
switch (Area)
{
case SideBarLocation::SideBarBottom:
case SideBarLocation::SideBarRight: return 0;
case SideBarLocation::SideBarTop:
case SideBarLocation::SideBarLeft: return 1;
default:
return 0;
}
return 0;
}
/**
* Private data of CAutoHideDockContainer - pimpl
*/
struct AutoHideDockContainerPrivate
{
CAutoHideDockContainer* _this;
CDockAreaWidget* DockArea{nullptr};
CDockWidget* DockWidget{nullptr};
SideBarLocation SideTabBarArea = SideBarNone;
QBoxLayout* Layout = nullptr;
CResizeHandle* ResizeHandle = nullptr;
QSize Size; // creates invalid size
QPointer<CAutoHideTab> SideTab;
/**
* Private data constructor
*/
AutoHideDockContainerPrivate(CAutoHideDockContainer *_public);
/**
* Convenience function to get a dock widget area
*/
DockWidgetArea getDockWidgetArea(SideBarLocation area)
{
switch (area)
{
case SideBarLocation::SideBarLeft: return LeftDockWidgetArea;
case SideBarLocation::SideBarRight: return RightDockWidgetArea;
case SideBarLocation::SideBarBottom: return BottomDockWidgetArea;
case SideBarLocation::SideBarTop: return TopDockWidgetArea;
default:
return LeftDockWidgetArea;
}
return LeftDockWidgetArea;
}
/**
* Update the resize limit of the resize handle
*/
void updateResizeHandleSizeLimitMax()
{
auto Rect = _this->dockContainer()->contentRect();
const auto maxResizeHandleSize = ResizeHandle->orientation() == Qt::Horizontal
? Rect.width() : Rect.height();
ResizeHandle->setMaxResizeSize(maxResizeHandleSize - ResizeMargin);
}
/**
* Convenience function to check, if this is an horizontal area
*/
bool isHorizontal() const
{
return isHorizontalArea(SideTabBarArea);
}
/**
* Forward this event to the dock container
*/
void forwardEventToDockContainer(QEvent* event)
{
auto DockContainer = _this->dockContainer();
if (DockContainer)
{
DockContainer->handleAutoHideWidgetEvent(event, _this);
}
}
}; // struct AutoHideDockContainerPrivate
//============================================================================
AutoHideDockContainerPrivate::AutoHideDockContainerPrivate(
CAutoHideDockContainer *_public) :
_this(_public)
{
}
//============================================================================
CDockContainerWidget* CAutoHideDockContainer::dockContainer() const
{
if (d->DockArea)
{
return d->DockArea->dockContainer();
}
else
{
return internal::findParent<CDockContainerWidget*>(this);
}
}
//============================================================================
CAutoHideDockContainer::CAutoHideDockContainer(CDockWidget* DockWidget, SideBarLocation area, CDockContainerWidget* parent) :
Super(parent),
d(new AutoHideDockContainerPrivate(this))
{
hide(); // auto hide dock container is initially always hidden
d->SideTabBarArea = area;
d->SideTab = componentsFactory()->createDockWidgetSideTab(nullptr);
connect(d->SideTab, &CAutoHideTab::pressed, this, &CAutoHideDockContainer::toggleCollapseState);
d->DockArea = new CDockAreaWidget(DockWidget->dockManager(), parent);
d->DockArea->setObjectName("autoHideDockArea");
d->DockArea->setAutoHideDockContainer(this);
setObjectName("autoHideDockContainer");
d->Layout = new QBoxLayout(isHorizontalArea(area) ? QBoxLayout::TopToBottom : QBoxLayout::LeftToRight);
d->Layout->setContentsMargins(0, 0, 0, 0);
d->Layout->setSpacing(0);
setLayout(d->Layout);
d->ResizeHandle = new CResizeHandle(edgeFromSideTabBarArea(area), this);
d->ResizeHandle->setMinResizeSize(64);
bool OpaqueResize = CDockManager::testConfigFlag(CDockManager::OpaqueSplitterResize);
d->ResizeHandle->setOpaqueResize(OpaqueResize);
d->Size = d->DockArea->size();
addDockWidget(DockWidget);
parent->registerAutoHideWidget(this);
// The dock area should not be added to the layout before it contains the
// dock widget. If you add it to the layout before it contains the dock widget
// then you will likely see this warning for OpenGL widgets or QAxWidgets:
// setGeometry: Unable to set geometry XxY+Width+Height on QWidgetWindow/'WidgetClassWindow
d->Layout->addWidget(d->DockArea);
d->Layout->insertWidget(resizeHandleLayoutPosition(area), d->ResizeHandle);
}
//============================================================================
void CAutoHideDockContainer::updateSize()
{
auto dockContainerParent = dockContainer();
if (!dockContainerParent)
{
return;
}
auto rect = dockContainerParent->contentRect();
switch (sideBarLocation())
{
case SideBarLocation::SideBarTop:
resize(rect.width(), qMin(rect.height() - ResizeMargin, d->Size.height()));
move(rect.topLeft());
break;
case SideBarLocation::SideBarLeft:
resize(qMin(d->Size.width(), rect.width() - ResizeMargin), rect.height());
move(rect.topLeft());
break;
case SideBarLocation::SideBarRight:
{
resize(qMin(d->Size.width(), rect.width() - ResizeMargin), rect.height());
QPoint p = rect.topRight();
p.rx() -= (width() - 1);
move(p);
}
break;
case SideBarLocation::SideBarBottom:
{
resize(rect.width(), qMin(rect.height() - ResizeMargin, d->Size.height()));
QPoint p = rect.bottomLeft();
p.ry() -= (height() - 1);
move(p);
}
break;
default:
break;
}
}
//============================================================================
CAutoHideDockContainer::~CAutoHideDockContainer()
{
ADS_PRINT("~CAutoHideDockContainer");
// Remove event filter in case there are any queued messages
qApp->removeEventFilter(this);
if (dockContainer())
{
dockContainer()->removeAutoHideWidget(this);
}
if (d->SideTab)
{
delete d->SideTab;
}
delete d;
}
//============================================================================
CAutoHideSideBar* CAutoHideDockContainer::sideBar() const
{
return dockContainer()->sideTabBar(d->SideTabBarArea);
}
//============================================================================
CAutoHideTab* CAutoHideDockContainer::autoHideTab() const
{
return d->SideTab;
}
//============================================================================
CDockWidget* CAutoHideDockContainer::dockWidget() const
{
return d->DockWidget;
}
//============================================================================
void CAutoHideDockContainer::addDockWidget(CDockWidget* DockWidget)
{
if (d->DockWidget)
{
// Remove the old dock widget at this area
d->DockArea->removeDockWidget(d->DockWidget);
}
d->DockWidget = DockWidget;
d->SideTab->setDockWidget(DockWidget);
CDockAreaWidget* OldDockArea = DockWidget->dockAreaWidget();
auto IsRestoringState = DockWidget->dockManager()->isRestoringState();
if (OldDockArea && !IsRestoringState)
{
// The initial size should be a little bit bigger than the original dock
// area size to prevent that the resize handle of this auto hid dock area
// is near of the splitter of the old dock area.
d->Size = OldDockArea->size() + QSize(16, 16);
OldDockArea->removeDockWidget(DockWidget);
}
d->DockArea->addDockWidget(DockWidget);
updateSize();
}
//============================================================================
SideBarLocation CAutoHideDockContainer::sideBarLocation() const
{
return d->SideTabBarArea;
}
//============================================================================
CDockAreaWidget* CAutoHideDockContainer::dockAreaWidget() const
{
return d->DockArea;
}
//============================================================================
void CAutoHideDockContainer::moveContentsToParent()
{
cleanupAndDelete();
// If we unpin the auto hide dock widget, then we insert it into the same
// location like it had as a auto hide widget. This brings the least surprise
// to the user and he does not have to search where the widget was inserted.
d->DockWidget->setDockArea(nullptr);
auto DockContainer = dockContainer();
DockContainer->addDockWidget(d->getDockWidgetArea(d->SideTabBarArea), d->DockWidget);
}
//============================================================================
void CAutoHideDockContainer::cleanupAndDelete()
{
const auto dockWidget = d->DockWidget;
if (dockWidget)
{
auto SideTab = d->SideTab;
SideTab->removeFromSideBar();
SideTab->setParent(nullptr);
SideTab->hide();
}
hide();
deleteLater();
}
//============================================================================
void CAutoHideDockContainer::saveState(QXmlStreamWriter& s)
{
s.writeStartElement("Widget");
s.writeAttribute("Name", d->DockWidget->objectName());
s.writeAttribute("Closed", QString::number(d->DockWidget->isClosed() ? 1 : 0));
s.writeAttribute("Size", QString::number(d->isHorizontal() ? d->Size.height() : d->Size.width()));
s.writeEndElement();
}
//============================================================================
void CAutoHideDockContainer::toggleView(bool Enable)
{
if (Enable)
{
if (d->SideTab)
{
d->SideTab->show();
}
}
else
{
if (d->SideTab)
{
d->SideTab->hide();
}
hide();
qApp->removeEventFilter(this);
}
}
//============================================================================
void CAutoHideDockContainer::collapseView(bool Enable)
{
if (Enable)
{
hide();
qApp->removeEventFilter(this);
}
else
{
updateSize();
d->updateResizeHandleSizeLimitMax();
raise();
show();
d->DockWidget->dockManager()->setDockWidgetFocused(d->DockWidget);
qApp->installEventFilter(this);
}
ADS_PRINT("CAutoHideDockContainer::collapseView " << Enable);
d->SideTab->updateStyle();
}
//============================================================================
void CAutoHideDockContainer::toggleCollapseState()
{
collapseView(isVisible());
}
//============================================================================
void CAutoHideDockContainer::setSize(int Size)
{
if (d->isHorizontal())
{
d->Size.setHeight(Size);
}
else
{
d->Size.setWidth(Size);
}
updateSize();
}
//============================================================================
bool CAutoHideDockContainer::eventFilter(QObject* watched, QEvent* event)
{
if (event->type() == QEvent::Resize)
{
if (!d->ResizeHandle->isResizing())
{
updateSize();
}
}
else if (event->type() == QEvent::MouseButtonPress)
{
auto Container = dockContainer();
// First we check, if the mouse button press is inside the container
// widget. If it is not, i.e. if someone resizes the main window or
// clicks into the application menu or toolbar, then we ignore the
// event
auto widget = qobject_cast<QWidget*>(watched);
bool IsContainer = false;
while (widget)
{
if (widget == Container)
{
IsContainer = true;
}
widget = widget->parentWidget();
}
if (!IsContainer)
{
return Super::eventFilter(watched, event);
}
// Now we check, if the user clicked inside of this auto hide container.
// If the click is inside of this auto hide container, then we can also
// ignore the event, because the auto hide overlay should not get collapsed if
// user works in it
QMouseEvent* me = static_cast<QMouseEvent*>(event);
auto pos = mapFromGlobal(me->globalPos());
if (rect().contains(pos))
{
return Super::eventFilter(watched, event);
}
// Now check, if the user clicked into the side tab and ignore this event,
// because the side tab click handler will call collapseView(). If we
// do not ignore this here, then we will collapse the container and the side tab
// click handler will uncollapse it
auto SideTab = d->SideTab;
pos = SideTab->mapFromGlobal(me->globalPos());
if (SideTab->rect().contains(pos))
{
return Super::eventFilter(watched, event);
}
// If the mouse button down event is in the dock manager but outside
// of the open auto hide container, then the auto hide dock widget
// should get collapsed
collapseView(true);
}
return Super::eventFilter(watched, event);
}
//============================================================================
void CAutoHideDockContainer::resizeEvent(QResizeEvent* event)
{
Super::resizeEvent(event);
if (d->ResizeHandle->isResizing())
{
d->Size = this->size();
d->updateResizeHandleSizeLimitMax();
}
}
//============================================================================
void CAutoHideDockContainer::leaveEvent(QEvent *event)
{
// Resizing of the dock container via the resize handle in non opaque mode
// mays cause a leave event that is not really a leave event. Therefore
// we check here, if we are really outside of our rect.
auto pos = mapFromGlobal(QCursor::pos());
if (!rect().contains(pos))
{
d->forwardEventToDockContainer(event);
}
Super::leaveEvent(event);
}
//============================================================================
bool CAutoHideDockContainer::event(QEvent* event)
{
switch (event->type())
{
case QEvent::Enter:
case QEvent::Hide:
d->forwardEventToDockContainer(event);
break;
default:
break;
}
return Super::event(event);
}
}