VirtualBox

Changeset 77205 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Feb 7, 2019 7:04:50 PM (6 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9373: Extend UIGraphicsScrollBar with hover on/off animation.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/widgets/graphics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/graphics/UIGraphicsScrollBar.cpp

    r77203 r77205  
    2121#include <QGraphicsSceneMouseEvent>
    2222#include <QPainter>
     23#include <QPropertyAnimation>
     24#include <QSignalTransition>
     25#include <QState>
     26#include <QStateMachine>
    2327#include <QStyle>
    2428#include <QStyleOptionGraphicsItem>
     29#include <QTimerEvent>
    2530
    2631/* GUI includes: */
     
    148153    , m_pButton2(0)
    149154    , m_pToken(0)
     155    , m_fHovered(false)
     156    , m_iHoverOffTimerId(0)
     157    , m_iAnimatedValue(0)
    150158{
    151159    pScene->addItem(this);
     
    164172    , m_pButton2(0)
    165173    , m_pToken(0)
     174    , m_fHovered(false)
     175    , m_iHoverOffTimerId(0)
     176    , m_iAnimatedValue(0)
    166177{
    167178    prepare();
     
    272283    sltTokenMoved(pEvent->pos());
    273284    layoutToken();
     285}
     286
     287void UIGraphicsScrollBar::hoverMoveEvent(QGraphicsSceneHoverEvent *)
     288{
     289    /* Only if not yet hovered, that way we
     290     * make sure trigger emitted just once: */
     291    if (!m_fHovered)
     292    {
     293        /* Emit hover-on trigger: */
     294        m_fHovered = true;
     295        emit sigHoverEnter();
     296    }
     297    /* Update in any case: */
     298    update();
     299}
     300
     301void UIGraphicsScrollBar::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
     302{
     303    /* Only if it's still hovered, that way we
     304     * make sure trigger emitted just once: */
     305    if (m_fHovered)
     306    {
     307        /* Start hover-off timer, handled in timerEvent() below: */
     308        m_iHoverOffTimerId = startTimer(1000);
     309        m_fHovered = false;
     310    }
     311    /* Update in any case: */
     312    update();
     313}
     314
     315void UIGraphicsScrollBar::timerEvent(QTimerEvent *pEvent)
     316{
     317    /* Kill timer in any case: */
     318    const int iTimerId = pEvent->timerId();
     319    killTimer(iTimerId);
     320
     321    /* If that timer is the one we expecting: */
     322    if (m_iHoverOffTimerId != 0 && iTimerId == m_iHoverOffTimerId)
     323    {
     324        /* Wait for timer no more: */
     325        m_iHoverOffTimerId = 0;
     326        /* Emit hover-off trigger if not hovered: */
     327        if (!m_fHovered)
     328            emit sigHoverLeave();
     329        /* Update in any case: */
     330        update();
     331    }
    274332}
    275333
     
    327385}
    328386
     387void UIGraphicsScrollBar::sltStateLeftDefault()
     388{
     389    m_pButton1->hide();
     390    m_pButton2->hide();
     391    m_pToken->hide();
     392}
     393
     394void UIGraphicsScrollBar::sltStateLeftHovered()
     395{
     396    m_pButton1->hide();
     397    m_pButton2->hide();
     398    m_pToken->hide();
     399}
     400
     401void UIGraphicsScrollBar::sltStateEnteredDefault()
     402{
     403    m_pButton1->hide();
     404    m_pButton2->hide();
     405    m_pToken->hide();
     406}
     407
     408void UIGraphicsScrollBar::sltStateEnteredHovered()
     409{
     410    m_pButton1->show();
     411    m_pButton2->show();
     412    m_pToken->show();
     413}
     414
    329415void UIGraphicsScrollBar::prepare()
    330416{
     417    /* Configure self: */
    331418    setAcceptHoverEvents(true);
    332419
     
    335422    updateExtent();
    336423    layoutWidgets();
     424
     425    /* Prepare animation: */
     426    prepareAnimation();
    337427}
    338428
     
    384474}
    385475
     476void UIGraphicsScrollBar::prepareAnimation()
     477{
     478    /* Create hovering animation machine: */
     479    QStateMachine *pHoveringMachine = new QStateMachine(this);
     480    if (pHoveringMachine)
     481    {
     482        /* Create 'default' state: */
     483        QState *pStateDefault = new QState(pHoveringMachine);
     484        /* Create 'hovered' state: */
     485        QState *pStateHovered = new QState(pHoveringMachine);
     486
     487        /* Configure 'default' state: */
     488        if (pStateDefault)
     489        {
     490            /* When we entering default state => we assigning animatedValue to 0: */
     491            pStateDefault->assignProperty(this, "animatedValue", 0);
     492            connect(pStateDefault, &QState::propertiesAssigned, this, &UIGraphicsScrollBar::sltStateEnteredDefault);
     493
     494            /* Add state transitions: */
     495            QSignalTransition *pDefaultToHovered = pStateDefault->addTransition(this, SIGNAL(sigHoverEnter()), pStateHovered);
     496            if (pDefaultToHovered)
     497            {
     498                connect(pDefaultToHovered, &QSignalTransition::triggered, this, &UIGraphicsScrollBar::sltStateLeftDefault);
     499
     500                /* Create forward animation: */
     501                QPropertyAnimation *pHoveringAnimationForward = new QPropertyAnimation(this, "animatedValue", this);
     502                if (pHoveringAnimationForward)
     503                {
     504                    pHoveringAnimationForward->setDuration(200);
     505                    pHoveringAnimationForward->setStartValue(0);
     506                    pHoveringAnimationForward->setEndValue(100);
     507
     508                    /* Add to transition: */
     509                    pDefaultToHovered->addAnimation(pHoveringAnimationForward);
     510                }
     511            }
     512        }
     513
     514        /* Configure 'hovered' state: */
     515        if (pStateHovered)
     516        {
     517            /* When we entering hovered state => we assigning animatedValue to 100: */
     518            pStateHovered->assignProperty(this, "animatedValue", 100);
     519            connect(pStateHovered, &QState::propertiesAssigned, this, &UIGraphicsScrollBar::sltStateEnteredHovered);
     520
     521            /* Add state transitions: */
     522            QSignalTransition *pHoveredToDefault = pStateHovered->addTransition(this, SIGNAL(sigHoverLeave()), pStateDefault);
     523            if (pHoveredToDefault)
     524            {
     525                connect(pHoveredToDefault, &QSignalTransition::triggered, this, &UIGraphicsScrollBar::sltStateLeftHovered);
     526
     527                /* Create backward animation: */
     528                QPropertyAnimation *pHoveringAnimationBackward = new QPropertyAnimation(this, "animatedValue", this);
     529                if (pHoveringAnimationBackward)
     530                {
     531                    pHoveringAnimationBackward->setDuration(200);
     532                    pHoveringAnimationBackward->setStartValue(100);
     533                    pHoveringAnimationBackward->setEndValue(0);
     534
     535                    /* Add to transition: */
     536                    pHoveredToDefault->addAnimation(pHoveringAnimationBackward);
     537                }
     538            }
     539        }
     540
     541        /* Initial state is 'default': */
     542        pHoveringMachine->setInitialState(pStateDefault);
     543        /* Start state-machine: */
     544        pHoveringMachine->start();
     545    }
     546}
     547
    386548void UIGraphicsScrollBar::updateExtent()
    387549{
     
    455617
    456618    /* Draw background: */
    457     pPainter->fillRect(rectangle, backgroundColor);
     619    QRect actualRectangle = rectangle;
     620    actualRectangle.setLeft(actualRectangle.left() + .8 * actualRectangle.width() * ((double)100 - m_iAnimatedValue) / 100);
     621    pPainter->fillRect(actualRectangle, backgroundColor);
    458622
    459623    /* Restore painter: */
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/graphics/UIGraphicsScrollBar.h

    r77203 r77205  
    3434{
    3535    Q_OBJECT;
     36    Q_PROPERTY(int animatedValue READ animatedValue WRITE setAnimatedValue);
    3637
    3738signals:
     39
     40    /** Notifies listeners about hover enter. */
     41    void sigHoverEnter();
     42    /** Notifies listeners about hover leave. */
     43    void sigHoverLeave();
    3844
    3945    /** Notifies listeners about @a iValue has changed. */
     
    8288    virtual void mousePressEvent(QGraphicsSceneMouseEvent *pEvent) /* override */;
    8389
     90    /** Handles hover enter @a pEvent. */
     91    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *pEvent) /* override */;
     92    /** Handles hover leave @a pEvent. */
     93    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *pEvent) /* override */;
     94
     95    /** Handles timer @a pEvent. */
     96    virtual void timerEvent(QTimerEvent *pEvent) /* override */;
     97
    8498private slots:
    8599
     
    92106    void sltTokenMoved(const QPointF &pos);
    93107
     108    /** Handles default state leaving. */
     109    void sltStateLeftDefault();
     110    /** Handles hovered state leaving. */
     111    void sltStateLeftHovered();
     112    /** Handles default state entering. */
     113    void sltStateEnteredDefault();
     114    /** Handles hovered state entering. */
     115    void sltStateEnteredHovered();
     116
    94117private:
    95118
     
    98121    /** Prepares widgets. */
    99122    void prepareWidgets();
     123    /** Prepares animation. */
     124    void prepareAnimation();
    100125
    101126    /** Updates scroll-bar extent value. */
     
    110135    /** Paints background using specified @a pPainter and certain @a rectangle. */
    111136    void paintBackground(QPainter *pPainter, const QRect &rectangle) const;
     137
     138    /** Defines item's animated @a iValue. */
     139    void setAnimatedValue(int iValue) { m_iAnimatedValue = iValue; update(); }
     140    /** Returns item's animated value. */
     141    int animatedValue() const { return m_iAnimatedValue; }
    112142
    113143    /** Holds the orientation. */
     
    133163    /** Holds the scroll-bar token instance. */
    134164    UIGraphicsScrollBarToken *m_pToken;
     165
     166    /** Holds whether item is hovered. */
     167    bool  m_fHovered;
     168    /** Holds the hover-off timer id. */
     169    int   m_iHoverOffTimerId;
     170    /** Holds the animated value. */
     171    int   m_iAnimatedValue;
    135172};
    136173
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