Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtabbar_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTABBAR_P_H
6#define QTABBAR_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtWidgets/private/qtwidgetsglobal_p.h>
20#include <QtCore/private/qflatmap_p.h>
21#include "qtabbar.h"
22#include "private/qwidget_p.h"
23
24#include <qicon.h>
25#include <qtoolbutton.h>
26#include <qbasictimer.h>
27#if QT_CONFIG(animation)
28#include <qvariantanimation.h>
29#endif
30
31#define ANIMATION_DURATION 250
32
33#include <qstyleoption.h>
34#include <utility>
35
37
38QT_BEGIN_NAMESPACE
39
40class QMovableTabWidget : public QWidget
41{
42public:
43 explicit QMovableTabWidget(QWidget *parent = nullptr);
44 void setPixmap(const QPixmap &pixmap);
45
46protected:
47 void paintEvent(QPaintEvent *e) override;
48
49private:
50 QPixmap m_pixmap;
51};
52
53class Q_WIDGETS_EXPORT QTabBarPrivate : public QWidgetPrivate
54{
55 Q_DECLARE_PUBLIC(QTabBar)
56public:
57 QTabBarPrivate()
58 : layoutDirty(false), drawBase(true), elideModeSetByUser(false), useScrollButtons(false),
59 useScrollButtonsSetByUser(false), expanding(true), closeButtonOnTabs(false),
60 movable(false), dragInProgress(false), documentMode(false),
61 autoHide(false), changeCurrentOnDrag(false)
62 {}
63 ~QTabBarPrivate()
64 {
65 qDeleteAll(tabList);
66 }
67
68 QRect hoverRect;
69 QPoint dragStartPosition;
70 QPoint mousePosition = {-1, -1};
71#if QT_CONFIG(wheelevent)
72 QPoint accumulatedAngleDelta;
73#endif
74 QSize iconSize;
75 QToolButton* rightB = nullptr; // right or bottom
76 QToolButton* leftB = nullptr; // left or top
77 QMovableTabWidget *movingTab = nullptr;
78 int hoverIndex = -1;
79 int switchTabCurrentIndex = -1;
80 QBasicTimer switchTabTimer;
81 Qt::TextElideMode elideMode = Qt::ElideNone;
82 QTabBar::SelectionBehavior selectionBehaviorOnRemove = QTabBar::SelectRightTab;
83 QTabBar::Shape shape = QTabBar::RoundedNorth;
84 Qt::MouseButtons mouseButtons = Qt::NoButton;
85
86 int currentIndex = -1;
87 int pressedIndex = -1;
88 int firstVisible = 0;
89 int lastVisible = -1;
90 int scrollOffset = 0;
91
92 bool layoutDirty : 1;
93 bool drawBase : 1;
94 bool elideModeSetByUser : 1;
95 bool useScrollButtons : 1;
96 bool useScrollButtonsSetByUser : 1;
97 bool expanding : 1;
98 bool closeButtonOnTabs : 1;
99 bool movable : 1;
100 bool dragInProgress : 1;
101 bool documentMode : 1;
102 bool autoHide : 1;
103 bool changeCurrentOnDrag : 1;
104
105 struct Tab {
106 inline Tab(const QIcon &ico, const QString &txt)
107 : text(txt), icon(ico), enabled(true), visible(true), measuringMinimum(false)
108 {
109 }
110 /*
111 Tabs are managed by instance; they are not the same even
112 if all properties are the same.
113 */
114 Q_DISABLE_COPY_MOVE(Tab);
115
116 QString text;
117#if QT_CONFIG(tooltip)
118 QString toolTip;
119#endif
120#if QT_CONFIG(whatsthis)
121 QString whatsThis;
122#endif
123#if QT_CONFIG(accessibility)
124 QString accessibleName;
125#endif
126 QIcon icon;
127 QRect rect;
128 QRect minRect;
129 QRect maxRect;
130
131 QColor textColor;
132 QVariant data;
133 QWidget *leftWidget = nullptr;
134 QWidget *rightWidget = nullptr;
135 int shortcutId = 0;
136 int lastTab = -1;
137 int dragOffset = 0;
138 uint enabled : 1;
139 uint visible : 1;
140 uint measuringMinimum : 1;
141
142#if QT_CONFIG(animation)
143 struct TabBarAnimation : public QVariantAnimation {
144 TabBarAnimation(Tab *t, QTabBarPrivate *_priv) : tab(t), priv(_priv)
145 { setEasingCurve(QEasingCurve::InOutQuad); }
146
147 void updateCurrentValue(const QVariant &current) override;
148
149 void updateState(State newState, State) override;
150 private:
151 //these are needed for the callbacks
152 Tab *tab;
153 QTabBarPrivate *priv;
154 };
155 std::unique_ptr<TabBarAnimation> animation;
156
157 void startAnimation(QTabBarPrivate *priv, int duration) {
158 if (!priv->isAnimated()) {
159 priv->moveTabFinished(priv->tabList.indexOf(this));
160 return;
161 }
162 if (!animation)
163 animation = std::make_unique<TabBarAnimation>(this, priv);
164 animation->setStartValue(dragOffset);
165 animation->setEndValue(0);
166 animation->setDuration(duration);
167 animation->start();
168 }
169#else
170 void startAnimation(QTabBarPrivate *priv, int duration)
171 { Q_UNUSED(duration); priv->moveTabFinished(priv->tabList.indexOf(this)); }
172#endif // animation
173 };
174 QList<Tab*> tabList;
175 mutable QVarLengthFlatMap<QString, QSize, 8> textSizes;
176
177 void calculateFirstLastVisible(int index, bool visible, bool remove);
178 int selectNewCurrentIndexFrom(int currentIndex);
179 int calculateNewPosition(int from, int to, int index) const;
180 void slide(int from, int to);
181 void init();
182
183 inline Tab *at(int index) { return tabList.value(index, nullptr); }
184 inline const Tab *at(int index) const { return tabList.value(index, nullptr); }
185
186 int indexAtPos(const QPoint &p) const;
187
188 inline bool isAnimated() const { Q_Q(const QTabBar); return q->style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, q) > 0; }
189 inline bool validIndex(int index) const { return index >= 0 && index < tabList.size(); }
190 void setCurrentNextEnabledIndex(int offset);
191
192 void scrollTabs();
193 void closeTab();
194 void moveTab(int index, int offset);
195 void moveTabFinished(int index);
196
197 void refresh();
198 void layoutTabs();
199 void layoutWidgets(int start = 0);
200 void layoutTab(int index);
201 void updateMacBorderMetrics();
202 bool isTabInMacUnifiedToolbarArea() const;
203 void setupMovableTab();
204 void autoHideTabs();
205 QRect scrollRect(int index = -1) const;
206 QRect normalizedScrollRect(int index = -1) const;
207 int hoveredTabIndex() const;
208
209 void initBasicStyleOption(QStyleOptionTab *option, int tabIndex) const;
210
211 void makeVisible(int index);
212 const Tab *lastVisibleTab() const
213 {
214 auto it = std::find_if(tabList.rbegin(), tabList.rend(),
215 [](const Tab *tab) { return tab->visible; });
216 return it == tabList.rend() ? nullptr : *it;
217 }
218
219 // shared by tabwidget and qtabbar
220 static void initStyleBaseOption(QStyleOptionTabBarBase *optTabBase, QTabBar *tabbar, QSize size)
221 {
222 QStyleOptionTab tabOverlap;
223 tabOverlap.shape = tabbar->shape();
224 int overlap = tabbar->style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tabOverlap, tabbar);
225 QWidget *theParent = tabbar->parentWidget();
226 optTabBase->initFrom(tabbar);
227 optTabBase->shape = tabbar->shape();
228 optTabBase->documentMode = tabbar->documentMode();
229 if (theParent && overlap > 0) {
230 QRect rect;
231 switch (tabOverlap.shape) {
232 case QTabBar::RoundedNorth:
233 case QTabBar::TriangularNorth:
234 rect.setRect(0, size.height()-overlap, size.width(), overlap);
235 break;
236 case QTabBar::RoundedSouth:
237 case QTabBar::TriangularSouth:
238 rect.setRect(0, 0, size.width(), overlap);
239 break;
240 case QTabBar::RoundedEast:
241 case QTabBar::TriangularEast:
242 rect.setRect(0, 0, overlap, size.height());
243 break;
244 case QTabBar::RoundedWest:
245 case QTabBar::TriangularWest:
246 rect.setRect(size.width() - overlap, 0, overlap, size.height());
247 break;
248 }
249 optTabBase->rect = rect;
250 }
251 }
252
253 void killSwitchTabTimer();
254
255};
256
257constexpr inline bool verticalTabs(QTabBar::Shape shape) noexcept
258{
259 return shape == QTabBar::RoundedWest
260 || shape == QTabBar::RoundedEast
261 || shape == QTabBar::TriangularWest
262 || shape == QTabBar::TriangularEast;
263}
264
265QT_END_NAMESPACE
266
267#endif
QT_REQUIRE_CONFIG(tabbar)
constexpr bool verticalTabs(QTabBar::Shape shape) noexcept
Definition qtabbar_p.h:257