VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIPopupPaneButtonPane.cpp@ 100347

Last change on this file since 100347 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: UIPopupPaneButtonPane.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIPopupPaneButtonPane class implementation.
4 */
5
6/*
7 * Copyright (C) 2013-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 <QApplication>
30#include <QHBoxLayout>
31#include <QKeyEvent>
32#include <QVBoxLayout>
33
34/* GUI includes: */
35#include "QIMessageBox.h"
36#include "QIToolButton.h"
37#include "UIIconPool.h"
38#include "UIPopupPaneButtonPane.h"
39
40
41UIPopupPaneButtonPane::UIPopupPaneButtonPane(QWidget *pParent /* = 0*/)
42 : QWidget(pParent)
43 , m_iDefaultButton(0)
44 , m_iEscapeButton(0)
45{
46 /* Prepare: */
47 prepare();
48}
49
50void UIPopupPaneButtonPane::setButtons(const QMap<int, QString> &buttonDescriptions)
51{
52 /* Make sure something changed: */
53 if (m_buttonDescriptions == buttonDescriptions)
54 return;
55
56 /* Assign new button-descriptions: */
57 m_buttonDescriptions = buttonDescriptions;
58
59 /* Recreate buttons: */
60 cleanupButtons();
61 prepareButtons();
62}
63
64void UIPopupPaneButtonPane::sltButtonClicked()
65{
66 /* Make sure the slot is called by the button: */
67 QIToolButton *pButton = qobject_cast<QIToolButton*>(sender());
68 if (!pButton)
69 return;
70
71 /* Make sure we still have that button: */
72 int iButtonID = m_buttons.key(pButton, 0);
73 if (!iButtonID)
74 return;
75
76 /* Notify listeners button was clicked: */
77 emit sigButtonClicked(iButtonID);
78}
79
80void UIPopupPaneButtonPane::prepare()
81{
82 /* Prepare layouts: */
83 prepareLayouts();
84}
85
86void UIPopupPaneButtonPane::prepareLayouts()
87{
88 /* Create main-layout: */
89 QVBoxLayout *pMainLayout = new QVBoxLayout(this);
90 if (pMainLayout)
91 {
92 /* Configure layout: */
93 pMainLayout->setSpacing(0);
94 pMainLayout->setContentsMargins(0, 0, 0, 0);
95
96 /* Create button-layout: */
97 m_pButtonLayout = new QHBoxLayout;
98 if (m_pButtonLayout)
99 {
100 /* Configure layout: */
101 m_pButtonLayout->setSpacing(0);
102 m_pButtonLayout->setContentsMargins(0, 0, 0, 0);
103
104 /* Add into layout: */
105 pMainLayout->addLayout(m_pButtonLayout);
106 }
107
108 /* Add stretch: */
109 pMainLayout->addStretch();
110 }
111}
112
113void UIPopupPaneButtonPane::prepareButtons()
114{
115 /* Add all the buttons: */
116 const QList<int> &buttonsIDs = m_buttonDescriptions.keys();
117 foreach (int iButtonID, buttonsIDs)
118 if (QIToolButton *pButton = addButton(iButtonID, m_buttonDescriptions[iButtonID]))
119 {
120 m_pButtonLayout->addWidget(pButton);
121 m_buttons[iButtonID] = pButton;
122 connect(pButton, &QIToolButton::clicked, this, &UIPopupPaneButtonPane::sltButtonClicked);
123 if (pButton->property("default").toBool())
124 m_iDefaultButton = iButtonID;
125 if (pButton->property("escape").toBool())
126 m_iEscapeButton = iButtonID;
127 }
128}
129
130void UIPopupPaneButtonPane::cleanupButtons()
131{
132 /* Remove all the buttons: */
133 const QList<int> &buttonsIDs = m_buttons.keys();
134 foreach (int iButtonID, buttonsIDs)
135 {
136 delete m_buttons[iButtonID];
137 m_buttons.remove(iButtonID);
138 }
139}
140
141void UIPopupPaneButtonPane::keyPressEvent(QKeyEvent *pEvent)
142{
143 /* Depending on pressed key: */
144 switch (pEvent->key())
145 {
146 case Qt::Key_Enter:
147 case Qt::Key_Return:
148 {
149 if (m_iDefaultButton)
150 {
151 pEvent->accept();
152 emit sigButtonClicked(m_iDefaultButton);
153 return;
154 }
155 break;
156 }
157 case Qt::Key_Escape:
158 {
159 if (m_iEscapeButton)
160 {
161 pEvent->accept();
162 emit sigButtonClicked(m_iEscapeButton);
163 return;
164 }
165 break;
166 }
167 default:
168 break;
169 }
170 /* Call to base-class: */
171 QWidget::keyPressEvent(pEvent);
172}
173
174/* static */
175QIToolButton *UIPopupPaneButtonPane::addButton(int iButtonID, const QString &strToolTip)
176{
177 /* Create button: */
178 QIToolButton *pButton = new QIToolButton;
179 if (pButton)
180 {
181 /* Configure button: */
182 pButton->removeBorder();
183 pButton->setToolTip(strToolTip.isEmpty() ? defaultToolTip(iButtonID) : strToolTip);
184 pButton->setIcon(defaultIcon(iButtonID));
185
186 /* Sign the 'default' button: */
187 if (iButtonID & AlertButtonOption_Default)
188 pButton->setProperty("default", true);
189 /* Sign the 'escape' button: */
190 if (iButtonID & AlertButtonOption_Escape)
191 pButton->setProperty("escape", true);
192 }
193
194 /* Return button: */
195 return pButton;
196}
197
198/* static */
199QString UIPopupPaneButtonPane::defaultToolTip(int iButtonID)
200{
201 QString strToolTip;
202 switch (iButtonID & AlertButtonMask)
203 {
204 case AlertButton_Ok: strToolTip = QIMessageBox::tr("OK"); break;
205 case AlertButton_Cancel:
206 {
207 switch (iButtonID & AlertOptionMask)
208 {
209 case AlertOption_AutoConfirmed: strToolTip = QApplication::translate("UIMessageCenter", "Do not show this message again"); break;
210 default: strToolTip = QIMessageBox::tr("Cancel"); break;
211 }
212 break;
213 }
214 case AlertButton_Choice1: strToolTip = QIMessageBox::tr("Yes"); break;
215 case AlertButton_Choice2: strToolTip = QIMessageBox::tr("No"); break;
216 default: strToolTip = QString(); break;
217 }
218 return strToolTip;
219}
220
221/* static */
222QIcon UIPopupPaneButtonPane::defaultIcon(int iButtonID)
223{
224 QIcon icon;
225 switch (iButtonID & AlertButtonMask)
226 {
227 case AlertButton_Ok: icon = UIIconPool::iconSet(":/ok_16px.png"); break;
228 case AlertButton_Cancel:
229 {
230 switch (iButtonID & AlertOptionMask)
231 {
232 case AlertOption_AutoConfirmed: icon = UIIconPool::iconSet(":/close_popup_16px.png"); break;
233 default: icon = UIIconPool::iconSet(":/cancel_16px.png"); break;
234 }
235 break;
236 }
237 case AlertButton_Choice1: break;
238 case AlertButton_Choice2: break;
239 default: break;
240 }
241 return icon;
242}
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