sorted_twists_widget.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2018-2019, Locus Robotics
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 copyright holder 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 from python_qt_binding.QtWidgets import QAbstractItemView, QTableWidgetSelectionRange
34 
35 from .table_widget import TableWidget
36 
37 HEADERS = ['Score', 'X', 'Theta']
38 
39 
41  """This widget displays a sorted list of all the trajectories in tabular format."""
42 
43  def __init__(self, parent, selection_callback):
44  TableWidget.__init__(self, HEADERS)
45  self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
46  self.table.itemSelectionChanged.connect(self.itemSelectionChanged)
47  self.evaluation = None
48  self.selected = []
49  self.selection_callback = selection_callback
50  self.sorted_twists = []
51  self.row_mapping = []
52  self.update_selection = True
53 
54  def setEvaluation(self, evaluation):
55  self.evaluation = evaluation
56  self.selected = []
57  self.table.resize(self.size())
58  self.table.setRowCount(len(evaluation.twists))
59 
60  # Tuples of original index and twist, sorted by increasing total cost
61  self.sorted_twists = sorted(enumerate(evaluation.twists), key=lambda x: (x[1].total < 0, x[1].total))
62 
63  self.row_mapping = [-1] * len(evaluation.twists)
64  for row_index, (original_index, twist) in enumerate(self.sorted_twists):
65  self.setValue(row_index, 0, '%5.2f' % twist.total)
66  self.setValue(row_index, 1, '%.2f' % twist.traj.velocity.x)
67  self.setValue(row_index, 2, '%.2f' % twist.traj.velocity.theta)
68  self.row_mapping[original_index] = row_index
69  self.update()
70 
71  def setSelected(self, selected):
72  for index in self.selected:
73  if index in selected:
74  continue
75  row = self.row_mapping[index]
76  self.setRowColor(row, None)
77 
78  self.selected = selected
79  self.update_selection = False
80 
81  # Deselect Everything
82  self.table.setRangeSelected(QTableWidgetSelectionRange(0, 0,
83  self.table.rowCount() - 1, self.table.columnCount() - 1
84  ),
85  False)
86 
87  for index in selected:
88  row = self.row_mapping[index]
89  self.setRowColor(row, selected[index])
90  self.update_selection = True
91 
92  def getSelectedRows(self):
93  rows = set()
94  for index in self.table.selectedIndexes():
95  rows.add(index.row())
96  return list(rows)
97 
99  if not self.update_selection:
100  return
101  rows = self.getSelectedRows()
102 
103  new_selected = list(self.selected)
104  for row_num in rows:
105  selection = self.sorted_twists[row_num][0]
106  if selection in self.selected:
107  new_selected.remove(selection)
108  else:
109  new_selected.append(selection)
110  self.selection_callback(new_selected)
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.selected
selected
Definition: sorted_twists_widget.py:48
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.getSelectedRows
def getSelectedRows(self)
Definition: sorted_twists_widget.py:92
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.evaluation
evaluation
Definition: sorted_twists_widget.py:47
rqt_dwb_plugin.table_widget.TableWidget.setValue
def setValue(self, x, y, value)
Definition: table_widget.py:62
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget
Definition: sorted_twists_widget.py:40
rqt_dwb_plugin.table_widget.TableWidget.table
table
Definition: table_widget.py:50
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.setSelected
def setSelected(self, selected)
Definition: sorted_twists_widget.py:71
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.setEvaluation
def setEvaluation(self, evaluation)
Definition: sorted_twists_widget.py:54
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.__init__
def __init__(self, parent, selection_callback)
Definition: sorted_twists_widget.py:43
rqt_dwb_plugin.table_widget.TableWidget
Definition: table_widget.py:39
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.sorted_twists
sorted_twists
Definition: sorted_twists_widget.py:50
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.itemSelectionChanged
def itemSelectionChanged(self)
Definition: sorted_twists_widget.py:98
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.selection_callback
selection_callback
Definition: sorted_twists_widget.py:49
rqt_dwb_plugin.table_widget.TableWidget.setRowColor
def setRowColor(self, row, color)
Definition: table_widget.py:76
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.update_selection
update_selection
Definition: sorted_twists_widget.py:52
rqt_dwb_plugin.sorted_twists_widget.SortedTwistsWidget.row_mapping
row_mapping
Definition: sorted_twists_widget.py:51


rqt_dwb_plugin
Author(s):
autogenerated on Sun May 18 2025 02:47:58