topic_completer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of the TU Darmstadt nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import roslib
34 import rospy
35 
36 from python_qt_binding.QtCore import qWarning
37 
38 from .message_tree_model import MessageTreeModel
39 from .tree_model_completer import TreeModelCompleter
40 
41 
42 class TopicCompleter(TreeModelCompleter):
43 
44  def __init__(self, parent=None):
45  super(TopicCompleter, self).__init__(parent)
46  self.setModel(MessageTreeModel())
47 
48  def splitPath(self, path):
49  # to handle array subscriptions, e.g. /topic/field[1]/subfield[2]
50  # we need to separate array subscriptions by an additional /
51  return super(TopicCompleter, self).splitPath(path.replace('[', '/['))
52 
53  def update_topics(self):
54  self.model().clear()
55  topic_list = rospy.get_published_topics()
56  for topic_path, topic_type in topic_list:
57  topic_name = topic_path.strip('/')
58  message_class = roslib.message.get_message_class(topic_type)
59  if message_class is None:
60  qWarning('TopicCompleter.update_topics(): '
61  'could not get message class for topic type "%s" on topic "%s"' %
62  (topic_type, topic_path))
63  continue
64  message_instance = message_class()
65  self.model().add_message(message_instance, topic_name, topic_type, topic_path)
66 
67 
68 if __name__ == '__main__':
69  import sys
70  from python_qt_binding.QtGui import QApplication, QComboBox, QLineEdit, QMainWindow, QTreeView, QVBoxLayout, QWidget
71  app = QApplication(sys.argv)
72  mw = QMainWindow()
73  widget = QWidget(mw)
74  layout = QVBoxLayout(widget)
75 
76  edit = QLineEdit()
77  edit_completer = TopicCompleter(edit)
78  # edit_completer.setCompletionMode(QCompleter.InlineCompletion)
79  edit.setCompleter(edit_completer)
80 
81  combo = QComboBox()
82  combo.setEditable(True)
83  combo_completer = TopicCompleter(combo)
84  # combo_completer.setCompletionMode(QCompleter.InlineCompletion)
85  combo.lineEdit().setCompleter(combo_completer)
86 
87  model_tree = QTreeView()
88  model_tree.setModel(combo_completer.model())
89  model_tree.expandAll()
90  for column in range(combo_completer.model().columnCount()):
91  model_tree.resizeColumnToContents(column)
92 
93  completion_tree = QTreeView()
94  completion_tree.setModel(combo_completer.completionModel())
95  completion_tree.expandAll()
96  for column in range(combo_completer.completionModel().columnCount()):
97  completion_tree.resizeColumnToContents(column)
98 
99  layout.addWidget(model_tree)
100  layout.addWidget(completion_tree)
101  layout.addWidget(edit)
102  layout.addWidget(combo)
103  layout.setStretchFactor(model_tree, 1)
104  widget.setLayout(layout)
105  mw.setCentralWidget(widget)
106 
107  mw.move(300, 0)
108  mw.resize(800, 900)
109  mw.show()
110  app.exec_()


rqt_py_common
Author(s): Dorian Scholz, Isaac Saito, Dirk Thomas
autogenerated on Mon Mar 22 2021 02:13:26