1 | /* $Id: QIDialogButtonBox.cpp 104358 2024-04-18 05:33:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIDialogButtonBox 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 | /* Qt includes: */
|
---|
28 | #include <QBoxLayout>
|
---|
29 | #include <QEvent>
|
---|
30 | #include <QPushButton>
|
---|
31 |
|
---|
32 | /* GUI includes: */
|
---|
33 | #include "QIDialogButtonBox.h"
|
---|
34 | #include "UICommon.h"
|
---|
35 | #include "UIHelpBrowserDialog.h"
|
---|
36 | #include "UISpecialControls.h"
|
---|
37 | #include "UITranslationEventListener.h"
|
---|
38 |
|
---|
39 | /* Other VBox includes: */
|
---|
40 | #include <iprt/assert.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | QIDialogButtonBox::QIDialogButtonBox(QWidget *pParent /* = 0 */)
|
---|
44 | : QDialogButtonBox(pParent)
|
---|
45 | , m_fDoNotPickDefaultButton(false)
|
---|
46 | {
|
---|
47 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
48 | this, &QIDialogButtonBox::sltRetranslateUI);
|
---|
49 | }
|
---|
50 |
|
---|
51 | QIDialogButtonBox::QIDialogButtonBox(Qt::Orientation enmOrientation, QWidget *pParent /* = 0 */)
|
---|
52 | : QDialogButtonBox(pParent)
|
---|
53 | , m_fDoNotPickDefaultButton(false)
|
---|
54 | {
|
---|
55 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
56 | this, &QIDialogButtonBox::sltRetranslateUI);
|
---|
57 | setOrientation(enmOrientation);
|
---|
58 | }
|
---|
59 |
|
---|
60 | QIDialogButtonBox::QIDialogButtonBox(StandardButtons enmButtonTypes, Qt::Orientation enmOrientation, QWidget *pParent)
|
---|
61 | : QDialogButtonBox(pParent)
|
---|
62 | , m_fDoNotPickDefaultButton(false)
|
---|
63 | {
|
---|
64 | setOrientation(enmOrientation);
|
---|
65 | setStandardButtons(enmButtonTypes);
|
---|
66 | sltRetranslateUI();
|
---|
67 | connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
|
---|
68 | this, &QIDialogButtonBox::sltRetranslateUI);
|
---|
69 | }
|
---|
70 |
|
---|
71 | QPushButton *QIDialogButtonBox::button(StandardButton enmButtonType) const
|
---|
72 | {
|
---|
73 | QPushButton *pButton = QDialogButtonBox::button(enmButtonType);
|
---|
74 | if ( !pButton
|
---|
75 | && enmButtonType == QDialogButtonBox::Help)
|
---|
76 | pButton = m_pHelpButton;
|
---|
77 | return pButton;
|
---|
78 | }
|
---|
79 |
|
---|
80 | QPushButton *QIDialogButtonBox::addButton(const QString &strText, ButtonRole enmRole)
|
---|
81 | {
|
---|
82 | QPushButton *pButton = QDialogButtonBox::addButton(strText, enmRole);
|
---|
83 | sltRetranslateUI();
|
---|
84 | return pButton;
|
---|
85 | }
|
---|
86 |
|
---|
87 | QPushButton *QIDialogButtonBox::addButton(StandardButton enmButtonType)
|
---|
88 | {
|
---|
89 | QPushButton *pButton = QDialogButtonBox::addButton(enmButtonType);
|
---|
90 | sltRetranslateUI();
|
---|
91 | return pButton;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void QIDialogButtonBox::setStandardButtons(StandardButtons enmButtonTypes)
|
---|
95 | {
|
---|
96 | QDialogButtonBox::setStandardButtons(enmButtonTypes);
|
---|
97 | sltRetranslateUI();
|
---|
98 | }
|
---|
99 |
|
---|
100 | void QIDialogButtonBox::addExtraWidget(QWidget *pInsertedWidget)
|
---|
101 | {
|
---|
102 | QBoxLayout *pLayout = boxLayout();
|
---|
103 | if (pLayout)
|
---|
104 | {
|
---|
105 | int iIndex = findEmptySpace(pLayout);
|
---|
106 | pLayout->insertWidget(iIndex + 1, pInsertedWidget);
|
---|
107 | pLayout->insertStretch(iIndex + 2);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | void QIDialogButtonBox::addExtraLayout(QLayout *pInsertedLayout)
|
---|
112 | {
|
---|
113 | QBoxLayout *pLayout = boxLayout();
|
---|
114 | if (pLayout)
|
---|
115 | {
|
---|
116 | int iIndex = findEmptySpace(pLayout);
|
---|
117 | pLayout->insertLayout(iIndex + 1, pInsertedLayout);
|
---|
118 | pLayout->insertStretch(iIndex + 2);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void QIDialogButtonBox::setDoNotPickDefaultButton(bool fDoNotPickDefaultButton)
|
---|
123 | {
|
---|
124 | m_fDoNotPickDefaultButton = fDoNotPickDefaultButton;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void QIDialogButtonBox::sltRetranslateUI()
|
---|
128 | {
|
---|
129 | QPushButton *pButton = QDialogButtonBox::button(QDialogButtonBox::Help);
|
---|
130 | if (pButton)
|
---|
131 | {
|
---|
132 | /* Use our very own help button if the user requested for one. */
|
---|
133 | if (!m_pHelpButton)
|
---|
134 | m_pHelpButton = new UIHelpButton;
|
---|
135 | m_pHelpButton->initFrom(pButton);
|
---|
136 | removeButton(pButton);
|
---|
137 | QDialogButtonBox::addButton(m_pHelpButton, QDialogButtonBox::HelpRole);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | void QIDialogButtonBox::showEvent(QShowEvent *pEvent)
|
---|
142 | {
|
---|
143 | // WORKAROUND:
|
---|
144 | // QDialogButtonBox has embedded functionality we'd like to avoid.
|
---|
145 | // It auto-picks default button if non is set, based on button role.
|
---|
146 | // Qt documentation states that happens in showEvent, so here we are.
|
---|
147 | // In rare case we'd like to have dialog with no default button at all.
|
---|
148 | if (m_fDoNotPickDefaultButton)
|
---|
149 | {
|
---|
150 | /* Unset all default-buttons in the dialog: */
|
---|
151 | foreach (QPushButton *pButton, findChildren<QPushButton*>())
|
---|
152 | if (pButton->isDefault())
|
---|
153 | pButton->setDefault(false);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* Call to base-class: */
|
---|
157 | return QDialogButtonBox::showEvent(pEvent);
|
---|
158 | }
|
---|
159 |
|
---|
160 | QBoxLayout *QIDialogButtonBox::boxLayout() const
|
---|
161 | {
|
---|
162 | QBoxLayout *pLayout = qobject_cast<QBoxLayout*>(layout());
|
---|
163 | AssertMsg(RT_VALID_PTR(pLayout), ("Layout of the QDialogButtonBox isn't a box layout."));
|
---|
164 | return pLayout;
|
---|
165 | }
|
---|
166 |
|
---|
167 | int QIDialogButtonBox::findEmptySpace(QBoxLayout *pLayout) const
|
---|
168 | {
|
---|
169 | /* Search for the first occurrence of QSpacerItem and return the index. */
|
---|
170 | int i = 0;
|
---|
171 | for (; i < pLayout->count(); ++i)
|
---|
172 | {
|
---|
173 | QLayoutItem *pItem = pLayout->itemAt(i);
|
---|
174 | if (pItem && pItem->spacerItem())
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | return i;
|
---|
178 | }
|
---|
179 |
|
---|
180 | void QIDialogButtonBox::sltHandleHelpRequest()
|
---|
181 | {
|
---|
182 | AssertReturnVoid(sender());
|
---|
183 | UIHelpBrowserDialog::findManualFileAndShow(uiCommon().helpKeyword(sender()));
|
---|
184 | }
|
---|