src/gui/widgets/qtabbar_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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), expanding(true), closeButtonOnTabs(false),
       
    79         selectionBehaviorOnRemove(QTabBar::SelectRightTab), paintWithOffsets(true), movable(false),
       
    80         dragInProgress(false), documentMode(false), movingTab(0) {}
       
    81 
       
    82     int currentIndex;
       
    83     int pressedIndex;
       
    84     QTabBar::Shape shape;
       
    85     bool layoutDirty;
       
    86     bool drawBase;
       
    87     int scrollOffset;
       
    88 
       
    89     struct Tab {
       
    90         inline Tab(const QIcon &ico, const QString &txt)
       
    91             : enabled(true) , shortcutId(0), text(txt), icon(ico),
       
    92             leftWidget(0), rightWidget(0), lastTab(-1), dragOffset(0)
       
    93 #ifndef QT_NO_ANIMATION
       
    94             , animation(0)
       
    95 #endif //QT_NO_ANIMATION
       
    96         {}
       
    97         bool operator==(const Tab &other) const { return &other == this; }
       
    98         bool enabled;
       
    99         int shortcutId;
       
   100         QString text;
       
   101 #ifndef QT_NO_TOOLTIP
       
   102         QString toolTip;
       
   103 #endif
       
   104 #ifndef QT_NO_WHATSTHIS
       
   105         QString whatsThis;
       
   106 #endif
       
   107         QIcon icon;
       
   108         QRect rect;
       
   109         QRect minRect;
       
   110         QRect maxRect;
       
   111 
       
   112         QColor textColor;
       
   113         QVariant data;
       
   114         QWidget *leftWidget;
       
   115         QWidget *rightWidget;
       
   116         int lastTab;
       
   117         int dragOffset;
       
   118 
       
   119 #ifndef QT_NO_ANIMATION
       
   120         ~Tab() { delete animation; }
       
   121         struct TabBarAnimation : public QVariantAnimation {
       
   122             TabBarAnimation(Tab *t, QTabBarPrivate *_priv) : tab(t), priv(_priv)
       
   123             { setEasingCurve(QEasingCurve::InOutQuad); }
       
   124 
       
   125             void updateCurrentValue(const QVariant &current)
       
   126             { priv->moveTab(priv->tabList.indexOf(*tab), current.toInt()); }
       
   127 
       
   128             void updateState(State, State newState)
       
   129             { if (newState == Stopped) priv->moveTabFinished(priv->tabList.indexOf(*tab)); }
       
   130         private:
       
   131             //these are needed for the callbacks
       
   132             Tab *tab;
       
   133             QTabBarPrivate *priv;
       
   134         } *animation;
       
   135 
       
   136         void startAnimation(QTabBarPrivate *priv, int duration) {
       
   137             if (!animation)
       
   138                 animation = new TabBarAnimation(this, priv);
       
   139             animation->setStartValue(dragOffset);
       
   140             animation->setEndValue(0);
       
   141             animation->setDuration(duration);
       
   142             animation->start();
       
   143         }
       
   144 #else
       
   145         void startAnimation(QTabBarPrivate *priv, int duration)
       
   146         { Q_UNUSED(duration); priv->moveTabFinished(priv->tabList.indexOf(*this)); }
       
   147 #endif //QT_NO_ANIMATION
       
   148     };
       
   149     QList<Tab> tabList;
       
   150 
       
   151     int calculateNewPosition(int from, int to, int index) const;
       
   152     void slide(int from, int to);
       
   153     void init();
       
   154     int extraWidth() const;
       
   155 
       
   156     Tab *at(int index);
       
   157     const Tab *at(int index) const;
       
   158 
       
   159     int indexAtPos(const QPoint &p) const;
       
   160 
       
   161     inline bool validIndex(int index) const { return index >= 0 && index < tabList.count(); }
       
   162     void setCurrentNextEnabledIndex(int offset);
       
   163 
       
   164     QSize minimumTabSizeHint(int index);
       
   165 
       
   166     QToolButton* rightB; // right or bottom
       
   167     QToolButton* leftB; // left or top
       
   168 
       
   169     void _q_scrollTabs();
       
   170     void _q_closeTab();
       
   171     void moveTab(int index, int offset);
       
   172     void moveTabFinished(int index);
       
   173     QRect hoverRect;
       
   174 
       
   175     void refresh();
       
   176     void layoutTabs();
       
   177     void layoutWidgets(int index = -1);
       
   178     void layoutTab(int index);
       
   179     void updateMacBorderMetrics();
       
   180     void setupMovableTab();
       
   181 
       
   182     void makeVisible(int index);
       
   183     QSize iconSize;
       
   184     Qt::TextElideMode elideMode;
       
   185     bool useScrollButtons;
       
   186 
       
   187     bool expanding;
       
   188     bool closeButtonOnTabs;
       
   189     QTabBar::SelectionBehavior selectionBehaviorOnRemove;
       
   190 
       
   191     QPoint dragStartPosition;
       
   192     bool paintWithOffsets;
       
   193     bool movable;
       
   194     bool dragInProgress;
       
   195     bool documentMode;
       
   196 
       
   197     QWidget *movingTab;
       
   198 
       
   199     // shared by tabwidget and qtabbar
       
   200     static void initStyleBaseOption(QStyleOptionTabBarBaseV2 *optTabBase, QTabBar *tabbar, QSize size)
       
   201     {
       
   202         QStyleOptionTab tabOverlap;
       
   203         tabOverlap.shape = tabbar->shape();
       
   204         int overlap = tabbar->style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tabOverlap, tabbar);
       
   205         QWidget *theParent = tabbar->parentWidget();
       
   206         optTabBase->init(tabbar);
       
   207         optTabBase->shape = tabbar->shape();
       
   208         optTabBase->documentMode = tabbar->documentMode();
       
   209         if (theParent && overlap > 0) {
       
   210             QRect rect;
       
   211             switch (tabOverlap.shape) {
       
   212             case QTabBar::RoundedNorth:
       
   213             case QTabBar::TriangularNorth:
       
   214                 rect.setRect(0, size.height()-overlap, size.width(), overlap);
       
   215                 break;
       
   216             case QTabBar::RoundedSouth:
       
   217             case QTabBar::TriangularSouth:
       
   218                 rect.setRect(0, 0, size.width(), overlap);
       
   219                 break;
       
   220             case QTabBar::RoundedEast:
       
   221             case QTabBar::TriangularEast:
       
   222                 rect.setRect(0, 0, overlap, size.height());
       
   223                 break;
       
   224             case QTabBar::RoundedWest:
       
   225             case QTabBar::TriangularWest:
       
   226                 rect.setRect(size.width() - overlap, 0, overlap, size.height());
       
   227                 break;
       
   228             }
       
   229             optTabBase->rect = rect;
       
   230         }
       
   231     }
       
   232 
       
   233 };
       
   234 
       
   235 class CloseButton : public QAbstractButton
       
   236 {
       
   237     Q_OBJECT
       
   238 
       
   239 public:
       
   240     CloseButton(QWidget *parent = 0);
       
   241 
       
   242     QSize sizeHint() const;
       
   243     inline QSize minimumSizeHint() const
       
   244         { return sizeHint(); }
       
   245     void enterEvent(QEvent *event);
       
   246     void leaveEvent(QEvent *event);
       
   247     void paintEvent(QPaintEvent *event);
       
   248 };
       
   249 
       
   250 
       
   251 QT_END_NAMESPACE
       
   252 
       
   253 #endif
       
   254 
       
   255 #endif