KDDockWidgets API Documentation  1.4
TabWidgetWidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2021 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"
21 #include "FrameworkWidgetFactory.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 
31 using namespace KDDockWidgets;
32 
33 TabWidgetWidget::TabWidgetWidget(Frame *parent)
34  : QTabWidget(parent)
35  , TabWidget(this, parent)
36  , m_tabBar(Config::self().frameworkWidgetFactory()->createTabBar(this))
37 {
38  setTabBar(static_cast<QTabBar *>(m_tabBar->asWidget()));
39  setTabsClosable(Config::self().flags() & Config::Flag_TabsHaveCloseButton);
40 
41  // In case tabs closable is set by the factory, a tabClosedRequested() is emitted when the user presses [x]
42  connect(this, &QTabWidget::tabCloseRequested, this, [this](int index) {
43  if (DockWidgetBase *dw = dockwidgetAt(index)) {
44  if (dw->options() & DockWidgetBase::Option_NotClosable) {
45  qWarning() << "QTabWidget::tabCloseRequested: Refusing to close dock widget with Option_NotClosable option. name=" << dw->uniqueName();
46  } else {
47  dw->close();
48  }
49  } else {
50  qWarning() << "QTabWidget::tabCloseRequested Couldn't find dock widget for index" << index << "; count=" << count();
51  }
52  });
53 
54  connect(this, &QTabWidget::currentChanged, this, [this](int index) {
55  onCurrentTabChanged(index);
56  Q_EMIT currentTabChanged(index);
57  Q_EMIT currentDockWidgetChanged(currentDockWidget());
58  });
59 
60  if (!QTabWidget::tabBar()->isVisible())
61  setFocusProxy(nullptr);
62 
63  setupTabBarButtons();
64 }
65 
66 TabBar *TabWidgetWidget::tabBar() const
67 {
68  return m_tabBar;
69 }
70 
71 int TabWidgetWidget::numDockWidgets() const
72 {
73  return count();
74 }
75 
76 void TabWidgetWidget::removeDockWidget(DockWidgetBase *dw)
77 {
78  removeTab(indexOf(dw));
79 }
80 
81 int TabWidgetWidget::indexOfDockWidget(const DockWidgetBase *dw) const
82 {
83  return indexOf(const_cast<DockWidgetBase *>(dw));
84 }
85 
86 void TabWidgetWidget::mouseDoubleClickEvent(QMouseEvent *ev)
87 {
88  if (onMouseDoubleClick(ev->pos())) {
89  ev->accept();
90  } else {
91  ev->ignore();
92  }
93 }
94 
95 void TabWidgetWidget::mousePressEvent(QMouseEvent *ev)
96 {
98 
99  if ((Config::self().flags() & Config::Flag_TitleBarIsFocusable) && !frame()->isFocused()) {
100  // User clicked on the tab widget itself
101  frame()->FocusScope::focus(Qt::MouseFocusReason);
102  }
103 }
104 
105 void TabWidgetWidget::tabInserted(int)
106 {
107  onTabInserted();
108 }
109 
110 void TabWidgetWidget::tabRemoved(int)
111 {
112  onTabRemoved();
113 }
114 
115 bool TabWidgetWidget::isPositionDraggable(QPoint p) const
116 {
117  if (tabPosition() != QTabWidget::North) {
118  qWarning() << Q_FUNC_INFO << "Not implemented yet. Only North is supported";
119  return false;
120  }
121 
122  return p.y() >= 0 && p.y() <= QTabWidget::tabBar()->height();
123 }
124 
125 void TabWidgetWidget::setCurrentDockWidget(int index)
126 {
127  setCurrentIndex(index);
128 }
129 
130 bool TabWidgetWidget::insertDockWidget(int index, DockWidgetBase *dw,
131  const QIcon &icon, const QString &title)
132 {
133  insertTab(index, dw, icon, title);
134  return true;
135 }
136 
137 void TabWidgetWidget::setTabBarAutoHide(bool b)
138 {
140 }
141 
142 void TabWidgetWidget::renameTab(int index, const QString &text)
143 {
144  setTabText(index, text);
145 }
146 
147 void TabWidgetWidget::changeTabIcon(int index, const QIcon &icon)
148 {
149  setTabIcon(index, icon);
150 }
151 
152 DockWidgetBase *TabWidgetWidget::dockwidgetAt(int index) const
153 {
154  return qobject_cast<DockWidgetBase *>(widget(index));
155 }
156 
157 int TabWidgetWidget::currentIndex() const
158 {
159  return QTabWidget::currentIndex();
160 }
161 
162 void TabWidgetWidget::setupTabBarButtons()
163 {
164  if (!(Config::self().flags() & Config::Flag_ShowButtonsOnTabBarIfTitleBarHidden))
165  return;
166 
167  auto factory = Config::self().frameworkWidgetFactory();
168  m_closeButton = factory->createTitleBarButton(this, TitleBarButtonType::Close);
169  m_floatButton = factory->createTitleBarButton(this, TitleBarButtonType::Float);
170 
171  auto cornerWidget = new QWidget(this);
172  cornerWidget->setObjectName(QStringLiteral("Corner Widget"));
173 
174  setCornerWidget(cornerWidget, Qt::TopRightCorner);
175 
176  m_cornerWidgetLayout = new QHBoxLayout(cornerWidget);
177 
178  m_cornerWidgetLayout->addWidget(m_floatButton);
179  m_cornerWidgetLayout->addWidget(m_closeButton);
180 
181  connect(m_floatButton, &QAbstractButton::clicked, this, [this] {
182  TitleBar *tb = frame()->titleBar();
183  tb->onFloatClicked();
184  });
185 
186  connect(m_closeButton, &QAbstractButton::clicked, this, [this] {
187  TitleBar *tb = frame()->titleBar();
188  tb->onCloseClicked();
189  });
190 
191  updateMargins();
192  connect(DockRegistry::self(), &DockRegistry::windowChangedScreen, this, [this](QWindow *w) {
193  if (w == window()->windowHandle())
194  updateMargins();
195  });
196 }
197 
198 void TabWidgetWidget::updateMargins()
199 {
200  const qreal factor = logicalDpiFactor(this);
201  m_cornerWidgetLayout->setContentsMargins(QMargins(0, 0, 2, 0) * factor);
202  m_cornerWidgetLayout->setSpacing(int(2 * factor));
203 }
QTabWidget
QMouseEvent::pos
QPoint pos() const const
QTabBar
QTabWidget::setTabBarAutoHide
void setTabBarAutoHide(bool enabled)
QWindow
QAbstractButton::clicked
void clicked(bool checked)
QWidget
QPoint::y
int y() const const
Qt::TopRightCorner
TopRightCorner
QMouseEvent
QTabWidget::North
North
QWidget::isVisible
bool isVisible() const const
Qt::MouseFocusReason
MouseFocusReason
QString
QTabWidget::currentChanged
void currentChanged(int index)
KDDockWidgets::Config
Singleton to allow to choose certain behaviours of the framework.
Definition: Config.h:55
QIcon
QMargins
Config.h
Application-wide config to tune certain behaviours of the framework.
QTabWidget::tabBar
QTabBar * tabBar() const const
QWidget::height
height
QHBoxLayout
QEvent::ignore
void ignore()
KDDockWidgets::DockWidgetBase
The DockWidget base-class. DockWidget and DockWidgetBase are only split in two so we can share some c...
Definition: DockWidgetBase.h:61
QTabWidget::currentIndex
currentIndex
KDDockWidgets
Definition: Config.cpp:36
QPoint
QEvent::accept
void accept()
FrameworkWidgetFactory.h
A factory class for allowing the user to customize some internal widgets.
QTabWidget::tabCloseRequested
void tabCloseRequested(int index)
QWidget::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event)

© 2019-2021 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 Mon Nov 15 2021 00:17:28 for KDDockWidgets API Documentation by doxygen 1.8.20