schrodinger.ui.qt.table_helper module¶
Classes to help in creating PyQt table models and views
- schrodinger.ui.qt.table_helper.data_method(*roles)¶
A decorator for
RowBasedTableModel
andRowBasedListModel
methods that provide data. The decorator itself must be given arguments of the Qt roles that the method will provide data for.The decorated
RowBasedTableModel
method must take two or three arguments. Two argument methods will be passed:The column number of the requested data (int)
The
ROW_CLASS
object representing the row to provide data for three argument methods will also be passed:The Qt role (int)
The decorated
RowBasedListModel
method must take one or two arguments. One argument methods will be passed:The
ROW_CLASS
object representing the row to provide data for Two argument methods will also be passed:The Qt role (int)
See table_helper_example for examples of decorated methods.
- schrodinger.ui.qt.table_helper.model_reset_method(func, self, *args, **kwargs)¶
A decorator for
RowBasedTableModel
andRowBasedListModel
methods that reset the data model. SeeModelResetContextMixin
for a context manager version of this.
- class schrodinger.ui.qt.table_helper.ModelResetContextMixin¶
Bases:
object
A mixin for
QtCore.QAbstractItemModel
subclasses that adds amodelResetContext
context manager to reset the model.- modelResetContext()¶
A context manager for resetting the model. See
model_reset_method
for a decorator version of this.
- class schrodinger.ui.qt.table_helper.DataMethodDecoratorMixin(*args, **kwargs)¶
Bases:
object
A mixin for
QtCore.QAbstractItemModel
subclasses that use thedata_method
mixin. Subclasses must define_genDataArgs
.- __init__(*args, **kwargs)¶
- data(index, role=ItemDataRole.DisplayRole)¶
Provide data for the specified index and role. Classes should not redefine this method. Instead, new methods should be created and decorated with
data_method
.See Qt documentation for an explanation of arguments and return value
- class schrodinger.ui.qt.table_helper.DataMethodDecoratorProxyMixin(*args, **kwargs)¶
Bases:
schrodinger.ui.qt.table_helper.DataMethodDecoratorMixin
A mixin for
QtCore.QAbstractProxyModel
subclasses that use thedata_method
mixin.- data(proxy_index, role=ItemDataRole.DisplayRole)¶
Provide data for the specified index and role. Classes should not redefine this method. Instead, new methods should be created and decorated with
data_method
. If no data method for the requested role is found, then the source model’s data() method will be called.See Qt documentation for an explanation of arguments and return value
- class schrodinger.ui.qt.table_helper.RowBasedModelMixin(parent=None)¶
Bases:
schrodinger.ui.qt.table_helper.ModelResetContextMixin
,schrodinger.ui.qt.table_helper.DataMethodDecoratorMixin
A table model where data is organized in rows. This class is intended to be subclassed and should not be instantiated directly. All subclasses must redefine
COLUMN
and must include at least one method decorated withdata_method
. Subclasses must also redefine:Data may be added to the table using
loadData
orappendRow
. Data may be deleted usingremoveRow
orremoveRows
. Subclass methods that reset the model may use themodel_reset_method
decorator.- Variables
Column (
TableColumns
) – A class describing the table’s columns. SeeTableColumns
.ROW_CLASS (type) – A class that represents a single row of the table. ROW_CLASS must be defined in any subclasses that use
appendRow
ROW_LIST_OFFSET (int) – The index of the first element in self._rows. Setting this value to 1 allows the class to be used with one-indexed lists.
SHOW_ROW_NUMBERS (bool) – Whether to show row numbers in the vertical header.
The following class variables are the deprecated way of specifying columns. They may not be given if
Column
is used:- Variables
COLUMN (type) – May not be given if
Column
is used. A alternative method for describing the table’s columns.Column
should be preferred for newly created RowTableModel subclasses. A class containing constants describing the table columns. COLUMN must also include the following attributes: (HEADERS: A list of column headers (list), NUM_COLS: The number of columns in the table (int), TOOLTIPS (optional): A list of column header tooltips (list)).EDITABLE_COLS (list) – May not be given if
Column
is used. Useeditable=True
in theTableColumn
declaration instead. A list of column numbers for columns that should be flagged as editable. Note that only one of EDITABLE_COLS andUNEDITABLE_COLS
may be provided. If neither are provided, then no columns will be editable.UNEDITABLE_COLS (list) – May not be given if
Column
is used. Useeditable=False
in theTableColumn
declaration instead. A list of column numbers for columns that should be flagged as uneditable. Not necessary ifCOLUMN
is aTableColumns
object. Note that only one ofEDITABLE_COLS
and UNEDITABLE_COLS may be provided. If neither are provided, then no columns will be editable.CHECKABLE_COLS (list) – May not be given if
Column
is used. Usecheckable=True
in theTableColumn
declaration instead. A list of column numbers for columns that should be flagged as user checkable.NO_DATA_CHANGED (object) – A flag that can be returned from
_setData
to indicate that setting the data succeeded, but that there’s no need to emit adataChanged
signal.
- COLUMN = None¶
- Column = None¶
- EDITABLE_COLS = <object object>¶
- UNEDITABLE_COLS = <object object>¶
- CHECKABLE_COLS = ()¶
- ROW_CLASS = None¶
- ROW_LIST_OFFSET = 0¶
- SHOW_ROW_NUMBERS = False¶
- NO_DATA_CHANGED = <object object>¶
- __init__(parent=None)¶
- reset()¶
Remove all data from the model
- columnCount(parent=None)¶
- rowCount(parent=None)¶
- property rows¶
Iterate over all rows in the model. If any data is changed, call rowChanged() method with the row’s 0-indexed number to update the view.
- rowChanged(row_number)¶
Call this method when a specific row object has been modified. Will cause the view to redraw that row.
- Parameters
row_number (int) – 0-indexed row number in the model. Corresponds to the index in the “.rows” iterator.
- columnChanged(col_number)¶
Call this method when a specific column object has been modified. Will cause the view to redraw that column.
- Parameters
col_number (int) – 0-indexed column number in the model.
- loadData(rows)¶
Load data into the table and replace all existing data.
- Parameters
rows (list) – A list of
ROW_CLASS
objects
- appendRow(*args, **kwargs)¶
Add a row to the table. All arguments are passed to
ROW_CLASS
initialization.- Returns
The row number of the new row
- Return type
int
- appendRowObjects(rows)¶
Add rows to the table.
- Parameters
rows (ROW_CLASS) – Row objects to add to the table.
- appendRowObject(row)¶
Add a row to the table.
- Parameters
row (
ROW_CLASS
) – Row object to add to the table.- Returns
The row number of the new row
- Return type
int
- removeRows(row, count, parent=None)¶
- removeRowsByRowNumbers(rows)¶
Remove the given rows from the model, specified by row number, 0-indexed.
- removeRowsByIndices(indices)¶
Remove all rows from the model specified by the given QModelIndex items.
- headerData(section, orientation, role=ItemDataRole.DisplayRole)¶
Provide column headers, and optionally column tooltips and row numbers.
See Qt documentation for an explanation of arguments and return value
- flags(index)¶
See Qt documentation for an method documentation.
- setData(index, value, role=ItemDataRole.EditRole)¶
Set data for the specified index and role. Whenever possible, sub- classes should redefine
_setData
rather than this method.See Qt documentation for an explanation of arguments and return value.
- formatFloat(value, role, digits, fmt='')¶
Format floating point values for display or sorting. If
role
isQt.DisplayRole
, thenvalue
will be returned as a string with the specified formatting. All otherrole
values are assumed to be a sorting role and value will be returned unchanged.- Parameters
value (float) – The floating point value to format
role (int) – The Qt data role
digits (int) – The number of digits to include after the decimal point for Qt.DisplayRole
fmt (str) – Additional floating point formatting options
- Returns
The formatted or unmodified value
- Return type
str or float
- af2SettingsGetValue()¶
This function adds support for the settings mixin. It allows to save table cell values in case this table is included in the settings panel. Returns list of rows if table model is of RowBasedTableModel class type.
- Returns
list of rows in tbe table’s model.
- Return type
list or None
- af2SettingsSetValue(value)¶
This function adds support for the settings mixin. It allows to set table cell values when this table is included in the settings panel.
- Parameters
value (list) – settings value, which is a list of row data here.
- replaceRows(new_rows)¶
Replace the contents of the model with the contents of the given list. The change will be presented to the view as a series of row insertions and deletions rather than as a model reset. This allows the view to properly update table selections and scroll bar position. This method may only be used if:
the
ROW_CLASS
objects can be compared using < and ==the contents of the model (i.e.
self._rows
) are sorted in ascending orderthe contents of
new_rows
are sorted in ascending order
This method is primarily intended for use when the table contains rows based on project table rows. On every project change, the project table can be reread and used to generate
new_list
and this method can then properly update the model.- Parameters
new_rows (list) – A list of
ROW_CLASS
objects
- class schrodinger.ui.qt.table_helper.RowBasedTableModel(parent=None)¶
Bases:
schrodinger.ui.qt.table_helper.RowBasedModelMixin
,PyQt6.QtCore.QAbstractTableModel
- class schrodinger.ui.qt.table_helper.RowBasedListModel(parent=None)¶
Bases:
schrodinger.ui.qt.table_helper.RowBasedModelMixin
,PyQt6.QtCore.QAbstractTableModel
A model class for use with
QtWidgets.QListView
views. The model has no headers and only one column. Note that theColumn
class variable is not needed.- columnCount(self, parent: QModelIndex = QModelIndex()) int ¶
- headerData(section, orientation, role=ItemDataRole.DisplayRole)¶
Provide column headers, and optionally column tooltips and row numbers.
See Qt documentation for an explanation of arguments and return value
- index(self, row: int, column: int, parent: QModelIndex = QModelIndex()) QModelIndex ¶
- class schrodinger.ui.qt.table_helper.PythonSortProxyModel(parent=None)¶
Bases:
PyQt6.QtCore.QSortFilterProxyModel
A sorting proxy model that uses Python (rather than C++) to compare values. This allows Python lists, tuples, and custom classes to be properly sorted.
- Variables
SORT_ROLE (int) – If specified in a subclass, this value will be used as the sort role. Otherwise, Qt defaults to Qt.DisplayRole.
DYNAMIC_SORT_FILTER (bool) – If specified in a subclass, this value will be used as the dynamic sorting and filtering setting (see
QtCore.QSortFilterProxyModel.setDynamicSortFilter
). Otherwise, Qt defaults to False in Qt4 and True in Qt5.
- SORT_ROLE = None¶
- DYNAMIC_SORT_FILTER = None¶
- __init__(parent=None)¶
- lessThan(left, right)¶
Comparison method for sorting rows and columns. Handle special case in which one or more sort data values is
None
by evaluating it as less than every other value.- Parameters
left (QtCore.QModelIndex) – table cell index
right (QtCore.QModelIndex) – table cell index
See Qt documentation for full method documentation.
- class schrodinger.ui.qt.table_helper.SampleDataTableViewMixin(parent=None, sample_data=None)¶
Bases:
object
A table view mixin that uses sample data to properly size columns. Additionally, the table size hint will attempt to display the full width of the table.
- Variables
SAMPLE_DATA (dict) – A dictionary of {column number: sample string}. Any columns that do not appear in this dictionary will not be resized. Can be set by passing
sample_data
to__init__
or by callingsetSampleData
after instantiation.MARGIN (int) – The additional width to add to each column included in
SAMPLE_DATA
- MARGIN = 20¶
- __init__(parent=None, sample_data=None)¶
- SAMPLE_DATA = {}¶
- setModel(model)¶
After setting the model, resize the columns using
SAMPLE_DATA
and the header data provided by the modelSee Qt documentation for an explanation of arguments
- setSampleData(new_sample_data)¶
Sets SAMPLE_DATA to new_sample_data and updates column widths if model is set.
- Parameters
new_sample_data (dict) – The new sample data
- sizeHintForColumn(col_num)¶
Provide a size hint for the specified column using
SAMPLE_DATA
. Note that this method does not take header width into account as the header width is already accounted for inresizeColumnToContents
.See Qt documentation for an explanation of arguments and return value
- sizeHint()¶
Provide a size hint that requests the full width of the table.
See Qt documentation for an explanation of arguments and return value
- class schrodinger.ui.qt.table_helper.SampleDataTableView(parent=None, sample_data=None)¶
Bases:
schrodinger.ui.qt.table_helper.SampleDataTableViewMixin
,schrodinger.ui.qt.swidgets.STableView
See SampleDataTableViewMixin for features.
- class schrodinger.ui.qt.table_helper.UserRolesEnumMeta(cls, bases, classdict, offset=None, step_size=None, **kwds)¶
Bases:
enum.EnumType
The metaclass for
UserRolesEnum
. SeeUserRolesEnum
for documentation.
- class schrodinger.ui.qt.table_helper.Column(title=None, tooltip=None, align=None, editable=False, checkable=False)¶
Bases:
object
A table column. This class is intended to be used in the
TableColumns
enum.- __init__(title=None, tooltip=None, align=None, editable=False, checkable=False)¶
- Parameters
title (str) – The column title to display in the header.
tooltip (str) – The tooltip to display when the user hovers over the column header.
align (int) – The alignment for cells in the column. If not given, Qt defaults to left alignment.
editable (bool) – Whether cells in the column are editable. (I.e., whether cells should be given the Qt.ItemIsEditable flag.)
checkable (bool) – Whether cells in the column can be checked or unchecked without opening an editor. (I.e., whether cells should be given the Qt.ItemIsUserCheckable flag.) Note that cells in checkable columns should provide data for Qt.CheckStateRole.
- schrodinger.ui.qt.table_helper.connect_signals(model, signal_map)¶
Connect all specified signals
- Parameters
model (
QtCore.Qbject
) – The model to connect signals fromsignal_map (dict) – A dictionary of {signal name (str): slot}.
- schrodinger.ui.qt.table_helper.disconnect_signals(model, signal_map)¶
Disconnect all specified signals
- Parameters
model (
QtCore.Qbject
) – The model to disconnect signals fromsignal_map (dict) – A dictionary of {signal name (str): slot}.
- schrodinger.ui.qt.table_helper.PtRowBasedCustomRole¶
alias of
schrodinger.ui.qt.table_helper.CustomRole
- class schrodinger.ui.qt.table_helper.Include(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
enum.IntEnum
- Only = 1¶
- Toggle = 2¶
- Range = 3¶
- class schrodinger.ui.qt.table_helper.PtRowBasedTableModel¶
Bases:
schrodinger.ui.qt.appframework2.maestro_callback.MaestroCallbackMixin
,schrodinger.ui.qt.table_helper.RowBasedTableModel
A table model that keeps track of the inclusion state of an entry between the Project Table and the table’s inclusion checkboxes. The inclusion lock state is also respected by not allowing the user to uncheck a inclusion locked entry.
Note: An ‘Inclusion’ column must be defined by the Column class as well as an ‘EntryId’ CustomRole to utilize this class. Moreover, the row object class for the PtRowBasedTableModel subclass should define an entry_id attribute, otherwise subclass needs to define a data method for PtRowBasedCustomRole.EntryId.
Lastly, if the subclass of PtRowBasedTableModel requires any additional custom roles, it should use a UserRolesEnum that inherits from the above PtRowBasedCustomRole to avoid the risk of role number conflicts.
- __init__()¶
- onInclusionChanged()¶
Called when the workspace’s inclusion changes. The emitted dataChanged() signal forces the view to update each entry’s inclusion state from the workspace by calling data().
- onProjectClosed()¶
Reset the table when a project is closed to avoid invalid data.
- onProjectUpdated()¶
Reset the PT instance.
- data(index, role=ItemDataRole.DisplayRole)¶
If the inclusion state is requested, the data will be retrieved from the PT. The inclusion states are not stored in the table to avoid updating in two locations.
See table_helper.RowBasedTableModel.data() for documentation on arguments and return value.
- setData(index, value, role=ItemDataRole.EditRole)¶
If the inclusion state is updated, the data will be set to the PT.
See table_helper.RowBasedTableModel.data() for documentation on arguments and return value.