Changeset 54538 in vbox for trunk/src/VBox/Frontends/VirtualBox
- Timestamp:
- Feb 26, 2015 7:11:24 PM (10 years ago)
- svn:sync-xref-src-repo-rev:
- 98656
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/extensions
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIDialog.cpp
r52730 r54538 45 45 QDialog::setVisible(fVisible); 46 46 47 /* Exit from the event-loop if 48 * 1. there is any and 49 * 2. we are changing our state from visible to invisible: */ 47 /* Exit from the event-loop if there is any and 48 * we are changing our state from visible to hidden. */ 50 49 if (m_pEventLoop && !fVisible) 51 50 m_pEventLoop->exit(); 52 51 } 53 52 54 int QIDialog::exec(bool fShow /* = true */, bool fApplicationModal /* = false */)53 int QIDialog::exec(bool fShow /* = true */, bool fApplicationModal /* = false */) 55 54 { 55 /* Check for the recursive run: */ 56 AssertMsgReturn(!m_pEventLoop, ("QIDialog::exec() is called recursively!\n"), QDialog::Rejected); 57 56 58 /* Reset the result-code: */ 57 59 setResult(QDialog::Rejected); 58 60 59 61 /* Should we delete ourself on close in theory? */ 60 bool fOldDeleteOnClose = testAttribute(Qt::WA_DeleteOnClose);62 const bool fOldDeleteOnClose = testAttribute(Qt::WA_DeleteOnClose); 61 63 /* For the exec() time, set this attribute to 'false': */ 62 64 setAttribute(Qt::WA_DeleteOnClose, false); 63 65 64 66 /* Which is the current window-modality? */ 65 Qt::WindowModality oldModality = windowModality();67 const Qt::WindowModality oldModality = windowModality(); 66 68 /* For the exec() time, set this attribute to 'window-modal' or 'application-modal': */ 67 69 setWindowModality(!fApplicationModal ? Qt::WindowModal : Qt::ApplicationModal); … … 91 93 92 94 /* Save the result-code early (we can delete ourself on close): */ 93 int iResultCode = result();95 const int iResultCode = result(); 94 96 95 97 /* Return old modality: */ -
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIMainDialog.cpp
r52733 r54538 1 1 /* $Id$ */ 2 2 /** @file 3 * VBox Qt GUI - VirtualBoxQt extensions: QIMainDialog class implementation.3 * VBox Qt GUI - Qt extensions: QIMainDialog class implementation. 4 4 */ 5 5 6 6 /* 7 * Copyright (C) 2008-201 2Oracle Corporation7 * Copyright (C) 2008-2015 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 20 20 #else /* !VBOX_WITH_PRECOMPILED_HEADERS */ 21 21 22 # include "QIMainDialog.h" 23 # include "VBoxUtils.h" 24 # include "VBoxGlobal.h" 25 26 # include <iprt/assert.h> 27 28 /* Qt includes */ 29 # include <QProcess> 30 # include <QEventLoop> 31 # include <QApplication> 22 /* Qt includes: */ 32 23 # include <QDir> 33 24 # include <QUrl> 34 25 # include <QMenu> 26 # include <QProcess> 27 # include <QSizeGrip> 28 # include <QEventLoop> 35 29 # include <QPushButton> 30 # include <QApplication> 36 31 # include <QDialogButtonBox> 37 32 33 /* GUI includes: */ 34 # include "QIMainDialog.h" 35 # include "VBoxGlobal.h" 36 # include "VBoxUtils.h" 37 38 /* Other VBox includes: */ 39 # include <iprt/assert.h> 40 38 41 #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */ 39 42 40 #include <QSizeGrip> 41 42 43 QIMainDialog::QIMainDialog (QWidget *aParent /* = 0 */, 44 Qt::WindowFlags aFlags /* = Qt::Dialog */) 45 : QMainWindow (aParent, aFlags) 46 , mRescode (QDialog::Rejected) 47 , mPolished (false) 48 , mIsAutoCentering (true) 49 , mCenterWidget (aParent) 50 { 51 qApp->installEventFilter (this); 52 } 53 54 QDialog::DialogCode QIMainDialog::exec() 43 QIMainDialog::QIMainDialog(QWidget *pParent /* = 0 */, 44 Qt::WindowFlags enmFlags /* = Qt::Dialog */, 45 bool fIsAutoCentering /* = true */) 46 : QMainWindow(pParent, enmFlags) 47 , m_fIsAutoCentering(fIsAutoCentering) 48 , m_fPolished(false) 49 , m_enmResult(QDialog::Rejected) 50 { 51 /* Install event-filter: */ 52 qApp->installEventFilter(this); 53 } 54 55 QDialog::DialogCode QIMainDialog::exec(bool fApplicationModal /* = true */) 55 56 { 56 57 /* Check for the recursive run: */ 57 AssertMsg (!mEventLoop, ("QIMainDialog::exec() is called recursively!\n"));58 AssertMsgReturn(!m_pEventLoop, ("QIMainDialog::exec() is called recursively!\n"), QDialog::Rejected); 58 59 59 60 /* Reset the result code: */ 60 61 setResult(QDialog::Rejected); 61 62 62 /* Tune some attributes:*/63 bool fDeleteOnClose = testAttribute(Qt::WA_DeleteOnClose); NOREF(fDeleteOnClose);64 AssertMsg(!fDeleteOnClose, ("QIMainDialog is NOT supposed to be run in 'delete-on-close' mode!"));63 /* Should we delete ourself on close in theory? */ 64 const bool fOldDeleteOnClose = testAttribute(Qt::WA_DeleteOnClose); 65 /* For the exec() time, set this attribute to 'false': */ 65 66 setAttribute(Qt::WA_DeleteOnClose, false); 66 bool fWasShowModal = testAttribute(Qt::WA_ShowModal); 67 setAttribute(Qt::WA_ShowModal, true); 67 68 /* Which is the current window-modality? */ 69 const Qt::WindowModality oldModality = windowModality(); 70 /* For the exec() time, set this attribute to 'window-modal' or 'application-modal': */ 71 setWindowModality(!fApplicationModal ? Qt::WindowModal : Qt::ApplicationModal); 72 73 /* Show ourself: */ 74 show(); 68 75 69 76 /* Create a local event-loop: */ 70 QEventLoop eventLoop; 71 mEventLoop = &eventLoop; 72 /* Show the window: */ 73 show(); 74 /* A guard to ourself for the case we destroy ourself: */ 75 QPointer<QIMainDialog> guard = this; 76 /* Start the event-loop: */ 77 eventLoop.exec(); 78 /* Check if dialog is still valid: */ 79 if (guard.isNull()) 80 return QDialog::Rejected; 81 mEventLoop = 0; 82 /* Prepare result: */ 83 QDialog::DialogCode res = result(); 84 /* Restore old show-modal attribute: */ 85 setAttribute(Qt::WA_ShowModal, fWasShowModal); 86 /* Return the final result: */ 87 return res; 88 } 89 90 QDialog::DialogCode QIMainDialog::result() const 91 { 92 return mRescode; 93 } 94 95 void QIMainDialog::setSizeGripEnabled (bool aEnabled) 96 { 97 if (!mSizeGrip && aEnabled) 98 { 99 mSizeGrip = new QSizeGrip (this); 100 mSizeGrip->resize (mSizeGrip->sizeHint()); 101 mSizeGrip->show(); 102 } 103 else if (mSizeGrip && !aEnabled) 104 delete mSizeGrip; 77 { 78 QEventLoop eventLoop; 79 m_pEventLoop = &eventLoop; 80 81 /* Guard ourself for the case 82 * we destroyed ourself in our event-loop: */ 83 QPointer<QIMainDialog> guard = this; 84 85 /* Start the blocking event-loop: */ 86 eventLoop.exec(); 87 88 /* Are we still valid? */ 89 if (guard.isNull()) 90 return QDialog::Rejected; 91 92 m_pEventLoop = 0; 93 } 94 95 /* Save the result code early (we can delete ourself on close): */ 96 const QDialog::DialogCode enmResultCode = result(); 97 98 /* Return old modality: */ 99 setWindowModality(oldModality); 100 101 /* Reset attribute to previous value: */ 102 setAttribute(Qt::WA_DeleteOnClose, fOldDeleteOnClose); 103 /* Delete ourself if we should do that on close: */ 104 if (fOldDeleteOnClose) 105 delete this; 106 107 /* Return the result code: */ 108 return enmResultCode; 109 } 110 111 QPushButton* QIMainDialog::defaultButton() const 112 { 113 return m_pDefaultButton; 114 } 115 116 void QIMainDialog::setDefaultButton(QPushButton *pButton) 117 { 118 m_pDefaultButton = pButton; 105 119 } 106 120 107 121 bool QIMainDialog::isSizeGripEnabled() const 108 122 { 109 return mSizeGrip; 110 } 111 112 void QIMainDialog::setDefaultButton (QPushButton* aButton) 113 { 114 mDefaultButton = aButton; 115 } 116 117 QPushButton* QIMainDialog::defaultButton() const 118 { 119 return mDefaultButton; 120 } 121 122 void QIMainDialog::setAutoCenteringEnabled(bool fIsAutoCentering) 123 { 124 mIsAutoCentering = fIsAutoCentering; 125 } 126 127 void QIMainDialog::setVisible (bool aVisible) 128 { 129 QMainWindow::setVisible (aVisible); 130 /* Exit from the event loop if there is any and we are changing our state 131 * from visible to invisible. */ 132 if (mEventLoop && !aVisible) 133 mEventLoop->exit(); 134 } 135 136 bool QIMainDialog::event (QEvent *aEvent) 137 { 138 switch (aEvent->type()) 139 { 140 case QEvent::Polish: 141 { 142 /* Initially search for the default button. */ 143 mDefaultButton = searchDefaultButton(); 144 break; 145 } 146 default: 147 break; 148 } 149 return QMainWindow::event (aEvent); 150 } 151 152 void QIMainDialog::showEvent (QShowEvent *aEvent) 153 { 154 QMainWindow::showEvent (aEvent); 155 156 /* Polishing border */ 157 if (mPolished) 123 return m_pSizeGrip; 124 } 125 126 void QIMainDialog::setSizeGripEnabled(bool fEnabled) 127 { 128 /* Create if missed: */ 129 if (!m_pSizeGrip && fEnabled) 130 { 131 m_pSizeGrip = new QSizeGrip(this); 132 m_pSizeGrip->resize(m_pSizeGrip->sizeHint()); 133 m_pSizeGrip->show(); 134 } 135 /* Destroy if present: */ 136 else if (m_pSizeGrip && !fEnabled) 137 { 138 delete m_pSizeGrip; 139 m_pSizeGrip = 0; 140 } 141 } 142 143 void QIMainDialog::setVisible(bool fVisible) 144 { 145 /* Call to base-class: */ 146 QMainWindow::setVisible(fVisible); 147 148 /* Exit from the event-loop if there is any and 149 * we are changing our state from visible to hidden. */ 150 if (m_pEventLoop && !fVisible) 151 m_pEventLoop->exit(); 152 } 153 154 bool QIMainDialog::event(QEvent *pEvent) 155 { 156 /* Depending on event-type: */ 157 switch (pEvent->type()) 158 { 159 case QEvent::Polish: 160 { 161 /* Initially search for the default-button: */ 162 m_pDefaultButton = searchDefaultButton(); 163 break; 164 } 165 default: 166 break; 167 } 168 169 /* Call to base-class: */ 170 return QMainWindow::event(pEvent); 171 } 172 173 void QIMainDialog::showEvent(QShowEvent *pEvent) 174 { 175 /* Make sure we should polish dialog: */ 176 if (m_fPolished) 158 177 return; 159 mPolished = true; 160 161 /* Explicit widget centering relatively to it's centering 162 * widget if any or desktop if centering widget is missed. */ 163 if (mIsAutoCentering) 164 VBoxGlobal::centerWidget (this, mCenterWidget, false); 165 } 166 167 void QIMainDialog::resizeEvent (QResizeEvent *aEvent) 168 { 169 QMainWindow::resizeEvent (aEvent); 170 171 /* Adjust the size-grip location for the current resize event */ 172 if (mSizeGrip) 178 179 /* Call to polish-event: */ 180 polishEvent(pEvent); 181 182 /* Mark dialog as polished: */ 183 m_fPolished = true; 184 } 185 186 void QIMainDialog::polishEvent(QShowEvent*) 187 { 188 /* Explicit centering according to our parent: */ 189 if (m_fIsAutoCentering) 190 VBoxGlobal::centerWidget(this, parentWidget(), false); 191 } 192 193 void QIMainDialog::resizeEvent(QResizeEvent *pEvent) 194 { 195 /* Call to base-class: */ 196 QMainWindow::resizeEvent(pEvent); 197 198 /* Adjust the size-grip location for the current resize event: */ 199 if (m_pSizeGrip) 173 200 { 174 201 if (isRightToLeft()) 175 m SizeGrip->move (rect().bottomLeft() - mSizeGrip->rect().bottomLeft());202 m_pSizeGrip->move(rect().bottomLeft() - m_pSizeGrip->rect().bottomLeft()); 176 203 else 177 mSizeGrip->move (rect().bottomRight() - mSizeGrip->rect().bottomRight()); 178 aEvent->accept(); 204 m_pSizeGrip->move(rect().bottomRight() - m_pSizeGrip->rect().bottomRight()); 179 205 } 180 206 } … … 234 260 default: break; 235 261 } 262 236 263 /* Call to base-class: */ 237 264 return QMainWindow::keyPressEvent(pEvent); 238 265 } 239 266 240 bool QIMainDialog::eventFilter (QObject *aObject, QEvent *aEvent) 241 { 267 bool QIMainDialog::eventFilter(QObject *pObject, QEvent *pEvent) 268 { 269 /* Skip for inactive window: */ 242 270 if (!isActiveWindow()) 243 return QMainWindow::eventFilter (aObject, aEvent); 244 245 if (qobject_cast<QWidget*> (aObject) && 246 qobject_cast<QWidget*> (aObject)->window() != this) 247 return QMainWindow::eventFilter (aObject, aEvent); 248 249 switch (aEvent->type()) 250 { 251 /* Auto-default button focus-in processor used to move the "default" 271 return QMainWindow::eventFilter(pObject, pEvent); 272 273 /* Skip for children of other than this one window: */ 274 if (qobject_cast<QWidget*>(pObject) && 275 qobject_cast<QWidget*>(pObject)->window() != this) 276 return QMainWindow::eventFilter(pObject, pEvent); 277 278 /* Depending on event-type: */ 279 switch (pEvent->type()) 280 { 281 /* Auto-default-button focus-in processor used to move the "default" 252 282 * button property into the currently focused button. */ 253 283 case QEvent::FocusIn: 254 284 { 255 if (qobject_cast<QPushButton*> (aObject) &&256 ( aObject->parent() == centralWidget() ||257 qobject_cast<QDialogButtonBox*> (aObject->parent())))258 { 259 qobject_cast<QPushButton*> (aObject)->setDefault (aObject != mDefaultButton);260 if (m DefaultButton)261 m DefaultButton->setDefault (aObject == mDefaultButton);262 } 263 break; 264 } 265 /* Auto-default 285 if (qobject_cast<QPushButton*>(pObject) && 286 (pObject->parent() == centralWidget() || 287 qobject_cast<QDialogButtonBox*>(pObject->parent()))) 288 { 289 qobject_cast<QPushButton*>(pObject)->setDefault(pObject != m_pDefaultButton); 290 if (m_pDefaultButton) 291 m_pDefaultButton->setDefault(pObject == m_pDefaultButton); 292 } 293 break; 294 } 295 /* Auto-default-button focus-out processor used to remove the "default" 266 296 * button property from the previously focused button. */ 267 297 case QEvent::FocusOut: 268 298 { 269 if (qobject_cast<QPushButton*> (aObject) &&270 ( aObject->parent() == centralWidget() ||271 qobject_cast<QDialogButtonBox*> (aObject->parent())))272 { 273 if (m DefaultButton)274 m DefaultButton->setDefault (aObject != mDefaultButton);275 qobject_cast<QPushButton*> (aObject)->setDefault (aObject == mDefaultButton);299 if (qobject_cast<QPushButton*>(pObject) && 300 (pObject->parent() == centralWidget() || 301 qobject_cast<QDialogButtonBox*>(pObject->parent()))) 302 { 303 if (m_pDefaultButton) 304 m_pDefaultButton->setDefault(pObject != m_pDefaultButton); 305 qobject_cast<QPushButton*>(pObject)->setDefault(pObject == m_pDefaultButton); 276 306 } 277 307 break; … … 280 310 break; 281 311 } 282 return QMainWindow::eventFilter (aObject, aEvent); 312 313 /* Call to base-class: */ 314 return QMainWindow::eventFilter(pObject, pEvent); 283 315 } 284 316 285 317 QPushButton* QIMainDialog::searchDefaultButton() const 286 318 { 287 /* Search for the first default button in the dialog. */ 288 QList<QPushButton*> list = qFindChildren<QPushButton*> (this); 289 foreach (QPushButton *button, list) 290 if (button->isDefault() && 291 (button->parent() == centralWidget() || 292 qobject_cast<QDialogButtonBox*> (button->parent()))) 293 return button; 294 return NULL; 295 } 296 297 298 void QIMainDialog::accept() 299 { 300 done (QDialog::Accepted); 301 } 302 303 void QIMainDialog::reject() 304 { 305 done (QDialog::Rejected); 306 } 307 308 void QIMainDialog::done (QDialog::DialogCode aResult) 309 { 310 /* Set the final result */ 311 setResult (aResult); 312 /* Hide this window */ 319 /* Search for the first default-button in the dialog: */ 320 QList<QPushButton*> list = qFindChildren<QPushButton*>(this); 321 foreach (QPushButton *pButton, list) 322 if (pButton->isDefault() && 323 (pButton->parent() == centralWidget() || 324 qobject_cast<QDialogButtonBox*>(pButton->parent()))) 325 return pButton; 326 return 0; 327 } 328 329 void QIMainDialog::done(QDialog::DialogCode enmResult) 330 { 331 /* Set the final result: */ 332 setResult(enmResult); 333 /* Hide: */ 313 334 hide(); 314 /* And close the window */ 315 close(); 316 } 317 318 void QIMainDialog::setResult (QDialog::DialogCode aRescode) 319 { 320 mRescode = aRescode; 321 } 322 335 } 336 -
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIMainDialog.h
r52727 r54538 4 4 5 5 /* 6 * Copyright (C) 2008-201 0Oracle Corporation6 * Copyright (C) 2008-2015 Oracle Corporation 7 7 * 8 8 * This file is part of VirtualBox Open Source Edition (OSE), as … … 15 15 */ 16 16 17 #ifndef __ QIMainDialog_h__18 #define __ QIMainDialog_h__17 #ifndef ___QIMainDialog_h___ 18 #define ___QIMainDialog_h___ 19 19 20 /* Qt includes */20 /* Qt includes: */ 21 21 #include <QMainWindow> 22 #include <QPointer> 22 23 #include <QDialog> 23 #include <QPointer>24 24 25 /* Forward declarations: */ 26 class QPushButton; 25 27 class QEventLoop; 26 28 class QSizeGrip; 27 29 30 /** QDialog analog based on QMainWindow. */ 28 31 class QIMainDialog: public QMainWindow 29 32 { … … 32 35 public: 33 36 34 QIMainDialog (QWidget *aParent = 0, Qt::WindowFlags aFlags = Qt::Dialog); 37 /** Constructor. 38 * @param pParent holds the parent widget passed to the base-class, 39 * @param enmFlags holds the cumulative window flags passed to the base-class, 40 * @param fIsAutoCentering defines whether this dialog should be centered according it's parent. */ 41 QIMainDialog(QWidget *pParent = 0, 42 Qt::WindowFlags enmFlags = Qt::Dialog, 43 bool fIsAutoCentering = true); 35 44 36 QDialog::DialogCode exec();37 QDialog::DialogCode result() const ;45 /** Returns the dialog's result code. */ 46 QDialog::DialogCode result() const { return m_enmResult; } 38 47 39 void setSizeGripEnabled (bool aEnabled); 40 bool isSizeGripEnabled () const; 48 /** Executes the dialog, launching local event-loop. 49 * @param fApplicationModal defines whether this dialog should be modal to application or window. */ 50 QDialog::DialogCode exec(bool fApplicationModal = true); 41 51 42 void setDefaultButton (QPushButton *aButton); 43 QPushButton* defaultButton () const; 52 /** Returns dialog's default-button. */ 53 QPushButton* defaultButton() const; 54 /** Defines dialog's default-button. */ 55 void setDefaultButton(QPushButton *pButton); 44 56 45 void setAutoCenteringEnabled(bool fIsAutoCentering); 57 /** Returns whether size-grip was enabled for that dialog. */ 58 bool isSizeGripEnabled() const; 59 /** Defines whether size-grip should be @a fEnabled for that dialog. */ 60 void setSizeGripEnabled(bool fEnabled); 46 61 47 62 public slots: 48 63 49 virtual void setVisible (bool aVisible); 64 /** Defines whether the dialog is @a fVisible. */ 65 virtual void setVisible(bool fVisible); 50 66 51 67 protected: 52 68 53 virtual bool event (QEvent *aEvent); 54 virtual void showEvent (QShowEvent *aEvent); 55 virtual void resizeEvent (QResizeEvent *aEvent); 69 /** General event handler. */ 70 virtual bool event(QEvent *pEvent); 71 /** Show event handler. */ 72 virtual void showEvent(QShowEvent *pEvent); 73 /** Our own polish event handler. */ 74 virtual void polishEvent(QShowEvent *pEvent); 75 /** Resize event handler. */ 76 virtual void resizeEvent(QResizeEvent *pEvent); 77 /** Key-press event handler. */ 56 78 virtual void keyPressEvent(QKeyEvent *pEvent); 57 virtual bool eventFilter (QObject *aObject, QEvent *aEvent); 79 /** General event filter. */ 80 virtual bool eventFilter(QObject *aObject, QEvent *pEvent); 58 81 82 /** Function to search for dialog's default-button. */ 59 83 QPushButton* searchDefaultButton() const; 60 61 void centerAccording (QWidget *aWidget) { mCenterWidget = aWidget; }62 84 63 85 protected slots: 64 86 65 virtual void accept();66 v irtual void reject();87 /** Sets the modal dialog's result code to @a enmResult. */ 88 void setResult(QDialog::DialogCode enmResult) { m_enmResult = enmResult; } 67 89 68 void done (QDialog::DialogCode aRescode); 69 void setResult (QDialog::DialogCode aRescode); 90 /** Closes the modal dialog and sets its result code to @a enmResult. 91 * If this dialog is shown with exec(), done() causes the local 92 * event-loop to finish, and exec() to return @a enmResult. */ 93 virtual void done(QDialog::DialogCode enmResult); 94 /** Hides the modal dialog and sets the result code to Accepted. */ 95 virtual void accept() { done(QDialog::Accepted); } 96 /** Hides the modal dialog and sets the result code to Rejected. */ 97 virtual void reject() { done(QDialog::Rejected); } 70 98 71 99 private: 72 100 73 /* Private member vars */ 74 QDialog::DialogCode mRescode; 75 QPointer<QEventLoop> mEventLoop; 101 /** Holds whether this dialog should be centered according it's parent. */ 102 const bool m_fIsAutoCentering; 103 /** Holds whether this dialog is polished. */ 104 bool m_fPolished; 76 105 77 QPointer<QSizeGrip> mSizeGrip; 78 QPointer<QPushButton> mDefaultButton; 106 /** Holds modal dialog's result code. */ 107 QDialog::DialogCode m_enmResult; 108 /** Holds modal dialog's event-loop. */ 109 QPointer<QEventLoop> m_pEventLoop; 79 110 80 bool mPolished; 81 bool mIsAutoCentering; 82 QWidget *mCenterWidget; 111 /** Holds dialog's default-button. */ 112 QPointer<QPushButton> m_pDefaultButton; 113 /** Holds dialog's size-grip. */ 114 QPointer<QSizeGrip> m_pSizeGrip; 83 115 }; 84 116 85 #endif /* __QIMainDialog_h__ */ 86 117 #endif /* !___QIMainDialog_h___ */
Note:
See TracChangeset
for help on using the changeset viewer.