1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
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 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QTABBAR_P_H
|
---|
43 | #define QTABBAR_P_H
|
---|
44 |
|
---|
45 | //
|
---|
46 | // W A R N I N G
|
---|
47 | // -------------
|
---|
48 | //
|
---|
49 | // This file is not part of the Qt API. It exists purely as an
|
---|
50 | // implementation detail. This header file may change from version to
|
---|
51 | // version without notice, or even be removed.
|
---|
52 | //
|
---|
53 | // We mean it.
|
---|
54 | //
|
---|
55 |
|
---|
56 | #include "qtabbar.h"
|
---|
57 | #include "private/qwidget_p.h"
|
---|
58 |
|
---|
59 | #include <qicon.h>
|
---|
60 | #include <qtoolbutton.h>
|
---|
61 | #include <qdebug.h>
|
---|
62 | #include <qvariantanimation.h>
|
---|
63 |
|
---|
64 | #ifndef QT_NO_TABBAR
|
---|
65 |
|
---|
66 | #define ANIMATION_DURATION 250
|
---|
67 |
|
---|
68 | #include <qstyleoption.h>
|
---|
69 |
|
---|
70 | QT_BEGIN_NAMESPACE
|
---|
71 |
|
---|
72 | class QTabBarPrivate : public QWidgetPrivate
|
---|
73 | {
|
---|
74 | Q_DECLARE_PUBLIC(QTabBar)
|
---|
75 | public:
|
---|
76 | QTabBarPrivate()
|
---|
77 | :currentIndex(-1), pressedIndex(-1), shape(QTabBar::RoundedNorth), layoutDirty(false),
|
---|
78 | drawBase(true), scrollOffset(0), useScrollButtonsSetByUser(false) , expanding(true), closeButtonOnTabs(false),
|
---|
79 | selectionBehaviorOnRemove(QTabBar::SelectRightTab), paintWithOffsets(true), movable(false),
|
---|
80 | dragInProgress(false), documentMode(false), movingTab(0)
|
---|
81 | #ifdef Q_WS_MAC
|
---|
82 | , previousPressedIndex(-1)
|
---|
83 | #endif
|
---|
84 | {}
|
---|
85 |
|
---|
86 | int currentIndex;
|
---|
87 | int pressedIndex;
|
---|
88 | QTabBar::Shape shape;
|
---|
89 | bool layoutDirty;
|
---|
90 | bool drawBase;
|
---|
91 | int scrollOffset;
|
---|
92 |
|
---|
93 | struct Tab {
|
---|
94 | inline Tab(const QIcon &ico, const QString &txt)
|
---|
95 | : enabled(true) , shortcutId(0), text(txt), icon(ico),
|
---|
96 | leftWidget(0), rightWidget(0), lastTab(-1), dragOffset(0)
|
---|
97 | #ifndef QT_NO_ANIMATION
|
---|
98 | , animation(0)
|
---|
99 | #endif //QT_NO_ANIMATION
|
---|
100 | {}
|
---|
101 | bool operator==(const Tab &other) const { return &other == this; }
|
---|
102 | bool enabled;
|
---|
103 | int shortcutId;
|
---|
104 | QString text;
|
---|
105 | #ifndef QT_NO_TOOLTIP
|
---|
106 | QString toolTip;
|
---|
107 | #endif
|
---|
108 | #ifndef QT_NO_WHATSTHIS
|
---|
109 | QString whatsThis;
|
---|
110 | #endif
|
---|
111 | QIcon icon;
|
---|
112 | QRect rect;
|
---|
113 | QRect minRect;
|
---|
114 | QRect maxRect;
|
---|
115 |
|
---|
116 | QColor textColor;
|
---|
117 | QVariant data;
|
---|
118 | QWidget *leftWidget;
|
---|
119 | QWidget *rightWidget;
|
---|
120 | int lastTab;
|
---|
121 | int dragOffset;
|
---|
122 |
|
---|
123 | #ifndef QT_NO_ANIMATION
|
---|
124 | ~Tab() { delete animation; }
|
---|
125 | struct TabBarAnimation : public QVariantAnimation {
|
---|
126 | TabBarAnimation(Tab *t, QTabBarPrivate *_priv) : tab(t), priv(_priv)
|
---|
127 | { setEasingCurve(QEasingCurve::InOutQuad); }
|
---|
128 |
|
---|
129 | void updateCurrentValue(const QVariant ¤t)
|
---|
130 | { priv->moveTab(priv->tabList.indexOf(*tab), current.toInt()); }
|
---|
131 |
|
---|
132 | void updateState(State, State newState)
|
---|
133 | { if (newState == Stopped) priv->moveTabFinished(priv->tabList.indexOf(*tab)); }
|
---|
134 | private:
|
---|
135 | //these are needed for the callbacks
|
---|
136 | Tab *tab;
|
---|
137 | QTabBarPrivate *priv;
|
---|
138 | } *animation;
|
---|
139 |
|
---|
140 | void startAnimation(QTabBarPrivate *priv, int duration) {
|
---|
141 | if (!animation)
|
---|
142 | animation = new TabBarAnimation(this, priv);
|
---|
143 | animation->setStartValue(dragOffset);
|
---|
144 | animation->setEndValue(0);
|
---|
145 | animation->setDuration(duration);
|
---|
146 | animation->start();
|
---|
147 | }
|
---|
148 | #else
|
---|
149 | void startAnimation(QTabBarPrivate *priv, int duration)
|
---|
150 | { Q_UNUSED(duration); priv->moveTabFinished(priv->tabList.indexOf(*this)); }
|
---|
151 | #endif //QT_NO_ANIMATION
|
---|
152 | };
|
---|
153 | QList<Tab> tabList;
|
---|
154 |
|
---|
155 | int calculateNewPosition(int from, int to, int index) const;
|
---|
156 | void slide(int from, int to);
|
---|
157 | void init();
|
---|
158 | int extraWidth() const;
|
---|
159 |
|
---|
160 | Tab *at(int index);
|
---|
161 | const Tab *at(int index) const;
|
---|
162 |
|
---|
163 | int indexAtPos(const QPoint &p) const;
|
---|
164 |
|
---|
165 | inline bool validIndex(int index) const { return index >= 0 && index < tabList.count(); }
|
---|
166 | void setCurrentNextEnabledIndex(int offset);
|
---|
167 |
|
---|
168 | QSize minimumTabSizeHint(int index);
|
---|
169 |
|
---|
170 | QToolButton* rightB; // right or bottom
|
---|
171 | QToolButton* leftB; // left or top
|
---|
172 |
|
---|
173 | void _q_scrollTabs();
|
---|
174 | void _q_closeTab();
|
---|
175 | void moveTab(int index, int offset);
|
---|
176 | void moveTabFinished(int index);
|
---|
177 | QRect hoverRect;
|
---|
178 |
|
---|
179 | void refresh();
|
---|
180 | void layoutTabs();
|
---|
181 | void layoutWidgets(int index = -1);
|
---|
182 | void layoutTab(int index);
|
---|
183 | void updateMacBorderMetrics();
|
---|
184 | void setupMovableTab();
|
---|
185 |
|
---|
186 | void makeVisible(int index);
|
---|
187 | QSize iconSize;
|
---|
188 | Qt::TextElideMode elideMode;
|
---|
189 | bool useScrollButtons;
|
---|
190 | bool useScrollButtonsSetByUser;
|
---|
191 |
|
---|
192 | bool expanding;
|
---|
193 | bool closeButtonOnTabs;
|
---|
194 | QTabBar::SelectionBehavior selectionBehaviorOnRemove;
|
---|
195 |
|
---|
196 | QPoint dragStartPosition;
|
---|
197 | bool paintWithOffsets;
|
---|
198 | bool movable;
|
---|
199 | bool dragInProgress;
|
---|
200 | bool documentMode;
|
---|
201 |
|
---|
202 | QWidget *movingTab;
|
---|
203 | #ifdef Q_WS_MAC
|
---|
204 | int previousPressedIndex;
|
---|
205 | #endif
|
---|
206 | // shared by tabwidget and qtabbar
|
---|
207 | static void initStyleBaseOption(QStyleOptionTabBarBaseV2 *optTabBase, QTabBar *tabbar, QSize size)
|
---|
208 | {
|
---|
209 | QStyleOptionTab tabOverlap;
|
---|
210 | tabOverlap.shape = tabbar->shape();
|
---|
211 | int overlap = tabbar->style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tabOverlap, tabbar);
|
---|
212 | QWidget *theParent = tabbar->parentWidget();
|
---|
213 | optTabBase->init(tabbar);
|
---|
214 | optTabBase->shape = tabbar->shape();
|
---|
215 | optTabBase->documentMode = tabbar->documentMode();
|
---|
216 | if (theParent && overlap > 0) {
|
---|
217 | QRect rect;
|
---|
218 | switch (tabOverlap.shape) {
|
---|
219 | case QTabBar::RoundedNorth:
|
---|
220 | case QTabBar::TriangularNorth:
|
---|
221 | rect.setRect(0, size.height()-overlap, size.width(), overlap);
|
---|
222 | break;
|
---|
223 | case QTabBar::RoundedSouth:
|
---|
224 | case QTabBar::TriangularSouth:
|
---|
225 | rect.setRect(0, 0, size.width(), overlap);
|
---|
226 | break;
|
---|
227 | case QTabBar::RoundedEast:
|
---|
228 | case QTabBar::TriangularEast:
|
---|
229 | rect.setRect(0, 0, overlap, size.height());
|
---|
230 | break;
|
---|
231 | case QTabBar::RoundedWest:
|
---|
232 | case QTabBar::TriangularWest:
|
---|
233 | rect.setRect(size.width() - overlap, 0, overlap, size.height());
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | optTabBase->rect = rect;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | };
|
---|
241 |
|
---|
242 | class CloseButton : public QAbstractButton
|
---|
243 | {
|
---|
244 | Q_OBJECT
|
---|
245 |
|
---|
246 | public:
|
---|
247 | CloseButton(QWidget *parent = 0);
|
---|
248 |
|
---|
249 | QSize sizeHint() const;
|
---|
250 | inline QSize minimumSizeHint() const
|
---|
251 | { return sizeHint(); }
|
---|
252 | void enterEvent(QEvent *event);
|
---|
253 | void leaveEvent(QEvent *event);
|
---|
254 | void paintEvent(QPaintEvent *event);
|
---|
255 | };
|
---|
256 |
|
---|
257 |
|
---|
258 | QT_END_NAMESPACE
|
---|
259 |
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | #endif
|
---|