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{
175}
176
177void TabBar::tabRemoved(int index)
178{
179 QTabBar::tabRemoved(index);
181}
182
184{
186}
187
189{
190 if (auto tw = dynamic_cast<Stack *>(d->m_controller->stack()->view()))
191 return tw;
192
193 qWarning() << Q_FUNC_INFO << "Unexpected null QTabWidget";
194 return nullptr;
195}
196
197void TabBar::renameTab(int index, const QString &text)
198{
199 setTabText(index, text);
200}
201
202void TabBar::changeTabIcon(int index, const QIcon &icon)
203{
204 setTabIcon(index, icon);
205}
206
208{
209 auto tabWidget = static_cast<QTabWidget *>(View_qt::asQWidget(m_tabBar->stack()));
211}
212
213void TabBar::insertDockWidget(int index, Core::DockWidget *dw, const QIcon &icon,
214 const QString &title)
215{
216 auto tabWidget = static_cast<QTabWidget *>(View_qt::asQWidget(m_tabBar->stack()));
217 tabWidget->insertTab(index, View_qt::asQWidget(dw), icon, title);
218}
219
221{
223}
224
226{
227 return d->m_controller;
228}
229
230void TabBar::Private::onTabMoved(int from, int to)
231{
232 if (from == to || m_controller->isMovingTab())
233 return;
234
235 // !m_controller->isMovingTab() means the move was initiated by Qt
236 // for example the user is reordering tabs with mouse
237 // We need to tell the controller we got a new order.
238 m_controller->dptr()->moveTabTo(from, to);
239}
240
241#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