def index(self, row, column, parent): """ Returns the index of an item at the given column/row. :param row: the index of the row :type row: int :param column: the index of the column :type column: int :param parent: the parent :type parent: QModelIndex :returns: the index :rtype: QModelIndex """ if not self.hasIndex(row, column, parent): return QModelIndex() if not parent.isValid(): parent_item = self.__root_item else: parent_item = parent.internalPointer() if isinstance(parent_item, TreeTopicItem): child_item = parent_item.get_child(row, self.parent(parent).internalPointer()) else: child_item = parent_item.get_child(row) if child_item: return self.createIndex(row, column, child_item) else: return QModelIndex()
def remove_rows(self, rowlist): """ :param rowlist: list of row indexes, ''list(int)'' :returns: True if the indexes were removed successfully, ''bool'' """ if len(rowlist) == 0: if len(self._messages) > 0: try: self.beginRemoveRows(QModelIndex(), 0, len(self._messages)) del self._messages[0:len(self._messages)] self.endRemoveRows() except: return False else: rowlist = list(set(rowlist)) rowlist.sort(reverse=True) dellist = [rowlist[0]] for row in rowlist[1:]: if dellist[-1] - 1 > row: try: self.beginRemoveRows(QModelIndex(), dellist[-1], dellist[0]) del self._messages[dellist[-1]:dellist[0] + 1] self.endRemoveRows() except: return False dellist = [] dellist.append(row) if len(dellist) > 0: try: self.beginRemoveRows(QModelIndex(), dellist[-1], dellist[0]) del self._messages[dellist[-1]:dellist[0] + 1] self.endRemoveRows() except: return False return True
def _get_hierarchy_level(model_index): hierarchyLevel = 1 seek_root = model_index while(seek_root.parent() != QModelIndex()): seek_root = seek_root.parent() hierarchyLevel += 1 return hierarchyLevel
def load_from_file(self, filehandle): """ Saves to an already open filehandle. :returns: True if loaded successfully, ''bool'' OR :returns: False if load fails, ''bool'' """ line = filehandle.readline() lines = [] if line == self._messages.header_print(): while 1: line = filehandle.readline() if not line: break while line[-2] != "\"": newline = filehandle.readline() if not line: qWarning('File does not appear to be a rqt_console message file: missing " at end of file') return False line = line + newline lines.append(line) self.beginInsertRows(QModelIndex(), len(self._messages.get_message_list()), len(self._messages.get_message_list()) + len(lines) - 1) for line in lines: try: self._messages.append_from_text(line) except ValueError: qWarning('File does not appear to be a rqt_console message file: File line is malformed %s' % line) self.endInsertRows() self._paused = True return True else: qWarning('File does not appear to be a rqt_console message file: missing file header.') return False
def insert_row(self, msg, notify_model=True): if notify_model: self.beginInsertRows(QModelIndex(), len(self._messages.get_message_list()), len(self._messages.get_message_list())) self._messages.add_message(msg) if notify_model: self.endInsertRows()
def _enumerate_indexes(self, parent=QModelIndex()): model = self.selectionModel.model() for row in range(0, model.rowCount(parent)): index = model.index(row, 0, parent) yield index if model.hasChildren(index): for child in self._enumerate_indexes(index): yield child
def insert_row(self, entry: Entry): """Appends an entry. :param entry: Entry to append to rows """ self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) self._entries.append(entry) self.endInsertRows()
def remove_rows(self, positions: List[int]): """Removes the rows with given indices. :param positions: positions to remove """ for row in sorted(positions, reverse=True): if 0 <= row < self.rowCount(): self.beginRemoveRows(QModelIndex(), row, row) del self._entries[row] self.endRemoveRows()
def insert_rows(self, msgs): # never try to insert more message than the limit if len(msgs) > self._message_limit: msgs = msgs[-self._message_limit:] # reduce model before insert limit = self._message_limit - len(msgs) self._enforce_message_limit(limit) # insert newest messages self.beginInsertRows(QModelIndex(), 0, len(msgs) - 1) self._messages.extend(msgs) self.endInsertRows()
def insert_rows(self, msgs): """ Wraps the insert_row function to minimize gui notification calls """ if len(msgs) == 0: return self.beginInsertRows(QModelIndex(), len(self._messages.get_message_list()), len(self._messages.get_message_list()) + len(msgs) - 1) for msg in msgs: self.insert_row(msg, False) self.endInsertRows() self.manage_message_limit()
def _create_widgets_for_launchfile(self, config): self._config = config # Delete old nodes' GUIs. self._node_controllers = [] # These lines seem to remove indexWidgets previously set on treeview. # Per suggestion in API doc, we are not using reset(). Instead, # using 2 methods below without any operation in between, which # I suspect might be inproper. # http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html#reset self._datamodel.beginResetModel() self._datamodel.endResetModel() # Need to store the num of nodes outside of the loop -- this will # be used for removing excessive previous node rows. order_xmlelement = 0 # Loop per xml element for order_xmlelement, node in enumerate(self._config.nodes): proxy = NodeProxy(self._run_id, self._config.master.uri, node) # TODO: consider using QIcon.fromTheme() status_label = StatusIndicator() qindex_nodewidget = self._datamodel.index(order_xmlelement, 0, QModelIndex()) node_widget = self._delegate.create_node_widget(qindex_nodewidget, proxy.config, status_label) #TODO: Ideally find a way so that we don't need this block. #BEGIN If these lines are missing, widget won't be shown either. std_item = QStandardItem( #node_widget.get_node_name() ) self._datamodel.setItem(order_xmlelement, 0, std_item) #END If these lines are missing, widget won't be shown either. self._treeview.setIndexWidget(qindex_nodewidget, node_widget) node_controller = NodeController(proxy, node_widget) self._node_controllers.append(node_controller) node_widget.connect_start_stop_button( \ node_controller.start_stop_slot) rospy.logdebug('loop #%d proxy.config.namespace=%s ' + 'proxy.config.name=%s', order_xmlelement, proxy.config.namespace, proxy.config.name) self._num_nodes_previous = order_xmlelement self._parent.set_node_controllers(self._node_controllers)
def remove_rows(self, position, rows=1): """Removes the rows with given indices. :type position: int :param position: Start position to remove :type rows: int :param rows: Amount of rows to remove :returns True when the removal was successful """ self.beginRemoveRows(QModelIndex(), position, position + rows - 1) self._entries = self._entries[:position] + self._entries[position + rows:] self.endRemoveRows()
def parent(self, index): """ Returns the QModelIndex of the parent from the child item specified via its index. :param index: the index of the child :type index: QModelIndex :returns: the parent :rtype: QModelIndex """ if not index.isValid(): return QModelIndex() child_item = index.internalPointer() parent_item = child_item.parent() if parent_item == self.__root_item: return QModelIndex() # row of treetopicItems depends on their position in the model.. if isinstance(parent_item, TreeTopicItem): return self.createIndex(parent_item.row(parent_item.parent()), 0, parent_item) return self.createIndex(parent_item.row(), 0, parent_item)
def _create_widgets_for_launchfile(self, config): self._config = config # Delete old nodes' GUIs. del self._node_controllers[:] self._delegate.clear_node_widgets() # reset the data model self._datamodel.clear() self._datamodel.setColumnCount(1) self._datamodel.setRowCount(len(self._config.nodes)) # Loop per xml element for i, node in enumerate(self._config.nodes): proxy = NodeProxy(self._run_id, self._config.master.uri, node) nodewidget_index = self._datamodel.index(i, 0, QModelIndex()) node_widget = self._delegate.create_node_widget( nodewidget_index, proxy.config, StatusIndicator() ) # TODO: use tree view delegates correctly instead of # empty QStandardItemModel self._datamodel.setItem(i, QStandardItem()) self._treeview.setIndexWidget(nodewidget_index, node_widget) node_controller = NodeController(proxy, node_widget) self._node_controllers.append(node_controller) rospy.logdebug( 'loop #%d proxy.config.namespace=%s ' + 'proxy.config.name=%s', i, proxy.config.namespace, proxy.config.name, ) self._parent.set_node_controllers(self._node_controllers)
def recursive_set_expanded(index): if (index != QModelIndex()) and (index.column() == 0): self.setExpanded(index, expanded)
def manage_message_limit(self): if len(self.get_message_list()) > self._message_limit: self.beginRemoveRows(QModelIndex(), 0, len(self.get_message_list()) - self._message_limit - 1) del self.get_message_list()[0:len(self.get_message_list()) - self._message_limit] self.endRemoveRows()
def recursive_set_expanded(index): if index != QModelIndex(): self.setExpanded(index, expanded) recursive_set_expanded(index.child(0, 0))
def _enforce_message_limit(self, limit): if len(self._messages) > limit: self.beginRemoveRows(QModelIndex(), limit, len(self._messages) - 1) del self._messages[limit:len(self._messages)] self.endRemoveRows()