KDDockWidgets API Documentation 1.7
Loading...
Searching...
No Matches
TabWidgetWidget.cpp
Go to the documentation of this file.
1/*
2 This file is part of KDDockWidgets.
3
4 SPDX-FileCopyrightText: 2019-2023 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
19#include "TabWidgetWidget_p.h"
20#include "Config.h"
22#include "../Frame_p.h"
23#include "../TitleBar_p.h"
24#include "../DockRegistry_p.h"
25
26#include <QMouseEvent>
27#include <QTabBar>
28#include <QHBoxLayout>
29#include <QAbstractButton>
30#include <QMenu>
31
32using namespace KDDockWidgets;
33
34TabWidgetWidget::TabWidgetWidget(Frame *parent, TabWidgetOptions options)
35 : QTabWidget(parent)
36 , TabWidget(this, parent)
37 , m_tabBar(Config::self().frameworkWidgetFactory()->createTabBar(this))
38{
39 setTabBar(static_cast<QTabBar *>(m_tabBar->asWidget()));
40 setTabsClosable(Config::self().flags() & Config::Flag_TabsHaveCloseButton);
41
42 setContextMenuPolicy(Qt::CustomContextMenu);
43 connect(this, &QTabWidget::customContextMenuRequested, this, &TabWidgetWidget::showContextMenu);
44
45 // In case tabs closable is set by the factory, a tabClosedRequested() is emitted when the user presses [x]
46 connect(this, &QTabWidget::tabCloseRequested, this, [this](int index) {
47 if (DockWidgetBase *dw = dockwidgetAt(index)) {
48 if (dw->options() & DockWidgetBase::Option_NotClosable) {
49 qWarning() << "QTabWidget::tabCloseRequested: Refusing to close dock widget with Option_NotClosable option. name=" << dw->uniqueName();
50 } else {
51 dw->close();
52 }
53 } else {
54 qWarning() << "QTabWidget::tabCloseRequested Couldn't find dock widget for index" << index << "; count=" << count();
55 }
56 });
57
58 connect(this, &QTabWidget::currentChanged, this, [this](int index) {
59 onCurrentTabChanged(index);
60 Q_EMIT currentTabChanged(index);
61 Q_EMIT currentDockWidgetChanged(currentDockWidget());
62 });
63
65 setFocusProxy(nullptr);
66
67 setupTabBarButtons();
68
69 setDocumentMode(options & TabWidgetOption_DocumentMode);
70}
71
72TabBar *TabWidgetWidget::tabBar() const
73{
74 return m_tabBar;
75}
76
77int TabWidgetWidget::numDockWidgets() const
78{
79 return count();
80}
81
82void TabWidgetWidget::removeDockWidget(DockWidgetBase *dw)
83{
84 removeTab(indexOf(dw));
85}
86
87int TabWidgetWidget::indexOfDockWidget(const DockWidgetBase *dw) const
88{
89 return indexOf(const_cast<DockWidgetBase *>(dw));
90}
91
92void TabWidgetWidget::mouseDoubleClickEvent(QMouseEvent *ev)
93{
94 if (onMouseDoubleClick(ev->pos())) {
95 ev->accept();
96 } else {
97 ev->ignore();
98 }
99}
100
101void TabWidgetWidget::mousePressEvent(QMouseEvent *ev)
102{
104
105 if ((Config::self().flags() & Config::Flag_TitleBarIsFocusable) && !frame()->isFocused()) {
106 // User clicked on the tab widget itself
107 frame()->FocusScope::focus(Qt::MouseFocusReason);
108 }
109}
110
111void TabWidgetWidget::tabInserted(int)
112{
113 onTabInserted();
114}
115
116void TabWidgetWidget::tabRemoved(int)
117{
118 onTabRemoved();
119}
120
121bool TabWidgetWidget::isPositionDraggable(QPoint p) const
122{
123 if (tabPosition() != QTabWidget::North) {
124 qWarning() << Q_FUNC_INFO << "Not implemented yet. Only North is supported";
125 return false;
126 }
127
128 return p.y() >= 0 && p.y() <= QTabWidget::tabBar()->height();
129}
130
131void TabWidgetWidget::setCurrentDockWidget(int index)
132{
133 setCurrentIndex(index);
134}
135
136bool TabWidgetWidget::insertDockWidget(int index, DockWidgetBase *dw,
137 const QIcon &icon, const QString &title)
138{
139 insertTab(index, dw, icon, title);
140 return true;
141}
142
143void TabWidgetWidget::setTabBarAutoHide(bool b)
144{
146}
147
148void TabWidgetWidget::renameTab(int index, const QString &text)
149{
150 setTabText(index, text);
151}
152
153void TabWidgetWidget::changeTabIcon(int index, const QIcon &icon)
154{
155 setTabIcon(index, icon);
156}
157
158DockWidgetBase *TabWidgetWidget::dockwidgetAt(int index) const
159{
160 return qobject_cast<DockWidgetBase *>(widget(index));
161}
162
163int TabWidgetWidget::currentIndex() const
164{
166}
167
168void TabWidgetWidget::setupTabBarButtons()
169{
171 return;
172
173 auto factory = Config::self().frameworkWidgetFactory();
174 m_closeButton = factory->createTitleBarButton(this, TitleBarButtonType::Close);
175 m_floatButton = factory->createTitleBarButton(this, TitleBarButtonType::Float);
176
177 auto cornerWidget = new QWidget(this);
178 cornerWidget->setObjectName(QStringLiteral("Corner Widget"));
179
180 setCornerWidget(cornerWidget, Qt::TopRightCorner);
181
182 m_cornerWidgetLayout = new QHBoxLayout(cornerWidget);
183
184 m_cornerWidgetLayout->addWidget(m_floatButton);
185 m_cornerWidgetLayout->addWidget(m_closeButton);
186
187 connect(m_floatButton, &QAbstractButton::clicked, this, [this] {
188 TitleBar *tb = frame()->titleBar();
189 tb->onFloatClicked();
190 });
191
192 connect(m_closeButton, &QAbstractButton::clicked, this, [this] {
193 TitleBar *tb = frame()->titleBar();
194 tb->onCloseClicked();
195 });
196
197 updateMargins();
198 connect(DockRegistry::self(), &DockRegistry::windowChangedScreen, this, [this](QWindow *w) {
199 if (w == window()->windowHandle())
200 updateMargins();
201 });
202}
203
204void TabWidgetWidget::updateMargins()
205{
206 const qreal factor = logicalDpiFactor(this);
207 m_cornerWidgetLayout->setContentsMargins(QMargins(0, 0, 2, 0) * factor);
208 m_cornerWidgetLayout->setSpacing(int(2 * factor));
209}
210
211void TabWidgetWidget::showContextMenu(QPoint pos)
212{
214 return;
215
216 QTabBar *tabBar = QTabWidget::tabBar();
217 // We don't want context menu if there is only one tab
218 if (tabBar->count() <= 1)
219 return;
220
221 // Click on a tab => No menu
222 if (tabBar->tabAt(pos) >= 0)
223 return;
224
225 // Right click is allowed only on the tabs area
226 QRect tabAreaRect = tabBar->rect();
227 tabAreaRect.setWidth(this->width());
228 if (!tabAreaRect.contains(pos))
229 return;
230
231 QMenu menu(this);
232 for (int i = 0; i < tabBar->count(); ++i) {
233 QAction *action = menu.addAction(tabText(i), this, [this, i] {
234 setCurrentIndex(i);
235 });
236 if (i == currentIndex())
237 action->setDisabled(true);
238 }
239 menu.exec(mapToGlobal(pos));
240}
Application-wide config to tune certain behaviours of the framework.
A factory class for allowing the user to customize some internal widgets.
Singleton to allow to choose certain behaviours of the framework.
Definition Config.h:75
FrameworkWidgetFactory * frameworkWidgetFactory() const
getter for the framework widget factory
Definition Config.cpp:145
static Config & self()
returns the singleton Config instance
Definition Config.cpp:84
@ Flag_AllowSwitchingTabsViaMenu
Allow switching tabs via a context menu when right clicking on the tab area.
Definition Config.h:108
@ Flag_ShowButtonsOnTabBarIfTitleBarHidden
When using Flag_HideTitleBarWhenTabsVisible the close/float buttons disappear with the title bar....
Definition Config.h:107
@ Flag_TabsHaveCloseButton
Tabs will have a close button. Equivalent to QTabWidget::setTabsClosable(true).
Definition Config.h:95
@ Flag_TitleBarIsFocusable
You can click the title bar and it will focus the last focused widget in the focus scope....
Definition Config.h:98
The DockWidget base-class. DockWidget and DockWidgetBase are only split in two so we can share some c...
@ Option_NotClosable
The DockWidget can't be closed on the [x], only programmatically.
virtual QAbstractButton * createTitleBarButton(QWidget *parent, TitleBarButtonType) const =0
Called internally by the framework to create a title bar button parent the button's parent.
void clicked(bool checked)
void setDisabled(bool b)
void accept()
void ignore()
QPoint pos() const const
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
void currentChanged(int index)
QTabBar * tabBar() const const
void setTabBarAutoHide(bool enabled)
void tabCloseRequested(int index)
void customContextMenuRequested(const QPoint &pos)
virtual void mousePressEvent(QMouseEvent *event)
bool isVisible() const const

© 2019-2023 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 on Wed Nov 1 2023 00:02:31 for KDDockWidgets API Documentation by doxygen 1.9.8