aboutsummaryrefslogtreecommitdiffstats
path: root/src/templates/qquickcombobox.cpp
blob: f4cb17afc0e94a6ac9cef5defcedb03803227548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Labs Templates module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qquickcombobox_p.h"
#include "qquickcontrol_p_p.h"
#include "qquickabstractbutton_p.h"
#include "qquickpopup_p.h"

#include <QtCore/qregexp.h>
#include <QtGui/qpa/qplatformtheme.h>
#include <QtQml/qjsvalue.h>
#include <QtQml/qqmlcontext.h>
#include <QtQml/private/qqmldelegatemodel_p.h>
#include <QtQuick/private/qquickevents_p_p.h>

QT_BEGIN_NAMESPACE

/*!
    \qmltype ComboBox
    \inherits Control
    \instantiates QQuickComboBox
    \inqmlmodule Qt.labs.controls
    \ingroup qtlabscontrols-input
    \brief A combo box control.

    \image qtlabscontrols-combobox.png

    ComboBox is a combined button and popup list. It provides a means of
    presenting a list of options to the user in a way that takes up the
    minimum amount of screen space.

    ComboBox is populated with a data model. The data model is commonly
    a JavaScript array, a \l ListModel or an integer, but also other types
    of \l {qml-data-models}{data models} are supported.

    \code
    ComboBox {
        model: ["First", "Second", "Third"]
    }
    \endcode

    ComboBox is able to visualize standard \l {qml-data-models}{data models}
    that provide the \c modelData role:
    \list
    \li models that have only one role
    \li models that do not have named roles (JavaScript array, integer)
    \endlist

    When using models that have multiple named roles, ComboBox must be configured
    to use a specific \l {textRole}{text role} for its \l {displayText}{display text}
    and \l delegate instances.

    \code
    ComboBox {
        textRole: "key"
        model: ListModel {
            ListElement { key: "First"; value: 123 }
            ListElement { key: "Second"; value: 456 }
            ListElement { key: "Third"; value: 789 }
        }
    }
    \endcode

    \note If ComboBox is assigned a data model that has multiple named roles, but
    \l textRole is not defined, ComboBox is unable to visualize it and throws a
    \c {ReferenceError: modelData is not defined}.

    \labs

    \sa {Customizing ComboBox}, {Input Controls}
*/

/*!
    \qmlsignal void Qt.labs.controls::ComboBox::activated(int index)

    This signal is emitted when the item at \a index is activated by the user.

    \sa currentIndex
*/

/*!
    \qmlsignal void Qt.labs.controls::ComboBox::highlighted(int index)

    This signal is emitted when the item at \a index in the popup list is highlighted by the user.

    \sa highlightedIndex
*/

class QQuickComboBoxDelegateModel : public QQmlDelegateModel
{
public:
    explicit QQuickComboBoxDelegateModel(QQuickComboBox *combo);
    QString stringValue(int index, const QString &role) Q_DECL_OVERRIDE;

private:
    QQuickComboBox *combo;
};

QQuickComboBoxDelegateModel::QQuickComboBoxDelegateModel(QQuickComboBox *combo) :
    QQmlDelegateModel(qmlContext(combo), combo), combo(combo)
{
}

QString QQuickComboBoxDelegateModel::stringValue(int index, const QString &role)
{
    QVariant model = combo->model();
    if (model.userType() == QMetaType::QVariantList) {
        QVariant object = model.toList().value(index);
        if (object.userType() == QMetaType::QVariantMap) {
            QVariantMap data = object.toMap();
            if (data.count() == 1 && role == QLatin1String("modelData"))
                return data.first().toString();
            return data.value(role).toString();
        }
    }
    return QQmlDelegateModel::stringValue(index, role);
}

class QQuickComboBoxPrivate : public QQuickControlPrivate
{
    Q_DECLARE_PUBLIC(QQuickComboBox)

public:
    QQuickComboBoxPrivate() : pressed(false), ownModel(false), hasDisplayText(false),
        highlightedIndex(-1), currentIndex(-1), delegateModel(Q_NULLPTR),
        delegate(Q_NULLPTR), popup(Q_NULLPTR) { }

    bool isPopupVisible() const;
    void showPopup();
    void hidePopup(bool accept);
    void togglePopup(bool accept);

    void itemClicked();

    void initItem(int index, QObject *object);
    void countChanged();
    void updateCurrentText();
    void increase();
    void decrease();
    void setHighlightedIndex(int index);

    void createDelegateModel();

    bool pressed;
    bool ownModel;
    bool hasDisplayText;
    int highlightedIndex;
    int currentIndex;
    QVariant model;
    QString textRole;
    QString currentText;
    QString displayText;
    QQuickItem *pressedItem;
    QQmlInstanceModel *delegateModel;
    QQmlComponent *delegate;
    QQuickPopup *popup;
};

bool QQuickComboBoxPrivate::isPopupVisible() const
{
    return popup && popup->isVisible();
}

void QQuickComboBoxPrivate::showPopup()
{
    if (popup && !popup->isVisible())
        popup->open();
    setHighlightedIndex(currentIndex);
}

void QQuickComboBoxPrivate::hidePopup(bool accept)
{
    Q_Q(QQuickComboBox);
    if (popup && popup->isVisible())
        popup->close();
    if (accept) {
        q->setCurrentIndex(highlightedIndex);
        emit q->activated(currentIndex);
    }
    setHighlightedIndex(-1);
}

void QQuickComboBoxPrivate::togglePopup(bool accept)
{
    if (!popup)
        return;

    if (popup->isVisible())
        hidePopup(accept);
    else
        showPopup();
}

void QQuickComboBoxPrivate::itemClicked()
{
    Q_Q(QQuickComboBox);
    int index = delegateModel->indexOf(q->sender(), Q_NULLPTR);
    if (index != -1) {
        setHighlightedIndex(index);
        emit q->highlighted(index);
        hidePopup(true);
    }
}

void QQuickComboBoxPrivate::initItem(int index, QObject *object)
{
    QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
    if (button)
        connect(button, &QQuickAbstractButton::clicked, this, &QQuickComboBoxPrivate::itemClicked);

    if (index == currentIndex)
        updateCurrentText();
}

void QQuickComboBoxPrivate::countChanged()
{
    Q_Q(QQuickComboBox);
    if (q->count() == 0)
        q->setCurrentIndex(-1);
    emit q->countChanged();
}

void QQuickComboBoxPrivate::updateCurrentText()
{
    Q_Q(QQuickComboBox);
    QString text = q->textAt(currentIndex);
    if (currentText != text) {
        currentText = text;
        emit q->currentTextChanged();
    }
    if (!hasDisplayText && displayText != text) {
        displayText = text;
        emit q->displayTextChanged();
    }
}

void QQuickComboBoxPrivate::increase()
{
    Q_Q(QQuickComboBox);
    if (isPopupVisible()) {
        if (highlightedIndex < q->count() - 1) {
            setHighlightedIndex(highlightedIndex + 1);
            emit q->highlighted(highlightedIndex);
        }
    } else {
        if (currentIndex < q->count() - 1) {
            q->setCurrentIndex(currentIndex + 1);
            emit q->activated(currentIndex);
        }
    }
}

void QQuickComboBoxPrivate::decrease()
{
    Q_Q(QQuickComboBox);
    if (isPopupVisible()) {
        if (highlightedIndex > 0) {
            setHighlightedIndex(highlightedIndex - 1);
            emit q->highlighted(highlightedIndex);
        }
    } else {
        if (currentIndex > 0) {
            q->setCurrentIndex(currentIndex - 1);
            emit q->activated(currentIndex);
        }
    }
}

void QQuickComboBoxPrivate::setHighlightedIndex(int index)
{
    Q_Q(QQuickComboBox);
    if (highlightedIndex != index) {
        highlightedIndex = index;
        emit q->highlightedIndexChanged();
    }
}

void QQuickComboBoxPrivate::createDelegateModel()
{
    Q_Q(QQuickComboBox);
    bool ownedOldModel = ownModel;
    QQmlInstanceModel* oldModel = delegateModel;
    if (oldModel) {
        disconnect(delegateModel, &QQmlInstanceModel::countChanged, this, &QQuickComboBoxPrivate::countChanged);
        disconnect(delegateModel, &QQmlInstanceModel::modelUpdated, this, &QQuickComboBoxPrivate::updateCurrentText);
        disconnect(delegateModel, &QQmlInstanceModel::initItem, this, &QQuickComboBoxPrivate::initItem);
    }

    ownModel = false;
    delegateModel = model.value<QQmlInstanceModel *>();

    if (!delegateModel && model.isValid()) {
        QQmlDelegateModel *dataModel = new QQuickComboBoxDelegateModel(q);
        dataModel->setModel(model);
        dataModel->setDelegate(delegate);
        if (q->isComponentComplete())
            dataModel->componentComplete();

        ownModel = true;
        delegateModel = dataModel;
    }

    if (delegateModel) {
        connect(delegateModel, &QQmlInstanceModel::countChanged, this, &QQuickComboBoxPrivate::countChanged);
        connect(delegateModel, &QQmlInstanceModel::modelUpdated, this, &QQuickComboBoxPrivate::updateCurrentText);
        connect(delegateModel, &QQmlInstanceModel::initItem, this, &QQuickComboBoxPrivate::initItem);
    }

    emit q->delegateModelChanged();

    if (ownedOldModel)
        delete oldModel;
}

QQuickComboBox::QQuickComboBox(QQuickItem *parent) :
    QQuickControl(*(new QQuickComboBoxPrivate), parent)
{
    setActiveFocusOnTab(true);
    setFlag(QQuickItem::ItemIsFocusScope);
    setAcceptedMouseButtons(Qt::LeftButton);
}

QQuickComboBox::~QQuickComboBox()
{
    Q_D(QQuickComboBox);
    delete d->popup;
    d->popup = Q_NULLPTR;
}

/*!
    \readonly
    \qmlproperty int Qt.labs.controls::ComboBox::count

    This property holds the number of items in the combo box.
*/
int QQuickComboBox::count() const
{
    Q_D(const QQuickComboBox);
    return d->delegateModel ? d->delegateModel->count() : 0;
}

/*!
    \qmlproperty model Qt.labs.controls::ComboBox::model

    This property holds the model providing data for the combo box.

    \code
    ComboBox {
        textRole: "key"
        model: ListModel {
            ListElement { key: "First"; value: 123 }
            ListElement { key: "Second"; value: 456 }
            ListElement { key: "Third"; value: 789 }
        }
    }
    \endcode

    \sa textRole, {qml-data-models}{Data Models}
*/
QVariant QQuickComboBox::model() const
{
    Q_D(const QQuickComboBox);
    return d->model;
}

void QQuickComboBox::setModel(const QVariant& m)
{
    Q_D(QQuickComboBox);
    QVariant model = m;
    if (model.userType() == qMetaTypeId<QJSValue>())
        model = model.value<QJSValue>().toVariant();

    if (d->model != model) {
        d->model = model;
        d->createDelegateModel();
        if (isComponentComplete()) {
            setCurrentIndex(count() > 0 ? 0 : -1);
            d->updateCurrentText();
        }
        emit modelChanged();
    }
}

/*!
    \internal
    \qmlproperty model Qt.labs.controls::ComboBox::delegateModel

    This property holds the model providing delegate instances for the combo box.
*/
QQmlInstanceModel *QQuickComboBox::delegateModel() const
{
    Q_D(const QQuickComboBox);
    return d->delegateModel;
}

/*!
    \qmlproperty bool Qt.labs.controls::ComboBox::pressed

    This property holds whether the combo box button is pressed.
*/
bool QQuickComboBox::isPressed() const
{
    Q_D(const QQuickComboBox);
    return d->pressed;
}

void QQuickComboBox::setPressed(bool pressed)
{
    Q_D(QQuickComboBox);
    if (d->pressed != pressed) {
        d->pressed = pressed;
        emit pressedChanged();
    }
}

/*!
    \qmlproperty int Qt.labs.controls::ComboBox::highlightedIndex

    This property holds the index of the highlighted item in the combo box popup list.

    \sa highlighted(), currentIndex
*/
int QQuickComboBox::highlightedIndex() const
{
    Q_D(const QQuickComboBox);
    return d->highlightedIndex;
}

/*!
    \qmlproperty int Qt.labs.controls::ComboBox::currentIndex

    This property holds the index of the current item in the combo box.

    \sa activated(), currentText
*/
int QQuickComboBox::currentIndex() const
{
    Q_D(const QQuickComboBox);
    return d->currentIndex;
}

void QQuickComboBox::setCurrentIndex(int index)
{
    Q_D(QQuickComboBox);
    if (d->currentIndex != index) {
        d->currentIndex = index;
        emit currentIndexChanged();
        if (isComponentComplete())
            d->updateCurrentText();
    }
}

/*!
    \readonly
    \qmlproperty string Qt.labs.controls::ComboBox::currentText

    This property holds the text of the current item in the combo box.

    \sa currentIndex, displayText, textRole
*/
QString QQuickComboBox::currentText() const
{
    Q_D(const QQuickComboBox);
    return d->currentText;
}

/*!
    \qmlproperty string Qt.labs.controls::ComboBox::displayText

    This property holds the text that is displayed on the combo box button.

    By default, the display text presents the current selection. That is,
    it follows the text of the current item. However, the default display
    text can be overridden with a custom value.

    \code
    ComboBox {
        currentIndex: 1
        displayText: "Size: " + currentText
        model: ["S", "M", "L"]
    }
    \endcode

    \sa currentText, textRole
*/
QString QQuickComboBox::displayText() const
{
    Q_D(const QQuickComboBox);
    return d->displayText;
}

void QQuickComboBox::setDisplayText(const QString &text)
{
    Q_D(QQuickComboBox);
    d->hasDisplayText = true;
    if (d->displayText != text) {
        d->displayText = text;
        emit displayTextChanged();
    }
}

void QQuickComboBox::resetDisplayText()
{
    Q_D(QQuickComboBox);
    if (d->hasDisplayText) {
        d->hasDisplayText = false;
        d->updateCurrentText();
    }
}

/*!
    \qmlproperty string Qt.labs.controls::ComboBox::textRole

    This property holds the model role used for populating the combo box.

    \sa model, currentText, displayText
*/
QString QQuickComboBox::textRole() const
{
    Q_D(const QQuickComboBox);
    return d->textRole;
}

void QQuickComboBox::setTextRole(const QString &role)
{
    Q_D(QQuickComboBox);
    if (d->textRole != role) {
        d->textRole = role;
        if (isComponentComplete())
            d->updateCurrentText();
        emit textRoleChanged();
    }
}

/*!
    \qmlproperty Component Qt.labs.controls::ComboBox::delegate

    This property holds a delegate that presents an item in the combo box popup.

    \sa ItemDelegate, {Customizing ComboBox}
*/
QQmlComponent *QQuickComboBox::delegate() const
{
    Q_D(const QQuickComboBox);
    return d->delegate;
}

void QQuickComboBox::setDelegate(QQmlComponent* delegate)
{
    Q_D(QQuickComboBox);
    if (d->delegate != delegate) {
        delete d->delegate;
        d->delegate = delegate;
        QQmlDelegateModel *delegateModel = qobject_cast<QQmlDelegateModel*>(d->delegateModel);
        if (delegateModel)
            delegateModel->setDelegate(d->delegate);
        emit delegateChanged();
    }
}

/*!
    \qmlproperty Popup Qt.labs.controls::ComboBox::popup

    This property holds the popup.

    \sa {Customizing ComboBox}
*/
QQuickPopup *QQuickComboBox::popup() const
{
    Q_D(const QQuickComboBox);
    return d->popup;
}

void QQuickComboBox::setPopup(QQuickPopup *popup)
{
    Q_D(QQuickComboBox);
    if (d->popup != popup) {
        delete d->popup;
        if (popup)
            popup->setClosePolicy(QQuickPopup::OnEscape | QQuickPopup::OnPressOutsideParent);
        d->popup = popup;
        emit popupChanged();
    }
}

/*!
    \qmlmethod string Qt.labs.controls::ComboBox::textAt(int index)

    Returns the text for the specified \a index, or an empty string
    if the index is out of bounds.

    \sa textRole
*/
QString QQuickComboBox::textAt(int index) const
{
    Q_D(const QQuickComboBox);
    if (!d->delegateModel || index < 0 || index >= d->delegateModel->count() || !d->delegateModel->object(index))
        return QString();
    return d->delegateModel->stringValue(index, d->textRole.isEmpty() ? QStringLiteral("modelData") : d->textRole);
}

/*!
    \qmlmethod int Qt.labs.controls::ComboBox::find(string text, flags = Qt.MatchExactly)

    Returns the index of the specified \a text, or \c -1 if no match is found.

    The way the search is performed is defined by the specified match \a flags. By default,
    combo box performs case sensitive exact matching (\c Qt.MatchExactly). All other match
    types are case-insensitive unless the \c Qt.MatchCaseSensitive flag is also specified.

    \value Qt.MatchExactly          The search term matches exactly (default).
    \value Qt.MatchRegExp           The search term matches as a regular expression.
    \value Qt.MatchWildcard         The search term matches using wildcards.
    \value Qt.MatchFixedString      The search term matches as a fixed string.
    \value Qt.MatchStartsWith       The search term matches the start of the item.
    \value Qt.MatchEndsWidth        The search term matches the end of the item.
    \value Qt.MatchContains         The search term is contained in the item.
    \value Qt.MatchCaseSensitive    The search is case sensitive.

    \sa textRole
*/
int QQuickComboBox::find(const QString &text, Qt::MatchFlags flags) const
{
    int itemCount = count();
    uint matchType = flags & 0x0F;
    Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;

    for (int idx = 0; idx < itemCount; ++idx) {
        QString t = textAt(idx);
        switch (matchType) {
        case Qt::MatchExactly:
            if (t == text)
                return idx;
            break;
        case Qt::MatchRegExp:
            if (QRegExp(text, cs).exactMatch(t))
                return idx;
            break;
        case Qt::MatchWildcard:
            if (QRegExp(text, cs, QRegExp::Wildcard).exactMatch(t))
                return idx;
            break;
        case Qt::MatchStartsWith:
            if (t.startsWith(text, cs))
                return idx;
            break;
        case Qt::MatchEndsWith:
            if (t.endsWith(text, cs))
                return idx;
            break;
        case Qt::MatchFixedString:
            if (t.compare(text, cs) == 0)
                return idx;
            break;
        case Qt::MatchContains:
        default:
            if (t.contains(text, cs))
                return idx;
            break;
        }
    }
    return -1;
}

void QQuickComboBox::focusOutEvent(QFocusEvent *event)
{
    Q_D(QQuickComboBox);
    QQuickControl::focusOutEvent(event);
    d->hidePopup(false);
    setPressed(false);
}

void QQuickComboBox::keyPressEvent(QKeyEvent *event)
{
    Q_D(QQuickComboBox);
    QQuickControl::keyPressEvent(event);
    if (!d->popup)
        return;

    switch (event->key()) {
    case Qt::Key_Space:
        if (!event->isAutoRepeat())
            setPressed(true);
        event->accept();
        break;
    case Qt::Key_Enter:
    case Qt::Key_Return:
        if (d->isPopupVisible())
            setPressed(true);
        event->accept();
        break;
    case Qt::Key_Up:
        d->decrease();
        event->accept();
        break;
    case Qt::Key_Down:
        d->increase();
        event->accept();
        break;
    default:
        break;
    }
}

void QQuickComboBox::keyReleaseEvent(QKeyEvent *event)
{
    Q_D(QQuickComboBox);
    QQuickControl::keyReleaseEvent(event);
    if (!d->popup || event->isAutoRepeat())
        return;

    switch (event->key()) {
    case Qt::Key_Space:
        d->togglePopup(true);
        setPressed(false);
        event->accept();
        break;
    case Qt::Key_Enter:
    case Qt::Key_Return:
        d->hidePopup(true);
        setPressed(false);
        event->accept();
        break;
    case Qt::Key_Escape:
        d->hidePopup(false);
        setPressed(false);
        event->accept();
        break;
    default:
        break;
    }
}

void QQuickComboBox::mousePressEvent(QMouseEvent *event)
{
    QQuickControl::mousePressEvent(event);
    setPressed(true);
}

void QQuickComboBox::mouseMoveEvent(QMouseEvent* event)
{
    QQuickControl::mouseMoveEvent(event);
    setPressed(contains(event->pos()));
}

void QQuickComboBox::mouseReleaseEvent(QMouseEvent *event)
{
    Q_D(QQuickComboBox);
    QQuickControl::mouseReleaseEvent(event);
    if (d->pressed) {
        setPressed(false);
        if (!d->isPopupVisible())
            forceActiveFocus(Qt::MouseFocusReason);
        d->togglePopup(false);
    }
}

void QQuickComboBox::mouseUngrabEvent()
{
    QQuickControl::mouseUngrabEvent();
    setPressed(false);
}

void QQuickComboBox::componentComplete()
{
    Q_D(QQuickComboBox);
    QQuickControl::componentComplete();

    if (d->delegateModel && d->ownModel)
        static_cast<QQmlDelegateModel *>(d->delegateModel)->componentComplete();

    if (count() > 0) {
        if (d->currentIndex == -1)
            setCurrentIndex(0);
        else
            d->updateCurrentText();
    }
}

QFont QQuickComboBox::defaultFont() const
{
    return QQuickControlPrivate::themeFont(QPlatformTheme::ComboMenuItemFont);
}

QT_END_NAMESPACE