~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/widgets/widgets/qtabbar_p.h

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the QtGui module of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
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
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
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), elideModeSetByUser(false), 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 &current)
 
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
    QToolButton* rightB; // right or bottom
 
169
    QToolButton* leftB; // left or top
 
170
 
 
171
    void _q_scrollTabs();
 
172
    void _q_closeTab();
 
173
    void moveTab(int index, int offset);
 
174
    void moveTabFinished(int index);
 
175
    QRect hoverRect;
 
176
 
 
177
    void refresh();
 
178
    void layoutTabs();
 
179
    void layoutWidgets(int start = 0);
 
180
    void layoutTab(int index);
 
181
    void updateMacBorderMetrics();
 
182
    void setupMovableTab();
 
183
 
 
184
    void makeVisible(int index);
 
185
    QSize iconSize;
 
186
    Qt::TextElideMode elideMode;
 
187
    bool elideModeSetByUser;
 
188
    bool useScrollButtons;
 
189
    bool useScrollButtonsSetByUser;
 
190
 
 
191
    bool expanding;
 
192
    bool closeButtonOnTabs;
 
193
    QTabBar::SelectionBehavior selectionBehaviorOnRemove;
 
194
 
 
195
    QPoint dragStartPosition;
 
196
    bool paintWithOffsets;
 
197
    bool movable;
 
198
    bool dragInProgress;
 
199
    bool documentMode;
 
200
 
 
201
    QWidget *movingTab;
 
202
#ifdef Q_WS_MAC
 
203
    int previousPressedIndex;
 
204
#endif
 
205
    // shared by tabwidget and qtabbar
 
206
    static void initStyleBaseOption(QStyleOptionTabBarBaseV2 *optTabBase, QTabBar *tabbar, QSize size)
 
207
    {
 
208
        QStyleOptionTab tabOverlap;
 
209
        tabOverlap.shape = tabbar->shape();
 
210
        int overlap = tabbar->style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tabOverlap, tabbar);
 
211
        QWidget *theParent = tabbar->parentWidget();
 
212
        optTabBase->init(tabbar);
 
213
        optTabBase->shape = tabbar->shape();
 
214
        optTabBase->documentMode = tabbar->documentMode();
 
215
        if (theParent && overlap > 0) {
 
216
            QRect rect;
 
217
            switch (tabOverlap.shape) {
 
218
            case QTabBar::RoundedNorth:
 
219
            case QTabBar::TriangularNorth:
 
220
                rect.setRect(0, size.height()-overlap, size.width(), overlap);
 
221
                break;
 
222
            case QTabBar::RoundedSouth:
 
223
            case QTabBar::TriangularSouth:
 
224
                rect.setRect(0, 0, size.width(), overlap);
 
225
                break;
 
226
            case QTabBar::RoundedEast:
 
227
            case QTabBar::TriangularEast:
 
228
                rect.setRect(0, 0, overlap, size.height());
 
229
                break;
 
230
            case QTabBar::RoundedWest:
 
231
            case QTabBar::TriangularWest:
 
232
                rect.setRect(size.width() - overlap, 0, overlap, size.height());
 
233
                break;
 
234
            }
 
235
            optTabBase->rect = rect;
 
236
        }
 
237
    }
 
238
 
 
239
};
 
240
 
 
241
class CloseButton : public QAbstractButton
 
242
{
 
243
    Q_OBJECT
 
244
 
 
245
public:
 
246
    CloseButton(QWidget *parent = 0);
 
247
 
 
248
    QSize sizeHint() const;
 
249
    inline QSize minimumSizeHint() const
 
250
        { return sizeHint(); }
 
251
    void enterEvent(QEvent *event);
 
252
    void leaveEvent(QEvent *event);
 
253
    void paintEvent(QPaintEvent *event);
 
254
};
 
255
 
 
256
 
 
257
QT_END_NAMESPACE
 
258
 
 
259
#endif
 
260
 
 
261
#endif