1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29 
30 #ifndef FANCYTABWIDGET_H
31 #define FANCYTABWIDGET_H
32 
33 #include "qzcommon.h"
34 
35 #include <QIcon>
36 #include <QProxyStyle>
37 #include <QTabBar>
38 #include <QTimer>
39 #include <QWidget>
40 #include <QPropertyAnimation>
41 
42 class QActionGroup;
43 class QMenu;
44 class QPainter;
45 class QSignalMapper;
46 class QStackedLayout;
47 class QStatusBar;
48 class QVBoxLayout;
49 
50 namespace Core
51 {
52 namespace Internal
53 {
54 
55 class FALKON_EXPORT FancyTabProxyStyle : public QProxyStyle
56 {
57     Q_OBJECT
58 
59 public:
60     void drawControl(ControlElement element, const QStyleOption* option,
61                      QPainter* painter, const QWidget* widget) const override;
62     void polish(QWidget* widget) override;
63     void polish(QApplication* app) override;
64     void polish(QPalette &palette) override;
65 
66 protected:
67     bool eventFilter(QObject* o, QEvent* e) override;
68 };
69 
70 class FALKON_EXPORT FancyTab : public QWidget
71 {
72     Q_OBJECT
73 
74     Q_PROPERTY(float fader READ fader WRITE setFader)
75 public:
76     FancyTab(QWidget* tabbar);
fader()77     float fader() { return m_fader; }
78     void setFader(float value);
79 
80     QSize sizeHint() const override;
81 
82     void fadeIn();
83     void fadeOut();
84 
85     QIcon icon;
86     QString text;
87 
88 protected:
89     void enterEvent(QEvent*) override;
90     void leaveEvent(QEvent*) override;
91 
92 private:
93     QPropertyAnimation animator;
94     QWidget* tabbar;
95     float m_fader;
96 };
97 
98 class FALKON_EXPORT FancyTabBar : public QWidget
99 {
100     Q_OBJECT
101 
102 public:
103     explicit FancyTabBar(QWidget* parent = nullptr);
104     ~FancyTabBar() override;
105 
106     void paintEvent(QPaintEvent* event) override;
107     void paintTab(QPainter* painter, int tabIndex) const;
108     void mousePressEvent(QMouseEvent*) override;
validIndex(int index)109     bool validIndex(int index) const { return index >= 0 && index < m_tabs.count(); }
110 
111     QSize sizeHint() const override;
112     QSize minimumSizeHint() const override;
113 
114     void addTab(const QIcon &icon, const QString &label);
115     void addSpacer(int size = 40);
removeTab(int index)116     void removeTab(int index) {
117         FancyTab* tab = m_tabs.takeAt(index);
118         delete tab;
119     }
120     void setCurrentIndex(int index);
currentIndex()121     int currentIndex() const { return m_currentIndex; }
122 
123     void setTabToolTip(int index, const QString &toolTip);
124     QString tabToolTip(int index) const;
125 
tabIcon(int index)126     QIcon tabIcon(int index) const {return m_tabs.at(index)->icon; }
tabText(int index)127     QString tabText(int index) const { return m_tabs.at(index)->text; }
count()128     int count() const {return m_tabs.count(); }
129     QRect tabRect(int index) const;
130 
131 Q_SIGNALS:
132     void currentChanged(int);
133 
134 public Q_SLOTS:
135     void emitCurrentIndex();
136 
137 private:
138     static const int m_rounding;
139     static const int m_textPadding;
140     int m_currentIndex;
141     QList<FancyTab*> m_tabs;
142     QTimer m_triggerTimer;
143     QSize tabSizeHint(bool minimum = false) const;
144 
145 };
146 
147 class FALKON_EXPORT FancyTabWidget : public QWidget
148 {
149     Q_OBJECT
150 
151     Q_PROPERTY(QPixmap bgPixmap READ bgPixmap WRITE SetBackgroundPixmap)
152 
153 public:
154     explicit FancyTabWidget(QWidget* parent = nullptr);
155 
156     // Values are persisted - only add to the end
157     enum Mode {
158         Mode_None = 0,
159 
160         Mode_LargeSidebar = 1,
161         Mode_SmallSidebar = 2,
162         Mode_Tabs = 3,
163         Mode_IconOnlyTabs = 4,
164         Mode_PlainSidebar = 5,
165     };
166 
167     struct Item {
ItemItem168         Item(const QIcon &icon, const QString &label)
169             : type_(Type_Tab), tab_label_(label), tab_icon_(icon), spacer_size_(0) {}
ItemItem170         Item(int size) : type_(Type_Spacer), spacer_size_(size) {}
171 
172         enum Type {
173             Type_Tab,
174             Type_Spacer,
175         };
176 
177         Type type_;
178         QString tab_label_;
179         QIcon tab_icon_;
180         int spacer_size_;
181     };
182 
183     void AddTab(QWidget* tab, const QIcon &icon, const QString &label);
184     void AddSpacer(int size = 40);
185     void SetBackgroundPixmap(const QPixmap &pixmap);
186 
187     void AddBottomWidget(QWidget* widget);
188 
189     int current_index() const;
mode()190     Mode mode() const { return mode_; }
bgPixmap()191     QPixmap bgPixmap() { return background_pixmap_; }
192 
193 public Q_SLOTS:
194     void SetCurrentIndex(int index);
195     void SetMode(Mode mode);
SetMode(int mode)196     void SetMode(int mode) { SetMode(Mode(mode)); }
197 
198 Q_SIGNALS:
199     void CurrentChanged(int index);
200     void ModeChanged(FancyTabWidget::Mode mode);
201 
202 protected:
203     void paintEvent(QPaintEvent* event) override;
204     void contextMenuEvent(QContextMenuEvent* e) override;
205 
206 private Q_SLOTS:
207     void ShowWidget(int index);
208 
209 private:
210     void MakeTabBar(QTabBar::Shape shape, bool text, bool icons, bool fancy);
211     void AddMenuItem(QSignalMapper* mapper, QActionGroup* group,
212                      const QString &text, Mode mode);
213 
214     Mode mode_;
215     QList<Item> items_;
216 
217     QWidget* tab_bar_;
218     QStackedLayout* stack_;
219     QPixmap background_pixmap_;
220     QWidget* side_widget_;
221     QVBoxLayout* side_layout_;
222     QVBoxLayout* top_layout_;
223 
224     bool use_background_;
225 
226     QMenu* menu_;
227 
228     FancyTabProxyStyle* proxy_style_;
229 };
230 
231 } // namespace Internal
232 } // namespace Core
233 
234 using Core::Internal::FancyTab;
235 using Core::Internal::FancyTabBar;
236 using Core::Internal::FancyTabWidget;
237 
238 #endif // FANCYTABWIDGET_H
239