editable_combo_box.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <stdio.h>
31 
32 #include <QCompleter>
33 #include <QKeyEvent>
34 #include <QLineEdit>
35 
37 
38 namespace rviz
39 {
40 EditableComboBox::EditableComboBox(QWidget* parent) : ComboBox(parent)
41 {
42  setEditable(true);
43  completer()->setCompletionMode(QCompleter::PopupCompletion);
44  completer()->setCaseSensitivity(Qt::CaseInsensitive);
45 }
46 
47 QString findMaxCommonPrefix(const QStringList& strings)
48 {
49  if (strings.empty())
50  {
51  return "";
52  }
53  if (strings.size() == 1)
54  {
55  return strings[0];
56  }
57  QString common_prefix;
58  int char_index = 0;
59 
60  // loop over character index
61  while (true)
62  {
63  if (char_index >= strings[0].size())
64  {
65  return common_prefix;
66  }
67  const QChar c = strings[0][char_index];
68 
69  // loop over strings
70  for (int string_index = 1; string_index < strings.size(); string_index++)
71  {
72  const QString& str = strings[string_index];
73  if (char_index >= str.size() || str[char_index] != c)
74  {
75  return common_prefix;
76  }
77  }
78  common_prefix += c;
79  char_index++;
80  }
81  return ""; // just to satisfy compiler... I know it will never reach this.
82 }
83 
85 {
86  if (event->type() == QEvent::KeyPress)
87  {
88  QKeyEvent* k = (QKeyEvent*)event;
89  if (k->key() == Qt::Key_Tab && k->modifiers() == Qt::NoModifier)
90  {
91  QCompleter* comp = completer();
92 
93  QStringList completions;
94  for (int i = 0; comp->setCurrentRow(i); i++)
95  {
96  completions.push_back(comp->currentCompletion());
97  }
98  QString max_common_prefix = findMaxCommonPrefix(completions);
99  if (max_common_prefix.size() > currentText().size())
100  {
101  setEditText(max_common_prefix);
102  lineEdit()->setCursorPosition(max_common_prefix.size());
103  }
104 
105  event->accept();
106  return true;
107  }
108  }
109  return ComboBox::event(event);
110 }
111 
112 } // end namespace rviz
QString findMaxCommonPrefix(const QStringList &strings)
bool event(QEvent *event) override
If event is a tab key press, set the edit text to the longest common prefix from the completer...
EditableComboBox(QWidget *parent=nullptr)
QString currentText
Definition: combo_box.h:39


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Aug 15 2022 02:05:16