Changeset 57891 in vbox for trunk/src/VBox/Frontends/VirtualBox
- Timestamp:
- Sep 25, 2015 12:45:30 PM (9 years ago)
- 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 20 20 #else /* !VBOX_WITH_PRECOMPILED_HEADERS */ 21 21 22 /* GUI includes: */23 # include "UIWindowMenuManager.h"24 25 22 /* Qt includes: */ 26 23 # include <QApplication> 27 24 # include <QMenu> 28 25 26 /* GUI includes: */ 27 # include "UIWindowMenuManager.h" 28 29 /* Other VBox includes: */ 30 #include <iprt/assert.h> 31 29 32 #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */ 30 33 31 34 /** QObject extension 32 35 * used as Mac OS X 'Window' menu helper. */ 33 class UIMenuHelper : public QObject36 class UIMenuHelper : public QObject 34 37 { 35 38 Q_OBJECT; … … 37 40 public: 38 41 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: */ 43 48 m_pGroup = new QActionGroup(this); 44 49 m_pGroup->setExclusive(true); 50 /* Prepare 'Minimize' action: */ 45 51 m_pMinimizeAction = new QAction(this); 46 52 m_pWindowMenu->addAction(m_pMinimizeAction); 47 53 connect(m_pMinimizeAction, SIGNAL(triggered(bool)), 48 this, SLOT( minimizeActive(bool)));49 /* Make sure all already available windows are properly registered on50 * this menu.*/51 for (int i =0; i < winList.size(); ++i)52 addWindow(win List.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: */ 54 60 retranslateUi(); 55 61 } … … 58 64 ~UIMenuHelper() 59 65 { 66 /* Cleanup 'Window' menu: */ 60 67 delete m_pWindowMenu; 61 qDeleteAll(m_regWindows); 68 /* Cleanup actions: */ 69 qDeleteAll(m_windows); 62 70 } 63 71 64 72 /** 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. */ 68 76 QAction* addWindow(QWidget *pWindow) 69 77 { 70 78 QAction *pAction = 0; 71 if ( 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) 75 83 m_pWindowMenu->addSeparator(); 76 /* The main window always first */84 /* The main window always first: */ 77 85 pAction = new QAction(this); 78 86 pAction->setText(pWindow->windowTitle()); 79 87 pAction->setMenuRole(QAction::NoRole); 80 pAction->setData( qVariantFromValue(pWindow));88 pAction->setData(QVariant::fromValue(pWindow)); 81 89 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) 84 93 pAction->setShortcut(QKeySequence("Ctrl+0")); 85 94 m_pGroup->addAction(pAction); 86 95 connect(pAction, SIGNAL(triggered(bool)), 87 this, SLOT( raiseSender(bool)));96 this, SLOT(sltRaiseSender())); 88 97 m_pWindowMenu->addAction(pAction); 89 m_ regWindows[pWindow->windowTitle()] = pAction;98 m_windows[pWindow->windowTitle()] = pAction; 90 99 } 91 100 return pAction; 92 101 } 93 102 94 /** Removes window from 'Window' menu. */103 /** Removes @a pWindow from 'Window' menu. */ 95 104 void removeWindow(QWidget *pWindow) 96 105 { 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()); 101 110 } 102 111 } … … 105 114 void retranslateUi() 106 115 { 116 /* Translate menu: */ 107 117 m_pWindowMenu->setTitle(tr("&Window")); 118 119 /* Translate menu 'Minimize' action: */ 108 120 m_pMinimizeAction->setText(tr("Minimize")); 109 121 m_pMinimizeAction->setShortcut(QKeySequence("Ctrl+M")); 110 122 } 111 123 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: */ 121 137 else 122 138 { 139 /* Make sure corresponding action toggled off: */ 123 140 if (QAction *pChecked = m_pGroup->checkedAction()) 124 141 pChecked->setChecked(false); 125 142 } 126 127 143 } 128 144 … … 130 146 131 147 /** 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(); 136 152 } 137 153 138 154 /** 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*>()) 144 161 { 145 162 pWidget->show(); … … 159 176 QAction *m_pMinimizeAction; 160 177 /** Holds the hash of the registered menu-helper instances. */ 161 QHash<QString, QAction*> m_ regWindows;178 QHash<QString, QAction*> m_windows; 162 179 }; 163 180 181 /********************************************************************************************************************************* 182 * Class UIWindowMenuManager implementation. * 183 *********************************************************************************************************************************/ 184 164 185 /* static */ 165 UIWindowMenuManager *UIWindowMenuManager::m_pInstance = 0;186 UIWindowMenuManager* UIWindowMenuManager::m_spInstance = 0; 166 187 167 188 /* static */ 168 UIWindowMenuManager *UIWindowMenuManager::instance(QWidget *pParent /* = 0 */) 169 { 170 if (!m_pInstance) 171 m_pInstance = new UIWindowMenuManager(pParent); 172 173 return m_pInstance; 189 UIWindowMenuManager* 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; 174 197 } 175 198 … … 177 200 void UIWindowMenuManager::destroy() 178 201 { 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; 184 207 } 185 208 186 209 QMenu *UIWindowMenuManager::createMenu(QWidget *pWindow) 187 210 { 188 UIMenuHelper *pHelper = new UIMenuHelper(m_regWindows); 189 211 /* Create helper: */ 212 UIMenuHelper *pHelper = new UIMenuHelper(m_windows); 213 /* Register it: */ 190 214 m_helpers[pWindow] = pHelper; 191 215 216 /* Return menu of created helper: */ 192 217 return pHelper->menu(); 193 218 } … … 195 220 void UIWindowMenuManager::destroyMenu(QWidget *pWindow) 196 221 { 222 /* If window is registered: */ 197 223 if (m_helpers.contains(pWindow)) 198 224 { 199 delete m_helpers[pWindow]; 225 /* Delete helper: */ 226 delete m_helpers.value(pWindow); 227 /* Unregister it: */ 200 228 m_helpers.remove(pWindow); 201 229 } … … 204 232 void UIWindowMenuManager::addWindow(QWidget *pWindow) 205 233 { 206 m_regWindows.append(pWindow); 234 /* Register window: */ 235 m_windows.append(pWindow); 236 /* Add window to all menus we have: */ 207 237 QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin(); 208 238 while (i != m_helpers.constEnd()) … … 215 245 void UIWindowMenuManager::removeWindow(QWidget *pWindow) 216 246 { 247 /* Remove window from all menus we have: */ 217 248 QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin(); 218 249 while (i != m_helpers.constEnd()) … … 221 252 ++i; 222 253 } 223 m_regWindows.removeAll(pWindow); 254 /* Unregister window: */ 255 m_windows.removeAll(pWindow); 224 256 } 225 257 226 258 void UIWindowMenuManager::retranslateUi() 227 259 { 260 /* Translate all the helpers: */ 228 261 QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin(); 229 262 while (i != m_helpers.constEnd()) … … 234 267 } 235 268 236 bool UIWindowMenuManager::eventFilter(QObject *pObj, QEvent *pEvent) 237 { 238 QEvent::Type type = pEvent->type(); 269 bool UIWindowMenuManager::eventFilter(QObject *pObject, QEvent *pEvent) 270 { 271 /* Acquire event type: */ 272 const QEvent::Type type = pEvent->type(); 273 239 274 #if defined(VBOX_OSE) || (QT_VERSION < 0x040700) 240 275 /* Stupid Qt: Qt doesn't check if a window is minimized when a command is … … 243 278 * window before we let execute the command. 244 279 * 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() 249 285 && pWidget->parentWidget()->isMinimized()) 250 286 { … … 254 290 } 255 291 } 256 #endif /* defined(VBOX_OSE) || (QT_VERSION < 0x040700) */ 292 #endif /* VBOX_OSE || QT_VERSION < 0x040700 */ 293 257 294 /* We need to track several events which leads to different window 258 295 * activation and change the menu items in that case. */ … … 265 302 || type == QEvent::Hide) 266 303 { 267 QWidget *pActive = qApp->activeWindow();304 QWidget *pActiveWindow = qApp->activeWindow(); 268 305 QHash<QWidget*, UIMenuHelper*>::const_iterator i = m_helpers.constBegin(); 269 306 while (i != m_helpers.constEnd()) 270 307 { 271 i.value()->updateStatus(pActive );308 i.value()->updateStatus(pActiveWindow); 272 309 ++i; 273 310 } 274 311 } 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 317 UIWindowMenuManager::UIWindowMenuManager() 318 { 319 /* Assign instance: */ 320 m_spInstance = this; 321 322 /* Install global event-filter: */ 287 323 qApp->installEventFilter(this); 288 324 } … … 290 326 UIWindowMenuManager::~UIWindowMenuManager() 291 327 { 328 /* Cleanup all helpers: */ 292 329 qDeleteAll(m_helpers); 330 331 /* Unassign instance: */ 332 m_spInstance = 0; 293 333 } 294 334 -
trunk/src/VBox/Frontends/VirtualBox/src/platform/darwin/UIWindowMenuManager.h
r57890 r57891 23 23 #include <QHash> 24 24 25 /* GUI includes: */ 26 #include "QIWithRetranslateUI.h" 27 25 28 /* Forward declarations: */ 26 29 class UIMenuHelper; … … 29 32 /** Singleton QObject extension 30 33 * used as Mac OS X 'Window' menu Manager. */ 31 class UIWindowMenuManager : public QObject34 class UIWindowMenuManager : public QIWithRetranslateUI3<QObject> 32 35 { 33 36 Q_OBJECT; … … 36 39 37 40 /** Static constructor and instance provider. */ 38 static UIWindowMenuManager *instance( QWidget *pParent = 0);41 static UIWindowMenuManager *instance(); 39 42 /** Static destructor. */ 40 43 static void destroy(); 41 44 42 45 /** Creates 'Window' menu for passed @a pWindow. */ 43 QMenu *createMenu(QWidget *pWindow);46 QMenu* createMenu(QWidget *pWindow); 44 47 /** Destroys 'Window' menu for passed @a pWindow. */ 45 48 void destroyMenu(QWidget *pWindow); … … 51 54 52 55 /** Handles translation event. */ 53 v oid retranslateUi();56 virtual void retranslateUi(); 54 57 55 58 protected: 56 59 57 60 /** Preprocesses any Qt @a pEvent for passed @a pObject. */ 58 bool eventFilter(QObject *pObj , QEvent *pEvent);61 bool eventFilter(QObject *pObject, QEvent *pEvent); 59 62 60 63 private: 61 64 62 65 /** Constructs 'Window' menu Manager. */ 63 UIWindowMenuManager( QWidget *pParent = 0);66 UIWindowMenuManager(); 64 67 /** Destructs 'Window' menu Manager. */ 65 68 ~UIWindowMenuManager(); 66 69 67 70 /** 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; 72 72 73 73 /** Holds the list of the registered window references. */ 74 QList<QWidget*> m_ regWindows;74 QList<QWidget*> m_windows; 75 75 76 76 /** Holds the hash of the registered menu-helper instances. */ -
trunk/src/VBox/Frontends/VirtualBox/src/selector/UISelectorWindow.cpp
r57877 r57891 1212 1212 1213 1213 #ifdef Q_WS_MAC 1214 menuBar()->addMenu(UIWindowMenuManager::instance( this)->createMenu(this));1214 menuBar()->addMenu(UIWindowMenuManager::instance()->createMenu(this)); 1215 1215 #endif /* Q_WS_MAC */ 1216 1216
Note:
See TracChangeset
for help on using the changeset viewer.