File: fancytabwidget.h

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (229 lines) | stat: -rw-r--r-- 6,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 */
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#ifndef FANCYTABWIDGET_H
#define FANCYTABWIDGET_H

#include <QIcon>
#include <QProxyStyle>
#include <QScopedPointer>
#include <QStringList>
#include <QTabBar>
#include <QTimer>
#include <QWidget>

class QPainter;
class QSignalMapper;
class QStackedWidget;
class QStatusBar;
class QVBoxLayout;

class FancyTabProxyStyle : public QProxyStyle {
	Q_OBJECT

public:
	void drawPrimitive(PrimitiveElement element, const QStyleOption* option, QPainter* p, const QWidget* widget) const override;
	void drawControl(ControlElement element, const QStyleOption* option, QPainter* p, const QWidget* widget) const override;
	void polish(QWidget* widget) override;
	void polish(QApplication* app) override;
	void polish(QPalette& palette) override;

protected:
	bool eventFilter(QObject* o, QEvent* e) override;
};

class FancyTabBar;
class FancyTab : public QWidget {
public:
	FancyTab(FancyTabBar* tabbar);

	QSize sizeHint() const override;
	QIcon icon;
	QString text;
	bool underMouse;

protected:
	void enterEvent(QEnterEvent*) override;
	void leaveEvent(QEvent*) override;

private:
	FancyTabBar* tabbar;
};

class FancyTabBar : public QWidget {
	Q_OBJECT

public:
	enum Pos {
		Side,
		Top,
		Bot
	};

	FancyTabBar(QWidget* parent, bool text, int iSize, Pos pos);
	~FancyTabBar() override;

	void paintEvent(QPaintEvent* event) override;
	void paintTab(QPainter* painter, int tabIndex) const;
	void mousePressEvent(QMouseEvent*) override;
	void wheelEvent(QWheelEvent* ev) override;
	bool validIndex(int index) const { return index >= 0 && index < tabs.count(); }

	QSize sizeHint() const override;
	QSize minimumSizeHint() const override;

	void addTab(const QIcon& icon, const QString& label, const QString& tt);
	void addSpacer(int size = 40);
	void removeTab(int index) { delete tabs.takeAt(index); }
	void setCurrentIndex(int index);
	int currentIndex() const { return currentIdx; }

	void setTabToolTip(int index, const QString& toolTip);
	QString tabToolTip(int index) const;

	QIcon tabIcon(int index) const { return tabs.at(index)->icon; }
	QString tabText(int index) const { return tabs.at(index)->text; }
	int count() const { return tabs.count(); }
	QRect tabRect(int index) const;
	bool showText() const { return withText; }
	int iconSize() const { return icnSize; }
	QSize tabSizeHint() const;
	Pos position() const { return pos; }

Q_SIGNALS:
	void currentChanged(int);

public Q_SLOTS:
	void emitCurrentIndex();

private:
	int currentIdx;
	QList<FancyTab*> tabs;
	QTimer triggerTimer;
	bool withText : 1;
	Pos pos : 2;
	int icnSize;
};

class FancyTabWidget : public QWidget {
	Q_OBJECT

public:
	static void setup();
	static int iconSize(bool large = true);

	FancyTabWidget(QWidget* parent);

	enum Style {
		Side = 0x0001,
		Top = 0x0002,
		Bot = 0x0003,

		Large = 0x0010,
		Small = 0x0020,
		Tab = 0x0030,

		IconOnly = 0x0100,

		Position_Mask = 0x000F,
		Style_Mask = 0x00F0,
		Options_Mask = 0x0100,
		All_Mask = Position_Mask | Style_Mask | Options_Mask
	};

	struct Item {
		Item(const QIcon& icon, const QString& label, const QString& tt, bool en)
			: type(Type_Tab), tabLabel(label), tabTooltip(tt), tabIcon(icon), spacerSize(0), enabled(en), index(-1) {}
		Item(int size) : type(Type_Spacer), spacerSize(size), enabled(true), index(-1) {}

		enum Type {
			Type_Tab,
			Type_Spacer
		};

		Type type;
		QString tabLabel;
		QString tabTooltip;
		QIcon tabIcon;
		int spacerSize;
		bool enabled;
		int index;
	};

	void addTab(QWidget* tab, const QIcon& icon, const QString& label, const QString& tt = QString(), bool enabled = true);
	int currentIndex() const;
	QWidget* currentWidget() const;
	bool isEnabled(int index) const { return index >= 0 && index < items.count() ? items[index].enabled : false; }
	QWidget* widget(int index) const;
	int count() const;
	int visibleCount() const;
	int style() const { return styleSetting; }
	QSize tabSize() const;
	void setIcon(int index, const QIcon& icon);
	void setToolTip(int index, const QString& tt);
	void recreate();
	void setHiddenPages(const QStringList& hidden);
	QStringList hiddenPages() const;

public Q_SLOTS:
	void setCurrentIndex(int index);
	void setStyle(int s);
	void toggleTab(int tab, bool show);

Q_SIGNALS:
	void currentChanged(int index);
	void styleChanged(int styleSetting);
	void tabToggled(int index);

private Q_SLOTS:
	void showWidget(int index);

private:
	void makeTabBar(QTabBar::Shape shape, bool text, bool icons, bool fancy);
	int tabToIndex(int tab) const;
	int IndexToTab(int index) const { return index >= 0 && index < items.count() ? items[index].index : 0; }

private:
	int styleSetting;
	QList<Item> items;
	QWidget* tabBar;
	QStackedWidget* stack_;
	QWidget* sideWidget;
	QVBoxLayout* sideLayout;
	QVBoxLayout* topLayout;
	QScopedPointer<FancyTabProxyStyle> proxyStyle;
};

#endif// FANCYTABWIDGET_H