VirtualBox

Ignore:
Timestamp:
May 24, 2017 11:19:27 AM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
115717
Message:

FE/Qt: Extensions: UIMenuToolBar, a new UIToolBar extension representing single drop-down menu of actions.

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk

    r67017 r67060  
    452452        src/widgets/UIToolBar.h \
    453453        src/widgets/VBoxMediaComboBox.h \
     454        src/widgets/UIMenuToolBar.h \
    454455        src/widgets/UIMiniToolBar.h \
    455456        src/widgets/VBoxOSTypeSelectorButton.h \
     
    563564        src/settings/machine/UIMachineSettingsUSB.cpp \
    564565        src/widgets/UIHotKeyEditor.cpp \
     566        src/widgets/UIMenuToolBar.cpp \
    565567        src/widgets/UIMiniToolBar.cpp \
    566568        src/widgets/UIPortForwardingTable.cpp \
     
    759761        src/widgets/VBoxGuestRAMSlider.cpp \
    760762        src/widgets/VBoxMediaComboBox.cpp \
     763        src/widgets/UIMenuToolBar.cpp \
    761764        src/widgets/UIMiniToolBar.cpp \
    762765        src/widgets/VBoxOSTypeSelectorButton.cpp \
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIMenuToolBar.cpp

    r67016 r67060  
    11/* $Id$ */
    22/** @file
    3  * VBox Qt GUI - UIToolBar class implementation.
     3 * VBox Qt GUI - UIMenuToolBar class implementation.
    44 */
    55
    66/*
    7  * Copyright (C) 2006-2016 Oracle Corporation
     7 * Copyright (C) 2017 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2121
    2222/* Qt includes: */
    23 # include <QLayout>
    24 # include <QMainWindow>
     23# include <QApplication>
     24# include <QHBoxLayout>
     25# include <QPainter>
     26# include <QStyle>
    2527
    2628/* GUI includes: */
     29# include "UIMenuToolBar.h"
    2730# include "UIToolBar.h"
    28 # ifdef VBOX_WS_MAC
    29 #  include "VBoxUtils.h"
    30 # endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
     31
     32/* Other VBox includes: */
     33#include "iprt/assert.h"
    3134
    3235#endif /* VBOX_WS_MAC */
    3336
    34 /* Qt includes: */
    35 #if QT_VERSION < 0x050000
    36 # include <QWindowsStyle>
    37 # include <QCleanlooksStyle>
    38 #endif /* QT_VERSION < 0x050000 */
    39 
    40 
    41 UIToolBar::UIToolBar(QWidget *pParent /* = 0 */)
    42     : QToolBar(pParent)
    43     , m_pMainWindow(qobject_cast<QMainWindow*>(pParent))
     37
     38/** UIToolBar extension
     39  * holding single drop-down menu of actions. */
     40class UIMenuToolBarPrivate : public UIToolBar
     41{
     42    Q_OBJECT;
     43
     44public:
     45
     46    /** Constructs toolbar. */
     47    UIMenuToolBarPrivate(QWidget *pParent = 0);
     48
     49    /** Rebuilds toolbar shape. */
     50    void rebuildShape();
     51
     52    /** Defines toolbar alignment @a enmType. */
     53    void setAlignmentType(UIMenuToolBar::AlignmentType enmType);
     54
     55    /** Defines toolbar menu action. */
     56    void setMenuAction(QAction *pAction);
     57
     58protected:
     59
     60    /** Handles show @a pEvent. */
     61    virtual void showEvent(QShowEvent *pEvent) /* override */;
     62
     63    /** Handles polish @a pEvent. */
     64    virtual void polishEvent(QShowEvent *pEvent);
     65
     66    /** Handles resize @a pEvent. */
     67    virtual void resizeEvent(QResizeEvent *pEvent) /* override */;
     68
     69    /** Handles paint @a pEvent. */
     70    virtual void paintEvent(QPaintEvent *pEvent) /* override */;
     71
     72private:
     73
     74    /** Holds whether this widget was polished. */
     75    bool m_fPolished;
     76
     77    /** Holds the left margin instance. */
     78    QWidget *m_pMarginLeft;
     79    /** Holds the right margin instance. */
     80    QWidget *m_pMarginRight;
     81
     82    /** Holds the menu toolbar alignment type. */
     83    UIMenuToolBar::AlignmentType m_enmAlignmentType;
     84
     85    /** Holds the shape. */
     86    QPainterPath m_shape;
     87};
     88
     89
     90/*********************************************************************************************************************************
     91*   Class UIMenuToolBarPrivate implementation.                                                                                   *
     92*********************************************************************************************************************************/
     93
     94UIMenuToolBarPrivate::UIMenuToolBarPrivate(QWidget *pParent /* = 0 */)
     95    : UIToolBar(pParent)
     96    , m_fPolished(false)
     97    , m_pMarginLeft(0)
     98    , m_pMarginRight(0)
     99    , m_enmAlignmentType(UIMenuToolBar::AlignmentType_TopLeft)
     100{
     101    /* Rebuild shape: */
     102    rebuildShape();
     103}
     104
     105void UIMenuToolBarPrivate::rebuildShape()
     106{
     107    /* Get the metric: */
     108    const int iIconMetric = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize);
     109    const int iRounding = qMax(iIconMetric / 4, 4);
     110
     111    /* Configure margins: */
     112    if (m_pMarginLeft && m_pMarginRight)
     113    {
     114        const int iStandardMargin = iRounding;
     115        int iLeftMargin = iStandardMargin;
     116        int iRightMargin = iStandardMargin;
     117        if (   m_enmAlignmentType == UIMenuToolBar::AlignmentType_TopLeft
     118            || m_enmAlignmentType == UIMenuToolBar::AlignmentType_BottomLeft)
     119            iRightMargin += iRounding;
     120        if (   m_enmAlignmentType == UIMenuToolBar::AlignmentType_TopRight
     121            || m_enmAlignmentType == UIMenuToolBar::AlignmentType_BottomRight)
     122            iLeftMargin += iRounding;
     123        m_pMarginLeft->setMinimumWidth(iLeftMargin);
     124        m_pMarginRight->setMinimumWidth(iRightMargin);
     125    }
     126
     127    /* Rebuild shape: */
     128    QPainterPath shape;
     129    switch (m_enmAlignmentType)
     130    {
     131        case UIMenuToolBar::AlignmentType_TopLeft:
     132            shape.moveTo(width(), height());
     133            shape.lineTo(shape.currentPosition().x(), iRounding * 2);
     134            shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
     135                        .translated(-iRounding * 4, -iRounding * 2), 0, 90);
     136            shape.lineTo(0, shape.currentPosition().y());
     137            shape.lineTo(shape.currentPosition().x(), height());
     138            shape.closeSubpath();
     139            break;
     140        case UIMenuToolBar::AlignmentType_TopRight:
     141            shape.moveTo(0, height());
     142            shape.lineTo(shape.currentPosition().x(), iRounding * 2);
     143            shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
     144                        .translated(0, -iRounding * 2), 180, -90);
     145            shape.lineTo(width(), shape.currentPosition().y());
     146            shape.lineTo(shape.currentPosition().x(), height());
     147            shape.closeSubpath();
     148            break;
     149        case UIMenuToolBar::AlignmentType_BottomLeft:
     150            shape.moveTo(width(), 0);
     151            shape.lineTo(shape.currentPosition().x(), height() - iRounding * 2);
     152            shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
     153                        .translated(-iRounding * 4, -iRounding * 2), 0, -90);
     154            shape.lineTo(0, shape.currentPosition().y());
     155            shape.lineTo(shape.currentPosition().x(), 0);
     156            shape.closeSubpath();
     157            break;
     158        case UIMenuToolBar::AlignmentType_BottomRight:
     159            shape.moveTo(0, 0);
     160            shape.lineTo(shape.currentPosition().x(), height() - iRounding * 2);
     161            shape.arcTo(QRectF(shape.currentPosition(), QSizeF(iRounding * 4, iRounding * 4))
     162                        .translated(0, -iRounding * 2), 180, 90);
     163            shape.lineTo(width(), shape.currentPosition().y());
     164            shape.lineTo(shape.currentPosition().x(), 0);
     165            shape.closeSubpath();
     166            break;
     167    }
     168    m_shape = shape;
     169}
     170
     171void UIMenuToolBarPrivate::setAlignmentType(UIMenuToolBar::AlignmentType enmType)
     172{
     173    /* Set alignment type: */
     174    m_enmAlignmentType = enmType;
     175
     176    /* Rebuild shape: */
     177    rebuildShape();
     178}
     179
     180void UIMenuToolBarPrivate::setMenuAction(QAction *pAction)
     181{
     182    /* Clear first: */
     183    clear();
     184    delete m_pMarginLeft;
     185    m_pMarginLeft = 0;
     186    delete m_pMarginRight;
     187    m_pMarginRight = 0;
     188
     189    /* Create left margin: */
     190    m_pMarginLeft = widgetForAction(addWidget(new QWidget));
     191
     192    /* Add action itself: */
     193    addAction(pAction);
     194
     195    /* Create right margin: */
     196    m_pMarginRight = widgetForAction(addWidget(new QWidget));
     197
     198    /* Rebuild shape: */
     199    rebuildShape();
     200}
     201
     202void UIMenuToolBarPrivate::showEvent(QShowEvent *pEvent)
     203{
     204    /* Call to base-class: */
     205    UIToolBar::showEvent(pEvent);
     206
     207    /* Make sure we should polish dialog: */
     208    if (m_fPolished)
     209        return;
     210
     211    /* Call to polish-event: */
     212    polishEvent(pEvent);
     213
     214    /* Mark dialog as polished: */
     215    m_fPolished = true;
     216}
     217
     218void UIMenuToolBarPrivate::polishEvent(QShowEvent * /* pEvent */)
     219{
     220    /* Rebuild shape: */
     221    rebuildShape();
     222}
     223
     224void UIMenuToolBarPrivate::resizeEvent(QResizeEvent *pEvent)
     225{
     226    /* Call to base-class: */
     227    UIToolBar::resizeEvent(pEvent);
     228
     229    /* Rebuild shape: */
     230    rebuildShape();
     231}
     232
     233void UIMenuToolBarPrivate::paintEvent(QPaintEvent * /* pEvent */)
     234{
     235    /* Prepare painter: */
     236    QPainter painter(this);
     237
     238    /* Fill background: */
     239    if (!m_shape.isEmpty())
     240    {
     241        painter.setRenderHint(QPainter::Antialiasing);
     242        painter.setClipPath(m_shape);
     243    }
     244    QRect backgroundRect = rect();
     245    QColor backgroundColor = palette().color(QPalette::Window);
     246    QLinearGradient headerGradient(backgroundRect.bottomLeft(), backgroundRect.topLeft());
     247    headerGradient.setColorAt(0, backgroundColor.darker(120));
     248    headerGradient.setColorAt(1, backgroundColor.darker(104));
     249    painter.fillRect(backgroundRect, headerGradient);
     250}
     251
     252
     253/*********************************************************************************************************************************
     254*   Class UIToolBarMenu implementation.                                                                                          *
     255*********************************************************************************************************************************/
     256
     257UIMenuToolBar::UIMenuToolBar(QWidget *pParent /* = 0 */)
     258    : QWidget(pParent)
    44259{
    45260    /* Prepare: */
     
    47262}
    48263
    49 void UIToolBar::setUseTextLabels(bool fEnable)
    50 {
    51     /* Determine tool-button style on the basis of passed flag: */
    52     Qt::ToolButtonStyle tbs = fEnable ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly;
    53 
    54     /* Depending on parent, assign this style: */
    55     if (m_pMainWindow)
    56         m_pMainWindow->setToolButtonStyle(tbs);
    57     else
    58         setToolButtonStyle(tbs);
    59 }
    60 
    61 #ifdef VBOX_WS_MAC
    62 void UIToolBar::enableMacToolbar()
    63 {
    64     /* Depending on parent, enable unified title/tool-bar: */
    65     if (m_pMainWindow)
    66         m_pMainWindow->setUnifiedTitleAndToolBarOnMac(true);
    67 }
    68 
    69 void UIToolBar::setShowToolBarButton(bool fShow)
    70 {
    71     ::darwinSetShowsToolbarButton(this, fShow);
    72 }
    73 
    74 void UIToolBar::updateLayout()
    75 {
    76     /* There is a bug in Qt Cocoa which result in showing a "more arrow" when
    77        the necessary size of the toolbar is increased. Also for some languages
    78        the with doesn't match if the text increase. So manually adjust the size
    79        after changing the text. */
    80     QSizePolicy sp = sizePolicy();
    81     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    82     adjustSize();
    83     setSizePolicy(sp);
    84     layout()->invalidate();
    85     layout()->activate();
    86 }
    87 #endif /* VBOX_WS_MAC */
    88 
    89 void UIToolBar::prepare()
    90 {
    91     /* Configure tool-bar: */
    92     setFloatable(false);
    93     setMovable(false);
    94 
    95 #if QT_VERSION < 0x050000
    96     /* Remove that ugly frame panel around the toolbar.
    97      * Doing that currently for Cleanlooks & Windows styles. */
    98     if (qobject_cast <QWindowsStyle*>(QToolBar::style()) ||
    99         qobject_cast <QCleanlooksStyle*>(QToolBar::style()))
    100         setStyleSheet("QToolBar { border: 0px none black; }");
    101 #else /* QT_VERSION >= 0x050000 */
    102 # ifdef VBOX_WS_MAC
    103         setStyleSheet("QToolBar { border: 0px none black; }");
    104 # endif /* VBOX_WS_MAC */
    105 #endif /* QT_VERSION >= 0x050000 */
    106 
    107     /* Configure tool-bar' layout: */
    108     if (layout())
     264void UIMenuToolBar::prepare()
     265{
     266    /* Create layout: */
     267    new QHBoxLayout(this);
     268    AssertPtrReturnVoid(layout());
     269    {
     270        /* Configure layout: */
    109271        layout()->setContentsMargins(0, 0, 0, 0);
    110272
    111     /* Configure tool-bar' context-menu policy: */
    112     setContextMenuPolicy(Qt::NoContextMenu);
    113 }
    114 
     273        /* Create menu-toolbar: */
     274        m_pToolbar = new UIMenuToolBarPrivate;
     275        AssertPtrReturnVoid(m_pToolbar);
     276        {
     277            /* Configure menu-toolbar: */
     278            m_pToolbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
     279
     280            /* Add into layout: */
     281            layout()->addWidget(m_pToolbar);
     282        }
     283    }
     284}
     285
     286void UIMenuToolBar::setAlignmentType(AlignmentType enmType)
     287{
     288    /* Pass to private object: */
     289    return m_pToolbar->setAlignmentType(enmType);
     290}
     291
     292void UIMenuToolBar::setIconSize(const QSize &size)
     293{
     294    /* Pass to private object: */
     295    return m_pToolbar->setIconSize(size);
     296}
     297
     298void UIMenuToolBar::setMenuAction(QAction *pAction)
     299{
     300    /* Pass to private object: */
     301    return m_pToolbar->setMenuAction(pAction);
     302}
     303
     304void UIMenuToolBar::setToolButtonStyle(Qt::ToolButtonStyle enmStyle)
     305{
     306    /* Pass to private object: */
     307    return m_pToolbar->setToolButtonStyle(enmStyle);
     308}
     309
     310QWidget *UIMenuToolBar::widgetForAction(QAction *pAction) const
     311{
     312    /* Pass to private object: */
     313    return m_pToolbar->widgetForAction(pAction);
     314}
     315
     316#include "UIMenuToolBar.moc"
     317
  • trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIMenuToolBar.h

    r67016 r67060  
    11/* $Id$ */
    22/** @file
    3  * VBox Qt GUI - UIToolBar class declaration.
     3 * VBox Qt GUI - UIMenuToolBar class declaration.
    44 */
    55
    66/*
    7  * Copyright (C) 2006-2016 Oracle Corporation
     7 * Copyright (C) 2017 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1616 */
    1717
    18 #ifndef ___UIToolBar_h___
    19 #define ___UIToolBar_h___
     18#ifndef ___UIMenuToolBar_h___
     19#define ___UIMenuToolBar_h___
    2020
    2121/* Qt includes: */
    22 #include <QToolBar>
     22#include <QWidget>
    2323
    2424/* Forward declarations: */
    25 class QMainWindow;
     25class UIMenuToolBarPrivate;
    2626
    27 /** QToolBar extension
    28   * with few settings presets. */
    29 class UIToolBar : public QToolBar
     27
     28/** QWidget wrapper for UIToolBar extension
     29  * holding single drop-down menu of actions. */
     30class UIMenuToolBar : public QWidget
    3031{
    3132    Q_OBJECT;
     
    3334public:
    3435
    35     /** Constructor, passes @a pParent to the QToolBar constructor. */
    36     UIToolBar(QWidget *pParent = 0);
     36    /** Menu toolbar alignment types. */
     37    enum AlignmentType
     38    {
     39        AlignmentType_TopLeft,
     40        AlignmentType_TopRight,
     41        AlignmentType_BottomLeft,
     42        AlignmentType_BottomRight,
     43    };
    3744
    38     /** Defines whether tool-bar should use text-labels.
    39       * Default value if @a false. */
    40     void setUseTextLabels(bool fEnable);
     45    /** Constructs menu-toolbar wrapper. */
     46    UIMenuToolBar(QWidget *pParent = 0);
    4147
    42 #ifdef VBOX_WS_MAC
    43     /** Mac OS X: Defines whether native tool-bar should be used. */
    44     void enableMacToolbar();
    45     /** Mac OS X: Defines whether native tool-bar button should be shown. */
    46     void setShowToolBarButton(bool fShow);
    47     /** Mac OS X: Updates native tool-bar layout. */
    48     void updateLayout();
    49 #endif /* VBOX_WS_MAC */
     48    /** Defines toolbar alignment @a enmType. */
     49    void setAlignmentType(AlignmentType enmType);
     50
     51    /** Defines toolbar icon @a size. */
     52    void setIconSize(const QSize &size);
     53
     54    /** Defines toolbar menu action. */
     55    void setMenuAction(QAction *pAction);
     56
     57    /** Defines toolbar tool button @a enmStyle. */
     58    void setToolButtonStyle(Qt::ToolButtonStyle enmStyle);
     59
     60    /** Returns toolbar widget for passed @a pAction. */
     61    QWidget *widgetForAction(QAction *pAction) const;
    5062
    5163private:
    5264
    53     /** Prepare routine. */
     65    /** Prepares all. */
    5466    void prepare();
    5567
    56     /** Holds the parent main-window isntance. */
    57     QMainWindow *m_pMainWindow;
     68    /** Holds the menu-toolbar instance. */
     69    UIMenuToolBarPrivate *m_pToolbar;
    5870};
    5971
    60 #endif /* !___UIToolBar_h___ */
     72#endif /* !___UIMenuToolBar_h___ */
     73
Note: See TracChangeset for help on using the changeset viewer.

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