MatchFilterComboBox.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <QAbstractItemView>
20 #include <QKeyEvent>
21 #include <QLineEdit>
22 
24 
25 namespace rqt_multiplot {
26 
27 /*****************************************************************************/
28 /* Constructors and Destructor */
29 /*****************************************************************************/
30 
32  QComboBox(parent),
33  matchFilterCompleter_(new MatchFilterCompleter(this, Qt::MatchContains)) {
34  connect(matchFilterCompleter_, SIGNAL(activated(const QString&)), this,
35  SLOT(matchFilterCompleterActivated(const QString&)));
36 }
37 
39 }
40 
41 /*****************************************************************************/
42 /* Accessors */
43 /*****************************************************************************/
44 
45 void MatchFilterComboBox::setEditable(bool editable) {
46  if (editable != QComboBox::isEditable()) {
47  QComboBox::setEditable(editable);
48 
49  if (lineEdit()) {
50  matchFilterCompleter_->setModel(model());
51  matchFilterCompleter_->setWidget(this);
52 
53  connect(lineEdit(), SIGNAL(editingFinished()), this,
54  SLOT(lineEditEditingFinished()));
55  }
56  else
57  matchFilterCompleter_->setModel(model());
58  }
59 }
60 
62  return matchFilterCompleter_;
63 }
64 
65 /*****************************************************************************/
66 /* Methods */
67 /*****************************************************************************/
68 
69 void MatchFilterComboBox::keyPressEvent(QKeyEvent* event) {
70  bool doComplete = (count() >= 0);
71 
72  if (matchFilterCompleter_->popup()->isVisible()) {
73  switch (event->key()) {
74  case Qt::Key_Escape:
75  case Qt::Key_Tab:
76  case Qt::Key_Backtab:
77  event->ignore();
78  return;
79  case Qt::Key_Enter:
80  case Qt::Key_Return:
81  if (matchFilterCompleter_->popup()->currentIndex().isValid()) {
82  event->ignore();
83  return;
84  }
85  else {
86  matchFilterCompleter_->popup()->hide();
87  doComplete = false;
88  }
89  }
90  }
91 
92  bool isShortcut = (event->modifiers() & Qt::ControlModifier) &&
93  (event->key() == Qt::Key_E);
94  bool ctrlOrShift = event->modifiers() &
95  (Qt::ControlModifier | Qt::ShiftModifier);
96 
97  if (!isShortcut)
98  QComboBox::keyPressEvent(event);
99 
100  if (!isShortcut && !ctrlOrShift && (event->modifiers() != Qt::NoModifier)) {
101  matchFilterCompleter_->popup()->hide();
102  return;
103  }
104 
105  if (doComplete) {
106  matchFilterCompleter_->setCompletionPrefix(currentText());
107  matchFilterCompleter_->complete();
108  matchFilterCompleter_->popup()->setCurrentIndex(QModelIndex());
109  }
110 }
111 
112 /*****************************************************************************/
113 /* Slots */
114 /*****************************************************************************/
115 
117  setEditText(text);
118  lineEdit()->selectAll();
119 
120  setCurrentIndex(findText(text));
121 
122  matchFilterCompleter_->popup()->hide();
123 }
124 
126  if (!matchFilterCompleter_->popup()->isVisible()) {
127  int index = findText(currentText());
128 
129  if (index < 0)
130  setEditText(currentText());
131  else
132  setCurrentIndex(index);
133  }
134  else
135  matchFilterCompleter_->popup()->hide();
136 }
137 
138 }
MatchFilterCompleter * matchFilterCompleter_
MatchFilterCompleter * getMatchFilterCompleter() const
void matchFilterCompleterActivated(const QString &text)


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44