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