VirtualBox

Changeset 71407 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
Mar 20, 2018 2:55:53 PM (7 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9049: Full and heavy cleanup for QISplitter.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/extensions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/extensions/QISplitter.cpp

    r71027 r71407  
    55
    66/*
    7  * Copyright (C) 2009-2017 Oracle Corporation
     7 * Copyright (C) 2009-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3232
    3333
    34 /* A simple shaded line: */
    35 class QIShadeSplitterHandle: public QSplitterHandle
     34/** QSplitterHandle subclass representing shaded line. */
     35class QIShadeSplitterHandle : public QSplitterHandle
    3636{
    3737    Q_OBJECT;
     
    3939public:
    4040
    41     QIShadeSplitterHandle(Qt::Orientation orientation, QISplitter *pParent)
    42         : QSplitterHandle(orientation, pParent)
    43     {
    44         QPalette pal = qApp->palette();
    45         QColor windowColor = pal.color(QPalette::Active, QPalette::Window);
    46         QColor darkColor = pal.color(QPalette::Active, QPalette::Dark);
    47         m_color1 = windowColor;
    48         m_color2 = windowColor;
    49         m_color = darkColor;
    50     }
    51 
    52     void configureColors(const QColor &color1, const QColor &color2)
    53     {
    54         m_color1 = color1;
    55         m_color2 = color2;
    56         update();
    57     }
     41    /** Constructs shaded splitter handle passing @a enmOrientation and @a pParent to the base-class. */
     42    QIShadeSplitterHandle(Qt::Orientation enmOrientation, QISplitter *pParent);
     43
     44    /** Defines colors to passed @a color1 and @a color2. */
     45    void configureColors(const QColor &color1, const QColor &color2);
    5846
    5947protected:
    6048
    61     void paintEvent(QPaintEvent *pEvent)
    62     {
    63         QPainter painter(this);
    64         QLinearGradient gradient;
    65         QGradientStop point1(0, m_color1);
    66         QGradientStop point2(0.5, m_color);
    67         QGradientStop point3(1, m_color2);
    68         QGradientStops stops;
    69         stops << point1 << point2 << point3;
    70         gradient.setStops(stops);
    71         if (orientation() == Qt::Horizontal)
    72         {
    73             gradient.setStart(rect().left() + 1, 0);
    74             gradient.setFinalStop(rect().right(), 0);
    75         }
    76         else
    77         {
    78             gradient.setStart(0, rect().top() + 1);
    79             gradient.setFinalStop(0, rect().bottom());
    80         }
    81         painter.fillRect(pEvent->rect(), gradient);
    82     }
     49    /** Handles paint @a pEvent. */
     50    virtual void paintEvent(QPaintEvent *pEvent) /* override */;
    8351
    8452private:
    8553
     54    /** Holds the main color. */
    8655    QColor m_color;
     56    /** Holds the color1. */
    8757    QColor m_color1;
     58    /** Holds the color2. */
    8859    QColor m_color2;
    8960};
    9061
    91 #ifdef RT_OS_DARWIN
    92 /* The Mac OS X version. */
    93 class QIDarwinSplitterHandle: public QSplitterHandle
     62
     63#ifdef VBOX_WS_MAC
     64/** QSplitterHandle subclass representing shaded line for macOS. */
     65class QIDarwinSplitterHandle : public QSplitterHandle
    9466{
    9567    Q_OBJECT;
     
    9769public:
    9870
    99     QIDarwinSplitterHandle(Qt::Orientation orientation, QISplitter *pParent)
    100         : QSplitterHandle(orientation, pParent)
    101     {}
    102 
    103     QSize sizeHint() const
    104     {
    105         QSize parent = QSplitterHandle::sizeHint();
    106         if (orientation() == Qt::Vertical)
    107             return parent + QSize(0, 3);
    108         else
    109             return QSize(1, parent.height());
    110     }
     71    /** Constructs shaded splitter handle passing @a enmOrientation and @a pParent to the base-class. */
     72    QIDarwinSplitterHandle(Qt::Orientation enmOrientation, QISplitter *pParent);
     73
     74    /** Returns size-hint. */
     75    QSize sizeHint() const;
    11176
    11277protected:
    11378
    114     void paintEvent(QPaintEvent*)
    115     {
    116         QPainter painter(this);
    117 
    118         QColor topColor(145, 145, 145);
    119         QColor bottomColor(142, 142, 142);
    120         QColor gradientStart(252, 252, 252);
    121         QColor gradientStop(223, 223, 223);
    122 
    123         if (orientation() == Qt::Vertical)
    124         {
    125             painter.setPen(topColor);
    126             painter.drawLine(0, 0, width(), 0);
    127             painter.setPen(bottomColor);
    128             painter.drawLine(0, height() - 1, width(), height() - 1);
    129 
    130             QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, height() -3));
    131             linearGrad.setColorAt(0, gradientStart);
    132             linearGrad.setColorAt(1, gradientStop);
    133             painter.fillRect(QRect(QPoint(0,1), size() - QSize(0, 2)), QBrush(linearGrad));
    134         }
    135         else
    136         {
    137             painter.setPen(topColor);
    138             painter.drawLine(0, 0, 0, height());
    139         }
    140     }
     79    /** Handles paint @a pEvent. */
     80    virtual void paintEvent(QPaintEvent *pEvent) /* override */;
    14181};
    142 #endif /* RT_OS_DARWIN */
     82#endif /* VBOX_WS_MAC */
     83
     84
     85/*********************************************************************************************************************************
     86*   Class QIShadeSplitterHandle implementation.                                                                                  *
     87*********************************************************************************************************************************/
     88
     89QIShadeSplitterHandle::QIShadeSplitterHandle(Qt::Orientation enmOrientation, QISplitter *pParent)
     90    : QSplitterHandle(enmOrientation, pParent)
     91{
     92    QPalette pal = qApp->palette();
     93    QColor windowColor = pal.color(QPalette::Active, QPalette::Window);
     94    QColor darkColor = pal.color(QPalette::Active, QPalette::Dark);
     95    m_color1 = windowColor;
     96    m_color2 = windowColor;
     97    m_color = darkColor;
     98}
     99
     100void QIShadeSplitterHandle::configureColors(const QColor &color1, const QColor &color2)
     101{
     102    m_color1 = color1;
     103    m_color2 = color2;
     104    update();
     105}
     106
     107void QIShadeSplitterHandle::paintEvent(QPaintEvent *pEvent)
     108{
     109    QPainter painter(this);
     110    QLinearGradient gradient;
     111    QGradientStop point1(0, m_color1);
     112    QGradientStop point2(0.5, m_color);
     113    QGradientStop point3(1, m_color2);
     114    QGradientStops stops;
     115    stops << point1 << point2 << point3;
     116    gradient.setStops(stops);
     117    if (orientation() == Qt::Horizontal)
     118    {
     119        gradient.setStart(rect().left() + 1, 0);
     120        gradient.setFinalStop(rect().right(), 0);
     121    }
     122    else
     123    {
     124        gradient.setStart(0, rect().top() + 1);
     125        gradient.setFinalStop(0, rect().bottom());
     126    }
     127    painter.fillRect(pEvent->rect(), gradient);
     128}
     129
     130
     131#ifdef VBOX_WS_MAC
     132
     133/*********************************************************************************************************************************
     134*   Class QIDarwinSplitterHandle implementation.                                                                                 *
     135*********************************************************************************************************************************/
     136
     137QIDarwinSplitterHandle::QIDarwinSplitterHandle(Qt::Orientation enmOrientation, QISplitter *pParent)
     138    : QSplitterHandle(enmOrientation, pParent)
     139{
     140}
     141
     142QSize QIDarwinSplitterHandle::sizeHint() const
     143{
     144    QSize parent = QSplitterHandle::sizeHint();
     145    if (orientation() == Qt::Vertical)
     146        return parent + QSize(0, 3);
     147    else
     148        return QSize(1, parent.height());
     149}
     150
     151void QIDarwinSplitterHandle::paintEvent(QPaintEvent *)
     152{
     153    QPainter painter(this);
     154
     155    QColor topColor(145, 145, 145);
     156    QColor bottomColor(142, 142, 142);
     157    QColor gradientStart(252, 252, 252);
     158    QColor gradientStop(223, 223, 223);
     159
     160    if (orientation() == Qt::Vertical)
     161    {
     162        painter.setPen(topColor);
     163        painter.drawLine(0, 0, width(), 0);
     164        painter.setPen(bottomColor);
     165        painter.drawLine(0, height() - 1, width(), height() - 1);
     166
     167        QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, height() -3));
     168        linearGrad.setColorAt(0, gradientStart);
     169        linearGrad.setColorAt(1, gradientStop);
     170        painter.fillRect(QRect(QPoint(0,1), size() - QSize(0, 2)), QBrush(linearGrad));
     171    }
     172    else
     173    {
     174        painter.setPen(topColor);
     175        painter.drawLine(0, 0, 0, height());
     176    }
     177}
     178
     179#endif /* VBOX_WS_MAC */
     180
     181
     182/*********************************************************************************************************************************
     183*   Class QISplitter  implementation.                                                                                            *
     184*********************************************************************************************************************************/
    143185
    144186QISplitter::QISplitter(QWidget *pParent /* = 0 */)
    145187    : QSplitter(pParent)
     188    , m_type(Shade)
    146189    , m_fPolished(false)
     190#ifdef VBOX_WS_MAC
     191    , m_fHandleGrabbed(false)
     192#endif /* VBOX_WS_MAC */
     193{
     194    qApp->installEventFilter(this);
     195}
     196
     197QISplitter::QISplitter(Qt::Orientation enmOrientation, QWidget *pParent /* = 0 */)
     198    : QSplitter(enmOrientation, pParent)
    147199    , m_type(Shade)
     200    , m_fPolished(false)
    148201#ifdef VBOX_WS_MAC
    149202    , m_fHandleGrabbed(false)
    150203#endif /* VBOX_WS_MAC */
    151204{
    152     qApp->installEventFilter(this);
    153 }
    154 
    155 QISplitter::QISplitter(Qt::Orientation orientation, QWidget *pParent /* = 0 */)
    156     : QSplitter(orientation, pParent)
    157     , m_fPolished(false)
    158     , m_type(Shade)
    159 #ifdef VBOX_WS_MAC
    160     , m_fHandleGrabbed(false)
    161 #endif /* VBOX_WS_MAC */
    162 {
    163205    qApp->installEventFilter (this);
    164206}
     
    166208bool QISplitter::eventFilter(QObject *pWatched, QEvent *pEvent)
    167209{
     210    /* Handles events for handle: */
    168211    if (pWatched == handle(1))
    169212    {
    170213        switch (pEvent->type())
    171214        {
     215            /* Restore default position on double-click: */
    172216            case QEvent::MouseButtonDblClick:
    173217                restoreState(m_baseState);
     
    177221        }
    178222    }
    179 #ifdef VBOX_WS_MAC
    180     /* Special handling on the Mac. Cause there the horizontal handle is only 1
    181      * pixel wide, its hard to catch. Therefor we make some invisible area
    182      * around the handle and forwarding the mouse events to the handle, if the
    183      * user presses the left mouse button. */
     223
     224#ifdef VBOX_WS_MAC
     225    // WORKAROUND:
     226    // Special handling on the Mac. Cause there the horizontal handle is only 1
     227    // pixel wide, its hard to catch. Therefor we make some invisible area
     228    // around the handle and forwarding the mouse events to the handle, if the
     229    // user presses the left mouse button.
    184230    else if (   m_type == Native
    185231             && orientation() == Qt::Horizontal
     
    210256                        if (pEvent->type() == QEvent::MouseButtonPress)
    211257                        {
    212                             /* If we have a handle position hit and the left
    213                              * button is pressed, start the grabbing. */
     258                            /* If we have a handle position hit and the left button is pressed, start the grabbing. */
    214259                            if (   fMarginHit
    215260                                && pMouseEvent->buttons().testFlag(Qt::LeftButton))
     
    223268                        else if (pEvent->type() == QEvent::MouseMove)
    224269                        {
    225                             /* If we are in the near of the handle or currently
    226                              * dragging, forward the mouse event. */
     270                            /* If we are in the near of the handle or currently dragging, forward the mouse event. */
    227271                            if (   fMarginHit
    228272                                || (   m_fHandleGrabbed
     
    257301#endif /* VBOX_WS_MAC */
    258302
     303    /* Call to base-class: */
    259304    return QSplitter::eventFilter(pWatched, pEvent);
    260305}
     
    262307void QISplitter::showEvent(QShowEvent *pEvent)
    263308{
     309    /* Remember default position: */
    264310    if (!m_fPolished)
    265311    {
     
    268314    }
    269315
     316    /* Call to base-class: */
    270317    return QSplitter::showEvent(pEvent);
    271318}
    272319
    273 QSplitterHandle* QISplitter::createHandle()
    274 {
     320QSplitterHandle *QISplitter::createHandle()
     321{
     322    /* Create native handle: */
    275323    if (m_type == Native)
    276324    {
    277 #ifdef RT_OS_DARWIN
     325#ifdef VBOX_WS_MAC
    278326        return new QIDarwinSplitterHandle(orientation(), this);
    279 #else /* RT_OS_DARWIN */
     327#else
    280328        return new QSplitterHandle(orientation(), this);
    281 #endif /* RT_OS_DARWIN */
    282     }
     329#endif
     330    }
     331    /* Create our own handle: */
    283332    else
    284333    {
     
    291340
    292341#include "QISplitter.moc"
     342
  • trunk/src/VBox/Frontends/VirtualBox/src/extensions/QISplitter.h

    r71027 r71407  
    55
    66/*
    7  * Copyright (C) 2009-2017 Oracle Corporation
     7 * Copyright (C) 2009-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1616 */
    1717
    18 #ifndef _QISplitter_h_
    19 #define _QISplitter_h_
     18#ifndef ___QISplitter_h___
     19#define ___QISplitter_h___
    2020
    21 /* Global includes */
     21/* Qt includes: */
    2222#include <QSplitter>
    2323
     24/* Forward declarations: */
     25class QSplitterHandle;
     26
     27/** QSplitter subclass with extended functionality. */
    2428class QISplitter : public QSplitter
    2529{
     
    2832public:
    2933
     34    /** Handle types. */
    3035    enum Type { Native, Shade };
    3136
     37    /** Constructs splitter passing @a pParent to the base-class. */
    3238    QISplitter(QWidget *pParent = 0);
     39    /** Constructs splitter passing @a orientation and @a pParent to the base-class. */
    3340    QISplitter(Qt::Orientation orientation, QWidget *pParent = 0);
    3441
     42    /** Defines handle @a type. */
    3543    void setHandleType(Type type) { m_type = type; }
     44    /** Returns handle type. */
    3645    Type handleType() const { return m_type; }
    3746
     47    /** Configure custom colors defined as @a color1 and @a color2. */
    3848    void configureColors(const QColor &color1, const QColor &color2) { m_color1 = color1; m_color2 = color2; }
    3949
    4050protected:
    4151
    42     bool eventFilter(QObject *pWatched, QEvent *pEvent);
     52    /** Preprocesses Qt @a pEvent for passed @a pObject. */
     53    virtual bool eventFilter(QObject *pObject, QEvent *pEvent) /* override */;
     54
     55    /** Handles show @a pEvent. */
    4356    void showEvent(QShowEvent *pEvent);
    4457
    45     QSplitterHandle* createHandle();
     58    /** Creates handle. */
     59    QSplitterHandle *createHandle();
    4660
    4761private:
    4862
     63    /** Holds the serialized base-state. */
    4964    QByteArray m_baseState;
    5065
    51     bool m_fPolished;
     66    /** Holds the handle type. */
    5267    Type m_type;
     68
     69    /** Holds whether the splitter is polished. */
     70    bool m_fPolished : 1;
    5371#ifdef VBOX_WS_MAC
    54     bool m_fHandleGrabbed;
     72    /** Holds whether handle is grabbed. */
     73    bool m_fHandleGrabbed : 1;
    5574#endif /* VBOX_WS_MAC */
    5675
     76    /** Holds color1. */
    5777    QColor m_color1;
     78    /** Holds color2. */
    5879    QColor m_color2;
    5980};
    6081
    61 #endif /* _QISplitter_h_ */
     82#endif /* !___QISplitter_h___ */
    6283
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette