KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/Stack.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2019 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"
14#include "core/Controller.h"
15#include "core/Stack.h"
16#include "core/TitleBar.h"
17#include "core/Window_p.h"
18#include "core/DockRegistry_p.h"
19#include "core/Stack_p.h"
20#include "Config.h"
21#include "core/View_p.h"
22
24
25#include <QMouseEvent>
26#include <QTabBar>
27#include <QHBoxLayout>
28#include <QAbstractButton>
29#include <QMenu>
30
31#include "kdbindings/signal.h"
32
33using namespace KDDockWidgets;
34using namespace KDDockWidgets::QtWidgets;
35
37class Stack::Private
38{
39public:
40 KDBindings::ScopedConnection tabBarAutoHideChanged;
41 KDBindings::ScopedConnection screenChangedConnection;
42 QHBoxLayout *cornerWidgetLayout = nullptr;
43 QAbstractButton *floatButton = nullptr;
44 QAbstractButton *closeButton = nullptr;
45};
46}
47
48
49Stack::Stack(Core::Stack *controller, QWidget *parent)
50 : View<QTabWidget>(controller, Core::ViewType::Stack, parent)
51 , StackViewInterface(controller)
52 , d(new Private())
53{
54}
55
57{
58 delete d;
59}
60
62{
65
68
69 // In case tabs closable is set by the factory, a tabClosedRequested() is emitted when the user
70 // presses [x]
71 connect(this, &QTabWidget::tabCloseRequested, this, [this](int index) {
72 if (auto dw = m_stack->tabBar()->dockWidgetAt(index)) {
73 if (dw->options() & DockWidgetOption_NotClosable) {
74 qWarning() << "QTabWidget::tabCloseRequested: Refusing to close dock widget with "
75 "Option_NotClosable option. name="
76 << dw->uniqueName();
77 } else {
78 dw->view()->close();
79 }
80 } else {
81 qWarning() << "QTabWidget::tabCloseRequested Couldn't find dock widget for index"
82 << index << "; count=" << count();
83 }
84 });
85
86 QTabWidget::setTabBarAutoHide(m_stack->tabBarAutoHide());
87
88 d->tabBarAutoHideChanged = m_stack->d->tabBarAutoHideChanged.connect(
89 [this](bool is) { QTabWidget::setTabBarAutoHide(is); });
90
92 setFocusProxy(nullptr);
93
94 setupTabBarButtons();
95
96 setDocumentMode(m_stack->options() & StackOption_DocumentMode);
97}
98
100{
101 if (m_stack->onMouseDoubleClick(ev->pos())) {
102 ev->accept();
103 } else {
104 ev->ignore();
105 }
106}
107
109{
111
113 && !m_stack->group()->isFocused()) {
114 // User clicked on the tab widget itself
115 m_stack->group()->FocusScope::focus(Qt::MouseFocusReason);
116 }
117}
118
119void Stack::setupTabBarButtons()
120{
122 return;
123
124 auto factory = static_cast<ViewFactory *>(Config::self().viewFactory());
125 d->closeButton = factory->createTitleBarButton(this, TitleBarButtonType::Close);
126 d->floatButton = factory->createTitleBarButton(this, TitleBarButtonType::Float);
127
128 auto cornerWidget = new QWidget(this);
129 cornerWidget->setObjectName(QStringLiteral("Corner Widget"));
130
132
133 d->cornerWidgetLayout = new QHBoxLayout(cornerWidget);
134
135 d->cornerWidgetLayout->addWidget(d->floatButton);
136 d->cornerWidgetLayout->addWidget(d->closeButton);
137
138 connect(d->floatButton, &QAbstractButton::clicked, this, [this] {
139 Core::TitleBar *tb = m_stack->group()->titleBar();
140 tb->onFloatClicked();
141 });
142
143 connect(d->closeButton, &QAbstractButton::clicked, this, [this] {
144 Core::TitleBar *tb = m_stack->group()->titleBar();
145 tb->onCloseClicked();
146 });
147
148 updateMargins();
149 d->screenChangedConnection = DockRegistry::self()->dptr()->windowChangedScreen.connect([this](Core::Window::Ptr w) {
150 if (View::d->isInWindow(w))
151 updateMargins();
152 });
153}
154
155void Stack::updateMargins()
156{
157 const qreal factor = logicalDpiFactor(this);
158 d->cornerWidgetLayout->setContentsMargins(QMargins(0, 0, 2, 0) * factor);
159 d->cornerWidgetLayout->setSpacing(int(2 * factor));
160}
161
163{
165 return;
166
168 // We don't want context menu if there is only one tab
169 if (tabBar->count() <= 1)
170 return;
171
172 // Click on a tab => No menu
173 if (tabBar->tabAt(pos) >= 0)
174 return;
175
176 // Right click is allowed only on the tabs area
177 QRect tabAreaRect = tabBar->rect();
178 tabAreaRect.setWidth(this->width());
179 if (!tabAreaRect.contains(pos))
180 return;
181
182 QMenu menu(this);
183 for (int i = 0; i < tabBar->count(); ++i) {
184 QAction *action = menu.addAction(tabText(i), this, [this, i] { setCurrentIndex(i); });
185 if (i == currentIndex())
186 action->setDisabled(true);
187 }
188 menu.exec(mapToGlobal(pos));
189}
190
192{
193 return static_cast<QTabBar *>(View_qt::asQWidget((m_stack->tabBar())));
194}
195
200
202{
203 return m_stack;
204}
205
207{
209 qWarning() << Q_FUNC_INFO << "Not implemented yet. Only North is supported";
210 return false;
211 }
212
213 return p.y() >= 0 && p.y() <= tabBar()->height();
214}
Application-wide config to tune certain behaviours of the framework.
A ScopedConnection is a RAII-style way to make sure a Connection is disconnected.
Definition signal.h:533
Core::ViewFactory * viewFactory() const
getter for the framework view factory
Definition Config.cpp:162
static Config & self()
returns the singleton Config instance
Definition Config.cpp:87
@ Flag_ShowButtonsOnTabBarIfTitleBarHidden
Definition Config.h:125
@ Flag_TabsHaveCloseButton
Tabs will have a close button. Equivalent to QTabWidget::setTabsClosable(true).
Definition Config.h:93
bool isFocused() const
Returns true if this FocusScope is focused. This is similar to the QWidget::hasFocus(),...
Group * group() const
getter for the group
Core::TabBar * tabBar() const
Returns the tab bar.
bool onMouseDoubleClick(Point localPos)
DockWidget * dockWidgetAt(int index) const
returns the dock widgets at tab number index
virtual bool is(ViewType) const
Returns whether the view is of the specified type Virtual so it can be overridden by ViewWrapper....
static DockRegistry * self()
void setDocumentMode(bool) override
Sets QTabWidget::documentMode(). Only implemented for QtWidgets. Probably not interesting for other f...
Core::Stack * stack() const
Returns the controller.
bool isPositionDraggable(QPoint p) const override
virtual void showContextMenu(QPoint pos)
Shows the context menu. Override to implement your own context menu. By default it's used to honour C...
Stack(Core::Stack *controller, QWidget *parent=nullptr)
void mousePressEvent(QMouseEvent *) override
void mouseDoubleClickEvent(QMouseEvent *) override
QTabBar * tabBar() const
Returns the QTabBar associated with this QTabWidget.
The default ViewFactory for QtWidgets frontend.
QPoint mapToGlobal(QPoint localPt) const override
qreal logicalDpiFactor(const QWidget *w)
Class to abstract QAction, so code still works with QtQuick and Flutter.
void clicked(bool checked)
void setDisabled(bool b)
void accept()
void ignore()
QAction * addAction(const QString &text)
QAction * exec()
QPoint pos() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setObjectName(const QString &name)
int y() const const
bool contains(const QRect &rectangle, bool proper) const const
void setWidth(int width)
CustomContextMenu
TopRightCorner
MouseFocusReason
int tabAt(const QPoint &position) const const
QWidget * cornerWidget(Qt::Corner corner) const const
void setCurrentIndex(int index)
void setDocumentMode(bool set)
void setCornerWidget(QWidget *widget, Qt::Corner corner)
void setTabBar(QTabBar *tb)
QTabBar * tabBar() const const
void setTabBarAutoHide(bool enabled)
void tabCloseRequested(int index)
QString tabText(int index) const const
void setTabsClosable(bool closeable)
A factory class for allowing the user to customize some internal widgets.
Represents a dock widget.
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
void customContextMenuRequested(const QPoint &pos)
virtual void mousePressEvent(QMouseEvent *event)
bool isVisible() const const

© 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