VirtualBox

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


Ignore:
Timestamp:
Mar 23, 2018 12:07:29 PM (7 years ago)
Author:
vboxsync
Message:

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

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

Legend:

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

    r69726 r71484  
    55
    66/*
    7  * Copyright (C) 2010-2017 Oracle Corporation
     7 * Copyright (C) 2010-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2222/* Qt includes: */
    2323# include <QApplication>
    24 # include <QStyle>
    2524# include <QLabel>
    2625# include <QPainter>
     26# include <QPaintEvent>
     27# include <QStyle>
    2728# include <QVBoxLayout>
    28 # include <QPaintEvent>
    2929
    3030/* GUI includes: */
     
    3636#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    3737
     38
     39/*********************************************************************************************************************************
     40*   Class UIPopupBox implementation.                                                                                             *
     41*********************************************************************************************************************************/
     42
    3843UIPopupBox::UIPopupBox(QWidget *pParent)
    3944  : QWidget(pParent)
    40   , m_pTitleIcon(new QLabel(this))
    41   , m_pWarningIcon(new QLabel(this))
    42   , m_pTitleLabel(new QLabel(this))
     45  , m_pTitleIcon(0)
     46  , m_pWarningIcon(0)
     47  , m_pTitleLabel(0)
    4348  , m_fLinkEnabled(false)
     49  , m_fOpened(true)
     50  , m_fHovered(false)
    4451  , m_pContentWidget(0)
    45   , m_fOpen(true)
    4652  , m_pLabelPath(0)
    4753  , m_iArrowWidth(9)
    48   , m_fHeaderHover(false)
    49 {
    50     /* Setup main-layout: */
    51     QVBoxLayout *pMainLayout = new QVBoxLayout(this);
    52     /* Setup title-layout: */
    53     QHBoxLayout *pTitleLayout = new QHBoxLayout;
    54     pTitleLayout->addWidget(m_pTitleIcon);
    55     pTitleLayout->addWidget(m_pWarningIcon);
    56     pTitleLayout->addWidget(m_pTitleLabel, Qt::AlignLeft);
    57     /* Add title-layout into main-layout: */
    58     pMainLayout->addLayout(pTitleLayout);
    59 
    60     /* Configure widgets: */
    61     m_pWarningIcon->setHidden(true);
     54{
     55    /* Configure self: */
     56    installEventFilter(this);
     57
     58    /* Configure painter-path: */
    6259    m_arrowPath.lineTo(m_iArrowWidth / 2.0, m_iArrowWidth / 2.0);
    6360    m_arrowPath.lineTo(m_iArrowWidth, 0);
    6461
    65     /* Setup connections: */
    66     connect(m_pTitleLabel, SIGNAL(linkActivated(const QString)), this, SIGNAL(sigTitleClicked(const QString)));
    67 
    68     /* Install local event-filters: */
    69     installEventFilter(this);
    70     m_pTitleIcon->installEventFilter(this);
    71     m_pWarningIcon->installEventFilter(this);
    72     m_pTitleLabel->installEventFilter(this);
     62    /* Create main-layout: */
     63    QVBoxLayout *pMainLayout = new QVBoxLayout(this);
     64    if (pMainLayout)
     65    {
     66        /* Create title-layout: */
     67        QHBoxLayout *pTitleLayout = new QHBoxLayout;
     68        if (pTitleLayout)
     69        {
     70            /* Create title-icon label. */
     71            m_pTitleIcon = new QLabel;
     72            if (m_pTitleIcon)
     73            {
     74                /* Configure label: */
     75                m_pTitleIcon->installEventFilter(this);
     76
     77                /* Add into layout: */
     78                pTitleLayout->addWidget(m_pTitleIcon);
     79            }
     80            /* Create warning-icon label. */
     81            m_pWarningIcon = new QLabel;
     82            if (m_pWarningIcon)
     83            {
     84                /* Configure label: */
     85                m_pWarningIcon->setHidden(true);
     86                m_pWarningIcon->installEventFilter(this);
     87
     88                /* Add into layout: */
     89                pTitleLayout->addWidget(m_pWarningIcon);
     90            }
     91            /* Create title-text label. */
     92            m_pTitleLabel = new QLabel;
     93            if (m_pTitleLabel)
     94            {
     95                /* Configure label: */
     96                m_pTitleLabel->installEventFilter(this);
     97                connect(m_pTitleLabel, SIGNAL(linkActivated(const QString)),
     98                        this, SIGNAL(sigTitleClicked(const QString)));
     99
     100                /* Add into layout: */
     101                pTitleLayout->addWidget(m_pTitleLabel, Qt::AlignLeft);
     102            }
     103
     104            /* Add into layout: */
     105            pMainLayout->addLayout(pTitleLayout);
     106        }
     107    }
     108
    73109}
    74110
    75111UIPopupBox::~UIPopupBox()
    76112{
     113    /* Delete label painter-path if any: */
    77114    if (m_pLabelPath)
    78115        delete m_pLabelPath;
     
    170207}
    171208
    172 void UIPopupBox::setOpen(bool fOpen)
     209void UIPopupBox::setOpen(bool fOpened)
    173210{
    174211    /* Check if we should toggle popup-box: */
    175     if (m_fOpen == fOpen)
     212    if (m_fOpened == fOpened)
    176213        return;
    177214
    178215    /* Store new value: */
    179     m_fOpen = fOpen;
     216    m_fOpened = fOpened;
    180217
    181218    /* Update content-widget if present or this itself: */
    182219    if (m_pContentWidget)
    183         m_pContentWidget->setVisible(m_fOpen);
     220        m_pContentWidget->setVisible(m_fOpened);
    184221    else
    185222        update();
     
    193230{
    194231    /* Switch 'opened' state: */
    195     setOpen(!m_fOpen);
     232    setOpen(!m_fOpened);
    196233
    197234    /* Notify listeners about toggling: */
    198     emit sigToggled(m_fOpen);
     235    emit sigToggled(m_fOpened);
    199236}
    200237
    201238bool UIPopupBox::isOpen() const
    202239{
    203     return m_fOpen;
    204 }
    205 
    206 bool UIPopupBox::eventFilter(QObject *pWatched, QEvent *pEvent)
     240    return m_fOpened;
     241}
     242
     243bool UIPopupBox::eventFilter(QObject *pObject, QEvent *pEvent)
    207244{
    208245    /* Handle all mouse-event to update hover: */
     
    214251        updateHover();
    215252    /* Call to base-class: */
    216     return QWidget::eventFilter(pWatched, pEvent);
     253    return QWidget::eventFilter(pObject, pEvent);
    217254}
    218255
     
    223260    /* Call to base-class: */
    224261    QWidget::resizeEvent(pEvent);
    225 }
    226 
    227 void UIPopupBox::mouseDoubleClickEvent(QMouseEvent * /* pEvent */)
    228 {
    229     /* Toggle popup-box: */
    230     toggleOpen();
    231262}
    232263
     
    250281    lg.setColorAt(1, base.darker(110));
    251282    int theight = rect.height();
    252     if (m_fOpen)
     283    if (m_fOpened)
    253284        theight = 2 * 5 + iMaxHeightHint;
    254285    painter.fillRect(QRect(rect.x(), rect.y(), rect.width(), theight), lg);
     
    257288    painter.strokePath(*m_pLabelPath, base.darker(110));
    258289    /* Arrow */
    259     if (m_fHeaderHover)
     290    if (m_fHovered)
    260291    {
    261292        painter.setBrush(base.darker(106));
    262293        painter.setPen(QPen(base.darker(128), 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    263294        QSizeF s = m_arrowPath.boundingRect().size();
    264         if (m_fOpen)
     295        if (m_fOpened)
    265296        {
    266297            painter.translate(rect.x() + rect.width() - s.width() - 10, rect.y() + theight / 2 + s.height() / 2);
     
    274305        painter.drawPath(m_arrowPath);
    275306    }
     307}
     308
     309void UIPopupBox::mouseDoubleClickEvent(QMouseEvent *)
     310{
     311    /* Toggle popup-box: */
     312    toggleOpen();
    276313}
    277314
     
    308345        QPalette pal = m_pTitleLabel->palette();
    309346        m_pTitleLabel->setText(QString("<b><a style=\"text-decoration: none; color: %1\" href=\"%2\">%3</a></b>")
    310                                .arg(m_fHeaderHover ? pal.color(QPalette::Link).name() : pal.color(QPalette::WindowText).name())
     347                               .arg(m_fHovered ? pal.color(QPalette::Link).name() : pal.color(QPalette::WindowText).name())
    311348                               .arg(m_strLink)
    312349                               .arg(m_strTitle));
     
    317354{
    318355    /* Calculate new header-hover state: */
    319     bool fNewHeaderHover = m_fHeaderHover;
     356    bool fNewHovered = m_fHovered;
    320357    if (m_pLabelPath && m_pLabelPath->contains(mapFromGlobal(QCursor::pos())))
    321         fNewHeaderHover = true;
     358        fNewHovered = true;
    322359    else
    323         fNewHeaderHover = false;
     360        fNewHovered = false;
    324361
    325362    /* Check if we should toggle hover: */
    326     if (m_fHeaderHover == fNewHeaderHover)
     363    if (m_fHovered == fNewHovered)
    327364        return;
    328365
    329366    /* If header-hover state switched from disabled to enabled: */
    330     if (!m_fHeaderHover && fNewHeaderHover)
     367    if (!m_fHovered && fNewHovered)
    331368        /* Notify listeners: */
    332369        emit sigGotHover();
    333370
    334371    /* Toggle hover: */
    335     toggleHover(fNewHeaderHover);
     372    toggleHover(fNewHovered);
    336373}
    337374
     
    339376{
    340377    /* Check if we should toggle hover: */
    341     if (m_fHeaderHover == false)
     378    if (m_fHovered == false)
    342379        return;
    343380
     
    349386{
    350387    /* Remember header-hover state: */
    351     m_fHeaderHover = fHeaderHover;
     388    m_fHovered = fHeaderHover;
    352389
    353390    /* Update title: */
     
    373410}
    374411
     412
     413/*********************************************************************************************************************************
     414*   Class UIPopupBoxGroup implementation.                                                                                        *
     415*********************************************************************************************************************************/
     416
    375417UIPopupBoxGroup::UIPopupBoxGroup(QObject *pParent)
    376418    : QObject(pParent)
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIPopupBox.h

    r69500 r71484  
    55
    66/*
    7  * Copyright (C) 2010-2017 Oracle Corporation
     7 * Copyright (C) 2010-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1616 */
    1717
    18 #ifndef __UIPopupBoxStuff_h__
    19 #define __UIPopupBoxStuff_h__
    20 
    21 /* Global includes: */
     18#ifndef ___UIPopupBoxStuff_h___
     19#define ___UIPopupBoxStuff_h___
     20
     21/* Qt includes: */
    2222#include <QIcon>
    2323#include <QWidget>
    2424
    2525/* Forward declarations: */
     26class QEvent;
     27class QIcon;
    2628class QLabel;
    27 
    28 /* QWidget reimplementation,
    29  * wraps content-widget with nice/collapsable frame: */
     29class QMouseEvent;
     30class QObject;
     31class QPainterPath;
     32class QPaintEvent;
     33class QResizeEvent;
     34class QString;
     35class QWidget;
     36
     37
     38/** QWidget extension,
     39  * wrapping content-widget with nice collapsable frame. */
    3040class UIPopupBox : public QWidget
    3141{
     
    3444signals:
    3545
    36     /* Signals: */
     46    /** Notifies about title with @a strLink was clicked. */
    3747    void sigTitleClicked(const QString &strLink);
     48
     49    /** Notifies box was toggled and currently @a fOpened. */
    3850    void sigToggled(bool fOpened);
     51
     52    /** Asks to update contents widget. */
    3953    void sigUpdateContentWidget();
     54
     55    /** Notify about box is hovered. */
    4056    void sigGotHover();
    4157
    4258public:
    4359
    44     /* Constructor/destructor: */
     60    /** Construct popup-box passing @a pParent to the base-class. */
    4561    UIPopupBox(QWidget *pParent);
    46     ~UIPopupBox();
    47 
    48     /* Title-icon stuff: */
     62    /** Destruct popup-box. */
     63    virtual ~UIPopupBox() /* override */;
     64
     65    /** Defines title @a icon. */
    4966    void setTitleIcon(const QIcon &icon);
     67    /** Returns title icon. */
    5068    QIcon titleIcon() const;
    5169
    52     /* Warning-icon stuff: */
     70    /** Defines warning @a icon. */
    5371    void setWarningIcon(const QIcon &icon);
     72    /** Returns warnings icon. */
    5473    QIcon warningIcon() const;
    5574
    56     /* Title stuff: */
     75    /** Defines @a strTitle. */
    5776    void setTitle(const QString &strTitle);
     77    /** Returns title. */
    5878    QString title() const;
    5979
    60     /* Link stuff: */
     80    /** Defines title @a strLink. */
    6181    void setTitleLink(const QString &strLink);
     82    /** Returns title link. */
    6283    QString titleLink() const;
     84    /** Defines whether title link is @a fEnabled. */
    6385    void setTitleLinkEnabled(bool fEnabled);
     86    /** Returns whether title link is enabled. */
    6487    bool isTitleLinkEnabled() const;
    6588
    66     /* Content-widget stuff: */
     89    /** Defines content @a pWidget. */
    6790    void setContentWidget(QWidget *pWidget);
    68     QWidget* contentWidget() const;
    69 
    70     /* Toggle stuff: */
    71     void setOpen(bool fOpen);
     91    /** Returns content widget. */
     92    QWidget *contentWidget() const;
     93
     94    /** Defines whether box is @a fOpened. */
     95    void setOpen(bool fOpened);
     96    /** Toggles current opened state. */
    7297    void toggleOpen();
     98    /** Returns whether box is opened. */
    7399    bool isOpen() const;
    74100
    75     /* Update stuff: */
     101    /** Calls for content iwdget update. */
    76102    void callForUpdateContentWidget() { emit sigUpdateContentWidget(); }
    77103
    78104protected:
    79105
    80     /* Event filter: */
    81     bool eventFilter(QObject *pWatched, QEvent *pEvent);
    82 
    83     /* Event handlers: */
    84     void resizeEvent(QResizeEvent *pEvent);
    85     void mouseDoubleClickEvent(QMouseEvent *pEvent);
    86     void paintEvent(QPaintEvent *pEvent);
     106    /** Pre-handles standard Qt @a pEvent for passed @a pObject. */
     107    virtual bool eventFilter(QObject *pObject, QEvent *pEvent) /* override */;
     108
     109    /** Handles resize @a pEvent. */
     110    virtual void resizeEvent(QResizeEvent *pEvent) /* override */;
     111
     112    /** Handles paint @a pEvent. */
     113    virtual void paintEvent(QPaintEvent *pEvent) /* override */;
     114
     115    /** Handles mouse double-click @a pEvent. */
     116    virtual void mouseDoubleClickEvent(QMouseEvent *pEvent) /* override */;
    87117
    88118private:
    89119
    90     /* Helpers: */
     120    /** Updates title icon. */
    91121    void updateTitleIcon();
     122    /** Updates warning icon. */
    92123    void updateWarningIcon();
     124    /** Updates title. */
    93125    void updateTitle();
     126    /** Updates hovered state. */
    94127    void updateHover();
     128    /** Revokes hovered state. */
    95129    void revokeHover();
     130    /** Toggles hovered state to @a fHeaderHover. */
    96131    void toggleHover(bool fHeaderHover);
     132    /** Recalculates geometry. */
    97133    void recalc();
    98134
    99     /* Widgets: */
     135    /** Holds the title icon label. */
    100136    QLabel *m_pTitleIcon;
     137    /** Holds the warning icon label. */
    101138    QLabel *m_pWarningIcon;
     139    /** Holds the title label. */
    102140    QLabel *m_pTitleLabel;
    103141
    104     /* Variables: */
    105     QIcon m_titleIcon;
    106     QIcon m_warningIcon;
    107     QString m_strTitle;
    108     QString m_strLink;
    109     bool m_fLinkEnabled;
     142    /** Holds the title icon. */
     143    QIcon    m_titleIcon;
     144    /** Holds the warning icon. */
     145    QIcon    m_warningIcon;
     146    /** Holds the title text. */
     147    QString  m_strTitle;
     148    /** Holds the link icon. */
     149    QString  m_strLink;
     150
     151    /** Holds whether the link is enabled. */
     152    bool m_fLinkEnabled : 1;
     153    /** Holds whether box is opened. */
     154    bool m_fOpened      : 1;
     155    /** Holds whether header is hovered. */
     156    bool m_fHovered     : 1;
     157
     158    /** Holds the content widget. */
    110159    QWidget *m_pContentWidget;
    111     bool m_fOpen;
     160
     161    /** Holds the label painter path. */
    112162    QPainterPath *m_pLabelPath;
     163
     164    /** Holds the arrow width. */
    113165    const int m_iArrowWidth;
     166    /** Holds the arrow painter-path. */
    114167    QPainterPath m_arrowPath;
    115     bool m_fHeaderHover;
    116 
    117     /* Friend class: */
     168
     169    /** Allow popup-box group to access private API. */
    118170    friend class UIPopupBoxGroup;
    119171};
    120172
    121 /* QObject reimplementation,
    122  * provides a container to organize groups of popup-boxes: */
     173
     174/** QObject extension,
     175  * provides a container to organize groups of popup-boxes. */
    123176class UIPopupBoxGroup : public QObject
    124177{
     
    127180public:
    128181
    129     /* Constructor/destructor: */
     182    /** Construct popup-box passing @a pParent to the base-class. */
    130183    UIPopupBoxGroup(QObject *pParent);
    131     ~UIPopupBoxGroup();
    132 
    133     /* Add popup-box: */
     184    /** Destruct popup-box. */
     185    virtual ~UIPopupBoxGroup() /* override */;
     186
     187    /** Adds @a pPopupBox into group. */
    134188    void addPopupBox(UIPopupBox *pPopupBox);
    135189
    136190private slots:
    137191
    138     /* Hover-change handler: */
     192    /** Handles group hovering. */
    139193    void sltHoverChanged();
    140194
    141195private:
    142196
    143     /* Variables: */
     197    /** Holds the list of popup-boxes. */
    144198    QList<UIPopupBox*> m_list;
    145199};
    146200
    147 #endif /* !__UIPopupBoxStuff_h__ */
    148 
     201
     202#endif /* !___UIPopupBoxStuff_h___ */
     203
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