1 | /* $Id: QIComboBox.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Qt extensions: QIComboBox class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2024 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 <QAccessibleWidget>
|
---|
30 | #include <QHBoxLayout>
|
---|
31 | #include <QLineEdit>
|
---|
32 |
|
---|
33 | /* GUI includes: */
|
---|
34 | #include "QIComboBox.h"
|
---|
35 | #include "QILineEdit.h"
|
---|
36 |
|
---|
37 | /* Other VBox includes: */
|
---|
38 | #include "iprt/assert.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /** QAccessibleWidget extension used as an accessibility interface for QIComboBox. */
|
---|
42 | class QIAccessibilityInterfaceForQIComboBox : public QAccessibleWidget
|
---|
43 | {
|
---|
44 | public:
|
---|
45 |
|
---|
46 | /** Returns an accessibility interface for passed @a strClassname and @a pObject. */
|
---|
47 | static QAccessibleInterface *pFactory(const QString &strClassname, QObject *pObject)
|
---|
48 | {
|
---|
49 | /* Creating QIComboBox accessibility interface: */
|
---|
50 | if (pObject && strClassname == QLatin1String("QIComboBox"))
|
---|
51 | return new QIAccessibilityInterfaceForQIComboBox(qobject_cast<QWidget*>(pObject));
|
---|
52 |
|
---|
53 | /* Null by default: */
|
---|
54 | return 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Constructs an accessibility interface passing @a pWidget to the base-class. */
|
---|
58 | QIAccessibilityInterfaceForQIComboBox(QWidget *pWidget)
|
---|
59 | : QAccessibleWidget(pWidget, QAccessible::ComboBox)
|
---|
60 | {}
|
---|
61 |
|
---|
62 | /** Returns the number of children. */
|
---|
63 | virtual int childCount() const RT_OVERRIDE;
|
---|
64 | /** Returns the child with the passed @a iIndex. */
|
---|
65 | virtual QAccessibleInterface *child(int iIndex) const RT_OVERRIDE;
|
---|
66 | /** Returns the index of the passed @a pChild. */
|
---|
67 | virtual int indexOfChild(const QAccessibleInterface *pChild) const RT_OVERRIDE;
|
---|
68 |
|
---|
69 | private:
|
---|
70 |
|
---|
71 | /** Returns corresponding QIComboBox. */
|
---|
72 | QIComboBox *combo() const { return qobject_cast<QIComboBox*>(widget()); }
|
---|
73 | };
|
---|
74 |
|
---|
75 |
|
---|
76 | /*********************************************************************************************************************************
|
---|
77 | * Class QIAccessibilityInterfaceForQIComboBox implementation. *
|
---|
78 | *********************************************************************************************************************************/
|
---|
79 |
|
---|
80 | int QIAccessibilityInterfaceForQIComboBox::childCount() const
|
---|
81 | {
|
---|
82 | /* Make sure combo still alive: */
|
---|
83 | AssertPtrReturn(combo(), 0);
|
---|
84 |
|
---|
85 | /* Return the number of children: */
|
---|
86 | return combo()->subElementCount();
|
---|
87 | }
|
---|
88 |
|
---|
89 | QAccessibleInterface *QIAccessibilityInterfaceForQIComboBox::child(int iIndex) const
|
---|
90 | {
|
---|
91 | /* Make sure combo still alive: */
|
---|
92 | AssertPtrReturn(combo(), 0);
|
---|
93 | /* Make sure index is valid: */
|
---|
94 | AssertReturn(iIndex >= 0 && iIndex < childCount(), 0);
|
---|
95 |
|
---|
96 | /* Return the child with the passed iIndex: */
|
---|
97 | return QAccessible::queryAccessibleInterface(combo()->subElement(iIndex));
|
---|
98 | }
|
---|
99 |
|
---|
100 | int QIAccessibilityInterfaceForQIComboBox::indexOfChild(const QAccessibleInterface *pChild) const
|
---|
101 | {
|
---|
102 | /* Search for corresponding child: */
|
---|
103 | for (int i = 0; i < childCount(); ++i)
|
---|
104 | if (child(i) == pChild)
|
---|
105 | return i;
|
---|
106 |
|
---|
107 | /* -1 by default: */
|
---|
108 | return -1;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | /*********************************************************************************************************************************
|
---|
114 | * Class QIComboBox implementation. *
|
---|
115 | *********************************************************************************************************************************/
|
---|
116 |
|
---|
117 | QIComboBox::QIComboBox(QWidget *pParent /* = 0 */)
|
---|
118 | : QWidget(pParent)
|
---|
119 | , m_pComboBox(0)
|
---|
120 | {
|
---|
121 | /* Prepare all: */
|
---|
122 | prepare();
|
---|
123 | }
|
---|
124 |
|
---|
125 | int QIComboBox::subElementCount() const
|
---|
126 | {
|
---|
127 | /* Depending on 'editable' property: */
|
---|
128 | return !isEditable() ? (int)SubElement_Max : (int)SubElementEditable_Max;
|
---|
129 | }
|
---|
130 |
|
---|
131 | QWidget *QIComboBox::subElement(int iIndex) const
|
---|
132 | {
|
---|
133 | /* Make sure index is inside the bounds: */
|
---|
134 | AssertReturn(iIndex >= 0 && iIndex < subElementCount(), 0);
|
---|
135 |
|
---|
136 | /* For 'non-editable' case: */
|
---|
137 | if (!isEditable())
|
---|
138 | {
|
---|
139 | switch (iIndex)
|
---|
140 | {
|
---|
141 | case SubElement_Selector: return m_pComboBox;
|
---|
142 | default: break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | /* For 'editable' case: */
|
---|
146 | else
|
---|
147 | {
|
---|
148 | switch (iIndex)
|
---|
149 | {
|
---|
150 | case SubElementEditable_Editor: return lineEdit();
|
---|
151 | case SubElementEditable_Selector: return m_pComboBox;
|
---|
152 | default: break;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* Null otherwise: */
|
---|
157 | return 0;
|
---|
158 | }
|
---|
159 |
|
---|
160 | QLineEdit *QIComboBox::lineEdit() const
|
---|
161 | {
|
---|
162 | /* Redirect to combo-box: */
|
---|
163 | AssertPtrReturn(m_pComboBox, 0);
|
---|
164 | return m_pComboBox->lineEdit();
|
---|
165 | }
|
---|
166 |
|
---|
167 | QComboBox *QIComboBox::comboBox() const
|
---|
168 | {
|
---|
169 | return m_pComboBox;
|
---|
170 | }
|
---|
171 |
|
---|
172 | QAbstractItemView *QIComboBox::view() const
|
---|
173 | {
|
---|
174 | /* Redirect to combo-box: */
|
---|
175 | AssertPtrReturn(m_pComboBox, 0);
|
---|
176 | return m_pComboBox->view();
|
---|
177 | }
|
---|
178 |
|
---|
179 | QSize QIComboBox::iconSize() const
|
---|
180 | {
|
---|
181 | /* Redirect to combo-box: */
|
---|
182 | AssertPtrReturn(m_pComboBox, QSize());
|
---|
183 | return m_pComboBox->iconSize();
|
---|
184 | }
|
---|
185 |
|
---|
186 | QComboBox::InsertPolicy QIComboBox::insertPolicy() const
|
---|
187 | {
|
---|
188 | /* Redirect to combo-box: */
|
---|
189 | AssertPtrReturn(m_pComboBox, QComboBox::NoInsert);
|
---|
190 | return m_pComboBox->insertPolicy();
|
---|
191 | }
|
---|
192 |
|
---|
193 | bool QIComboBox::isEditable() const
|
---|
194 | {
|
---|
195 | /* Redirect to combo-box: */
|
---|
196 | AssertPtrReturn(m_pComboBox, false);
|
---|
197 | return m_pComboBox->isEditable();
|
---|
198 | }
|
---|
199 |
|
---|
200 | int QIComboBox::count() const
|
---|
201 | {
|
---|
202 | /* Redirect to combo-box: */
|
---|
203 | AssertPtrReturn(m_pComboBox, 0);
|
---|
204 | return m_pComboBox->count();
|
---|
205 | }
|
---|
206 |
|
---|
207 | int QIComboBox::currentIndex() const
|
---|
208 | {
|
---|
209 | /* Redirect to combo-box: */
|
---|
210 | AssertPtrReturn(m_pComboBox, -1);
|
---|
211 | return m_pComboBox->currentIndex();
|
---|
212 | }
|
---|
213 |
|
---|
214 | QString QIComboBox::currentText() const
|
---|
215 | {
|
---|
216 | /* Redirect to combo-box: */
|
---|
217 | AssertPtrReturn(m_pComboBox, QString());
|
---|
218 | return m_pComboBox->currentText();
|
---|
219 | }
|
---|
220 |
|
---|
221 | QVariant QIComboBox::currentData(int iRole /* = Qt::UserRole */) const
|
---|
222 | {
|
---|
223 | /* Redirect to combo-box: */
|
---|
224 | AssertPtrReturn(m_pComboBox, QVariant());
|
---|
225 | return m_pComboBox->currentData(iRole);
|
---|
226 | }
|
---|
227 |
|
---|
228 | void QIComboBox::addItems(const QStringList &items) const
|
---|
229 | {
|
---|
230 | /* Redirect to combo-box: */
|
---|
231 | AssertPtrReturnVoid(m_pComboBox);
|
---|
232 | return m_pComboBox->addItems(items);
|
---|
233 | }
|
---|
234 |
|
---|
235 | void QIComboBox::addItem(const QString &strText, const QVariant &userData /* = QVariant() */) const
|
---|
236 | {
|
---|
237 | /* Redirect to combo-box: */
|
---|
238 | AssertPtrReturnVoid(m_pComboBox);
|
---|
239 | return m_pComboBox->addItem(strText, userData);
|
---|
240 | }
|
---|
241 |
|
---|
242 | void QIComboBox::insertItems(int iIndex, const QStringList &items)
|
---|
243 | {
|
---|
244 | /* Redirect to combo-box: */
|
---|
245 | AssertPtrReturnVoid(m_pComboBox);
|
---|
246 | return m_pComboBox->insertItems(iIndex, items);
|
---|
247 | }
|
---|
248 |
|
---|
249 | void QIComboBox::insertItem(int iIndex, const QString &strText, const QVariant &userData /* = QVariant() */) const
|
---|
250 | {
|
---|
251 | /* Redirect to combo-box: */
|
---|
252 | AssertPtrReturnVoid(m_pComboBox);
|
---|
253 | return m_pComboBox->insertItem(iIndex, strText, userData);
|
---|
254 | }
|
---|
255 |
|
---|
256 | void QIComboBox::removeItem(int iIndex) const
|
---|
257 | {
|
---|
258 | /* Redirect to combo-box: */
|
---|
259 | AssertPtrReturnVoid(m_pComboBox);
|
---|
260 | return m_pComboBox->removeItem(iIndex);
|
---|
261 | }
|
---|
262 |
|
---|
263 | QVariant QIComboBox::itemData(int iIndex, int iRole /* = Qt::UserRole */) const
|
---|
264 | {
|
---|
265 | /* Redirect to combo-box: */
|
---|
266 | AssertPtrReturn(m_pComboBox, QVariant());
|
---|
267 | return m_pComboBox->itemData(iIndex, iRole);
|
---|
268 | }
|
---|
269 |
|
---|
270 | QIcon QIComboBox::itemIcon(int iIndex) const
|
---|
271 | {
|
---|
272 | /* Redirect to combo-box: */
|
---|
273 | AssertPtrReturn(m_pComboBox, QIcon());
|
---|
274 | return m_pComboBox->itemIcon(iIndex);
|
---|
275 | }
|
---|
276 |
|
---|
277 | QString QIComboBox::itemText(int iIndex) const
|
---|
278 | {
|
---|
279 | /* Redirect to combo-box: */
|
---|
280 | AssertPtrReturn(m_pComboBox, QString());
|
---|
281 | return m_pComboBox->itemText(iIndex);
|
---|
282 | }
|
---|
283 |
|
---|
284 | int QIComboBox::findData(const QVariant &data,
|
---|
285 | int iRole /* = Qt::UserRole */,
|
---|
286 | Qt::MatchFlags flags /* = static_cast<Qt::MatchFlags>(Qt::MatchExactly | Qt::MatchCaseSensitive) */) const
|
---|
287 | {
|
---|
288 | /* Redirect to combo-box: */
|
---|
289 | AssertPtrReturn(m_pComboBox, -1);
|
---|
290 | return m_pComboBox->findData(data, iRole, flags);
|
---|
291 | }
|
---|
292 |
|
---|
293 | int QIComboBox::findText(const QString &strText,
|
---|
294 | Qt::MatchFlags flags /* = static_cast<Qt::MatchFlags>(Qt::MatchExactly | Qt::MatchCaseSensitive) */) const
|
---|
295 | {
|
---|
296 | /* Redirect to combo-box: */
|
---|
297 | AssertPtrReturn(m_pComboBox, -1);
|
---|
298 | return m_pComboBox->findText(strText, flags);
|
---|
299 | }
|
---|
300 |
|
---|
301 | QComboBox::SizeAdjustPolicy QIComboBox::sizeAdjustPolicy() const
|
---|
302 | {
|
---|
303 | /* Redirect to combo-box: */
|
---|
304 | AssertPtrReturn(m_pComboBox, QComboBox::AdjustToContentsOnFirstShow);
|
---|
305 | return m_pComboBox->sizeAdjustPolicy();
|
---|
306 | }
|
---|
307 |
|
---|
308 | void QIComboBox::setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy enmPolicy)
|
---|
309 | {
|
---|
310 | /* Redirect to combo-box: */
|
---|
311 | AssertPtrReturnVoid(m_pComboBox);
|
---|
312 | m_pComboBox->setSizeAdjustPolicy(enmPolicy);
|
---|
313 | }
|
---|
314 |
|
---|
315 | void QIComboBox::mark(bool fError, const QString &strErrorMessage, const QString &strNoErrorMessage)
|
---|
316 | {
|
---|
317 | AssertPtrReturnVoid(m_pComboBox);
|
---|
318 | QILineEdit *pLineEdit = isEditable() ? qobject_cast<QILineEdit*>(m_pComboBox->lineEdit()) : 0;
|
---|
319 | setMarkable(true);
|
---|
320 | if (pLineEdit)
|
---|
321 | pLineEdit->mark(fError, strErrorMessage, strNoErrorMessage);
|
---|
322 | }
|
---|
323 |
|
---|
324 | void QIComboBox::insertSeparator(int iIndex)
|
---|
325 | {
|
---|
326 | AssertPtrReturnVoid(m_pComboBox);
|
---|
327 | m_pComboBox->insertSeparator(iIndex);
|
---|
328 | }
|
---|
329 |
|
---|
330 | void QIComboBox::clear()
|
---|
331 | {
|
---|
332 | /* Redirect to combo-box: */
|
---|
333 | AssertPtrReturnVoid(m_pComboBox);
|
---|
334 | m_pComboBox->clear();
|
---|
335 | }
|
---|
336 |
|
---|
337 | void QIComboBox::setIconSize(const QSize &size) const
|
---|
338 | {
|
---|
339 | /* Redirect to combo-box: */
|
---|
340 | AssertPtrReturnVoid(m_pComboBox);
|
---|
341 | m_pComboBox->setIconSize(size);
|
---|
342 | }
|
---|
343 |
|
---|
344 | void QIComboBox::setInsertPolicy(QComboBox::InsertPolicy policy) const
|
---|
345 | {
|
---|
346 | /* Redirect to combo-box: */
|
---|
347 | AssertPtrReturnVoid(m_pComboBox);
|
---|
348 | m_pComboBox->setInsertPolicy(policy);
|
---|
349 | }
|
---|
350 |
|
---|
351 | void QIComboBox::setEditable(bool fEditable) const
|
---|
352 | {
|
---|
353 | /* Redirect to combo-box: */
|
---|
354 | AssertPtrReturnVoid(m_pComboBox);
|
---|
355 | m_pComboBox->setEditable(fEditable);
|
---|
356 |
|
---|
357 | /* Replace the line-edit so that we can mark errors: */
|
---|
358 | if (isEditable())
|
---|
359 | m_pComboBox->setLineEdit(new QILineEdit);
|
---|
360 | }
|
---|
361 |
|
---|
362 | void QIComboBox::setCurrentIndex(int iIndex) const
|
---|
363 | {
|
---|
364 | /* Redirect to combo-box: */
|
---|
365 | AssertPtrReturnVoid(m_pComboBox);
|
---|
366 | m_pComboBox->setCurrentIndex(iIndex);
|
---|
367 | }
|
---|
368 |
|
---|
369 | void QIComboBox::setItemData(int iIndex, const QVariant &value, int iRole /* = Qt::UserRole */) const
|
---|
370 | {
|
---|
371 | /* Redirect to combo-box: */
|
---|
372 | AssertPtrReturnVoid(m_pComboBox);
|
---|
373 | m_pComboBox->setItemData(iIndex, value, iRole);
|
---|
374 | }
|
---|
375 |
|
---|
376 | void QIComboBox::setItemIcon(int iIndex, const QIcon &icon) const
|
---|
377 | {
|
---|
378 | /* Redirect to combo-box: */
|
---|
379 | AssertPtrReturnVoid(m_pComboBox);
|
---|
380 | m_pComboBox->setItemIcon(iIndex, icon);
|
---|
381 | }
|
---|
382 |
|
---|
383 | void QIComboBox::setItemText(int iIndex, const QString &strText) const
|
---|
384 | {
|
---|
385 | /* Redirect to combo-box: */
|
---|
386 | AssertPtrReturnVoid(m_pComboBox);
|
---|
387 | m_pComboBox->setItemText(iIndex, strText);
|
---|
388 | }
|
---|
389 |
|
---|
390 | void QIComboBox::setMarkable(bool fMarkable)
|
---|
391 | {
|
---|
392 | QILineEdit *pLineEdit = isEditable() ? qobject_cast<QILineEdit*>(m_pComboBox->lineEdit()) : 0;
|
---|
393 | if (pLineEdit)
|
---|
394 | pLineEdit->setMarkable(fMarkable);
|
---|
395 | }
|
---|
396 |
|
---|
397 | void QIComboBox::prepare()
|
---|
398 | {
|
---|
399 | /* Install QIComboBox accessibility interface factory: */
|
---|
400 | QAccessible::installFactory(QIAccessibilityInterfaceForQIComboBox::pFactory);
|
---|
401 |
|
---|
402 | /* Create layout: */
|
---|
403 | QHBoxLayout *pLayout = new QHBoxLayout(this);
|
---|
404 | AssertPtrReturnVoid(pLayout);
|
---|
405 | {
|
---|
406 | /* Configure layout: */
|
---|
407 | pLayout->setContentsMargins(0, 0, 0, 0);
|
---|
408 | pLayout->setSpacing(0);
|
---|
409 |
|
---|
410 | /* Create combo-box: */
|
---|
411 | m_pComboBox = new QComboBox;
|
---|
412 | AssertPtrReturnVoid(m_pComboBox);
|
---|
413 | {
|
---|
414 | /* Configure combo-box: */
|
---|
415 | setFocusProxy(m_pComboBox);
|
---|
416 | connect(m_pComboBox, &QComboBox::activated, this, &QIComboBox::activated);
|
---|
417 | connect(m_pComboBox, &QComboBox::textActivated, this, &QIComboBox::textActivated);
|
---|
418 | connect(m_pComboBox, &QComboBox::currentIndexChanged, this, &QIComboBox::currentIndexChanged);
|
---|
419 | connect(m_pComboBox, &QComboBox::currentTextChanged, this, &QIComboBox::currentTextChanged);
|
---|
420 | connect(m_pComboBox, &QComboBox::editTextChanged, this, &QIComboBox::editTextChanged);
|
---|
421 | connect(m_pComboBox, &QComboBox::textHighlighted, this, &QIComboBox::textHighlighted);
|
---|
422 | /* Add combo-box into layout: */
|
---|
423 | pLayout->addWidget(m_pComboBox);
|
---|
424 | }
|
---|
425 | }
|
---|
426 | }
|
---|