VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIComboBox.cpp

Last change on this file was 101563, checked in by vboxsync, 7 months ago

FE/Qt: bugref:10450: Get rid of Qt5 stuff; This one is about signal connection ambiguity stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: QIComboBox.cpp 101563 2023-10-23 23:36:38Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Qt extensions: QIComboBox class implementation.
4 */
5
6/*
7 * Copyright (C) 2016-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 <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. */
42class QIAccessibilityInterfaceForQIComboBox : public QAccessibleWidget
43{
44public:
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
69private:
70
71 /** Returns corresponding QIComboBox. */
72 QIComboBox *combo() const { return qobject_cast<QIComboBox*>(widget()); }
73};
74
75
76/*********************************************************************************************************************************
77* Class QIAccessibilityInterfaceForQIComboBox implementation. *
78*********************************************************************************************************************************/
79
80int 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
89QAccessibleInterface *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
100int 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
117QIComboBox::QIComboBox(QWidget *pParent /* = 0 */)
118 : QWidget(pParent)
119 , m_pComboBox(0)
120{
121 /* Prepare all: */
122 prepare();
123}
124
125int QIComboBox::subElementCount() const
126{
127 /* Depending on 'editable' property: */
128 return !isEditable() ? (int)SubElement_Max : (int)SubElementEditable_Max;
129}
130
131QWidget *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
160QLineEdit *QIComboBox::lineEdit() const
161{
162 /* Redirect to combo-box: */
163 AssertPtrReturn(m_pComboBox, 0);
164 return m_pComboBox->lineEdit();
165}
166
167QComboBox *QIComboBox::comboBox() const
168{
169 return m_pComboBox;
170}
171
172QAbstractItemView *QIComboBox::view() const
173{
174 /* Redirect to combo-box: */
175 AssertPtrReturn(m_pComboBox, 0);
176 return m_pComboBox->view();
177}
178
179QSize QIComboBox::iconSize() const
180{
181 /* Redirect to combo-box: */
182 AssertPtrReturn(m_pComboBox, QSize());
183 return m_pComboBox->iconSize();
184}
185
186QComboBox::InsertPolicy QIComboBox::insertPolicy() const
187{
188 /* Redirect to combo-box: */
189 AssertPtrReturn(m_pComboBox, QComboBox::NoInsert);
190 return m_pComboBox->insertPolicy();
191}
192
193bool QIComboBox::isEditable() const
194{
195 /* Redirect to combo-box: */
196 AssertPtrReturn(m_pComboBox, false);
197 return m_pComboBox->isEditable();
198}
199
200int QIComboBox::count() const
201{
202 /* Redirect to combo-box: */
203 AssertPtrReturn(m_pComboBox, 0);
204 return m_pComboBox->count();
205}
206
207int QIComboBox::currentIndex() const
208{
209 /* Redirect to combo-box: */
210 AssertPtrReturn(m_pComboBox, -1);
211 return m_pComboBox->currentIndex();
212}
213
214QString QIComboBox::currentText() const
215{
216 /* Redirect to combo-box: */
217 AssertPtrReturn(m_pComboBox, QString());
218 return m_pComboBox->currentText();
219}
220
221QVariant 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
228void QIComboBox::addItems(const QStringList &items) const
229{
230 /* Redirect to combo-box: */
231 AssertPtrReturnVoid(m_pComboBox);
232 return m_pComboBox->addItems(items);
233}
234
235void 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
242void 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
249void 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
256void QIComboBox::removeItem(int iIndex) const
257{
258 /* Redirect to combo-box: */
259 AssertPtrReturnVoid(m_pComboBox);
260 return m_pComboBox->removeItem(iIndex);
261}
262
263QVariant 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
270QIcon QIComboBox::itemIcon(int iIndex) const
271{
272 /* Redirect to combo-box: */
273 AssertPtrReturn(m_pComboBox, QIcon());
274 return m_pComboBox->itemIcon(iIndex);
275}
276
277QString QIComboBox::itemText(int iIndex) const
278{
279 /* Redirect to combo-box: */
280 AssertPtrReturn(m_pComboBox, QString());
281 return m_pComboBox->itemText(iIndex);
282}
283
284int 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
293int 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
301QComboBox::SizeAdjustPolicy QIComboBox::sizeAdjustPolicy() const
302{
303 /* Redirect to combo-box: */
304 AssertPtrReturn(m_pComboBox, QComboBox::AdjustToContentsOnFirstShow);
305 return m_pComboBox->sizeAdjustPolicy();
306}
307
308void QIComboBox::setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy enmPolicy)
309{
310 /* Redirect to combo-box: */
311 AssertPtrReturnVoid(m_pComboBox);
312 m_pComboBox->setSizeAdjustPolicy(enmPolicy);
313}
314
315void QIComboBox::mark(bool fError, const QString &strErrorMessage /* = QString() */)
316{
317 AssertPtrReturnVoid(m_pComboBox);
318 QILineEdit *pLineEdit = isEditable() ? qobject_cast<QILineEdit*>(m_pComboBox->lineEdit()) : 0;
319 if (pLineEdit)
320 pLineEdit->mark(fError, strErrorMessage);
321}
322
323void QIComboBox::insertSeparator(int iIndex)
324{
325 AssertPtrReturnVoid(m_pComboBox);
326 m_pComboBox->insertSeparator(iIndex);
327}
328
329void QIComboBox::clear()
330{
331 /* Redirect to combo-box: */
332 AssertPtrReturnVoid(m_pComboBox);
333 m_pComboBox->clear();
334}
335
336void QIComboBox::setIconSize(const QSize &size) const
337{
338 /* Redirect to combo-box: */
339 AssertPtrReturnVoid(m_pComboBox);
340 m_pComboBox->setIconSize(size);
341}
342
343void QIComboBox::setInsertPolicy(QComboBox::InsertPolicy policy) const
344{
345 /* Redirect to combo-box: */
346 AssertPtrReturnVoid(m_pComboBox);
347 m_pComboBox->setInsertPolicy(policy);
348}
349
350void QIComboBox::setEditable(bool fEditable) const
351{
352 /* Redirect to combo-box: */
353 AssertPtrReturnVoid(m_pComboBox);
354 m_pComboBox->setEditable(fEditable);
355
356 /* Replace the line-edit so that we can mark errors: */
357 if (isEditable())
358 m_pComboBox->setLineEdit(new QILineEdit);
359}
360
361void QIComboBox::setCurrentIndex(int iIndex) const
362{
363 /* Redirect to combo-box: */
364 AssertPtrReturnVoid(m_pComboBox);
365 m_pComboBox->setCurrentIndex(iIndex);
366}
367
368void QIComboBox::setItemData(int iIndex, const QVariant &value, int iRole /* = Qt::UserRole */) const
369{
370 /* Redirect to combo-box: */
371 AssertPtrReturnVoid(m_pComboBox);
372 m_pComboBox->setItemData(iIndex, value, iRole);
373}
374
375void QIComboBox::setItemIcon(int iIndex, const QIcon &icon) const
376{
377 /* Redirect to combo-box: */
378 AssertPtrReturnVoid(m_pComboBox);
379 m_pComboBox->setItemIcon(iIndex, icon);
380}
381
382void QIComboBox::setItemText(int iIndex, const QString &strText) const
383{
384 /* Redirect to combo-box: */
385 AssertPtrReturnVoid(m_pComboBox);
386 m_pComboBox->setItemText(iIndex, strText);
387}
388
389void QIComboBox::prepare()
390{
391 /* Install QIComboBox accessibility interface factory: */
392 QAccessible::installFactory(QIAccessibilityInterfaceForQIComboBox::pFactory);
393
394 /* Create layout: */
395 QHBoxLayout *pLayout = new QHBoxLayout(this);
396 AssertPtrReturnVoid(pLayout);
397 {
398 /* Configure layout: */
399 pLayout->setContentsMargins(0, 0, 0, 0);
400 pLayout->setSpacing(0);
401
402 /* Create combo-box: */
403 m_pComboBox = new QComboBox;
404 AssertPtrReturnVoid(m_pComboBox);
405 {
406 /* Configure combo-box: */
407 setFocusProxy(m_pComboBox);
408 connect(m_pComboBox, &QComboBox::activated, this, &QIComboBox::activated);
409 connect(m_pComboBox, &QComboBox::textActivated, this, &QIComboBox::textActivated);
410 connect(m_pComboBox, &QComboBox::currentIndexChanged, this, &QIComboBox::currentIndexChanged);
411 connect(m_pComboBox, &QComboBox::currentTextChanged, this, &QIComboBox::currentTextChanged);
412 connect(m_pComboBox, &QComboBox::editTextChanged, this, &QIComboBox::editTextChanged);
413 connect(m_pComboBox, &QComboBox::textHighlighted, this, &QIComboBox::textHighlighted);
414 /* Add combo-box into layout: */
415 pLayout->addWidget(m_pComboBox);
416 }
417 }
418}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use