1 | /* $Id: UIPopupPaneButtonPane.cpp 80914 2019-09-20 06:16:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIPopupPaneButtonPane class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* Qt includes: */
|
---|
19 | #include <QApplication>
|
---|
20 | #include <QHBoxLayout>
|
---|
21 | #include <QKeyEvent>
|
---|
22 | #include <QVBoxLayout>
|
---|
23 |
|
---|
24 | /* GUI includes: */
|
---|
25 | #include "QIMessageBox.h"
|
---|
26 | #include "QIToolButton.h"
|
---|
27 | #include "UIIconPool.h"
|
---|
28 | #include "UIPopupPaneButtonPane.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | UIPopupPaneButtonPane::UIPopupPaneButtonPane(QWidget *pParent /* = 0*/)
|
---|
32 | : QWidget(pParent)
|
---|
33 | , m_iDefaultButton(0)
|
---|
34 | , m_iEscapeButton(0)
|
---|
35 | {
|
---|
36 | /* Prepare: */
|
---|
37 | prepare();
|
---|
38 | }
|
---|
39 |
|
---|
40 | void UIPopupPaneButtonPane::setButtons(const QMap<int, QString> &buttonDescriptions)
|
---|
41 | {
|
---|
42 | /* Make sure something changed: */
|
---|
43 | if (m_buttonDescriptions == buttonDescriptions)
|
---|
44 | return;
|
---|
45 |
|
---|
46 | /* Assign new button-descriptions: */
|
---|
47 | m_buttonDescriptions = buttonDescriptions;
|
---|
48 |
|
---|
49 | /* Recreate buttons: */
|
---|
50 | cleanupButtons();
|
---|
51 | prepareButtons();
|
---|
52 | }
|
---|
53 |
|
---|
54 | void UIPopupPaneButtonPane::sltButtonClicked()
|
---|
55 | {
|
---|
56 | /* Make sure the slot is called by the button: */
|
---|
57 | QIToolButton *pButton = qobject_cast<QIToolButton*>(sender());
|
---|
58 | if (!pButton)
|
---|
59 | return;
|
---|
60 |
|
---|
61 | /* Make sure we still have that button: */
|
---|
62 | int iButtonID = m_buttons.key(pButton, 0);
|
---|
63 | if (!iButtonID)
|
---|
64 | return;
|
---|
65 |
|
---|
66 | /* Notify listeners button was clicked: */
|
---|
67 | emit sigButtonClicked(iButtonID);
|
---|
68 | }
|
---|
69 |
|
---|
70 | void UIPopupPaneButtonPane::prepare()
|
---|
71 | {
|
---|
72 | /* Prepare layouts: */
|
---|
73 | prepareLayouts();
|
---|
74 | }
|
---|
75 |
|
---|
76 | void UIPopupPaneButtonPane::prepareLayouts()
|
---|
77 | {
|
---|
78 | /* Create main-layout: */
|
---|
79 | QVBoxLayout *pMainLayout = new QVBoxLayout(this);
|
---|
80 | if (pMainLayout)
|
---|
81 | {
|
---|
82 | /* Configure layout: */
|
---|
83 | pMainLayout->setSpacing(0);
|
---|
84 | pMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
85 |
|
---|
86 | /* Create button-layout: */
|
---|
87 | m_pButtonLayout = new QHBoxLayout;
|
---|
88 | if (m_pButtonLayout)
|
---|
89 | {
|
---|
90 | /* Configure layout: */
|
---|
91 | m_pButtonLayout->setSpacing(0);
|
---|
92 | m_pButtonLayout->setContentsMargins(0, 0, 0, 0);
|
---|
93 |
|
---|
94 | /* Add into layout: */
|
---|
95 | pMainLayout->addLayout(m_pButtonLayout);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* Add stretch: */
|
---|
99 | pMainLayout->addStretch();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | void UIPopupPaneButtonPane::prepareButtons()
|
---|
104 | {
|
---|
105 | /* Add all the buttons: */
|
---|
106 | const QList<int> &buttonsIDs = m_buttonDescriptions.keys();
|
---|
107 | foreach (int iButtonID, buttonsIDs)
|
---|
108 | if (QIToolButton *pButton = addButton(iButtonID, m_buttonDescriptions[iButtonID]))
|
---|
109 | {
|
---|
110 | m_pButtonLayout->addWidget(pButton);
|
---|
111 | m_buttons[iButtonID] = pButton;
|
---|
112 | connect(pButton, &QIToolButton::clicked, this, &UIPopupPaneButtonPane::sltButtonClicked);
|
---|
113 | if (pButton->property("default").toBool())
|
---|
114 | m_iDefaultButton = iButtonID;
|
---|
115 | if (pButton->property("escape").toBool())
|
---|
116 | m_iEscapeButton = iButtonID;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | void UIPopupPaneButtonPane::cleanupButtons()
|
---|
121 | {
|
---|
122 | /* Remove all the buttons: */
|
---|
123 | const QList<int> &buttonsIDs = m_buttons.keys();
|
---|
124 | foreach (int iButtonID, buttonsIDs)
|
---|
125 | {
|
---|
126 | delete m_buttons[iButtonID];
|
---|
127 | m_buttons.remove(iButtonID);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | void UIPopupPaneButtonPane::keyPressEvent(QKeyEvent *pEvent)
|
---|
132 | {
|
---|
133 | /* Depending on pressed key: */
|
---|
134 | switch (pEvent->key())
|
---|
135 | {
|
---|
136 | case Qt::Key_Enter:
|
---|
137 | case Qt::Key_Return:
|
---|
138 | {
|
---|
139 | if (m_iDefaultButton)
|
---|
140 | {
|
---|
141 | pEvent->accept();
|
---|
142 | emit sigButtonClicked(m_iDefaultButton);
|
---|
143 | return;
|
---|
144 | }
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | case Qt::Key_Escape:
|
---|
148 | {
|
---|
149 | if (m_iEscapeButton)
|
---|
150 | {
|
---|
151 | pEvent->accept();
|
---|
152 | emit sigButtonClicked(m_iEscapeButton);
|
---|
153 | return;
|
---|
154 | }
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | default:
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | /* Call to base-class: */
|
---|
161 | QWidget::keyPressEvent(pEvent);
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* static */
|
---|
165 | QIToolButton *UIPopupPaneButtonPane::addButton(int iButtonID, const QString &strToolTip)
|
---|
166 | {
|
---|
167 | /* Create button: */
|
---|
168 | QIToolButton *pButton = new QIToolButton;
|
---|
169 | if (pButton)
|
---|
170 | {
|
---|
171 | /* Configure button: */
|
---|
172 | pButton->removeBorder();
|
---|
173 | pButton->setToolTip(strToolTip.isEmpty() ? defaultToolTip(iButtonID) : strToolTip);
|
---|
174 | pButton->setIcon(defaultIcon(iButtonID));
|
---|
175 |
|
---|
176 | /* Sign the 'default' button: */
|
---|
177 | if (iButtonID & AlertButtonOption_Default)
|
---|
178 | pButton->setProperty("default", true);
|
---|
179 | /* Sign the 'escape' button: */
|
---|
180 | if (iButtonID & AlertButtonOption_Escape)
|
---|
181 | pButton->setProperty("escape", true);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* Return button: */
|
---|
185 | return pButton;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* static */
|
---|
189 | QString UIPopupPaneButtonPane::defaultToolTip(int iButtonID)
|
---|
190 | {
|
---|
191 | QString strToolTip;
|
---|
192 | switch (iButtonID & AlertButtonMask)
|
---|
193 | {
|
---|
194 | case AlertButton_Ok: strToolTip = QIMessageBox::tr("OK"); break;
|
---|
195 | case AlertButton_Cancel:
|
---|
196 | {
|
---|
197 | switch (iButtonID & AlertOptionMask)
|
---|
198 | {
|
---|
199 | case AlertOption_AutoConfirmed: strToolTip = QApplication::translate("UIMessageCenter", "Do not show this message again"); break;
|
---|
200 | default: strToolTip = QIMessageBox::tr("Cancel"); break;
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | case AlertButton_Choice1: strToolTip = QIMessageBox::tr("Yes"); break;
|
---|
205 | case AlertButton_Choice2: strToolTip = QIMessageBox::tr("No"); break;
|
---|
206 | default: strToolTip = QString(); break;
|
---|
207 | }
|
---|
208 | return strToolTip;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* static */
|
---|
212 | QIcon UIPopupPaneButtonPane::defaultIcon(int iButtonID)
|
---|
213 | {
|
---|
214 | QIcon icon;
|
---|
215 | switch (iButtonID & AlertButtonMask)
|
---|
216 | {
|
---|
217 | case AlertButton_Ok: icon = UIIconPool::iconSet(":/ok_16px.png"); break;
|
---|
218 | case AlertButton_Cancel:
|
---|
219 | {
|
---|
220 | switch (iButtonID & AlertOptionMask)
|
---|
221 | {
|
---|
222 | case AlertOption_AutoConfirmed: icon = UIIconPool::iconSet(":/close_popup_16px.png"); break;
|
---|
223 | default: icon = UIIconPool::iconSet(":/cancel_16px.png"); break;
|
---|
224 | }
|
---|
225 | break;
|
---|
226 | }
|
---|
227 | case AlertButton_Choice1: break;
|
---|
228 | case AlertButton_Choice2: break;
|
---|
229 | default: break;
|
---|
230 | }
|
---|
231 | return icon;
|
---|
232 | }
|
---|