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 
42  : ComboBox( parent )
43 {
44  setEditable( true );
45  completer()->setCompletionMode( QCompleter::PopupCompletion );
46  completer()->setCaseSensitivity( Qt::CaseInsensitive );
47 }
48 
49 QString findMaxCommonPrefix( const QStringList& strings )
50 {
51  if( strings.size() == 0 )
52  {
53  return "";
54  }
55  if( strings.size() == 1 )
56  {
57  return strings[ 0 ];
58  }
59  QString common_prefix;
60  int char_index = 0;
61 
62  // loop over character index
63  while( true )
64  {
65  if( char_index >= strings[ 0 ].size() )
66  {
67  return common_prefix;
68  }
69  const QChar c = strings[ 0 ][ char_index ];
70 
71  // loop over strings
72  for( int string_index = 1; string_index < strings.size(); string_index++ )
73  {
74  const QString& str = strings[ string_index ];
75  if( char_index >= str.size() ||
76  str[ char_index ] != c )
77  {
78  return common_prefix;
79  }
80  }
81  common_prefix += c;
82  char_index++;
83  }
84  return ""; // just to satisfy compiler... I know it will never reach this.
85 }
86 
88 {
89  if( event->type() == QEvent::KeyPress )
90  {
91  QKeyEvent* k = (QKeyEvent*) event;
92  if( k->key() == Qt::Key_Tab && k->modifiers() == Qt::NoModifier )
93  {
94  QCompleter* comp = completer();
95 
96  QStringList completions;
97  for( int i = 0; comp->setCurrentRow( i ); i++ )
98  {
99  completions.push_back( comp->currentCompletion() );
100  }
101  QString max_common_prefix = findMaxCommonPrefix( completions );
102  if( max_common_prefix.size() > currentText().size() )
103  {
104  setEditText( max_common_prefix );
105  lineEdit()->setCursorPosition( max_common_prefix.size() );
106  }
107 
108  event->accept();
109  return true;
110  }
111  }
112  return ComboBox::event( event );
113 }
114 
115 } // end namespace rviz
QString findMaxCommonPrefix(const QStringList &strings)
virtual bool event(QEvent *event)
If event is a tab key press, set the edit text to the longest common prefix from the completer...
QString currentText
Definition: combo_box.h:40
EditableComboBox(QWidget *parent=0)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:41