VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIDialogButtonBox.cpp@ 100347

Last change on this file since 100347 was 99947, checked in by vboxsync, 20 months ago

FE/Qt: bugref:10451. build fix.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette