1 | /* $Id: QIDialog.cpp 100073 2023-06-05 15:52:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIDialog class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QEventLoop>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "QIDialog.h"
|
---|
33 | #include "UICommon.h"
|
---|
34 | #include "UIDesktopWidgetWatchdog.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | QIDialog::QIDialog(QWidget *pParent /* = 0 */, Qt::WindowFlags enmFlags /* = Qt::WindowFlags() */)
|
---|
38 | : QDialog(pParent, enmFlags)
|
---|
39 | , m_fPolished(false)
|
---|
40 | {
|
---|
41 | /* Do not count that window as important for application,
|
---|
42 | * it will NOT be taken into account when other top-level windows will be closed: */
|
---|
43 | setAttribute(Qt::WA_QuitOnClose, false);
|
---|
44 | }
|
---|
45 |
|
---|
46 | void QIDialog::setVisible(bool fVisible)
|
---|
47 | {
|
---|
48 | /* Call to base-class: */
|
---|
49 | QDialog::setVisible(fVisible);
|
---|
50 |
|
---|
51 | /* Exit from the event-loop if there is any and
|
---|
52 | * we are changing our state from visible to hidden. */
|
---|
53 | if (m_pEventLoop && !fVisible)
|
---|
54 | m_pEventLoop->exit();
|
---|
55 | }
|
---|
56 |
|
---|
57 | int QIDialog::execute(bool fShow /* = true */, bool fApplicationModal /* = false */)
|
---|
58 | {
|
---|
59 | /* Check for the recursive run: */
|
---|
60 | AssertMsgReturn(!m_pEventLoop, ("QIDialog::execute() is called recursively!\n"), QDialog::Rejected);
|
---|
61 |
|
---|
62 | /* Reset the result-code: */
|
---|
63 | setResult(QDialog::Rejected);
|
---|
64 |
|
---|
65 | /* Should we delete ourself on close in theory? */
|
---|
66 | const bool fOldDeleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
|
---|
67 | /* For the exec() time, set this attribute to 'false': */
|
---|
68 | setAttribute(Qt::WA_DeleteOnClose, false);
|
---|
69 |
|
---|
70 | /* Which is the current window-modality? */
|
---|
71 | const Qt::WindowModality oldModality = windowModality();
|
---|
72 | /* For the exec() time, set this attribute to 'window-modal' or 'application-modal': */
|
---|
73 | setWindowModality(!fApplicationModal ? Qt::WindowModal : Qt::ApplicationModal);
|
---|
74 |
|
---|
75 | /* Show ourself if requested: */
|
---|
76 | if (fShow)
|
---|
77 | show();
|
---|
78 |
|
---|
79 | /* Create a local event-loop: */
|
---|
80 | {
|
---|
81 | QEventLoop eventLoop;
|
---|
82 | m_pEventLoop = &eventLoop;
|
---|
83 |
|
---|
84 | /* Guard ourself for the case
|
---|
85 | * we destroyed ourself in our event-loop: */
|
---|
86 | QPointer<QIDialog> guard = this;
|
---|
87 |
|
---|
88 | /* Start the blocking event-loop: */
|
---|
89 | eventLoop.exec();
|
---|
90 |
|
---|
91 | /* Are we still valid? */
|
---|
92 | if (guard.isNull())
|
---|
93 | return QDialog::Rejected;
|
---|
94 |
|
---|
95 | m_pEventLoop = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* Save the result-code early (we can delete ourself on close): */
|
---|
99 | const int iResultCode = result();
|
---|
100 |
|
---|
101 | /* Return old modality: */
|
---|
102 | setWindowModality(oldModality);
|
---|
103 |
|
---|
104 | /* Reset attribute to previous value: */
|
---|
105 | setAttribute(Qt::WA_DeleteOnClose, fOldDeleteOnClose);
|
---|
106 | /* Delete ourself if we should do that on close: */
|
---|
107 | if (fOldDeleteOnClose)
|
---|
108 | delete this;
|
---|
109 |
|
---|
110 | /* Return the result-code: */
|
---|
111 | return iResultCode;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void QIDialog::showEvent(QShowEvent *pEvent)
|
---|
115 | {
|
---|
116 | /* Make sure we should polish dialog: */
|
---|
117 | if (m_fPolished)
|
---|
118 | return;
|
---|
119 |
|
---|
120 | /* Call to polish-event: */
|
---|
121 | polishEvent(pEvent);
|
---|
122 |
|
---|
123 | /* Mark dialog as polished: */
|
---|
124 | m_fPolished = true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void QIDialog::polishEvent(QShowEvent *)
|
---|
128 | {
|
---|
129 | /* Make sure layout is polished: */
|
---|
130 | adjustSize();
|
---|
131 | #ifdef VBOX_WS_MAC
|
---|
132 | /* And dialog have fixed size: */
|
---|
133 | setFixedSize(size());
|
---|
134 | #endif /* VBOX_WS_MAC */
|
---|
135 |
|
---|
136 | /* Explicit centering according to parent if any: */
|
---|
137 | if (parentWidget())
|
---|
138 | gpDesktop->centerWidget(this, parentWidget(), false);
|
---|
139 | }
|
---|