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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
# (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!
from traits.api import Dict, Instance, List, Union
from .grid_model import GridModel, GridRow
class CompositeGridModel(GridModel):
""" A CompositeGridModel is a model whose underlying data is
a collection of other grid models. """
# The models this model is comprised of.
data = List(Instance(GridModel))
# The rows in the model.
rows = Union(None, List(Instance(GridRow)))
# The cached data indexes.
_data_index = Dict()
# ------------------------------------------------------------------------
# 'object' interface.
# ------------------------------------------------------------------------
def __init__(self, **traits):
""" Create a CompositeGridModel object. """
# Base class constructor
super().__init__(**traits)
self._row_count = None
# ------------------------------------------------------------------------
# 'GridModel' interface.
# ------------------------------------------------------------------------
def get_column_count(self):
""" Return the number of columns for this table. """
# for the composite grid model, this is simply the sum of the
# column counts for the underlying models
count = 0
for model in self.data:
count += model.get_column_count()
return count
def get_column_name(self, index):
""" Return the name of the column specified by the
(zero-based) index. """
model, new_index = self._resolve_column_index(index)
return model.get_column_name(new_index)
def get_column_size(self, index):
""" Return the size in pixels of the column indexed by col.
A value of -1 or None means use the default. """
model, new_index = self._resolve_column_index(index)
return model.get_column_size(new_index)
def get_cols_drag_value(self, cols):
""" Return the value to use when the specified columns are dragged or
copied and pasted. cols is a list of column indexes. """
values = []
for col in cols:
model, real_col = self._resolve_column_index(col)
values.append(model.get_cols_drag_value([real_col]))
return values
def get_cols_selection_value(self, cols):
""" Return the value to use when the specified cols are selected.
This value should be enough to specify to other listeners what is
going on in the grid. rows is a list of row indexes. """
return self.get_cols_drag_value(self, cols)
def get_column_context_menu(self, col):
""" Return a MenuManager object that will generate the appropriate
context menu for this column."""
model, new_index = self._resolve_column_index(col)
return model.get_column_context_menu(new_index)
def sort_by_column(self, col, reverse=False):
""" Sort model data by the column indexed by col. The reverse flag
indicates that the sort should be done in reverse. """
pass
def is_column_read_only(self, index):
""" Return True if the column specified by the zero-based index
is read-only. """
model, new_index = self._resolve_column_index(index)
return model.is_column_read_only(new_index)
def get_row_count(self):
""" Return the number of rows for this table. """
# see if we've already calculated the row_count
if self._row_count is None:
row_count = 0
# return the maximum rows of any of the contained models
for model in self.data:
rows = model.get_row_count()
if rows > row_count:
row_count = rows
# save the result for next time
self._row_count = row_count
return self._row_count
def get_row_name(self, index):
""" Return the name of the row specified by the
(zero-based) index. """
label = None
# if the rows list exists then grab the label from there...
if self.rows is not None:
if len(self.rows) > index:
label = self.rows[index].label
# ... otherwise generate it from the zero-based index.
else:
label = str(index + 1)
return label
def get_rows_drag_value(self, rows):
""" Return the value to use when the specified rows are dragged or
copied and pasted. rows is a list of row indexes. """
row_values = []
for rindex in rows:
row = []
for model in self.data:
new_data = model.get_rows_drag_value([rindex])
# if it's a list then we assume that it represents more than
# one column's worth of values
if isinstance(new_data, list):
row.extend(new_data)
else:
row.append(new_data)
# now save our new row value
row_values.append(row)
return row_values
def is_row_read_only(self, index):
""" Return True if the row specified by the zero-based index
is read-only. """
read_only = False
if self.rows is not None and len(self.rows) > index:
read_only = self.rows[index].read_only
return read_only
def get_type(self, row, col):
""" Return the type of the value stored in the table at (row, col). """
model, new_col = self._resolve_column_index(col)
return model.get_type(row, new_col)
def get_value(self, row, col):
""" Return the value stored in the table at (row, col). """
model, new_col = self._resolve_column_index(col)
return model.get_value(row, new_col)
def get_cell_selection_value(self, row, col):
""" Return the value stored in the table at (row, col). """
model, new_col = self._resolve_column_index(col)
return model.get_cell_selection_value(row, new_col)
def resolve_selection(self, selection_list):
""" Returns a list of (row, col) grid-cell coordinates that
correspond to the objects in selection_list. For each coordinate, if
the row is -1 it indicates that the entire column is selected. Likewise
coordinates with a column of -1 indicate an entire row that is
selected. Note that the objects in selection_list are
model-specific. """
coords = []
for selection in selection_list:
# we have to look through each of the models in order
# for the selected object
for model in self.data:
cells = model.resolve_selection([selection])
# we know this model found the object if cells comes back
# non-empty
if cells is not None and len(cells) > 0:
coords.extend(cells)
break
return coords
# fixme: this context menu stuff is going in here for now, but it
# seems like this is really more of a view piece than a model piece.
# this is how the tree control does it, however, so we're duplicating
# that here.
def get_cell_context_menu(self, row, col):
""" Return a MenuManager object that will generate the appropriate
context menu for this cell."""
model, new_col = self._resolve_column_index(col)
return model.get_cell_context_menu(row, new_col)
def is_cell_empty(self, row, col):
""" Returns True if the cell at (row, col) has a None value,
False otherwise."""
model, new_col = self._resolve_column_index(col)
if model is None:
return True
else:
return model.is_cell_empty(row, new_col)
def is_cell_editable(self, row, col):
""" Returns True if the cell at (row, col) is editable,
False otherwise. """
model, new_col = self._resolve_column_index(col)
return model.is_cell_editable(row, new_col)
def is_cell_read_only(self, row, col):
""" Returns True if the cell at (row, col) is not editable,
False otherwise. """
model, new_col = self._resolve_column_index(col)
return model.is_cell_read_only(row, new_col)
def get_cell_bg_color(self, row, col):
""" Return a wxColour object specifying what the background color
of the specified cell should be. """
model, new_col = self._resolve_column_index(col)
return model.get_cell_bg_color(row, new_col)
def get_cell_text_color(self, row, col):
""" Return a wxColour object specifying what the text color
of the specified cell should be. """
model, new_col = self._resolve_column_index(col)
return model.get_cell_text_color(row, new_col)
def get_cell_font(self, row, col):
""" Return a wxFont object specifying what the font
of the specified cell should be. """
model, new_col = self._resolve_column_index(col)
return model.get_cell_font(row, new_col)
def get_cell_halignment(self, row, col):
""" Return a string specifying what the horizontal alignment
of the specified cell should be.
Return 'left' for left alignment, 'right' for right alignment,
or 'center' for center alignment. """
model, new_col = self._resolve_column_index(col)
return model.get_cell_halignment(row, new_col)
def get_cell_valignment(self, row, col):
""" Return a string specifying what the vertical alignment
of the specified cell should be.
Return 'top' for top alignment, 'bottom' for bottom alignment,
or 'center' for center alignment. """
model, new_col = self._resolve_column_index(col)
return model.get_cell_valignment(row, new_col)
# ------------------------------------------------------------------------
# protected 'GridModel' interface.
# ------------------------------------------------------------------------
def _delete_rows(self, pos, num_rows):
""" Implementation method for delete_rows. Should return the
number of rows that were deleted. """
for model in self.data:
model._delete_rows(pos, num_rows)
return num_rows
def _insert_rows(self, pos, num_rows):
""" Implementation method for insert_rows. Should return the
number of rows that were inserted. """
for model in self.data:
model._insert_rows(pos, num_rows)
return num_rows
def _set_value(self, row, col, value):
""" Implementation method for set_value. Should return the
number of rows, if any, that were appended. """
model, new_col = self._resolve_column_index(col)
model._set_value(row, new_col, value)
return 0
# ------------------------------------------------------------------------
# private interface
# ------------------------------------------------------------------------
def _resolve_column_index(self, index):
""" Resolves a column index into the correct model and adjusted
index. Returns the target model and the corrected index. """
real_index = index
cached = None # self._data_index.get(index)
if cached is not None:
model, col_index = cached
else:
model = None
for m in self.data:
cols = m.get_column_count()
if real_index < cols:
model = m
break
else:
real_index -= cols
self._data_index[index] = (model, real_index)
return model, real_index
def _data_changed(self):
""" Called when the data trait is changed.
Since this is called when our underlying models change, the cached
results of the column lookups is wrong and needs to be invalidated.
"""
self._data_index.clear()
def _data_items_changed(self):
""" Called when the members of the data trait have changed.
Since this is called when our underlying model change, the cached
results of the column lookups is wrong and needs to be invalidated.
"""
self._data_index.clear()
|