1 | /* $Id: QIManagerDialog.cpp 99946 2023-05-24 06:53:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIManagerDialog class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 <QMenuBar>
|
---|
30 | #include <QPushButton>
|
---|
31 | #include <QStyle>
|
---|
32 | #include <QVBoxLayout>
|
---|
33 |
|
---|
34 | /* GUI includes: */
|
---|
35 | #include "QIDialogButtonBox.h"
|
---|
36 | #include "QIManagerDialog.h"
|
---|
37 | #include "UICommon.h"
|
---|
38 | #include "UIDesktopWidgetWatchdog.h"
|
---|
39 | #include "UIHelpBrowserDialog.h"
|
---|
40 | #include "UIMessageCenter.h"
|
---|
41 | #ifdef VBOX_WS_MAC
|
---|
42 | # include "QIToolBar.h"
|
---|
43 | # include "UIWindowMenuManager.h"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Class QIManagerDialogFactory implementation. *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 |
|
---|
51 | void QIManagerDialogFactory::prepare(QIManagerDialog *&pDialog, QWidget *pCenterWidget /* = 0 */)
|
---|
52 | {
|
---|
53 | create(pDialog, pCenterWidget);
|
---|
54 | pDialog->prepare();
|
---|
55 | }
|
---|
56 |
|
---|
57 | void QIManagerDialogFactory::cleanup(QIManagerDialog *&pDialog)
|
---|
58 | {
|
---|
59 | pDialog->cleanup();
|
---|
60 | pDialog->deleteLater();
|
---|
61 | pDialog = 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /*********************************************************************************************************************************
|
---|
66 | * Class QIManagerDialog implementation. *
|
---|
67 | *********************************************************************************************************************************/
|
---|
68 |
|
---|
69 | QIManagerDialog::QIManagerDialog(QWidget *pCenterWidget)
|
---|
70 | : m_pCenterWidget(pCenterWidget)
|
---|
71 | , m_fCloseEmitted(false)
|
---|
72 | , m_pWidget(0)
|
---|
73 | #ifdef VBOX_WS_MAC
|
---|
74 | , m_pWidgetToolbar(0)
|
---|
75 | #endif
|
---|
76 | , m_pButtonBox(0)
|
---|
77 | {
|
---|
78 | }
|
---|
79 |
|
---|
80 | void QIManagerDialog::closeEvent(QCloseEvent *pEvent)
|
---|
81 | {
|
---|
82 | /* Ignore the event itself: */
|
---|
83 | pEvent->ignore();
|
---|
84 | /* But tell the listener to close us (once): */
|
---|
85 | if (!m_fCloseEmitted)
|
---|
86 | {
|
---|
87 | m_fCloseEmitted = true;
|
---|
88 | emit sigClose();
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | void QIManagerDialog::sltHandleHelpRequested()
|
---|
93 | {
|
---|
94 | UIHelpBrowserDialog::findManualFileAndShow(uiCommon().helpKeyword(m_pWidget));
|
---|
95 | }
|
---|
96 |
|
---|
97 | void QIManagerDialog::prepare()
|
---|
98 | {
|
---|
99 | /* Tell the application we are not that important: */
|
---|
100 | setAttribute(Qt::WA_QuitOnClose, false);
|
---|
101 |
|
---|
102 | /* Invent initial size: */
|
---|
103 | QSize proposedSize;
|
---|
104 | const int iHostScreen = UIDesktopWidgetWatchdog::screenNumber(m_pCenterWidget);
|
---|
105 | if (iHostScreen >= 0 && iHostScreen < UIDesktopWidgetWatchdog::screenCount())
|
---|
106 | {
|
---|
107 | /* On the basis of current host-screen geometry if possible: */
|
---|
108 | const QRect screenGeometry = gpDesktop->screenGeometry(iHostScreen);
|
---|
109 | if (screenGeometry.isValid())
|
---|
110 | proposedSize = screenGeometry.size() * 7 / 15;
|
---|
111 | }
|
---|
112 | /* Fallback to default size if we failed: */
|
---|
113 | if (proposedSize.isNull())
|
---|
114 | proposedSize = QSize(800, 600);
|
---|
115 | /* Resize to initial size: */
|
---|
116 | resize(proposedSize);
|
---|
117 |
|
---|
118 | /* Configure: */
|
---|
119 | configure();
|
---|
120 |
|
---|
121 | /* Prepare central-widget: */
|
---|
122 | prepareCentralWidget();
|
---|
123 | /* Prepare menu-bar: */
|
---|
124 | prepareMenuBar();
|
---|
125 | #ifdef VBOX_WS_MAC
|
---|
126 | /* Prepare toolbar: */
|
---|
127 | prepareToolBar();
|
---|
128 | #endif
|
---|
129 |
|
---|
130 | /* Finalize: */
|
---|
131 | finalize();
|
---|
132 |
|
---|
133 | /* Center according requested widget: */
|
---|
134 | gpDesktop->centerWidget(this, m_pCenterWidget, false);
|
---|
135 |
|
---|
136 | /* Load the dialog's settings from extradata */
|
---|
137 | loadSettings();
|
---|
138 | }
|
---|
139 |
|
---|
140 | void QIManagerDialog::prepareCentralWidget()
|
---|
141 | {
|
---|
142 | /* Create central-widget: */
|
---|
143 | setCentralWidget(new QWidget);
|
---|
144 | AssertPtrReturnVoid(centralWidget());
|
---|
145 | {
|
---|
146 | /* Create main-layout: */
|
---|
147 | new QVBoxLayout(centralWidget());
|
---|
148 | AssertPtrReturnVoid(centralWidget()->layout());
|
---|
149 | {
|
---|
150 | /* Configure layout: */
|
---|
151 | const int iL = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin) / 2;
|
---|
152 | const int iT = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin) / 2;
|
---|
153 | const int iR = qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin) / 2;
|
---|
154 | const int iB = qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin) / 2;
|
---|
155 | centralWidget()->layout()->setContentsMargins(iL, iT, iR, iB);
|
---|
156 |
|
---|
157 | /* Configure central-widget: */
|
---|
158 | configureCentralWidget();
|
---|
159 |
|
---|
160 | /* Prepare button-box: */
|
---|
161 | prepareButtonBox();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | void QIManagerDialog::prepareButtonBox()
|
---|
167 | {
|
---|
168 | /* Create button-box: */
|
---|
169 | m_pButtonBox = new QIDialogButtonBox;
|
---|
170 | AssertPtrReturnVoid(m_pButtonBox);
|
---|
171 | {
|
---|
172 | /* Configure button-box: */
|
---|
173 | #ifdef VBOX_WS_WIN
|
---|
174 | m_pButtonBox->setStandardButtons(QDialogButtonBox::Reset | QDialogButtonBox::Save | QDialogButtonBox::Close | QDialogButtonBox::Help);
|
---|
175 | #else
|
---|
176 | m_pButtonBox->setStandardButtons(QDialogButtonBox::Reset | QDialogButtonBox::Apply | QDialogButtonBox::Close | QDialogButtonBox::Help);
|
---|
177 | #endif
|
---|
178 | m_buttons[ButtonType_Reset] = m_pButtonBox->button(QDialogButtonBox::Reset);
|
---|
179 | #ifdef VBOX_WS_WIN
|
---|
180 | m_buttons[ButtonType_Apply] = m_pButtonBox->button(QDialogButtonBox::Save);
|
---|
181 | #else
|
---|
182 | m_buttons[ButtonType_Apply] = m_pButtonBox->button(QDialogButtonBox::Apply);
|
---|
183 | #endif
|
---|
184 | m_buttons[ButtonType_Close] = m_pButtonBox->button(QDialogButtonBox::Close);
|
---|
185 | m_buttons[ButtonType_Help] = m_pButtonBox->button(QDialogButtonBox::Help);
|
---|
186 |
|
---|
187 | /* Assign shortcuts: */
|
---|
188 | button(ButtonType_Close)->setShortcut(Qt::Key_Escape);
|
---|
189 | button(ButtonType_Help)->setShortcut(QKeySequence::HelpContents);
|
---|
190 |
|
---|
191 | /* Hide 'Reset' and 'Apply' initially: */
|
---|
192 | button(ButtonType_Reset)->hide();
|
---|
193 | button(ButtonType_Apply)->hide();
|
---|
194 | /* Disable 'Reset' and 'Apply' initially: */
|
---|
195 | button(ButtonType_Reset)->setEnabled(false);
|
---|
196 | button(ButtonType_Apply)->setEnabled(false);
|
---|
197 | connect(m_pButtonBox, &QIDialogButtonBox::rejected, this, &QIManagerDialog::close);
|
---|
198 | /* Connections to enable the context sensitive help: */
|
---|
199 | connect(m_pButtonBox, &QDialogButtonBox::helpRequested, this, &QIManagerDialog::sltHandleHelpRequested);
|
---|
200 |
|
---|
201 | /* Configure button-box: */
|
---|
202 | configureButtonBox();
|
---|
203 |
|
---|
204 | /* Add into layout: */
|
---|
205 | centralWidget()->layout()->addWidget(m_pButtonBox);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | void QIManagerDialog::prepareMenuBar()
|
---|
210 | {
|
---|
211 | /* Skip the call if there are no menus to add: */
|
---|
212 | if (m_widgetMenus.isEmpty())
|
---|
213 | return;
|
---|
214 |
|
---|
215 | /* Add all the widget menus: */
|
---|
216 | foreach (QMenu *pMenu, m_widgetMenus)
|
---|
217 | menuBar()->addMenu(pMenu);
|
---|
218 |
|
---|
219 | #ifdef VBOX_WS_MAC
|
---|
220 | /* Prepare 'Window' menu: */
|
---|
221 | if (gpWindowMenuManager)
|
---|
222 | {
|
---|
223 | menuBar()->addMenu(gpWindowMenuManager->createMenu(this));
|
---|
224 | gpWindowMenuManager->addWindow(this);
|
---|
225 | }
|
---|
226 | #endif
|
---|
227 | }
|
---|
228 |
|
---|
229 | #ifdef VBOX_WS_MAC
|
---|
230 | void QIManagerDialog::prepareToolBar()
|
---|
231 | {
|
---|
232 | if (!m_pWidgetToolbar)
|
---|
233 | return;
|
---|
234 | /* Enable unified toolbar on macOS: */
|
---|
235 | addToolBar(m_pWidgetToolbar);
|
---|
236 | m_pWidgetToolbar->enableMacToolbar();
|
---|
237 | }
|
---|
238 | #endif
|
---|
239 |
|
---|
240 | void QIManagerDialog::cleanupMenuBar()
|
---|
241 | {
|
---|
242 | #ifdef VBOX_WS_MAC
|
---|
243 | /* Cleanup 'Window' menu: */
|
---|
244 | if (gpWindowMenuManager)
|
---|
245 | {
|
---|
246 | gpWindowMenuManager->removeWindow(this);
|
---|
247 | gpWindowMenuManager->destroyMenu(this);
|
---|
248 | }
|
---|
249 | #endif
|
---|
250 | }
|
---|
251 |
|
---|
252 | void QIManagerDialog::cleanup()
|
---|
253 | {
|
---|
254 | saveSettings();
|
---|
255 | /* Cleanup menu-bar: */
|
---|
256 | cleanupMenuBar();
|
---|
257 | }
|
---|