summaryrefslogtreecommitdiff
path: root/src/libs/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/utils')
-rw-r--r--src/libs/utils/treeviewcombobox.cpp67
-rw-r--r--src/libs/utils/treeviewcombobox.h5
2 files changed, 63 insertions, 9 deletions
diff --git a/src/libs/utils/treeviewcombobox.cpp b/src/libs/utils/treeviewcombobox.cpp
index 804b0e6eb8..dc6e5c5fbd 100644
--- a/src/libs/utils/treeviewcombobox.cpp
+++ b/src/libs/utils/treeviewcombobox.cpp
@@ -57,18 +57,41 @@ TreeViewComboBox::TreeViewComboBox(QWidget *parent)
m_view->viewport()->installEventFilter(this);
}
+QModelIndex TreeViewComboBox::indexAbove(QModelIndex index)
+{
+ do
+ index = m_view->indexAbove(index);
+ while (index.isValid() && !(model()->flags(index) & Qt::ItemIsSelectable));
+ return index;
+}
+
+QModelIndex TreeViewComboBox::indexBelow(QModelIndex index)
+{
+ do
+ index = m_view->indexBelow(index);
+ while (index.isValid() && !(model()->flags(index) & Qt::ItemIsSelectable));
+ return index;
+}
+
+QModelIndex TreeViewComboBox::lastIndex(const QModelIndex index)
+{
+ if (index.isValid() && !m_view->isExpanded(index))
+ return index;
+
+ int rows = m_view->model()->rowCount(index);
+ if (rows == 0)
+ return index;
+ return lastIndex(m_view->model()->index(rows - 1, 0, index));
+}
+
void TreeViewComboBox::wheelEvent(QWheelEvent *e)
{
QModelIndex index = m_view->currentIndex();
- if (e->delta() > 0) {
- do
- index = m_view->indexAbove(index);
- while (index.isValid() && !(model()->flags(index) & Qt::ItemIsSelectable));
- } else if (e->delta() < 0) {
- do
- index = m_view->indexBelow(index);
- while (index.isValid() && !(model()->flags(index) & Qt::ItemIsSelectable));
- }
+ if (e->delta() > 0)
+ index = indexAbove(index);
+ else if (e->delta() < 0)
+ index = indexBelow(index);
+
e->accept();
if (!index.isValid())
return;
@@ -79,8 +102,34 @@ void TreeViewComboBox::wheelEvent(QWheelEvent *e)
emit activated(index.row());
}
+void TreeViewComboBox::keyPressEvent(QKeyEvent *e)
+{
+ if (e->key() == Qt::Key_Up || e->key() == Qt::Key_PageUp) {
+ setCurrentIndex(indexAbove(m_view->currentIndex()));
+ } else if (e->key() == Qt::Key_Down || e->key() == Qt::Key_PageDown) {
+ setCurrentIndex(indexBelow(m_view->currentIndex()));
+ } else if (e->key() == Qt::Key_Home) {
+ QModelIndex index = m_view->model()->index(0, 0);
+ if (index.isValid() && !model()->flags(index) & Qt::ItemIsSelectable)
+ index = indexBelow(index);
+ setCurrentIndex(index);
+ } else if (e->key() == Qt::Key_End) {
+ QModelIndex index = lastIndex(m_view->rootIndex());
+ if (index.isValid() && !model()->flags(index) & Qt::ItemIsSelectable)
+ index = indexAbove(index);
+ setCurrentIndex(index);
+ } else {
+ QComboBox::keyPressEvent(e);
+ return;
+ }
+
+ e->accept();
+}
+
void TreeViewComboBox::setCurrentIndex(const QModelIndex &index)
{
+ if (!index.isValid())
+ return;
setRootModelIndex(model()->parent(index));
QComboBox::setCurrentIndex(index.row());
setRootModelIndex(QModelIndex());
diff --git a/src/libs/utils/treeviewcombobox.h b/src/libs/utils/treeviewcombobox.h
index f2dabb5792..543a237d92 100644
--- a/src/libs/utils/treeviewcombobox.h
+++ b/src/libs/utils/treeviewcombobox.h
@@ -52,6 +52,7 @@ public:
TreeViewComboBox(QWidget *parent = 0);
void wheelEvent(QWheelEvent *e);
+ void keyPressEvent(QKeyEvent *e);
void setCurrentIndex(const QModelIndex &index);
bool eventFilter(QObject* object, QEvent* event);
void showPopup();
@@ -60,6 +61,10 @@ public:
TreeViewComboBoxView *view() const;
private:
+ QModelIndex indexBelow(QModelIndex index);
+ QModelIndex indexAbove(QModelIndex index);
+ QModelIndex lastIndex(const QModelIndex index);
+
TreeViewComboBoxView *m_view;
bool m_skipNextHide;
};