table_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.QtGui import QColor
34 from python_qt_binding.QtWidgets import QAbstractItemView, QHeaderView, QTableWidget, QTableWidgetItem, QWidget
35 
36 WHITE = QColor(255, 255, 255)
37 
38 
39 class TableWidget(QWidget):
40  """Workaround for apparent bug in embedding QTableWidget.
41 
42  For unknown reasons, embedding a QTableWidget in a Widget directly results in the Table being
43  displayed in the wrong spot. To avoid that, this Widget embeds it within a widget within the widget.
44  It's widgets all the way down. This class also provides a few helper functions for quickly setting
45  table properties.
46  """
47 
48  def __init__(self, headers, stretch_column=0):
49  QWidget.__init__(self)
50  self.table = QTableWidget(self)
51  self.table.setColumnCount(len(headers))
52  self.table.setHorizontalHeaderLabels(headers)
53  header = self.table.horizontalHeader()
54  header.setSectionResizeMode(stretch_column, QHeaderView.Stretch)
55  self.table.resize(self.size())
56  self.table.resizeColumnsToContents()
57  self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
58 
59  def resizeEvent(self, event):
60  self.table.resize(event.size())
61 
62  def setValue(self, x, y, value):
63  self.table.setItem(x, y, QTableWidgetItem(str(value)))
64 
65  def setColumnHeader(self, column, value):
66  self.table.setHorizontalHeaderItem(column, QTableWidgetItem(str(value)))
67 
68  def setColor(self, x, y, color):
69  item = self.table.item(x, y)
70  if not item:
71  return
72  if color is None:
73  color = WHITE
74  item.setBackground(color)
75 
76  def setRowColor(self, row, color):
77  for column in range(self.table.columnCount()):
78  self.setColor(row, column, color)
79 
80  def setColumnColor(self, column, color):
81  for row in range(self.table.rowCount()):
82  self.setColor(row, column, color)
rqt_dwb_plugin.table_widget.TableWidget.setColumnColor
def setColumnColor(self, column, color)
Definition: table_widget.py:80
rqt_dwb_plugin.table_widget.TableWidget.setValue
def setValue(self, x, y, value)
Definition: table_widget.py:62
rqt_dwb_plugin.table_widget.TableWidget.setColumnHeader
def setColumnHeader(self, column, value)
Definition: table_widget.py:65
rqt_dwb_plugin.table_widget.TableWidget.table
table
Definition: table_widget.py:50
rqt_dwb_plugin.table_widget.TableWidget
Definition: table_widget.py:39
rqt_dwb_plugin.table_widget.TableWidget.resizeEvent
def resizeEvent(self, event)
Definition: table_widget.py:59
rqt_dwb_plugin.table_widget.TableWidget.setRowColor
def setRowColor(self, row, color)
Definition: table_widget.py:76
rqt_dwb_plugin.table_widget.TableWidget.setColor
def setColor(self, x, y, color)
Definition: table_widget.py:68
rqt_dwb_plugin.table_widget.TableWidget.__init__
def __init__(self, headers, stretch_column=0)
Definition: table_widget.py:48


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