| 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 ([email protected]) |
| 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 <QIcon> |
| 34 | #include <QPropertyAnimation> |
| 35 | #include <QProxyStyle> |
| 36 | #include <QTabBar> |
| 37 | #include <QTimer> |
| 38 | #include <QWidget> |
| 39 | |
| 40 | //#include <boost/scoped_ptr.hpp> |
| 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 | namespace Internal { |
| 52 | |
| 53 | class FancyTabProxyStyle : public QProxyStyle { |
| 54 | Q_OBJECT |
| 55 | |
| 56 | public: |
| 57 | void drawControl(ControlElement element, const QStyleOption* option, |
| 58 | QPainter* painter, const QWidget* widget) const; |
| 59 | void polish(QWidget* widget); |
| 60 | void polish(QApplication* app); |
| 61 | void polish(QPalette& palette); |
| 62 | |
| 63 | protected: |
| 64 | bool eventFilter(QObject* o, QEvent* e); |
| 65 | }; |
| 66 | |
| 67 | class FancyTab : public QWidget{ |
| 68 | Q_OBJECT |
| 69 | |
| 70 | Q_PROPERTY(float fader READ fader WRITE setFader) |
| 71 | public: |
| 72 | FancyTab(QWidget *tabbar); |
| 73 | float fader() { return m_fader; } |
| 74 | void setFader(float value); |
| 75 | |
| 76 | QSize sizeHint() const; |
| 77 | |
| 78 | void fadeIn(); |
| 79 | void fadeOut(); |
| 80 | |
| 81 | QIcon icon; |
| 82 | QString text; |
| 83 | |
| 84 | protected: |
| 85 | void enterEvent(QEvent *); |
| 86 | void leaveEvent(QEvent *); |
| 87 | |
| 88 | private: |
| 89 | QPropertyAnimation animator; |
| 90 | QWidget *tabbar; |
| 91 | float m_fader; |
| 92 | }; |
| 93 | |
| 94 | class FancyTabBar : public QWidget |
| 95 | { |
| 96 | Q_OBJECT |
| 97 | |
| 98 | public: |
| 99 | FancyTabBar(QWidget *parent = 0); |
| 100 | ~FancyTabBar(); |
| 101 | |
| 102 | void paintEvent(QPaintEvent *event); |
| 103 | void paintTab(QPainter *painter, int tabIndex) const; |
| 104 | void mousePressEvent(QMouseEvent *); |
| 105 | bool validIndex(int index) const { return index >= 0 && index < m_tabs.count(); } |
| 106 | |
| 107 | QSize sizeHint() const; |
| 108 | QSize minimumSizeHint() const; |
| 109 | |
| 110 | void addTab(const QIcon &icon, const QString &label); |
| 111 | void addSpacer(int size = 40); |
| 112 | void removeTab(int index) { |
| 113 | FancyTab *tab = m_tabs.takeAt(index); |
| 114 | delete tab; |
| 115 | } |
| 116 | void setCurrentIndex(int index); |
| 117 | int currentIndex() const { return m_currentIndex; } |
| 118 | |
| 119 | void setTabToolTip(int index, const QString& toolTip); |
| 120 | QString tabToolTip(int index) const; |
| 121 | |
| 122 | QIcon tabIcon(int index) const {return m_tabs.at(index)->icon; } |
| 123 | QString tabText(int index) const { return m_tabs.at(index)->text; } |
| 124 | int count() const {return m_tabs.count(); } |
| 125 | QRect tabRect(int index) const; |
| 126 | |
| 127 | signals: |
| 128 | void currentChanged(int); |
| 129 | |
| 130 | public slots: |
| 131 | void emitCurrentIndex(); |
| 132 | |
| 133 | private: |
| 134 | static const int m_rounding; |
| 135 | static const int m_textPadding; |
| 136 | int m_currentIndex; |
| 137 | QList<FancyTab*> m_tabs; |
| 138 | QTimer m_triggerTimer; |
| 139 | QSize tabSizeHint(bool minimum = false) const; |
| 140 | |
| 141 | }; |
| 142 | |
| 143 | class FancyTabWidget : public QWidget { |
| 144 | Q_OBJECT |
| 145 | |
| 146 | public: |
| 147 | FancyTabWidget(QWidget* parent = 0); |
| 148 | |
| 149 | // Values are persisted - only add to the end |
| 150 | enum Mode { |
| 151 | Mode_None = 0, |
| 152 | |
| 153 | Mode_LargeSidebar = 1, |
| 154 | Mode_SmallSidebar = 2, |
| 155 | Mode_Tabs = 3, |
| 156 | Mode_IconOnlyTabs = 4, |
| 157 | Mode_PlainSidebar = 5, |
| 158 | }; |
| 159 | |
| 160 | struct Item { |
| 161 | Item(const QIcon& icon, const QString& label) |
| 162 | : type_(Type_Tab), tab_label_(label), tab_icon_(icon), spacer_size_(0) {} |
| 163 | Item(int size) : type_(Type_Spacer), spacer_size_(size) {} |
| 164 | |
| 165 | enum Type { |
| 166 | Type_Tab, |
| 167 | Type_Spacer, |
| 168 | }; |
| 169 | |
| 170 | Type type_; |
| 171 | QString tab_label_; |
| 172 | QIcon tab_icon_; |
| 173 | int spacer_size_; |
| 174 | }; |
| 175 | |
| 176 | void AddTab(QWidget *tab, const QIcon &icon, const QString &label); |
| 177 | void AddSpacer(int size = 40); |
| 178 | void SetBackgroundPixmap(const QPixmap& pixmap); |
| 179 | |
| 180 | void AddBottomWidget(QWidget* widget); |
| 181 | |
| 182 | int current_index() const; |
| 183 | Mode mode() const { return mode_; } |
| 184 | |
| 185 | public slots: |
| 186 | void SetCurrentIndex(int index); |
| 187 | void SetMode(Mode mode); |
| 188 | void SetMode(int mode) { SetMode(Mode(mode)); } |
| 189 | |
| 190 | signals: |
| 191 | void CurrentChanged(int index); |
| 192 | void ModeChanged(FancyTabWidget::Mode mode); |
| 193 | |
| 194 | protected: |
| 195 | void paintEvent(QPaintEvent *event); |
| 196 | void contextMenuEvent(QContextMenuEvent* e); |
| 197 | |
| 198 | private slots: |
| 199 | void ShowWidget(int index); |
| 200 | |
| 201 | private: |
| 202 | void MakeTabBar(QTabBar::Shape shape, bool text, bool icons, bool fancy); |
| 203 | void AddMenuItem(QSignalMapper* mapper, QActionGroup* group, |
| 204 | const QString& text, Mode mode); |
| 205 | |
| 206 | Mode mode_; |
| 207 | QList<Item> items_; |
| 208 | |
| 209 | QWidget* tab_bar_; |
| 210 | QStackedLayout* stack_; |
| 211 | QPixmap background_pixmap_; |
| 212 | QWidget* side_widget_; |
| 213 | QVBoxLayout* side_layout_; |
| 214 | QVBoxLayout* top_layout_; |
| 215 | |
| 216 | bool use_background_; |
| 217 | |
| 218 | QMenu* menu_; |
| 219 | |
| 220 | //boost::scoped_ptr<FancyTabProxyStyle> proxy_style_; |
| 221 | QScopedPointer<FancyTabProxyStyle> proxy_style_; |
| 222 | }; |
| 223 | |
| 224 | } // namespace Internal |
| 225 | } // namespace Core |
| 226 | |
| 227 | Q_DECLARE_METATYPE(QPropertyAnimation*); |
| 228 | |
| 229 | using Core::Internal::FancyTab; |
| 230 | using Core::Internal::FancyTabBar; |
| 231 | using Core::Internal::FancyTabWidget; |
| 232 | |
| 233 | #endif // FANCYTABWIDGET_H |