KDDockWidgets API Documentation 2.1
Loading...
Searching...
No Matches
core/Stack.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2020 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: SĂ©rgio Martins <sergio.martins@kdab.com>
6
7 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
8
9 Contact KDAB at <info@kdab.com> for commercial licensing options.
10*/
11
12#include "Stack.h"
13#include "Stack_p.h"
14#include "Config.h"
15#include "ViewFactory.h"
16#include "Logging_p.h"
17#include "Utils_p.h"
18#include "WindowBeingDragged_p.h"
19#include "DockWidget_p.h"
20#include "TabBar.h"
21#include "Group.h"
22#include "FloatingWindow.h"
23#include "ObjectGuard_p.h"
24
26
27using namespace KDDockWidgets;
28using namespace KDDockWidgets::Core;
29
30Stack::Stack(Group *group, StackOptions options)
31 : Controller(ViewType::Stack, Config::self().viewFactory()->createStack(this, group->view()))
32 , Draggable(view(),
33 Config::self().flags()
34 & (Config::Flag_HideTitleBarWhenTabsVisible | Config::Flag_AlwaysShowTabs))
35 , d(new Private(group, options, this))
36
37{
38 // needs to be initialized out of Private(), as tabbar view's init will call into stack's private
39 d->m_tabBar = new TabBar(this);
40
41 view()->init();
42}
43
45{
46 delete d->m_tabBar;
47 delete d;
48}
49
50StackOptions Stack::options() const
51{
52 return d->m_options;
53}
54
55bool Stack::isPositionDraggable(Point p) const
56{
57 if (auto svi = dynamic_cast<Core::StackViewInterface *>(view()))
58 return svi->isPositionDraggable(p);
59
60 return false;
61}
62
67
68bool Stack::insertDockWidget(DockWidget *dock, int index)
69{
70 assert(dock);
71
72 if (index < 0)
73 index = 0;
74 if (index > numDockWidgets())
75 index = numDockWidgets();
76
77 if (contains(dock)) {
78 KDDW_ERROR("Refusing to add already existing widget");
79 return false;
80 }
81
82 ObjectGuard<Group> oldFrame = dock->d->group();
83
84 d->m_tabBar->insertDockWidget(index, dock, dock->icon(IconPlace::TabBar), dock->title());
85 d->m_tabBar->setCurrentIndex(index);
86
87 if (oldFrame && oldFrame->beingDeletedLater()) {
88 // give it a push and delete it immediately.
89 // Having too many deleteLater() puts us in an inconsistent state. For example if
90 // LayoutSaver::saveState() would to be called while the Frame hadn't been deleted yet it
91 // would count with that group unless hacks. Also the unit-tests are full of
92 // waitForDeleted() due to deleteLater.
93
94 // Ideally we would just remove the deleteLater from Group.cpp, but QStack::insertTab()
95 // would crash, as it accesses the old tab-widget we're stealing from
96
97 delete oldFrame;
98 }
99
100 return true;
101}
102
104{
105 return d->m_tabBar->indexOfDockWidget(dw) != -1;
106}
107
109{
110 return d->m_group;
111}
112
113std::unique_ptr<WindowBeingDragged> Stack::makeWindow()
114{
115 // This is called when using Flag_HideTitleBarWhenTabsVisible
116 // For detaching individual tabs, TabBar::makeWindow() is called.
117
118 if (auto fw = view()->rootView()->asFloatingWindowController()) {
119 if (fw->hasSingleGroup()) {
120 // We're already in a floating window, and it only has 1 dock widget.
121 // So there's no detachment to be made, we just move the window.
122 return std::make_unique<WindowBeingDragged>(fw, this);
123 }
124 }
125
126 Rect r = d->m_group->view()->geometry();
127
128 const Point globalPoint = view()->mapToGlobal(Point(0, 0));
129
130 auto floatingWindow = new FloatingWindow(d->m_group, {});
131 r.moveTopLeft(globalPoint);
132 floatingWindow->setSuggestedGeometry(r, SuggestedGeometryHint_GeometryIsFromDocked);
133 floatingWindow->view()->show();
134
135 return std::make_unique<WindowBeingDragged>(floatingWindow, this);
136}
137
138bool Stack::isWindow() const
139{
140 if (auto fw = view()->rootView()->asFloatingWindowController()) {
141 // Case of dragging via the tab widget when the title bar is hidden
142 return fw->hasSingleGroup();
143 }
144
145 return false;
146}
147
149{
150 if (d->m_group->hasSingleDockWidget()) {
151 const auto dockWidgets = d->m_group->dockWidgets();
152 return dockWidgets.first();
153 }
154
155 return nullptr;
156}
157
158bool Stack::isMDI() const
159{
160 return d->m_group && d->m_group->isMDI();
161}
162
163bool Stack::onMouseDoubleClick(Point localPos)
164{
165 // User clicked the empty space of the tab widget and we don't have title bar
166 // We float the entire group.
167
169 || tabBar()->dockWidgetAt(localPos))
170 return false;
171
172 Group *group = this->group();
173
174 // When using MainWindowOption_HasCentralFrame. The central group is never detachable.
175 if (group->isCentralGroup())
176 return false;
177
178 if (FloatingWindow *fw = group->floatingWindow()) {
179 if (!fw->hasSingleGroup()) {
180 makeWindow();
181 return true;
182 }
183 } else if (group->isInMainWindow()) {
184 makeWindow();
185 return true;
186 }
187
188 return false;
189}
190
192{
193 if (is == d->m_tabBarAutoHide)
194 return;
195
196 d->m_tabBarAutoHide = is;
197 d->tabBarAutoHideChanged.emit(is);
198}
199
201{
202 return d->m_tabBarAutoHide;
203}
204
206{
207 return d->m_tabBar;
208}
209
211{
212 return d->m_tabBar->numDockWidgets();
213}
214
216{
217 dynamic_cast<Core::StackViewInterface *>(view())->setDocumentMode(is);
218}
219
220void Stack::setHideDisabledButtons(TitleBarButtonTypes types)
221{
222 if (d->m_buttonsToHideIfDisabled != types) {
223 d->m_buttonsToHideIfDisabled = types;
224 d->buttonsToHideIfDisabledChanged.emit();
225 }
226}
227
229{
230 return d->m_buttonsToHideIfDisabled & type;
231}
Application-wide config to tune certain behaviours of the framework.
Singleton to allow to choose certain behaviours of the framework.
Definition Config.h:64
static Config & self()
returns the singleton Config instance
Definition Config.cpp:88
@ Flag_HideTitleBarWhenTabsVisible
Definition Config.h:89
View * view() const
Returns the view associated with this controller, if any.
ViewType type() const
Returns the type of this controller.
bool is(ViewType) const
Returns whether this controller is of the specified type.
The DockWidget base-class. DockWidget and Core::DockWidget are only split in two so we can share some...
Icon icon(IconPlace place=IconPlace::TitleBar) const
Returns the dock widget's titlebar, tabbar, or toggle action icon (depending on the passed place)
QString title() const
Returns the dock widget's title. This title is visible in title bars and tab bars.
bool isCentralGroup() const
returns if this widget is the central group MainWindow supports a mode where the middle group is pers...
bool isInMainWindow() const
Returns whether this group is docked inside a MainWindow.
FloatingWindow * floatingWindow() const
returns the FloatingWindow this group is in, if any
The interface that Stack views share.
bool insertDockWidget(DockWidget *dockwidget, int index)
inserts dockwidget into the TabWidget, at index
bool isWindow() const override
friend class QtQuick::TabBar
Definition core/Stack.h:100
void setHideDisabledButtons(TitleBarButtonTypes)
std::unique_ptr< WindowBeingDragged > makeWindow() override
DockWidget * singleDockWidget() const override final
bool isPositionDraggable(Point p) const override
int numDockWidgets() const
returns the number of dock widgets in this TabWidget
bool contains(DockWidget *dw) const
Returns whether dockwidget dw is contained in this tab widget Equivalent to indexOf(dw) !...
Group * group() const
getter for the group
Core::TabBar * tabBar() const
Returns the tab bar.
bool buttonHidesIfDisabled(TitleBarButtonType) const
void setDocumentMode(bool)
Enables document mode. Default is false.
virtual ~Stack() override
StackOptions options() const
bool onMouseDoubleClick(Point localPos)
friend class QtWidgets::Stack
Definition core/Stack.h:99
void addDockWidget(DockWidget *)
appends a dock widget into this TabWidget
bool isMDI() const override
virtual Point mapToGlobal(Point) const =0
ViewType
Each View type also has a specific Controller associated with, except for ViewType::None.
Definition Controller.h:26
Class to abstract QAction, so code still works with QtQuick and Flutter.
@ SuggestedGeometryHint_GeometryIsFromDocked
TitleBarButtonType
describes a type of button you can have in the title bar
A factory class for allowing the user to customize some internal widgets.

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated by doxygen 1.9.8