Falkon Develop
Cross-platform Qt-based web browser
combotabbar.h
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2013-2014 S. Razi Alavizadeh <s.r.alavizadeh@gmail.com>
4* Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
5*
6* This program is free software: you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <http://www.gnu.org/licenses/>.
18* ============================================================ */
19#ifndef COMBOTABBAR_H
20#define COMBOTABBAR_H
21
22#include "qzcommon.h"
23#include "wheelhelper.h"
24
25#include <QTabBar>
26#include <QScrollBar>
27#include <QAbstractButton>
28#include <QEasingCurve>
29#include <QStyleOption>
30
31class QScrollArea;
32class QPropertyAnimation;
33class QHBoxLayout;
34
36class TabBarHelper;
37class ToolButton;
38
39class FALKON_EXPORT ComboTabBar : public QWidget
40{
41 Q_OBJECT
42 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentChanged)
43 Q_PROPERTY(int count READ count)
44
45public:
46 enum SizeType {
52 ExtraReservedWidth
53 };
54
57 AfterTab
58 };
59
60 explicit ComboTabBar(QWidget* parent = nullptr);
61
62 int addTab(const QString &text);
63 int addTab(const QIcon &icon, const QString &text);
64
65 int insertTab(int index, const QString &text);
66 int insertTab(int index, const QIcon &icon, const QString &text, bool pinned = false);
67
68 void removeTab(int index);
69 void moveTab(int from, int to);
70
71 bool isTabEnabled(int index) const;
72 void setTabEnabled(int index, bool enabled);
73
74 QColor tabTextColor(int index) const;
75 void setTabTextColor(int index, const QColor &color);
76
77 QRect tabRect(int index) const;
78 QRect draggedTabRect() const;
79 QPixmap tabPixmap(int index) const;
80
81 // Returns tab index at pos, or -1
82 int tabAt(const QPoint &pos) const;
83
84 // Returns true if there is an empty area at pos
85 // (returns false if there are buttons or other widgets on the pos)
86 bool emptyArea(const QPoint &pos) const;
87
88 int mainTabBarCurrentIndex() const;
89 int currentIndex() const;
90 int count() const;
91
92 void setDrawBase(bool drawTheBase);
93 bool drawBase() const;
94
95 Qt::TextElideMode elideMode() const;
96 void setElideMode(Qt::TextElideMode elide);
97
98 QString tabText(int index) const;
99 void setTabText(int index, const QString &text);
100
101 void setTabToolTip(int index, const QString &tip);
102 QString tabToolTip(int index) const;
103
104 bool tabsClosable() const;
105 void setTabsClosable(bool closable);
106
107 void setTabButton(int index, QTabBar::ButtonPosition position, QWidget* widget);
108 QWidget* tabButton(int index, QTabBar::ButtonPosition position) const;
109
110 QTabBar::SelectionBehavior selectionBehaviorOnRemove() const;
111 void setSelectionBehaviorOnRemove(QTabBar::SelectionBehavior behavior);
112
113 bool expanding() const;
114 void setExpanding(bool enabled);
115
116 bool isMovable() const;
117 void setMovable(bool movable);
118
119 bool documentMode() const;
120 void setDocumentMode(bool set);
121
122 int pinnedTabsCount() const;
123 int normalTabsCount() const;
124 bool isPinned(int index) const;
125
126 void setFocusPolicy(Qt::FocusPolicy policy);
127 void setObjectName(const QString &name);
128 void setMouseTracking(bool enable);
129
130 void insertCloseButton(int index);
131 void setCloseButtonsToolTip(const QString &tip);
132
133 QTabBar::ButtonPosition iconButtonPosition() const;
134 QTabBar::ButtonPosition closeButtonPosition() const;
135
136 QSize iconButtonSize() const;
137 QSize closeButtonSize() const;
138
139 bool validIndex(int index) const;
140 void setCurrentNextEnabledIndex(int offset);
141
142 bool usesScrollButtons() const;
143 void setUsesScrollButtons(bool useButtons);
144
145 void showDropIndicator(int index, DropIndicatorPosition position);
146 void clearDropIndicator();
147
148 bool isDragInProgress() const;
149 bool isScrollInProgress() const;
150 bool isMainBarOverflowed() const;
151
152 // Width of all widgets in the corner
153 int cornerWidth(Qt::Corner corner) const;
154 // Add widget to the left/right corner
155 void addCornerWidget(QWidget* widget, Qt::Corner corner);
156
157 // Duration of tab slide animation when releasing dragged tab
158 static int slideAnimationDuration();
159
160public Q_SLOTS:
161 void setUpLayout();
162 void ensureVisible(int index = -1, int xmargin = -1);
163 void setCurrentIndex(int index);
164
165Q_SIGNALS:
166 void overFlowChanged(bool overFlow);
167 void currentChanged(int index);
168 void tabCloseRequested(int index);
169 void tabMoved(int from, int to);
171
172private Q_SLOTS:
173 void setMinimumWidths();
174 void slotCurrentChanged(int index);
175 void slotTabCloseRequested(int index);
176 void slotTabMoved(int from, int to);
177 void closeTabFromButton();
178 void updateTabBars();
179 void emitOverFlowChanged();
180
181protected:
182 int mainTabBarWidth() const;
183 int pinTabBarWidth() const;
184
185 bool event(QEvent *event) override;
186 void wheelEvent(QWheelEvent* event) override;
187 bool eventFilter(QObject* obj, QEvent* ev) override;
188 void paintEvent(QPaintEvent* ev) override;
189
190 virtual int comboTabBarPixelMetric(SizeType sizeType) const;
191 virtual QSize tabSizeHint(int index, bool fast = false) const;
192 virtual void tabInserted(int index);
193 virtual void tabRemoved(int index);
194
195private:
196 TabBarHelper* mainTabBar() const;
197 TabBarHelper* localTabBar(int index = -1) const;
198
199 int toLocalIndex(int globalIndex) const;
200 QRect mapFromLocalTabRect(const QRect &rect, QWidget *tabBar) const;
201 void updatePinnedTabBarVisibility();
202
203 QHBoxLayout* m_mainLayout;
204 QHBoxLayout* m_leftLayout;
205 QHBoxLayout* m_rightLayout;
206 QWidget* m_leftContainer;
207 QWidget* m_rightContainer;
208
209 TabBarHelper* m_mainTabBar;
210 TabBarHelper* m_pinnedTabBar;
211
212 TabBarScrollWidget* m_mainTabBarWidget;
213 TabBarScrollWidget* m_pinnedTabBarWidget;
214
215 QString m_closeButtonsToolTip;
216 bool m_mainBarOverFlowed;
217 bool m_lastAppliedOverflow;
218 bool m_usesScrollButtons;
219 bool m_blockCurrentChangedSignal;
220
221 WheelHelper m_wheelHelper;
222
223 friend class TabBarHelper;
224 friend class TabStackedWidget;
225};
226
227class FALKON_EXPORT TabBarHelper : public QTabBar
228{
229 Q_OBJECT
230 Q_PROPERTY(int tabPadding READ tabPadding WRITE setTabPadding)
231 Q_PROPERTY(QColor baseColor READ baseColor WRITE setBaseColor)
232
233public:
234 explicit TabBarHelper(bool isPinnedTabBar, ComboTabBar* comboTabBar);
235
236 int tabPadding() const;
237 void setTabPadding(int padding);
238
239 QColor baseColor() const;
240 void setBaseColor(const QColor &color);
241
242 void setTabButton(int index, QTabBar::ButtonPosition position, QWidget* widget);
243
244 QSize tabSizeHint(int index) const override;
245 QSize baseClassTabSizeHint(int index) const;
246
247 QRect draggedTabRect() const;
248 QPixmap tabPixmap(int index) const;
249
250 bool isActiveTabBar();
251 void setActiveTabBar(bool activate);
252
253 void removeTab(int index);
254
255 void setScrollArea(QScrollArea* scrollArea);
256 void useFastTabSizeHint(bool enabled);
257
258 void showDropIndicator(int index, ComboTabBar::DropIndicatorPosition position);
259 void clearDropIndicator();
260
261 bool isDisplayedOnViewPort(int globalLeft, int globalRight);
262 bool isDragInProgress() const;
263
264 static void initStyleBaseOption(QStyleOptionTabBarBase* optTabBase, QTabBar* tabbar, QSize size);
265
266public Q_SLOTS:
267 void setCurrentIndex(int index);
268
269private:
270 bool event(QEvent* ev) override;
271 void paintEvent(QPaintEvent* event) override;
272 void mousePressEvent(QMouseEvent* event) override;
273 void mouseMoveEvent(QMouseEvent *event) override;
274 void mouseReleaseEvent(QMouseEvent* event) override;
275
276 int dragOffset(QStyleOptionTab *option, int tabIndex) const;
277 void initStyleOption(QStyleOptionTab* option, int tabIndex) const;
278
279 ComboTabBar* m_comboTabBar;
280 QScrollArea* m_scrollArea;
281
282 int m_tabPadding = -1;
283 QColor m_baseColor;
284 int m_pressedIndex;
285 bool m_dragInProgress;
286 QPoint m_dragStartPosition;
287 class QMovableTabWidget *m_movingTab = nullptr;
288 bool m_activeTabBar;
289 bool m_isPinnedTabBar;
290 bool m_useFastTabSizeHint;
291 int m_dropIndicatorIndex = -1;
292 ComboTabBar::DropIndicatorPosition m_dropIndicatorPosition;
293};
294
295class FALKON_EXPORT TabScrollBar : public QScrollBar
296{
297 Q_OBJECT
298public:
299 explicit TabScrollBar(QWidget* parent = nullptr);
301
302 bool isScrolling() const;
303
304 void animateToValue(int to, QEasingCurve::Type type = QEasingCurve::OutQuad);
305
306private:
307 QPropertyAnimation* m_animation;
308};
309
310
311class FALKON_EXPORT TabBarScrollWidget : public QWidget
312{
313 Q_OBJECT
314public:
315 explicit TabBarScrollWidget(QTabBar* tabBar, QWidget* parent = nullptr);
316
317 QTabBar* tabBar();
318 QScrollArea* scrollArea();
319 TabScrollBar* scrollBar();
320
321 void scrollByWheel(QWheelEvent* event);
322
323 int scrollButtonsWidth() const;
324 bool usesScrollButtons() const;
325 void setUsesScrollButtons(bool useButtons);
326
327 bool isOverflowed() const;
328 int tabAt(const QPoint &pos) const;
329
330public Q_SLOTS:
331 void ensureVisible(int index = -1, int xmargin = 132);
332 void scrollToLeft(int n = 5, QEasingCurve::Type type = QEasingCurve::OutQuad);
333 void scrollToRight(int n = 5, QEasingCurve::Type type = QEasingCurve::OutQuad);
334 void scrollToLeftEdge();
335 void scrollToRightEdge();
336 void setUpLayout();
337
338private Q_SLOTS:
339 void overFlowChanged(bool overflowed);
340 void scrollStart();
341 void updateScrollButtonsState();
342
343private:
344 void mouseMoveEvent(QMouseEvent* event) override;
345 void resizeEvent(QResizeEvent* event) override;
346
347 QTabBar* m_tabBar;
348 QScrollArea* m_scrollArea;
349 TabScrollBar* m_scrollBar;
350 ToolButton* m_rightScrollButton;
351 ToolButton* m_leftScrollButton;
352 bool m_usesScrollButtons;
353 int m_totalVerticalDeltas;
354};
355
356// Class for close button on tabs
357// * taken from qtabbar.cpp
358class CloseButton : public QAbstractButton
359{
360 Q_OBJECT
361
362public:
363 CloseButton(QWidget *parent = nullptr);
364
365 QSize sizeHint() const override;
366
367 void enterEvent(QEnterEvent *event) override;
368 void leaveEvent(QEvent *event) override;
369 void paintEvent(QPaintEvent *event) override;
370};
371#endif // COMBOTABBAR_H
void leaveEvent(QEvent *event) override
void paintEvent(QPaintEvent *event) override
QSize sizeHint() const override
void enterEvent(QEnterEvent *event) override
CloseButton(QWidget *parent=nullptr)
void tabCloseRequested(int index)
@ OverflowedTabWidth
Definition: combotabbar.h:51
@ ActiveTabMinimumWidth
Definition: combotabbar.h:48
@ NormalTabMaximumWidth
Definition: combotabbar.h:50
@ NormalTabMinimumWidth
Definition: combotabbar.h:49
void scrollBarValueChanged(int value)
void currentChanged(int index)
void overFlowChanged(bool overFlow)
void tabMoved(int from, int to)
int value(const QColor &c)
Definition: colors.cpp:238
#define FALKON_EXPORT
Definition: qzcommon.h:28