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