KDDockWidgets API Documentation  1.6
TabWidgetWidget.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KDDockWidgets.
3 
4  SPDX-FileCopyrightText: 2019-2022 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 #include <QMenu>
31 
32 using namespace KDDockWidgets;
33 
34 TabWidgetWidget::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 
64  if (!QTabWidget::tabBar()->isVisible())
65  setFocusProxy(nullptr);
66 
67  setupTabBarButtons();
68 
69  setDocumentMode(options & TabWidgetOption_DocumentMode);
70 }
71 
72 TabBar *TabWidgetWidget::tabBar() const
73 {
74  return m_tabBar;
75 }
76 
77 int TabWidgetWidget::numDockWidgets() const
78 {
79  return count();
80 }
81 
82 void TabWidgetWidget::removeDockWidget(DockWidgetBase *dw)
83 {
84  removeTab(indexOf(dw));
85 }
86 
87 int TabWidgetWidget::indexOfDockWidget(const DockWidgetBase *dw) const
88 {
89  return indexOf(const_cast<DockWidgetBase *>(dw));
90 }
91 
92 void TabWidgetWidget::mouseDoubleClickEvent(QMouseEvent *ev)
93 {
94  if (onMouseDoubleClick(ev->pos())) {
95  ev->accept();
96  } else {
97  ev->ignore();
98  }
99 }
100 
101 void 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 
111 void TabWidgetWidget::tabInserted(int)
112 {
113  onTabInserted();
114 }
115 
116 void TabWidgetWidget::tabRemoved(int)
117 {
118  onTabRemoved();
119 }
120 
121 bool 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 
131 void TabWidgetWidget::setCurrentDockWidget(int index)
132 {
133  setCurrentIndex(index);
134 }
135 
136 bool 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 
143 void TabWidgetWidget::setTabBarAutoHide(bool b)
144 {
146 }
147 
148 void TabWidgetWidget::renameTab(int index, const QString &text)
149 {
150  setTabText(index, text);
151 }
152 
153 void TabWidgetWidget::changeTabIcon(int index, const QIcon &icon)
154 {
155  setTabIcon(index, icon);
156 }
157 
158 DockWidgetBase *TabWidgetWidget::dockwidgetAt(int index) const
159 {
160  return qobject_cast<DockWidgetBase *>(widget(index));
161 }
162 
163 int TabWidgetWidget::currentIndex() const
164 {
165  return QTabWidget::currentIndex();
166 }
167 
168 void TabWidgetWidget::setupTabBarButtons()
169 {
170  if (!(Config::self().flags() & Config::Flag_ShowButtonsOnTabBarIfTitleBarHidden))
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 
204 void 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 
211 void TabWidgetWidget::showContextMenu(QPoint pos)
212 {
213  if (!(Config::self().flags() & Config::Flag_AllowSwitchingTabsViaMenu))
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 }
QTabWidget
QMouseEvent::pos
QPoint pos() const const
QTabBar::count
count
QRect::setWidth
void setWidth(int width)
QRect
QWidget::customContextMenuRequested
void customContextMenuRequested(const QPoint &pos)
QWidget::rect
rect
QTabBar
QTabWidget::setTabBarAutoHide
void setTabBarAutoHide(bool enabled)
QWindow
QAbstractButton::clicked
void clicked(bool checked)
QWidget
QMenu
Qt::CustomContextMenu
CustomContextMenu
QPoint::y
int y() const const
Qt::TopRightCorner
TopRightCorner
QTabBar::tabAt
int tabAt(const QPoint &position) const const
QRect::contains
bool contains(const QRect &rectangle, bool proper) const const
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:75
QAction::setDisabled
void setDisabled(bool b)
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
QAction
KDDockWidgets
Definition: Config.cpp:37
KDDockWidgets::TabWidgetOption_DocumentMode
@ TabWidgetOption_DocumentMode
Definition: KDDockWidgets.h:286
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-2022 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 Thu Sep 15 2022 00:16:29 for KDDockWidgets API Documentation by doxygen 1.8.20