TableView (hwx.gui)#
- class TableView(values=[], **kwds)#
Bases:
Frame
A widget that presents data in a spreadsheet-like table view and provides means of visual observation and interaction with them.
The data stored in TableView.values are a list of list. The outer list contains the rows and each nested list contains the values for the columns. When defyining a TableView, one MUST define the columns of the TableView. A column must contain information of the same data type. Consequently, each nested list of TableView.values must provide data of the correct type, and be of size equal to the TableView.columns.
Examples of columns types are: views.Bool, views.Int, views.String, views.Float and views.Enum.
To use it, it’s suggested to inherit from it and define the type of columns.
# Name
Type
columns
dict
property
property
property
property
property
property
property
# Name
Description
addRow
(self, row)Appends a row.
clearSelection
(self, emit=False)Unselects all cells.
values
(self)(list[list]) The data the TableView holds.
getSelectedCells
(self)A list of tuples specifying which cells are selected and in which order.
getSelectedRows
(self)A list of indices specifying which rows are selected and in which order.
onCommand
(self, event=None)removeRow
(self, index)Deletes a row based on index.
selectCell
(self, row, col, clear=True, emit=False)Selects a cells.
selectColumn
(self, col, clear=True, emit=False)Selects all cells of a column.
selectRow
(self, row, clear=True, emit=False)Selects all cells of a row.
values
(self, values)setHResizeMode
(self, mode, col=’all’)Controls resize behavior of the cells in the horizontal axis.
setVResizeMode
(self, mode, row=’all’)Controls resize behavior of the cells in the vertical axis.
Example
import csv import random import string from hwx.gui import views from hwx import gui class BoundedValue(views.Float): """An example of how a Float that must be bounded can be implemented""" def castForSet(self, value): value = super().castForSet(value) if not (self.minVal <= value <= self.maxVal): # This will ensure that the cell will keep the previous value raise ValueError("Value must be between limits") return value class ExampleTableView(views.TableView): """An example of how a TableView with different types of columns can be implemented""" def getMaterials(self, *args): return ["Steel (AISI 304)", "Steel (AISI 316)", "Steel (AISI 1015)"] columns = dict( name = views.String(label="Name"), id = views.Int(label="Id"), transparency = BoundedValue(label="Transparency", minVal=0, maxVal=100), visible = views.Bool(label="Visible"), x = views.Float(label="CoG X", units="length"), mass = BoundedValue(label="Mass", units="mass", minVal=0, maxVal=float("inf")), material = views.Enum(values=getMaterials, label="Material"), ) def saveCommand(self): fname = gui.getSaveFileName(caption="Export", filter="CVS (*.csv)") if fname: with open(fname, "w", newline='') as csvFile: writer = csv.writer(csvFile) labels = [col.label for name, col in table.columns.items()] rows = [labels] + self.values writer.writerows(rows) def addCommand(self): def generateRow(): return [ random.choice(string.ascii_letters).upper(), # Name random.randint(1, 10), # Id random.uniform(0, 100), # Transparency random.choice([True, False]), # Visible random.uniform(0, 1), # CoG X random.uniform(0, 100), # Mass random.choice(list(self.getMaterials())), # Material ] self.addRow(generateRow()) def deleteCommand(self): selected = self.getSelectedRows() for idx, rowIdx in enumerate(selected): self.removeRow(rowIdx-idx) def keyCommand(self, event): import hwui if gui.isCtrlDown() and event.Key() == hwui.ui.Key_S: self.saveCommand() elif event.Key() == hwui.ui.Key_Delete: self.deleteCommand() def createButttons(self): self.controlBar.button( icon="glyphFileSaveStrip-16.png", command=self.saveCommand ) self.controlBar.button( icon="toolbarEditAddStrip-16.png", command=self.addCommand ) self.controlBar.button( icon="toolbarDeleteStrip-16.png", command=self.deleteCommand ) self.controlBar.addSpacer() def buildContextMenu(self, menu, selectedCells): menu.insertItem( gui.tr('Add'), icon="toolbarEditAddStrip-16.png", command=lambda *args: self.addCommand() ) menu.insertItem( gui.tr('Delete'), icon='glyphDeleteStrip-16.png', accel="Del", command=lambda *args: self.deleteCommand() ) values = [['A', 1, 50, True, 0.1, "1kg", "Steel (AISI 304)"]] table = ExampleTableView(values=values) table.createButttons() table.onContextMenu.connect(table.buildContextMenu) table.onKeyPress.connect(table.keyCommand) dialog = gui.Dialog(caption="Table", children=table, height=200, width=800) show(dialog)
- property numRows#
(int) The number of rows.
- property numCols#
(int) The number of columns.
- property values#
(list[list]) The data the TableView holds.
The outer list contains the rows and each nested list contains the values for the columns.
- addRow(row)#
Appends a row.
- removeRow(index)#
Deletes a row based on index.
- class ControlBar(flags=0, **kwds)#
Bases:
Frame
Horizontal Bar with layout and methods to add widgets.
# Name
Description
addSpacer
(self)addSpacing
(self, val)button
(self, icon, command, **kwds)
- class selectionModeType(value)#
Bases:
Enum
An enumeration.
# Name
Type
ContiguousSelection
selectionModeType
ExtendedSelection
selectionModeType
MultiSelection
selectionModeType
NoSelection
selectionModeType
SingleSelection
selectionModeType
- property selectionMode#
(TableView.selectionModeType) Determines whether selection, selects none, one or many items.
In many-item selections, whether the selection must be a continuous range of items, extended or multi-selection.
- class selectionBehaviorType(value)#
Bases:
Enum
An enumeration.
# Name
Type
SelectColumns
selectionBehaviorType
SelectItems
selectionBehaviorType
SelectRows
selectionBehaviorType
- property selectionBehavior#
(TableView.selectionBehaviorType) Determines whether selection, selects single items, rows or columns.
- class ResizeMode(value)#
Bases:
Enum
An enumeration.
# Name
Type
Fixed
ResizeMode
Interactive
ResizeMode
ResizeToContents
ResizeMode
Stretch
ResizeMode
- setHResizeMode(mode, col='all')#
Controls resize behavior of the cells in the horizontal axis.
- Parameters:
mode (TableView.ResizeMode) –
col (int | 'all') –
- setVResizeMode(mode, row='all')#
Controls resize behavior of the cells in the vertical axis.
- Parameters:
mode (TableView.ResizeMode) –
row (int | 'all') –
- property vResizeMode#
(TableView.ResizeMode) The resize mode that applies to the vertical header.
- property hResizeMode#
(TableView.ResizeMode) The resize mode that applies to the horizontal header.
- getSelectedRows()#
A list of indices specifying which rows are selected and in which order.
- Returns:
list
- getSelectedCells()#
A list of tuples specifying which cells are selected and in which order.
- Returns:
list(tuple)
- selectCell(row, col, clear=True, emit=False)#
Selects a cells.
- Parameters:
row (int) – The index of the row to be selected.
col (int) – The index of the column to be selected.
clear (bool) – Determines whether the previously selected will be cleared.
emit (bool) – Determines whether slots connected to onSelectionChange will be called or not.
- selectRow(row, clear=True, emit=False)#
Selects all cells of a row.
- Parameters:
row (int) – The index of the row to be selected.
clear (bool) – Determines whether the previously selected will be cleared.
emit (bool) – Determines whether slots connected to onSelectionChange will be called or not.
- selectColumn(col, clear=True, emit=False)#
Selects all cells of a column.
- Parameters:
col (int) – The index of the column to be selected.
clear (bool) – Determines whether the previously selected will be cleared.
emit (bool) – Determines whether slots connected to onSelectionChange will be called or not.
- clearSelection(emit=False)#
Unselects all cells.
- Parameters:
emit (bool) – Determines whether slots connected to onSelectionChange will be called or not.
- get()#
(list[list]) The data the TableView holds.
The outer list contains the rows and each nested list contains the values for the columns.