KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
qtwidgets/views/TabBar.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 "TabBar.h"
13#include "DockWidget.h"
14#include "Stack.h"
15#include "kddockwidgets/core/DockWidget.h"
16#include "kddockwidgets/core/TabBar.h"
17#include "kddockwidgets/core/Stack.h"
18#include "core/Utils_p.h"
19#include "core/TabBar_p.h"
20#include "core/Logging_p.h"
21#include "Config.h"
23#include "kddockwidgets/core/DockRegistry.h"
24
25#include <QMouseEvent>
26#include <QApplication>
27#include <QProxyStyle>
28
29using namespace KDDockWidgets;
30using namespace KDDockWidgets::QtWidgets;
31
32namespace KDDockWidgets {
33namespace { // anonymous namespace to silence -Wweak-vtables
34class MyProxy : public QProxyStyle
35{
36 Q_OBJECT
37public:
38 MyProxy()
39 : QProxyStyle(qApp->style())
40 {
41 setParent(qApp);
42 }
43
44 int styleHint(QStyle::StyleHint hint, const QStyleOption *option = nullptr,
45 const QWidget *widget = nullptr,
46 QStyleHintReturn *returnData = nullptr) const override
47 {
49 // QTabBar has a bug which causes the paint event to dereference a tab which was already
50 // removed. Because, after the tab being removed, the d->pressedIndex is only reset
51 // after the animation ends. So disable the animation. Crash can be repro by enabling
52 // movable tabs, and detaching a tab quickly from a floating window containing two dock
53 // widgets. Reproduced on Windows
54 return 0;
55 }
56 return baseStyle()->styleHint(hint, option, widget, returnData);
57 }
58};
59}
60
61static MyProxy *proxyStyle()
62{
63 static auto *proxy = new MyProxy;
64 return proxy;
65}
66
67class QtWidgets::TabBar::Private
68{
69public:
70 explicit Private(Core::TabBar *controller)
71 : m_controller(controller)
72 {
73 }
74
75 void onTabMoved(int from, int to);
76
77 Core::TabBar *const m_controller;
78 KDBindings::ScopedConnection m_currentDockWidgetChangedConnection;
79};
80
81}
82
83TabBar::TabBar(Core::TabBar *controller, QWidget *parent)
84 : View(controller, Core::ViewType::TabBar, parent)
85 , TabBarViewInterface(controller)
86 , d(new Private(controller))
87{
89}
90
92{
93 delete d;
94}
95
97{
99 connect(this, &QTabBar::tabMoved, this, [this](int from, int to) {
100 d->onTabMoved(from, to);
101 });
102
103 d->m_currentDockWidgetChangedConnection = d->m_controller->dptr()->currentDockWidgetChanged.connect([this](KDDockWidgets::Core::DockWidget *dw) {
105 });
106}
107
108int TabBar::tabAt(QPoint localPos) const
109{
110 return QTabBar::tabAt(localPos);
111}
112
114{
115 d->m_controller->onMousePress(e->pos());
117}
118
120{
121 if (count() > 1) {
122 // Only allow to re-order tabs if we have more than 1 tab, otherwise it's just weird.
124 }
125}
126
128{
129 d->m_controller->onMouseDoubleClick(e->pos());
130}
131
133{
134 // Qt has a bug in QWidgetPrivate::deepestFocusProxy(), it doesn't honour visibility
135 // of the focus scope. Once an hidden widget is focused the chain is broken and tab
136 // stops working (#180)
137
138 auto parent = parentWidget();
139 if (!parent) {
140 // NOLINTNEXTLINE(bugprone-parent-virtual-call)
141 return QTabBar::event(ev);
142 }
143
144 // NOLINTNEXTLINE(bugprone-parent-virtual-call)
145 const bool result = QTabBar::event(ev);
146
147 if (ev->type() == QEvent::Show) {
148 parent->setFocusProxy(this);
149 } else if (ev->type() == QEvent::Hide) {
150 parent->setFocusProxy(nullptr);
151 }
152
153 return result;
154}
155
156QString TabBar::text(int index) const
157{
158 return tabText(index);
159}
160
161QRect TabBar::rectForTab(int index) const
162{
163 return QTabBar::tabRect(index);
164}
165
166void TabBar::moveTabTo(int from, int to)
167{
168 moveTab(from, to);
169}
170
171void TabBar::tabInserted(int index)
172{
176}
177
178void TabBar::tabRemoved(int index)
179{
180 QTabBar::tabRemoved(index);
183}
184
186{
188}
189
191{
192 if (auto tw = dynamic_cast<Stack *>(d->m_controller->stack()->view()))
193 return tw;
194
195 qWarning() << Q_FUNC_INFO << "Unexpected null QTabWidget";
196 return nullptr;
197}
198
199void TabBar::renameTab(int index, const QString &text)
200{
201 setTabText(index, text);
202}
203
204void TabBar::changeTabIcon(int index, const QIcon &icon)
205{
206 setTabIcon(index, icon);
207}
208
210{
211 auto tabWidget = static_cast<QTabWidget *>(View_qt::asQWidget(m_tabBar->stack()));
213}
214
215void TabBar::insertDockWidget(int index, Core::DockWidget *dw, const QIcon &icon,
216 const QString &title)
217{
218 auto tabWidget = static_cast<QTabWidget *>(View_qt::asQWidget(m_tabBar->stack()));
219 tabWidget->insertTab(index, View_qt::asQWidget(dw), icon, title);
220}
221
223{
225}
226
228{
229 return d->m_controller;
230}
231
232void TabBar::Private::onTabMoved(int from, int to)
233{
234 if (from == to || m_controller->isMovingTab())
235 return;
236
237 // !m_controller->isMovingTab() means the move was initiated by Qt
238 // for example the user is reordering tabs with mouse
239 // We need to tell the controller we got a new order.
240 m_controller->dptr()->moveTabTo(from, to);
241}
242
243#include "TabBar.moc"
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
The DockWidget base-class. DockWidget and Core::DockWidget are only split in two so we can share some...
void setCurrentIndex(int index)
int indexOfDockWidget(const Core::DockWidget *dw) const
Returns the tab index of the specified dock widget.
Controller * controller() const
Returns this view's controller.
QRect rectForTab(int index) const override
void mousePressEvent(QMouseEvent *) override
void renameTab(int index, const QString &) override
void changeTabIcon(int index, const QIcon &icon) override
void mouseMoveEvent(QMouseEvent *e) override
TabBar(Core::TabBar *controller, QWidget *parent=nullptr)
QString text(int index) const override
Returns the tab text for the specified index This is only used by tests, to make sure your tab's text...
void setTabsAreMovable(bool) override
Implement if your frontend will support reordering tabs with mouse Currently only the QtWidgets front...
void insertDockWidget(int index, Core::DockWidget *, const QIcon &, const QString &title) override
void moveTabTo(int from, int to) override
void mouseDoubleClickEvent(QMouseEvent *e) override
int tabAt(QPoint localPos) const override
void removeDockWidget(Core::DockWidget *) override
void currentDockWidgetChanged(KDDockWidgets::Core::DockWidget *)
Class to abstract QAction, so code still works with QtQuick and Flutter.
static MyProxy * proxyStyle()
QEvent::Type type() const const
QPoint pos() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
void currentChanged(int index)
void setCurrentIndex(int index)
virtual bool event(QEvent *event) override
virtual void mouseMoveEvent(QMouseEvent *event) override
virtual void mousePressEvent(QMouseEvent *event) override
void setMovable(bool movable)
void moveTab(int from, int to)
void setTabIcon(int index, const QIcon &icon)
void setTabText(int index, const QString &text)
int tabAt(const QPoint &position) const const
virtual void tabInserted(int index)
void tabMoved(int from, int to)
QRect tabRect(int index) const const
virtual void tabRemoved(int index)
QString tabText(int index) const const
int insertTab(int index, QWidget *page, const QString &label)
void removeTab(int index)
A factory class for allowing the user to customize some internal widgets.
Represents a dock widget.
QWidget * parentWidget() const const
void setStyle(QStyle *style)

© 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