1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" A row-oriented data model implementation.
This module provides a concrete implementation of a data model for the
case of non-hierarchical, row-oriented data.
"""
from collections.abc import Sequence
from traits.api import Instance, List, observe
from traits.observation.api import trait
from pyface.data_view.abstract_data_model import (
AbstractDataModel, DataViewSetError
)
from pyface.data_view.index_manager import IntIndexManager
from pyface.data_view.data_models.data_accessors import AbstractDataAccessor
class RowTableDataModel(AbstractDataModel):
""" A data model that presents a sequence of objects as rows.
The data is expected to be a sequence of row objects, each object
providing values for the columns via an AbstractDataAccessor subclass.
Concrete implementations can be found in the data_accessors module that
get data from attributes, indices of sequences, and keys of mappings,
but for more complex situations, custom accessors can be defined.
"""
#: A sequence of objects to display as rows.
data = Instance(Sequence, allow_none=False)
#: An object which describes how to map data for the row headers.
row_header_data = Instance(AbstractDataAccessor, allow_none=False)
#: An object which describes how to map data for each column.
column_data = List(Instance(AbstractDataAccessor, allow_none=False))
#: The index manager that helps convert toolkit indices to data view
#: indices.
index_manager = Instance(IntIndexManager, args=(), allow_none=False)
# Data structure methods
def get_column_count(self):
""" How many columns in the data view model.
Returns
-------
column_count : non-negative int
The number of columns that the data view provides.
"""
return len(self.column_data)
def can_have_children(self, row):
""" Whether or not a row can have child rows.
Only the root has children.
Parameters
----------
row : sequence of int
The indices of the row as a sequence from root to leaf.
Returns
-------
can_have_children : bool
Whether or not the row can ever have child rows.
"""
return len(row) == 0
def get_row_count(self, row):
""" How many child rows the row currently has.
Parameters
----------
row : sequence of int
The indices of the row as a sequence from root to leaf.
Returns
-------
row_count : non-negative int
The number of child rows that the row has.
"""
if len(row) == 0:
return len(self.data)
else:
return 0
# Data value methods
def get_value(self, row, column):
""" Return the Python value for the row and column.
This uses the row_header_data and column_data accessors to extract
values for the row and column.
Parameters
----------
row : sequence of int
The indices of the row as a sequence from root to leaf.
column : sequence of int
The indices of the column as a sequence of length 0 or 1.
Returns
-------
value : Any
The value represented by the given row and column.
"""
if len(column) == 0:
column_data = self.row_header_data
else:
column_data = self.column_data[column[0]]
if len(row) == 0:
return column_data.title
obj = self.data[row[0]]
return column_data.get_value(obj)
def can_set_value(self, row, column):
""" Whether the value in the indicated row and column can be set.
This uses the row_header_data and column_data accessors to determine
if the value may be changed.
Parameters
----------
row : sequence of int
The indices of the row as a sequence from root to leaf.
column : sequence of int
The indices of the column as a sequence of length 0 or 1.
Returns
-------
can_set_value : bool
Whether or not the value can be set.
"""
if len(row) == 0:
return False
if len(column) == 0:
column_data = self.row_header_data
else:
column_data = self.column_data[column[0]]
obj = self.data[row[0]]
return column_data.can_set_value(obj)
def set_value(self, row, column, value):
""" Set the Python value for the row and column.
This uses the row_header_data and column_data accessors to set
the value.
Parameters
----------
row : sequence of int
The indices of the row as a sequence from root to leaf.
column : sequence of int
The indices of the column as a sequence of length 0 or 1.
value : Any
The new value for the given row and column.
Raises
-------
DataViewSetError
If the value cannot be set.
"""
if len(row) == 0:
raise DataViewSetError("Can't set column titles.")
if len(column) == 0:
column_data = self.row_header_data
else:
column_data = self.column_data[column[0]]
obj = self.data[row[0]]
column_data.set_value(obj, value)
self.values_changed = (row, column, row, column)
def get_value_type(self, row, column):
""" Return the value type of the given row and column.
This uses the row_header_data and column_data accessors to get
the value type.
Parameters
----------
row : sequence of int
The indices of the row as a sequence from root to leaf.
column : sequence of int
The indices of the column as a sequence of length 0 or 1.
Returns
-------
value_type : AbstractValueType or None
The value type of the given row and column, or None if no value
should be displayed.
"""
if len(column) == 0:
column_data = self.row_header_data
else:
column_data = self.column_data[column[0]]
if len(row) == 0:
return column_data.title_type
return column_data.value_type
# data update methods
@observe("data")
def _update_data(self, event):
self.structure_changed = True
@observe(trait("data", notify=False).list_items(optional=True))
def _update_data_items(self, event):
if len(event.added) != len(event.removed):
# number of rows has changed
self.structure_changed = True
else:
if isinstance(event.index, int):
start = event.index
stop = min(event.index + len(event.added), len(self.data)) - 1
else:
start = event.index.start
stop = min(event.index.stop, len(self.data)) - 1
self.values_changed = ((start,), (), (stop,), ())
@observe('row_header_data')
def _update_row_header_data(self, event):
self.values_changed = ((), (), (), ())
@observe('row_header_data:updated')
def _update_row_header_data_event(self, event):
if event.new[1] == 'value':
if len(self.data) > 0:
self.values_changed = ((0,), (), (len(self.data) - 1,), ())
else:
self.values_changed = ((), (), (), ())
@observe('column_data.items')
def _update_all_column_data_items(self, event):
self.structure_changed = True
@observe('column_data:items:updated')
def _update_column_data(self, event):
index = self.column_data.index(event.new[0])
if event.new[1] == 'value':
if len(self.data) > 0:
self.values_changed = (
(0,), (index,), (len(self.data) - 1,), (index,)
)
else:
self.values_changed = ((), (index,), (), (index,))
# default data value
def _data_default(self):
return []
|