diff options
Diffstat (limited to 'src/quicktemplates2/qquickcombobox.cpp')
-rw-r--r-- | src/quicktemplates2/qquickcombobox.cpp | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/src/quicktemplates2/qquickcombobox.cpp b/src/quicktemplates2/qquickcombobox.cpp index 55ff0e55..b4d9bead 100644 --- a/src/quicktemplates2/qquickcombobox.cpp +++ b/src/quicktemplates2/qquickcombobox.cpp @@ -37,6 +37,7 @@ #include "qquickcombobox_p.h" #include "qquickcontrol_p_p.h" #include "qquickabstractbutton_p.h" +#include "qquickabstractbutton_p_p.h" #include "qquickpopup_p_p.h" #include "qquickdeferredexecute_p_p.h" @@ -51,6 +52,7 @@ #include <QtQml/private/qqmldelegatemodel_p.h> #include <QtQuick/private/qquickevents_p_p.h> #include <QtQuick/private/qquicktextinput_p.h> +#include <QtQuick/private/qquickitemview_p.h> QT_BEGIN_NAMESPACE @@ -226,6 +228,7 @@ public: void popupVisibleChanged(); void itemClicked(); + void itemHovered(); void createdItem(int index, QObject *object); void modelUpdated(); @@ -264,6 +267,7 @@ public: bool hasDown; bool pressed; bool ownModel; + bool keyNavigating; bool hasDisplayText; bool hasCurrentIndex; int highlightedIndex; @@ -302,6 +306,7 @@ QQuickComboBoxPrivate::QQuickComboBoxPrivate() hasDown(false), pressed(false), ownModel(false), + keyNavigating(false), hasDisplayText(false), hasCurrentIndex(false), highlightedIndex(-1), @@ -352,7 +357,15 @@ void QQuickComboBoxPrivate::popupVisibleChanged() if (isPopupVisible()) QGuiApplication::inputMethod()->reset(); + QQuickItemView *itemView = popup->findChild<QQuickItemView *>(); + if (itemView) + itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange); + updateHighlightedIndex(); + + if (itemView) + itemView->positionViewAtIndex(highlightedIndex, QQuickItemView::Beginning); + if (!hasDown) { q->setDown(pressed || isPopupVisible()); hasDown = false; @@ -369,6 +382,25 @@ void QQuickComboBoxPrivate::itemClicked() } } +void QQuickComboBoxPrivate::itemHovered() +{ + Q_Q(QQuickComboBox); + if (keyNavigating) + return; + + QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender()); + if (!button || !button->isHovered() || QQuickAbstractButtonPrivate::get(button)->touchId != -1) + return; + + int index = delegateModel->indexOf(button, nullptr); + if (index != -1) { + setHighlightedIndex(index, Highlight); + + if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>()) + itemView->positionViewAtIndex(index, QQuickItemView::Contain); + } +} + void QQuickComboBoxPrivate::createdItem(int index, QObject *object) { Q_Q(QQuickComboBox); @@ -385,6 +417,7 @@ void QQuickComboBoxPrivate::createdItem(int index, QObject *object) if (button) { button->setFocusPolicy(Qt::NoFocus); connect(button, &QQuickAbstractButton::clicked, this, &QQuickComboBoxPrivate::itemClicked); + connect(button, &QQuickAbstractButton::hoveredChanged, this, &QQuickComboBoxPrivate::itemHovered); } if (index == currentIndex && !q->isEditable()) @@ -1081,6 +1114,9 @@ void QQuickComboBox::setPopup(QQuickPopup *popup) QQuickPopupPrivate::get(popup)->allowVerticalFlip = true; popup->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent); QObjectPrivate::connect(popup, &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged); + + if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>()) + itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange); } d->popup = popup; if (!d->popup.isExecuting()) @@ -1449,9 +1485,12 @@ bool QQuickComboBox::eventFilter(QObject *object, QEvent *event) d->hidePopup(false); break; case QEvent::KeyPress: { - const int key = static_cast<QKeyEvent *>(event)->key(); + QKeyEvent *ke = static_cast<QKeyEvent *>(event); + if (d->filterKeyEvent(ke, false)) + return true; + event->accept(); if (d->extra.isAllocated()) - d->extra->allowComplete = key != Qt::Key_Backspace && key != Qt::Key_Delete; + d->extra->allowComplete = ke->key() != Qt::Key_Backspace && ke->key() != Qt::Key_Delete; break; } case QEvent::FocusOut: @@ -1521,14 +1560,17 @@ void QQuickComboBox::keyPressEvent(QKeyEvent *event) event->accept(); break; case Qt::Key_Up: + d->keyNavigating = true; d->decrementCurrentIndex(); event->accept(); break; case Qt::Key_Down: + d->keyNavigating = true; d->incrementCurrentIndex(); event->accept(); break; case Qt::Key_Home: + d->keyNavigating = true; if (d->isPopupVisible()) d->setHighlightedIndex(0, Highlight); else @@ -1536,6 +1578,7 @@ void QQuickComboBox::keyPressEvent(QKeyEvent *event) event->accept(); break; case Qt::Key_End: + d->keyNavigating = true; if (d->isPopupVisible()) d->setHighlightedIndex(count() - 1, Highlight); else @@ -1555,6 +1598,7 @@ void QQuickComboBox::keyReleaseEvent(QKeyEvent *event) { Q_D(QQuickComboBox); QQuickControl::keyReleaseEvent(event); + d->keyNavigating = false; if (event->isAutoRepeat()) return; @@ -1658,6 +1702,11 @@ QFont QQuickComboBox::defaultFont() const return QQuickControlPrivate::themeFont(QPlatformTheme::ComboMenuItemFont); } +QPalette QQuickComboBox::defaultPalette() const +{ + return QQuickControlPrivate::themePalette(QPlatformTheme::ComboBoxPalette); +} + #if QT_CONFIG(accessibility) QAccessible::Role QQuickComboBox::accessibleRole() const { |