VirtualBox

Ignore:
Timestamp:
Sep 25, 2015 12:45:30 PM (9 years ago)
Author:
vboxsync
Message:

FE/Qt: Mac OS X: Window menu Manager cleanup/rework (step 2): Refactoring.

Location:
trunk/src/VBox/Frontends/VirtualBox/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UIWindowMenuManager.cpp

    r57890 r57891  
    2020#else  /* !VBOX_WITH_PRECOMPILED_HEADERS */
    2121
    22 /* GUI includes: */
    23 # include "UIWindowMenuManager.h"
    24 
    2522/* Qt includes: */
    2623# include <QApplication>
    2724# include <QMenu>
    2825
     26/* GUI includes: */
     27# include "UIWindowMenuManager.h"
     28
     29/* Other VBox includes: */
     30#include <iprt/assert.h>
     31
    2932#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    3033
    3134/** QObject extension
    3235  * used as Mac OS X 'Window' menu helper. */
    33 class UIMenuHelper: public QObject
     36class UIMenuHelper : public QObject
    3437{
    3538    Q_OBJECT;
     
    3740public:
    3841
    39     /** Constructs menu-helper on the basis of passed @a winList. */
    40     UIMenuHelper(const QList<QWidget*> &winList)
    41     {
    42         m_pWindowMenu = new QMenu(0);
     42    /** Constructs menu-helper on the basis of passed @a windows. */
     43    UIMenuHelper(const QList<QWidget*> &windows)
     44    {
     45        /* Prepare 'Window' menu: */
     46        m_pWindowMenu = new QMenu;
     47        /* Prepare action group: */
    4348        m_pGroup = new QActionGroup(this);
    4449        m_pGroup->setExclusive(true);
     50        /* Prepare 'Minimize' action: */
    4551        m_pMinimizeAction = new QAction(this);
    4652        m_pWindowMenu->addAction(m_pMinimizeAction);
    4753        connect(m_pMinimizeAction, SIGNAL(triggered(bool)),
    48                 this, SLOT(minimizeActive(bool)));
    49         /* Make sure all already available windows are properly registered on
    50          * this menu. */
    51         for (int i=0; i < winList.size(); ++i)
    52             addWindow(winList.at(i));
    53 
     54                this, SLOT(sltMinimizeActiveWindow()));
     55        /* Make sure all already available windows are
     56         * properly registered within this menu: */
     57        for (int i = 0; i < windows.size(); ++i)
     58            addWindow(windows.at(i));
     59        /* Translate finally: */
    5460        retranslateUi();
    5561    }
     
    5864    ~UIMenuHelper()
    5965    {
     66        /* Cleanup 'Window' menu: */
    6067        delete m_pWindowMenu;
    61         qDeleteAll(m_regWindows);
     68        /* Cleanup actions: */
     69        qDeleteAll(m_windows);
    6270    }
    6371
    6472    /** Returns 'Window' menu. */
    65     QMenu *menu() const { return m_pWindowMenu; }
    66 
    67     /** Adds window into 'Window' menu. */
     73    QMenu* menu() const { return m_pWindowMenu; }
     74
     75    /** Adds @a pWindow into 'Window' menu. */
    6876    QAction* addWindow(QWidget *pWindow)
    6977    {
    7078        QAction *pAction = 0;
    71         if (    pWindow
    72             && !m_regWindows.contains(pWindow->windowTitle()))
    73         {
    74             if (m_regWindows.size() < 2)
     79        if (   pWindow
     80            && !m_windows.contains(pWindow->windowTitle()))
     81        {
     82            if (m_windows.size() < 2)
    7583                m_pWindowMenu->addSeparator();
    76             /* The main window always first */
     84            /* The main window always first: */
    7785            pAction = new QAction(this);
    7886            pAction->setText(pWindow->windowTitle());
    7987            pAction->setMenuRole(QAction::NoRole);
    80             pAction->setData(qVariantFromValue(pWindow));
     88            pAction->setData(QVariant::fromValue(pWindow));
    8189            pAction->setCheckable(true);
    82             /* The first registered one is always considered as the main window */
    83             if (m_regWindows.size() == 0)
     90            /* The first registered one is always
     91             * considered as the main window: */
     92            if (m_windows.size() == 0)
    8493                pAction->setShortcut(QKeySequence("Ctrl+0"));
    8594            m_pGroup->addAction(pAction);
    8695            connect(pAction, SIGNAL(triggered(bool)),
    87                     this, SLOT(raiseSender(bool)));
     96                    this, SLOT(sltRaiseSender()));
    8897            m_pWindowMenu->addAction(pAction);
    89             m_regWindows[pWindow->windowTitle()] = pAction;
     98            m_windows[pWindow->windowTitle()] = pAction;
    9099        }
    91100        return pAction;
    92101    }
    93102
    94     /** Removes window from 'Window' menu. */
     103    /** Removes @a pWindow from 'Window' menu. */
    95104    void removeWindow(QWidget *pWindow)
    96105    {
    97         if (m_regWindows.contains(pWindow->windowTitle()))
    98         {
    99             delete m_regWindows[pWindow->windowTitle()];
    100             m_regWindows.remove(pWindow->windowTitle());
     106        if (m_windows.contains(pWindow->windowTitle()))
     107        {
     108            delete m_windows.value(pWindow->windowTitle());
     109            m_windows.remove(pWindow->windowTitle());
    101110        }
    102111    }
     
    105114    void retranslateUi()
    106115    {
     116        /* Translate menu: */
    107117        m_pWindowMenu->setTitle(tr("&Window"));
     118
     119        /* Translate menu 'Minimize' action: */
    108120        m_pMinimizeAction->setText(tr("Minimize"));
    109121        m_pMinimizeAction->setShortcut(QKeySequence("Ctrl+M"));
    110122    }
    111123
    112     /** Updates toggle action states according to passed @a pActive. */
    113     void updateStatus(QWidget *pActive)
    114     {
    115         m_pMinimizeAction->setEnabled(pActive != 0);
    116         if (pActive)
    117         {
    118             if (m_regWindows.contains(pActive->windowTitle()))
    119                 m_regWindows[pActive->windowTitle()]->setChecked(true);
    120         }
     124    /** Updates toggle action states according to passed @a pActiveWindow. */
     125    void updateStatus(QWidget *pActiveWindow)
     126    {
     127        /* 'Minimize' action is enabled if there is active-window: */
     128        m_pMinimizeAction->setEnabled(pActiveWindow != 0);
     129        /* If there is active-window: */
     130        if (pActiveWindow)
     131        {
     132            /* Toggle corresponding action on: */
     133            if (m_windows.contains(pActiveWindow->windowTitle()))
     134                m_windows.value(pActiveWindow->windowTitle())->setChecked(true);
     135        }
     136        /* If there is no active-window: */
    121137        else
    122138        {
     139            /* Make sure corresponding action toggled off: */
    123140            if (QAction *pChecked = m_pGroup->checkedAction())
    124141                pChecked->setChecked(false);
    125142        }
    126 
    127143    }
    128144
     
    130146
    131147    /** Handles request to minimize active-window. */
    132     void minimizeActive(bool /* fToggle */)
    133     {
    134         if (QWidget *pActive = qApp->activeWindow())
    135             pActive->showMinimized();
     148    void sltMinimizeActiveWindow()
     149    {
     150        if (QWidget *pActiveWindow = qApp->activeWindow())
     151            pActiveWindow->showMinimized();
    136152    }
    137153
    138154    /** Handles request to raise sender window. */
    139     void raiseSender(bool /* fToggle */)
    140     {
    141         if (QAction *pAction= qobject_cast<QAction*>(sender()))
    142         {
    143             if (QWidget *pWidget = qVariantValue<QWidget*>(pAction->data()))
     155    void sltRaiseSender()
     156    {
     157        AssertReturnVoid(sender());
     158        if (QAction *pAction = qobject_cast<QAction*>(sender()))
     159        {
     160            if (QWidget *pWidget = pAction->data().value<QWidget*>())
    144161            {
    145162                pWidget->show();
     
    159176    QAction *m_pMinimizeAction;
    160177    /** Holds the hash of the registered menu-helper instances. */
    161     QHash<QString, QAction*> m_regWindows;
     178    QHash<QString, QAction*> m_windows;
    162179};
    163180
     181/*********************************************************************************************************************************
     182*   Class UIWindowMenuManager implementation.                                                                                    *
     183*********************************************************************************************************************************/
     184
    164185/* static */
    165 UIWindowMenuManager *UIWindowMenuManager::m_pInstance = 0;
     186UIWindowMenuManager* UIWindowMenuManager::m_spInstance = 0;
    166187
    167188/* static */
    168 UIWindowMenuManager *UIWindowMenuManager::instance(QWidget *pParent /* = 0 */)
    169 {
    170     if (!m_pInstance)
    171         m_pInstance = new UIWindowMenuManager(pParent);
    172 
    173     return m_pInstance;
     189UIWindowMenuManager* UIWindowMenuManager::instance()
     190{
     191    /* Make sure 'Window' menu Manager is created: */
     192    if (!m_spInstance)
     193        m_spInstance = new UIWindowMenuManager;
     194
     195    /* Return 'Window' menu Manager: */
     196    return m_spInstance;
    174197}
    175198
     
    177200void UIWindowMenuManager::destroy()
    178201{
    179     if (!m_pInstance)
    180     {
    181         delete m_pInstance;
    182         m_pInstance = 0;
    183     }
     202    /* Make sure 'Window' menu Manager is created: */
     203    AssertPtrReturnVoid(m_spInstance);
     204
     205    /* Delete 'Window' menu Manager: */
     206    delete m_spInstance;
    184207}
    185208
    186209QMenu *UIWindowMenuManager::createMenu(QWidget *pWindow)
    187210{
    188     UIMenuHelper *pHelper = new UIMenuHelper(m_regWindows);
    189 
     211    /* Create helper: */
     212    UIMenuHelper *pHelper = new UIMenuHelper(m_windows);
     213    /* Register it: */
    190214    m_helpers[pWindow] = pHelper;
    191215
     216    /* Return menu of created helper: */
    192217    return pHelper->menu();
    193218}
     
    195220void UIWindowMenuManager::destroyMenu(QWidget *pWindow)
    196221{
     222    /* If window is registered: */
    197223    if (m_helpers.contains(pWindow))
    198224    {
    199         delete m_helpers[pWindow];
     225        /* Delete helper: */
     226        delete m_helpers.value(pWindow);
     227        /* Unregister it: */
    200228        m_helpers.remove(pWindow);
    201229    }
     
    204232void UIWindowMenuManager::addWindow(QWidget *pWindow)
    205233{
    206     m_regWindows.append(pWindow);
     234    /* Register window: */
     235    m_windows.append(pWindow);
     236    /* Add window to all menus we have: */
    207237    QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin();
    208238    while (i != m_helpers.constEnd())
     
    215245void UIWindowMenuManager::removeWindow(QWidget *pWindow)
    216246{
     247    /* Remove window from all menus we have: */
    217248    QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin();
    218249    while (i != m_helpers.constEnd())
     
    221252        ++i;
    222253    }
    223     m_regWindows.removeAll(pWindow);
     254    /* Unregister window: */
     255    m_windows.removeAll(pWindow);
    224256}
    225257
    226258void UIWindowMenuManager::retranslateUi()
    227259{
     260    /* Translate all the helpers: */
    228261    QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin();
    229262    while (i != m_helpers.constEnd())
     
    234267}
    235268
    236 bool UIWindowMenuManager::eventFilter(QObject *pObj, QEvent *pEvent)
    237 {
    238     QEvent::Type type = pEvent->type();
     269bool UIWindowMenuManager::eventFilter(QObject *pObject, QEvent *pEvent)
     270{
     271    /* Acquire event type: */
     272    const QEvent::Type type = pEvent->type();
     273
    239274#if defined(VBOX_OSE) || (QT_VERSION < 0x040700)
    240275    /* Stupid Qt: Qt doesn't check if a window is minimized when a command is
     
    243278     * window before we let execute the command.
    244279     * Note: fixed in our local Qt build since 4.7.0. */
    245     if (type == QEvent::Show)
    246     {
    247         QWidget *pWidget = (QWidget*)pObj;
    248         if (   pWidget->parentWidget()
     280    if (pObject && type == QEvent::Show)
     281    {
     282        QWidget *pWidget = qobject_cast<QWidget*>(pObject);
     283        if (   pWidget
     284            && pWidget->parentWidget()
    249285            && pWidget->parentWidget()->isMinimized())
    250286        {
     
    254290        }
    255291    }
    256 #endif /* defined(VBOX_OSE) || (QT_VERSION < 0x040700) */
     292#endif /* VBOX_OSE || QT_VERSION < 0x040700 */
     293
    257294    /* We need to track several events which leads to different window
    258295     * activation and change the menu items in that case. */
     
    265302        || type == QEvent::Hide)
    266303    {
    267         QWidget *pActive = qApp->activeWindow();
     304        QWidget *pActiveWindow = qApp->activeWindow();
    268305        QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin();
    269306        while (i != m_helpers.constEnd())
    270307        {
    271             i.value()->updateStatus(pActive);
     308            i.value()->updateStatus(pActiveWindow);
    272309            ++i;
    273310        }
    274311    }
    275     /* Change the strings in all registers window menus */
    276     if (   type == QEvent::LanguageChange
    277         && pObj == m_pParent)
    278         retranslateUi();
    279 
    280     return false;
    281 }
    282 
    283 UIWindowMenuManager::UIWindowMenuManager(QWidget *pParent /* = 0 */)
    284   : QObject(pParent)
    285   , m_pParent(pParent)
    286 {
     312
     313    /* Call to base-class: */
     314    return QIWithRetranslateUI3<QObject>::eventFilter(pObject, pEvent);
     315}
     316
     317UIWindowMenuManager::UIWindowMenuManager()
     318{
     319    /* Assign instance: */
     320    m_spInstance = this;
     321
     322    /* Install global event-filter: */
    287323    qApp->installEventFilter(this);
    288324}
     
    290326UIWindowMenuManager::~UIWindowMenuManager()
    291327{
     328    /* Cleanup all helpers: */
    292329    qDeleteAll(m_helpers);
     330
     331    /* Unassign instance: */
     332    m_spInstance = 0;
    293333}
    294334
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UIWindowMenuManager.h

    r57890 r57891  
    2323#include <QHash>
    2424
     25/* GUI includes: */
     26#include "QIWithRetranslateUI.h"
     27
    2528/* Forward declarations: */
    2629class UIMenuHelper;
     
    2932/** Singleton QObject extension
    3033  * used as Mac OS X 'Window' menu Manager. */
    31 class UIWindowMenuManager: public QObject
     34class UIWindowMenuManager : public QIWithRetranslateUI3<QObject>
    3235{
    3336    Q_OBJECT;
     
    3639
    3740    /** Static constructor and instance provider. */
    38     static UIWindowMenuManager *instance(QWidget *pParent = 0);
     41    static UIWindowMenuManager *instance();
    3942    /** Static destructor. */
    4043    static void destroy();
    4144
    4245    /** Creates 'Window' menu for passed @a pWindow. */
    43     QMenu *createMenu(QWidget *pWindow);
     46    QMenu* createMenu(QWidget *pWindow);
    4447    /** Destroys 'Window' menu for passed @a pWindow. */
    4548    void destroyMenu(QWidget *pWindow);
     
    5154
    5255    /** Handles translation event. */
    53     void retranslateUi();
     56    virtual void retranslateUi();
    5457
    5558protected:
    5659
    5760    /** Preprocesses any Qt @a pEvent for passed @a pObject. */
    58     bool eventFilter(QObject *pObj, QEvent *pEvent);
     61    bool eventFilter(QObject *pObject, QEvent *pEvent);
    5962
    6063private:
    6164
    6265    /** Constructs 'Window' menu Manager. */
    63     UIWindowMenuManager(QWidget *pParent = 0);
     66    UIWindowMenuManager();
    6467    /** Destructs 'Window' menu Manager. */
    6568    ~UIWindowMenuManager();
    6669
    6770    /** Holds the static instance. */
    68     static UIWindowMenuManager *m_pInstance;
    69 
    70     /** Holds the passed parent reference. */
    71     QWidget *m_pParent;
     71    static UIWindowMenuManager *m_spInstance;
    7272
    7373    /** Holds the list of the registered window references. */
    74     QList<QWidget*> m_regWindows;
     74    QList<QWidget*> m_windows;
    7575
    7676    /** Holds the hash of the registered menu-helper instances. */
  • trunk/src/VBox/Frontends/VirtualBox/src/selector/UISelectorWindow.cpp

    r57877 r57891  
    12121212
    12131213#ifdef Q_WS_MAC
    1214     menuBar()->addMenu(UIWindowMenuManager::instance(this)->createMenu(this));
     1214    menuBar()->addMenu(UIWindowMenuManager::instance()->createMenu(this));
    12151215#endif /* Q_WS_MAC */
    12161216
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