00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 import threading
00033
00034 from python_qt_binding.QtCore import Qt, Signal
00035 from python_qt_binding.QtGui import QStandardItem
00036
00037 from rqt_py_common.message_tree_model import MessageTreeModel
00038 from rqt_py_common.data_items import ReadonlyItem, CheckableItem
00039
00040
00041 class PublisherTreeModel(MessageTreeModel):
00042 _column_names = ['topic', 'type', 'rate', 'expression']
00043 item_value_changed = Signal(int, str, str, str, object)
00044
00045 def __init__(self, parent=None):
00046 super(PublisherTreeModel, self).__init__(parent)
00047 self._column_index = {}
00048 for column_name in self._column_names:
00049 self._column_index[column_name] = len(self._column_index)
00050 self.clear()
00051
00052 self._item_change_lock = threading.Lock()
00053 self.itemChanged.connect(self.handle_item_changed)
00054
00055 def clear(self):
00056 super(PublisherTreeModel, self).clear()
00057 self.setHorizontalHeaderLabels(self._column_names)
00058
00059 def get_publisher_ids(self, index_list):
00060 return [item._user_data['publisher_id'] for item in self._get_toplevel_items(index_list)]
00061
00062 def remove_items_with_parents(self, index_list):
00063 for item in self._get_toplevel_items(index_list):
00064 self.removeRow(item.row())
00065
00066 def handle_item_changed(self, item):
00067 if not self._item_change_lock.acquire(False):
00068
00069 return
00070
00071 topic_name = item._path
00072 column_name = self._column_names[item.column()]
00073 if item.isCheckable():
00074 new_value = str(item.checkState() == Qt.Checked)
00075 else:
00076 new_value = item.text().strip()
00077
00078
00079 self.item_value_changed.emit(item._user_data['publisher_id'], topic_name, column_name, new_value, item.setText)
00080
00081
00082 self._item_change_lock.release()
00083
00084 def remove_publisher(self, publisher_id):
00085 for top_level_row_number in range(self.rowCount()):
00086 item = self.item(top_level_row_number)
00087 if item is not None and item._user_data['publisher_id'] == publisher_id:
00088 self.removeRow(top_level_row_number)
00089 return top_level_row_number
00090 return None
00091
00092 def update_publisher(self, publisher_info):
00093 top_level_row_number = self.remove_publisher(publisher_info['publisher_id'])
00094 self.add_publisher(publisher_info, top_level_row_number)
00095
00096 def add_publisher(self, publisher_info, top_level_row_number=None):
00097
00098 parent = self
00099 slot = publisher_info['message_instance']
00100 slot_name = publisher_info['topic_name']
00101 slot_type_name = publisher_info['message_instance']._type
00102 slot_path = publisher_info['topic_name']
00103 user_data = {'publisher_id': publisher_info['publisher_id']}
00104 kwargs = {
00105 'user_data': user_data,
00106 'top_level_row_number': top_level_row_number,
00107 'expressions': publisher_info['expressions'],
00108 }
00109 top_level_row = self._recursive_create_items(parent, slot, slot_name, slot_type_name, slot_path, **kwargs)
00110
00111
00112 if publisher_info['enabled']:
00113 top_level_row[self._column_index['topic']].setCheckState(Qt.Checked)
00114 top_level_row[self._column_index['rate']].setText(str(publisher_info['rate']))
00115
00116 def _get_data_items_for_path(self, slot_name, slot_type_name, slot_path, **kwargs):
00117 if slot_name.startswith('/'):
00118 return (CheckableItem(slot_name), ReadonlyItem(slot_type_name), QStandardItem(''), ReadonlyItem(''))
00119 expression_item = QStandardItem('')
00120 expression_item.setToolTip('enter valid Python expression here, using "i" as counter and functions from math, random and time modules')
00121 return (ReadonlyItem(slot_name), QStandardItem(slot_type_name), ReadonlyItem(''), expression_item)
00122
00123 def _recursive_create_items(self, parent, slot, slot_name, slot_type_name, slot_path, expressions={}, **kwargs):
00124 row, is_leaf_node = super(PublisherTreeModel, self)._recursive_create_items(parent, slot, slot_name, slot_type_name, slot_path, expressions=expressions, **kwargs)
00125 if is_leaf_node:
00126 expression_text = expressions.get(slot_path, repr(slot))
00127 row[self._column_index['expression']].setText(expression_text)
00128 return row