1 | /* $Id: QIDialog.cpp 103710 2024-03-06 16:53:27Z 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 "UIDesktopWidgetWatchdog.h"
|
---|
34 |
|
---|
35 | /* Other VBox includes: */
|
---|
36 | #include "iprt/assert.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | QIDialog::QIDialog(QWidget *pParent /* = 0 */, Qt::WindowFlags enmFlags /* = Qt::WindowFlags() */)
|
---|
40 | : QDialog(pParent, enmFlags)
|
---|
41 | , m_fPolished(false)
|
---|
42 | {
|
---|
43 | /* Do not count that window as important for application,
|
---|
44 | * it will NOT be taken into account when other top-level windows will be closed: */
|
---|
45 | setAttribute(Qt::WA_QuitOnClose, false);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void QIDialog::setVisible(bool fVisible)
|
---|
49 | {
|
---|
50 | /* Call to base-class: */
|
---|
51 | QDialog::setVisible(fVisible);
|
---|
52 |
|
---|
53 | /* Exit from the event-loop if there is any and
|
---|
54 | * we are changing our state from visible to hidden. */
|
---|
55 | if (m_pEventLoop && !fVisible)
|
---|
56 | m_pEventLoop->exit();
|
---|
57 | }
|
---|
58 |
|
---|
59 | int QIDialog::execute(bool fShow /* = true */, bool fApplicationModal /* = false */)
|
---|
60 | {
|
---|
61 | /* Check for the recursive run: */
|
---|
62 | AssertMsgReturn(!m_pEventLoop, ("QIDialog::execute() is called recursively!\n"), QDialog::Rejected);
|
---|
63 |
|
---|
64 | /* Reset the result-code: */
|
---|
65 | setResult(QDialog::Rejected);
|
---|
66 |
|
---|
67 | /* Should we delete ourself on close in theory? */
|
---|
68 | const bool fOldDeleteOnClose = testAttribute(Qt::WA_DeleteOnClose);
|
---|
69 | /* For the exec() time, set this attribute to 'false': */
|
---|
70 | setAttribute(Qt::WA_DeleteOnClose, false);
|
---|
71 |
|
---|
72 | /* Which is the current window-modality? */
|
---|
73 | const Qt::WindowModality oldModality = windowModality();
|
---|
74 | /* For the exec() time, set this attribute to 'window-modal' or 'application-modal': */
|
---|
75 | setWindowModality(!fApplicationModal ? Qt::WindowModal : Qt::ApplicationModal);
|
---|
76 |
|
---|
77 | /* Show ourself if requested: */
|
---|
78 | if (fShow)
|
---|
79 | show();
|
---|
80 |
|
---|
81 | /* Create a local event-loop: */
|
---|
82 | {
|
---|
83 | QEventLoop eventLoop;
|
---|
84 | m_pEventLoop = &eventLoop;
|
---|
85 |
|
---|
86 | /* Guard ourself for the case
|
---|
87 | * we destroyed ourself in our event-loop: */
|
---|
88 | QPointer<QIDialog> guard = this;
|
---|
89 |
|
---|
90 | /* Start the blocking event-loop: */
|
---|
91 | eventLoop.exec();
|
---|
92 |
|
---|
93 | /* Are we still valid? */
|
---|
94 | if (guard.isNull())
|
---|
95 | return QDialog::Rejected;
|
---|
96 |
|
---|
97 | m_pEventLoop = 0;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* Save the result-code early (we can delete ourself on close): */
|
---|
101 | const int iResultCode = result();
|
---|
102 |
|
---|
103 | /* Return old modality: */
|
---|
104 | setWindowModality(oldModality);
|
---|
105 |
|
---|
106 | /* Reset attribute to previous value: */
|
---|
107 | setAttribute(Qt::WA_DeleteOnClose, fOldDeleteOnClose);
|
---|
108 | /* Delete ourself if we should do that on close: */
|
---|
109 | if (fOldDeleteOnClose)
|
---|
110 | delete this;
|
---|
111 |
|
---|
112 | /* Return the result-code: */
|
---|
113 | return iResultCode;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void QIDialog::done(int iResult)
|
---|
117 | {
|
---|
118 | /* Call to base-class: */
|
---|
119 | QDialog::done(iResult);
|
---|
120 |
|
---|
121 | /* Make sure event-loop exited even if no dialog visibility changed, s.a. QIDialog::setVisible above.
|
---|
122 | * That is necessary to exit event-loop if dialog was executed with fShow == false. */
|
---|
123 | if (m_pEventLoop && m_pEventLoop->isRunning() && !QDialog::isVisible())
|
---|
124 | m_pEventLoop->exit();
|
---|
125 | }
|
---|
126 |
|
---|
127 | void QIDialog::showEvent(QShowEvent *pEvent)
|
---|
128 | {
|
---|
129 | /* Make sure we should polish dialog: */
|
---|
130 | if (m_fPolished)
|
---|
131 | return;
|
---|
132 |
|
---|
133 | /* Call to polish-event: */
|
---|
134 | polishEvent(pEvent);
|
---|
135 |
|
---|
136 | /* Mark dialog as polished: */
|
---|
137 | m_fPolished = true;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void QIDialog::polishEvent(QShowEvent *)
|
---|
141 | {
|
---|
142 | /* Make sure layout is polished: */
|
---|
143 | adjustSize();
|
---|
144 | #ifdef VBOX_WS_MAC
|
---|
145 | /* And dialog have fixed size: */
|
---|
146 | setFixedSize(size());
|
---|
147 | #endif /* VBOX_WS_MAC */
|
---|
148 |
|
---|
149 | /* Explicit centering according to parent if any: */
|
---|
150 | if (parentWidget())
|
---|
151 | gpDesktop->centerWidget(this, parentWidget(), false);
|
---|
152 | }
|
---|