VirtualBox

Ignore:
Timestamp:
May 8, 2018 3:57:55 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
122581
Message:

FE/Qt: bugref:9049: Full and heavy cleanup for UIWindowMenuManager and move it to VBoxGlobal library.

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

Legend:

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

    r72158 r72166  
    576576 endif
    577577endif
    578 
    579 VirtualBox_QT_MOCHDRS.darwin += \
    580         src/platform/darwin/UIWindowMenuManager.h
    581578
    582579ifndef VBOX_GUI_WITH_SHARED_LIBRARY
     
    723720
    724721 VirtualBox_QT_MOCHDRS.darwin += \
    725         src/platform/darwin/UICocoaSpecialControls.h
     722        src/platform/darwin/UICocoaSpecialControls.h \
     723        src/platform/darwin/UIWindowMenuManager.h
    726724endif
    727725
     
    873871
    874872VBoxGlobal_QT_MOCHDRS.darwin += \
    875         src/platform/darwin/UICocoaSpecialControls.h
     873        src/platform/darwin/UICocoaSpecialControls.h \
     874        src/platform/darwin/UIWindowMenuManager.h
    876875
    877876
     
    896895        src/widgets/UITabBar.cpp \
    897896        src/wizards/importappliance/UIWizardImportApp.cpp
    898 
    899 VirtualBox_QT_MOCSRCS.darwin += \
    900         src/platform/darwin/UIWindowMenuManager.cpp
    901897
    902898ifndef VBOX_GUI_WITH_SHARED_LIBRARY
     
    928924        src/widgets/UIStatusBarEditorWindow.cpp
    929925
     926 ifdef VBOX_GUI_WITH_NETWORK_MANAGER
     927  VirtualBox_QT_MOCSRCS += \
     928        src/net/UINetworkReply.cpp \
     929        src/net/UIUpdateManager.cpp
     930 endif
     931
     932 ifdef VBOX_WITH_XPCOM
     933  VirtualBox_QT_MOCSRCS += \
     934        src/globals/COMDefs.cpp
     935 endif
     936
     937 VirtualBox_QT_MOCSRCS.darwin += \
     938        src/platform/darwin/UIWindowMenuManager.cpp
     939
    930940 VirtualBox_QT_MOCSRCS.linux += \
    931941        src/globals/UIDesktopWidgetWatchdog.cpp
     
    934944 VirtualBox_QT_MOCSRCS.freebsd += \
    935945        src/globals/UIDesktopWidgetWatchdog.cpp
    936 
    937  ifdef VBOX_GUI_WITH_NETWORK_MANAGER
    938   VirtualBox_QT_MOCSRCS += \
    939         src/net/UINetworkReply.cpp \
    940         src/net/UIUpdateManager.cpp
    941  endif
    942 
    943  ifdef VBOX_WITH_XPCOM
    944   VirtualBox_QT_MOCSRCS += \
    945         src/globals/COMDefs.cpp
    946  endif
    947946endif
    948947
     
    979978        src/widgets/UIStatusBarEditorWindow.cpp
    980979
     980ifdef VBOX_GUI_WITH_NETWORK_MANAGER
     981 VBoxGlobal_QT_MOCSRCS += \
     982        src/net/UINetworkReply.cpp \
     983        src/net/UIUpdateManager.cpp
     984endif
     985
     986ifdef VBOX_WITH_XPCOM
     987 VBoxGlobal_QT_MOCSRCS += \
     988        src/globals/COMDefs.cpp
     989
     990VBoxGlobal_QT_MOCSRCS.darwin += \
     991        src/platform/darwin/UIWindowMenuManager.cpp
     992
    981993VBoxGlobal_QT_MOCSRCS.linux += \
    982994        src/globals/UIDesktopWidgetWatchdog.cpp
     
    985997VBoxGlobal_QT_MOCSRCS.freebsd += \
    986998        src/globals/UIDesktopWidgetWatchdog.cpp
    987 
    988 ifdef VBOX_GUI_WITH_NETWORK_MANAGER
    989  VBoxGlobal_QT_MOCSRCS += \
    990         src/net/UINetworkReply.cpp \
    991         src/net/UIUpdateManager.cpp
    992 endif
    993 
    994 ifdef VBOX_WITH_XPCOM
    995  VBoxGlobal_QT_MOCSRCS += \
    996         src/globals/COMDefs.cpp
    997999endif
    9981000
     
    11271129        src/platform/darwin/UIAbstractDockIconPreview.cpp \
    11281130        src/platform/darwin/UICocoaDockIconPreview.mm \
    1129         src/platform/darwin/UIWindowMenuManager.cpp \
     1131        src/platform/darwin/UIDesktopServices_darwin.cpp \
     1132        src/platform/darwin/UIDesktopServices_darwin_cocoa.mm
    11301133
    11311134if1of ($(KBUILD_TARGET), freebsd linux netbsd openbsd solaris) # X11
     
    11371140        src/platform/win/VirtualBox.rc \
    11381141        src/platform/win/UIDesktopServices_win.cpp
    1139 
    1140 VirtualBox_SOURCES.darwin += \
    1141         src/platform/darwin/UIDesktopServices_darwin.cpp \
    1142         src/platform/darwin/UIDesktopServices_darwin_cocoa.mm
    11431142
    11441143ifdef VBOX_WITH_ICHAT_THEATER
     
    13351334        src/platform/darwin/UICocoaApplication.mm \
    13361335        src/platform/darwin/UICocoaSpecialControls.mm \
     1336        src/platform/darwin/UIWindowMenuManager.cpp \
    13371337        src/platform/darwin/VBoxUtils-darwin-cocoa.mm \
    13381338        src/platform/darwin/VBoxUtils-darwin.cpp
     
    15111511        src/platform/darwin/UICocoaApplication.mm \
    15121512        src/platform/darwin/UICocoaSpecialControls.mm \
     1513        src/platform/darwin/UIWindowMenuManager.cpp \
    15131514        src/platform/darwin/VBoxUtils-darwin-cocoa.mm \
    15141515        src/platform/darwin/VBoxUtils-darwin.cpp
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UIWindowMenuManager.cpp

    r69500 r72166  
    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
     
    3232#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    3333
     34
    3435/** QObject extension
    3536  * used as Mac OS X 'Window' menu helper. */
     
    4142
    4243    /** 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: */
    48         m_pGroup = new QActionGroup(this);
    49         m_pGroup->setExclusive(true);
    50         /* Prepare 'Minimize' action: */
    51         m_pMinimizeAction = new QAction(this);
    52         m_pWindowMenu->addAction(m_pMinimizeAction);
    53         connect(m_pMinimizeAction, SIGNAL(triggered(bool)),
    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: */
    60         retranslateUi();
    61     }
    62 
     44    UIMenuHelper(const QList<QWidget*> &windows);
    6345    /** Destructs menu-helper. */
    64     ~UIMenuHelper()
    65     {
    66         /* Cleanup 'Window' menu: */
    67         delete m_pWindowMenu;
    68         /* Cleanup actions: */
    69         qDeleteAll(m_windows);
    70     }
     46    virtual ~UIMenuHelper() /* override */;
    7147
    7248    /** Returns 'Window' menu. */
    73     QMenu* menu() const { return m_pWindowMenu; }
     49    QMenu *menu() const { return m_pWindowMenu; }
    7450
    7551    /** Adds @a pWindow into 'Window' menu. */
    76     QAction* addWindow(QWidget *pWindow)
    77     {
    78         QAction *pAction = 0;
    79         if (   pWindow
    80             && !m_windows.contains(pWindow))
     52    QAction *addWindow(QWidget *pWindow);
     53    /** Removes @a pWindow from 'Window' menu. */
     54    void removeWindow(QWidget *pWindow);
     55
     56    /** Handles translation event. */
     57    void retranslateUi();
     58
     59    /** Updates toggle action states according to passed @a pActiveWindow. */
     60    void updateStatus(QWidget *pActiveWindow);
     61
     62private slots:
     63
     64    /** Handles request to minimize active-window. */
     65    void sltMinimizeActiveWindow();
     66
     67    /** Handles request to raise sender window. */
     68    void sltRaiseSender();
     69
     70private:
     71
     72    /** Holds the 'Window' menu instance. */
     73    QMenu                     *m_pWindowMenu;
     74    /** Holds the action group instance. */
     75    QActionGroup              *m_pGroup;
     76    /** Holds the 'Minimize' action instance. */
     77    QAction                   *m_pMinimizeAction;
     78    /** Holds the hash of the registered menu-helper instances. */
     79    QHash<QWidget*, QAction*>  m_windows;
     80};
     81
     82
     83/*********************************************************************************************************************************
     84*   Class UIMenuHelper implementation.                                                                                           *
     85*********************************************************************************************************************************/
     86
     87UIMenuHelper::UIMenuHelper(const QList<QWidget*> &windows)
     88{
     89    /* Prepare 'Window' menu: */
     90    m_pWindowMenu = new QMenu;
     91
     92    /* Prepare action group: */
     93    m_pGroup = new QActionGroup(this);
     94    m_pGroup->setExclusive(true);
     95
     96    /* Prepare 'Minimize' action: */
     97    m_pMinimizeAction = new QAction(this);
     98    m_pWindowMenu->addAction(m_pMinimizeAction);
     99    connect(m_pMinimizeAction, SIGNAL(triggered(bool)),
     100            this, SLOT(sltMinimizeActiveWindow()));
     101
     102    /* Make sure all already available windows are
     103     * properly registered within this menu: */
     104    for (int i = 0; i < windows.size(); ++i)
     105        addWindow(windows.at(i));
     106
     107    /* Apply language settings: */
     108    retranslateUi();
     109}
     110
     111UIMenuHelper::~UIMenuHelper()
     112{
     113    /* Cleanup 'Window' menu: */
     114    delete m_pWindowMenu;
     115
     116    /* Cleanup actions: */
     117    qDeleteAll(m_windows);
     118}
     119
     120QAction *UIMenuHelper::addWindow(QWidget *pWindow)
     121{
     122    QAction *pAction = 0;
     123    if (   pWindow
     124        && !m_windows.contains(pWindow))
     125    {
     126        if (m_windows.size() < 2)
     127            m_pWindowMenu->addSeparator();
     128
     129        /* The main window always first: */
     130        pAction = new QAction(this);
     131        pAction->setText(pWindow->windowTitle());
     132        pAction->setMenuRole(QAction::NoRole);
     133        pAction->setData(QVariant::fromValue(pWindow));
     134        pAction->setCheckable(true);
     135
     136        /* The first registered one is always
     137         * considered as the main window: */
     138        if (m_windows.size() == 0)
     139            pAction->setShortcut(QKeySequence("Ctrl+0"));
     140        m_pGroup->addAction(pAction);
     141        connect(pAction, SIGNAL(triggered(bool)),
     142                this, SLOT(sltRaiseSender()));
     143        m_pWindowMenu->addAction(pAction);
     144        m_windows[pWindow] = pAction;
     145    }
     146    return pAction;
     147}
     148
     149void UIMenuHelper::removeWindow(QWidget *pWindow)
     150{
     151    if (m_windows.contains(pWindow))
     152    {
     153        delete m_windows.value(pWindow);
     154        m_windows.remove(pWindow);
     155    }
     156}
     157
     158void UIMenuHelper::retranslateUi()
     159{
     160    /* Translate menu: */
     161    m_pWindowMenu->setTitle(QApplication::translate("UIActionPool", "&Window"));
     162
     163    /* Translate menu 'Minimize' action: */
     164    m_pMinimizeAction->setText(QApplication::translate("UIActionPool", "&Minimize"));
     165    m_pMinimizeAction->setShortcut(QKeySequence("Ctrl+M"));
     166
     167    /* Translate other menu-actions: */
     168    foreach (QAction *pAction, m_windows.values())
     169    {
     170        /* Get corresponding window from action's data: */
     171        QWidget *pWindow = pAction->data().value<QWidget*>();
     172        /* Use the window's title as the action's text: */
     173        pAction->setText(pWindow->windowTitle());
     174    }
     175}
     176
     177void UIMenuHelper::updateStatus(QWidget *pActiveWindow)
     178{
     179    /* 'Minimize' action is enabled if there is active-window: */
     180    m_pMinimizeAction->setEnabled(pActiveWindow != 0);
     181
     182    /* If there is active-window: */
     183    if (pActiveWindow)
     184    {
     185        /* Toggle corresponding action on: */
     186        if (m_windows.contains(pActiveWindow))
     187            m_windows.value(pActiveWindow)->setChecked(true);
     188    }
     189    /* If there is no active-window: */
     190    else
     191    {
     192        /* Make sure corresponding action toggled off: */
     193        if (QAction *pChecked = m_pGroup->checkedAction())
     194            pChecked->setChecked(false);
     195    }
     196}
     197
     198void UIMenuHelper::sltMinimizeActiveWindow()
     199{
     200    if (QWidget *pActiveWindow = qApp->activeWindow())
     201        pActiveWindow->showMinimized();
     202}
     203
     204void UIMenuHelper::sltRaiseSender()
     205{
     206    AssertReturnVoid(sender());
     207    if (QAction *pAction = qobject_cast<QAction*>(sender()))
     208    {
     209        if (QWidget *pWidget = pAction->data().value<QWidget*>())
    81210        {
    82             if (m_windows.size() < 2)
    83                 m_pWindowMenu->addSeparator();
    84             /* The main window always first: */
    85             pAction = new QAction(this);
    86             pAction->setText(pWindow->windowTitle());
    87             pAction->setMenuRole(QAction::NoRole);
    88             pAction->setData(QVariant::fromValue(pWindow));
    89             pAction->setCheckable(true);
    90             /* The first registered one is always
    91              * considered as the main window: */
    92             if (m_windows.size() == 0)
    93                 pAction->setShortcut(QKeySequence("Ctrl+0"));
    94             m_pGroup->addAction(pAction);
    95             connect(pAction, SIGNAL(triggered(bool)),
    96                     this, SLOT(sltRaiseSender()));
    97             m_pWindowMenu->addAction(pAction);
    98             m_windows[pWindow] = pAction;
     211            pWidget->show();
     212            pWidget->raise();
     213            pWidget->activateWindow();
    99214        }
    100         return pAction;
    101     }
    102 
    103     /** Removes @a pWindow from 'Window' menu. */
    104     void removeWindow(QWidget *pWindow)
    105     {
    106         if (m_windows.contains(pWindow))
    107         {
    108             delete m_windows.value(pWindow);
    109             m_windows.remove(pWindow);
    110         }
    111     }
    112 
    113     /** Handles translation event. */
    114     void retranslateUi()
    115     {
    116         /* Translate menu: */
    117         m_pWindowMenu->setTitle(QApplication::translate("UIActionPool", "&Window"));
    118 
    119         /* Translate menu 'Minimize' action: */
    120         m_pMinimizeAction->setText(QApplication::translate("UIActionPool", "&Minimize"));
    121         m_pMinimizeAction->setShortcut(QKeySequence("Ctrl+M"));
    122 
    123         /* Translate other menu-actions: */
    124         foreach (QAction *pAction, m_windows.values())
    125         {
    126             /* Get corresponding window from action's data: */
    127             QWidget *pWindow = pAction->data().value<QWidget*>();
    128             /* Use the window's title as the action's text: */
    129             pAction->setText(pWindow->windowTitle());
    130         }
    131     }
    132 
    133     /** Updates toggle action states according to passed @a pActiveWindow. */
    134     void updateStatus(QWidget *pActiveWindow)
    135     {
    136         /* 'Minimize' action is enabled if there is active-window: */
    137         m_pMinimizeAction->setEnabled(pActiveWindow != 0);
    138         /* If there is active-window: */
    139         if (pActiveWindow)
    140         {
    141             /* Toggle corresponding action on: */
    142             if (m_windows.contains(pActiveWindow))
    143                 m_windows.value(pActiveWindow)->setChecked(true);
    144         }
    145         /* If there is no active-window: */
    146         else
    147         {
    148             /* Make sure corresponding action toggled off: */
    149             if (QAction *pChecked = m_pGroup->checkedAction())
    150                 pChecked->setChecked(false);
    151         }
    152     }
    153 
    154 private slots:
    155 
    156     /** Handles request to minimize active-window. */
    157     void sltMinimizeActiveWindow()
    158     {
    159         if (QWidget *pActiveWindow = qApp->activeWindow())
    160             pActiveWindow->showMinimized();
    161     }
    162 
    163     /** Handles request to raise sender window. */
    164     void sltRaiseSender()
    165     {
    166         AssertReturnVoid(sender());
    167         if (QAction *pAction = qobject_cast<QAction*>(sender()))
    168         {
    169             if (QWidget *pWidget = pAction->data().value<QWidget*>())
    170             {
    171                 pWidget->show();
    172                 pWidget->raise();
    173                 pWidget->activateWindow();
    174             }
    175         }
    176     }
    177 
    178 private:
    179 
    180     /** Holds the 'Window' menu instance. */
    181     QMenu *m_pWindowMenu;
    182     /** Holds the action group instance. */
    183     QActionGroup *m_pGroup;
    184     /** Holds the 'Minimize' action instance. */
    185     QAction *m_pMinimizeAction;
    186     /** Holds the hash of the registered menu-helper instances. */
    187     QHash<QWidget*, QAction*> m_windows;
    188 };
     215    }
     216}
    189217
    190218
     
    194222
    195223/* static */
    196 UIWindowMenuManager* UIWindowMenuManager::m_spInstance = 0;
     224UIWindowMenuManager* UIWindowMenuManager::s_pInstance = 0;
    197225
    198226/* static */
     
    200228{
    201229    /* Make sure 'Window' menu Manager is not created: */
    202     AssertReturnVoid(!m_spInstance);
     230    AssertReturnVoid(!s_pInstance);
    203231
    204232    /* Create 'Window' menu Manager: */
     
    210238{
    211239    /* Make sure 'Window' menu Manager is created: */
    212     AssertPtrReturnVoid(m_spInstance);
     240    AssertPtrReturnVoid(s_pInstance);
    213241
    214242    /* Delete 'Window' menu Manager: */
    215     delete m_spInstance;
    216 }
    217 
    218 UIWindowMenuManager::UIWindowMenuManager()
    219 {
    220     /* Assign instance: */
    221     m_spInstance = this;
    222 
    223     /* Install global event-filter: */
    224     qApp->installEventFilter(this);
    225 }
    226 
    227 UIWindowMenuManager::~UIWindowMenuManager()
    228 {
    229     /* Cleanup all helpers: */
    230     qDeleteAll(m_helpers);
    231 
    232     /* Unassign instance: */
    233     m_spInstance = 0;
     243    delete s_pInstance;
    234244}
    235245
     
    282292}
    283293
     294UIWindowMenuManager::UIWindowMenuManager()
     295{
     296    /* Assign instance: */
     297    s_pInstance = this;
     298
     299    /* Install global event-filter: */
     300    qApp->installEventFilter(this);
     301}
     302
     303UIWindowMenuManager::~UIWindowMenuManager()
     304{
     305    /* Cleanup all helpers: */
     306    qDeleteAll(m_helpers);
     307
     308    /* Unassign instance: */
     309    s_pInstance = 0;
     310}
     311
    284312bool UIWindowMenuManager::eventFilter(QObject *pObject, QEvent *pEvent)
    285313{
     
    288316
    289317#ifdef VBOX_OSE /// @todo Do we still need it?
    290     /* Stupid Qt: Qt doesn't check if a window is minimized when a command is
    291      * executed. This leads to strange behaviour. The minimized window is
    292      * partly restored, but not usable. As a workaround we raise the parent
    293      * window before we let execute the command.
    294      * Note: fixed in our local Qt build since 4.7.0. */
     318    // WORKAROUND:
     319    // Stupid Qt: Qt doesn't check if a window is minimized when a command is
     320    // executed. This leads to strange behaviour. The minimized window is
     321    // partly restored, but not usable. As a workaround we raise the parent
     322    // window before we let execute the command.
     323    // Note: fixed in our local Qt build since 4.7.0.
    295324    if (pObject && type == QEvent::Show)
    296325    {
     
    335364}
    336365
     366
    337367#include "UIWindowMenuManager.moc"
    338 
  • trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UIWindowMenuManager.h

    r69500 r72166  
    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
     
    2020
    2121/* Qt includes: */
     22#include <QHash>
    2223#include <QObject>
    23 #include <QHash>
    2424
    2525/* GUI includes: */
     
    2727
    2828/* Forward declarations: */
     29class QMenu;
    2930class UIMenuHelper;
    30 class QMenu;
    3131
    3232/** Singleton QObject extension
    3333  * used as Mac OS X 'Window' menu Manager. */
    34 class UIWindowMenuManager : public QIWithRetranslateUI3<QObject>
     34class SHARED_LIBRARY_STUFF UIWindowMenuManager : public QIWithRetranslateUI3<QObject>
    3535{
    3636    Q_OBJECT;
     
    3838public:
    3939
    40     /** Static constructor. */
     40    /** Creates instance. */
    4141    static void create();
    42     /** Static destructor. */
     42    /** Destroyes instance. */
    4343    static void destroy();
    44     /** Static instance provider. */
    45     static UIWindowMenuManager* instance() { return m_spInstance; }
     44    /** Returns current instance. */
     45    static UIWindowMenuManager *instance() { return s_pInstance; }
    4646
    4747    /** Creates 'Window' menu for passed @a pWindow. */
    48     QMenu* createMenu(QWidget *pWindow);
     48    QMenu *createMenu(QWidget *pWindow);
    4949    /** Destroys 'Window' menu for passed @a pWindow. */
    5050    void destroyMenu(QWidget *pWindow);
     
    5656
    5757    /** Handles translation event. */
    58     virtual void retranslateUi();
     58    virtual void retranslateUi() /* override */;
    5959
    6060protected:
     
    6666
    6767    /** Preprocesses any Qt @a pEvent for passed @a pObject. */
    68     virtual bool eventFilter(QObject *pObject, QEvent *pEvent);
     68    virtual bool eventFilter(QObject *pObject, QEvent *pEvent) /* override */;
    6969
    7070private:
    7171
    7272    /** Holds the static instance. */
    73     static UIWindowMenuManager *m_spInstance;
     73    static UIWindowMenuManager *s_pInstance;
    7474
    7575    /** Holds the list of the registered window references. */
    76     QList<QWidget*> m_windows;
     76    QList<QWidget*>  m_windows;
    7777
    7878    /** Holds the hash of the registered menu-helper instances. */
    79     QHash<QWidget*, UIMenuHelper*> m_helpers;
     79    QHash<QWidget*, UIMenuHelper*>  m_helpers;
    8080};
    8181
     82/** Singleton 'Window' menu Manager 'official' name. */
    8283#define gpWindowMenuManager UIWindowMenuManager::instance()
    8384
    8485#endif /* !___UIWindowMenuManager_h___ */
    85 
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