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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
|
# (C) Copyright 2009-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!
# ------------------------------------------------------------------------------
# Copyright (c) 2008, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license.
#
# When used with the GPL version of PyQt the additional terms described
# in the PyQt GPL exception also apply.
#
# Author: Riverbank Computing Limited
# ------------------------------------------------------------------------------
""" Defines the table editor for the PyQt user interface toolkit.
"""
from pyface.qt import QtCore, QtGui, is_qt4
from pyface.image_resource import ImageResource
from pyface.timer.api import do_later
from pyface.ui_traits import Image
from traits.api import (
Any,
Bool,
Button,
Dict,
Event,
List,
HasTraits,
Instance,
Int,
Property,
Str,
cached_property,
observe,
)
from traitsui.api import (
EnumEditor,
InstanceEditor,
Group,
Item,
Label,
ObjectColumn,
TableColumn,
TableFilter,
UI,
View,
default_handler,
spring,
)
from traitsui.editors.table_editor import (
BaseTableEditor,
ReversedList,
customize_filter,
)
from traitsui.ui_traits import SequenceTypes
from .editor import Editor
from .table_model import TableModel, SortFilterTableModel
if is_qt4:
def set_qheader_section_resize_mode(header):
return header.setResizeMode
else:
def set_qheader_section_resize_mode(header):
return header.setSectionResizeMode
class TableEditor(Editor, BaseTableEditor):
"""Editor that presents data in a table. Optionally, tables can have
a set of filters that reduce the set of data displayed, according to
their criteria.
"""
# -------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------
#: The table view control associated with the editor:
table_view = Any()
def _table_view_default(self):
return TableView(editor=self)
#: A wrapper around the source model which provides filtering and sorting:
model = Instance(SortFilterTableModel)
def _model_default(self):
return SortFilterTableModel(editor=self)
#: The table model associated with the editor:
source_model = Instance(TableModel)
def _source_model_default(self):
return TableModel(editor=self)
#: The set of columns currently defined on the editor:
columns = List(TableColumn)
#: The currently selected row(s), column(s), or cell(s).
selected = Any()
#: The current selected row
selected_row = Property(Any, observe="selected")
selected_indices = Property(Any, observe="selected")
#: Current filter object (should be a TableFilter or callable or None):
filter = Any()
#: The indices of the table items currently passing the table filter:
filtered_indices = List(Int)
#: Current filter summary message
filter_summary = Str("All items")
#: Update the filtered contents.
update_filter = Event()
#: The event fired when a cell is clicked on:
click = Event()
#: The event fired when a cell is double-clicked on:
dclick = Event()
#: The Traits UI associated with the table editor toolbar:
toolbar_ui = Instance(UI)
#: The index of the row that was last right clicked on its vertical header
header_row = Int()
#: Whether to auto-size the columns or not.
auto_size = Bool(False)
#: Dictionary mapping image names to QIcons
images = Dict()
#: Dictionary mapping ImageResource objects to QIcons
image_resources = Dict()
#: An image being converted:
image = Image
def init(self, parent):
"""Finishes initializing the editor by creating the underlying toolkit
widget."""
factory = self.factory
self.filter = factory.filter
columns = factory.columns[:]
if (len(columns) == 0) and (len(self.value) > 0):
columns = [
ObjectColumn(name=name)
for name in self.value[0].editable_traits()
]
self.columns = columns
if factory.table_view_factory is not None:
self.table_view = factory.table_view_factory(editor=self)
if factory.source_model_factory is not None:
self.source_model = factory.source_model_factory(editor=self)
if factory.model_factory is not None:
self.model = factory.model_factory(editor=self)
# Create the table view and model
self.model.setDynamicSortFilter(True)
self.model.setSourceModel(self.source_model)
self.table_view.setModel(self.model)
# When sorting is enabled, the first column is initially displayed with
# the triangle indicating it is the sort index, even though no sorting
# has actually been done. Sort here for UI/model consistency.
if self.factory.sortable and not self.factory.reorderable:
self.model.sort(0, QtCore.Qt.SortOrder.AscendingOrder)
# Connect to the mode specific selection handler and select the first
# row/column/cell. Do this before creating the edit_view to make sure
# that it has a valid item to use when constructing its view.
smodel = self.table_view.selectionModel()
mode_slot = getattr(self, "_on_%s_selection" % factory.selection_mode)
smodel.selectionChanged.connect(mode_slot)
self.table_view.setCurrentIndex(self.model.index(0, 0))
# Create the toolbar if necessary
if factory.show_toolbar and len(factory.filters) > 0:
main_view = QtGui.QWidget()
layout = QtGui.QVBoxLayout(main_view)
layout.setContentsMargins(0, 0, 0, 0)
self.toolbar_ui = self.edit_traits(
parent=parent,
kind="subpanel",
view=View(
Group(
Item("filter{View}", editor=factory._filter_editor),
Item("filter_summary{Results}", style="readonly"),
spring,
orientation="horizontal",
),
resizable=True,
),
)
self.toolbar_ui.parent = self.ui
layout.addWidget(self.toolbar_ui.control)
layout.addWidget(self.table_view)
else:
main_view = self.table_view
# Create auxiliary editor and encompassing splitter if necessary
mode = factory.selection_mode
if (factory.edit_view == " ") or mode not in {"row", "rows"}:
self.control = main_view
else:
if factory.orientation == "horizontal":
self.control = QtGui.QSplitter(QtCore.Qt.Orientation.Horizontal)
else:
self.control = QtGui.QSplitter(QtCore.Qt.Orientation.Vertical)
self.control.setSizePolicy(
QtGui.QSizePolicy.Policy.Expanding, QtGui.QSizePolicy.Policy.Expanding
)
self.control.addWidget(main_view)
self.control.setStretchFactor(0, 2)
# Create the row editor below the table view
editor = InstanceEditor(view=factory.edit_view, kind="subpanel")
self._ui = self.edit_traits(
parent=self.control,
kind="subpanel",
view=View(
Item(
"selected_row",
style="custom",
editor=editor,
show_label=False,
resizable=True,
width=factory.edit_view_width,
height=factory.edit_view_height,
),
resizable=True,
handler=factory.edit_view_handler,
),
)
self._ui.parent = self.ui
self.control.addWidget(self._ui.control)
self.control.setStretchFactor(1, 1)
# Connect to the click and double click handlers
self.table_view.clicked.connect(self._on_click)
self.table_view.doubleClicked.connect(self._on_dclick)
# Make sure we listen for 'items' changes as well as complete list
# replacements
self.context_object.on_trait_change(
self.update_editor, self.extended_name + "_items", dispatch="ui"
)
# Listen for changes to traits on the objects in the list
self.context_object.on_trait_change(
self.refresh_editor, self.extended_name + ".-", dispatch="ui"
)
# Listen for changes on column definitions
self.on_trait_change(self._update_columns, "columns", dispatch="ui")
self.on_trait_change(
self._update_columns, "columns_items", dispatch="ui"
)
# Set up the required externally synchronized traits
is_list = mode in ("rows", "columns", "cells")
self.sync_value(factory.click, "click", "to")
self.sync_value(factory.dclick, "dclick", "to")
self.sync_value(factory.columns_name, "columns", is_list=True)
self.sync_value(factory.selected, "selected", is_list=is_list)
self.sync_value(
factory.selected_indices, "selected_indices", is_list=is_list
)
self.sync_value(factory.filter_name, "filter", "from")
self.sync_value(factory.filtered_indices, "filtered_indices", "to")
self.sync_value(factory.update_filter_name, "update_filter", "from")
self.auto_size = self.factory.auto_size
# Initialize the ItemDelegates for each column
self._update_columns()
def dispose(self):
"""Disposes of the contents of an editor."""
self.model.beginResetModel()
self.model.endResetModel()
# Make sure that the auxiliary UIs are properly disposed
if self.toolbar_ui is not None:
self.toolbar_ui.dispose()
if self._ui is not None:
self._ui.dispose()
# Remove listener for 'items' changes on object trait
self.context_object.on_trait_change(
self.update_editor, self.extended_name + "_items", remove=True
)
# Remove listener for changes to traits on the objects in the list
self.context_object.on_trait_change(
self.refresh_editor, self.extended_name + ".-", remove=True
)
# Remove listeners for column definition changes
self.on_trait_change(self._update_columns, "columns", remove=True)
self.on_trait_change(
self._update_columns, "columns_items", remove=True
)
super().dispose()
def update_editor(self):
"""Updates the editor when the object trait changes externally to the
editor."""
if self._no_notify:
return
self.table_view.setUpdatesEnabled(False)
try:
filtering = (
len(self.factory.filters) > 0 or self.filter is not None
)
if filtering:
self._update_filtering()
# invalidate the model, but do not reset it. Resetting the model
# may cause problems if the selection sync'ed traits are being used
# externally to manage the selections
self.model.invalidate()
self.table_view.resizeColumnsToContents()
if self.auto_size:
self.table_view.resizeRowsToContents()
finally:
self.table_view.setUpdatesEnabled(True)
def restore_prefs(self, prefs):
"""Restores any saved user preference information associated with the
editor.
"""
header = self.table_view.horizontalHeader()
if header is not None and "column_state" in prefs:
header.restoreState(prefs["column_state"])
def save_prefs(self):
"""Returns any user preference information associated with the editor."""
prefs = {}
header = self.table_view.horizontalHeader()
if header is not None:
prefs["column_state"] = header.saveState().data()
return prefs
def refresh_editor(self):
"""Requests that the underlying table widget to redraw itself."""
self.table_view.viewport().update()
def create_new_row(self):
"""Creates a new row object using the provided factory."""
factory = self.factory
kw = factory.row_factory_kw.copy()
if "__table_editor__" in kw:
kw["__table_editor__"] = self
return self.ui.evaluate(
factory.row_factory, *factory.row_factory_args, **kw
)
def items(self):
"""Returns the raw list of model objects."""
items = self.value
if not isinstance(items, SequenceTypes):
items = [items]
if self.factory and self.factory.reverse:
items = ReversedList(items)
return items
def callx(self, func, *args, **kw):
"""Call a function without notifying the underlying table view or
model."""
old = self._no_notify
self._no_notify = True
try:
func(*args, **kw)
finally:
self._no_notify = old
def setx(self, **keywords):
"""Set one or more attributes without notifying the underlying table
view or model."""
old = self._no_notify
self._no_notify = True
try:
for name, value in keywords.items():
setattr(self, name, value)
finally:
self._no_notify = old
def set_selection(self, objects=[], notify=True):
"""Sets the current selection to a set of specified objects."""
if not isinstance(objects, list):
objects = [objects]
mode = self.factory.selection_mode
indexes = []
flags = QtGui.QItemSelectionModel.SelectionFlag.ClearAndSelect
# In the case of row or column selection, we need a dummy value for the
# other dimension that has not been filtered.
source_index = self.model.mapToSource(self.model.index(0, 0))
source_row, source_column = source_index.row(), source_index.column()
# Selection mode is 'row' or 'rows'
if mode.startswith("row"):
flags |= QtGui.QItemSelectionModel.SelectionFlag.Rows
items = self.items()
for obj in objects:
try:
row = items.index(obj)
except ValueError:
continue
indexes.append(self.source_model.index(row, source_column))
# Selection mode is 'column' or 'columns'
elif mode.startswith("column"):
flags |= QtGui.QItemSelectionModel.SelectionFlag.Columns
for name in objects:
column = self._column_index_from_name(name)
if column != -1:
indexes.append(self.source_model.index(source_row, column))
# Selection mode is 'cell' or 'cells'
else:
items = self.items()
for obj, name in objects:
try:
row = items.index(obj)
except ValueError:
continue
column = self._column_index_from_name(name)
if column != -1:
indexes.append(self.source_model.index(row, column))
# Perform the selection so that only one signal is emitted
selection = QtGui.QItemSelection()
smodel = self.table_view.selectionModel()
if smodel is None:
# guard against selection during tear-down
return
for index in indexes:
index = self.model.mapFromSource(index)
if index.isValid():
smodel.setCurrentIndex(
index, QtGui.QItemSelectionModel.SelectionFlag.NoUpdate
)
selection.select(index, index)
smodel.blockSignals(not notify)
try:
if len(selection.indexes()):
smodel.clear()
smodel.select(selection, flags)
else:
smodel.clear()
finally:
smodel.blockSignals(False)
self.refresh_editor()
# -------------------------------------------------------------------------
# Private methods:
# -------------------------------------------------------------------------
def _column_index_from_name(self, name):
"""Returns the index of the column with the given name or -1 if no
column exists with that name."""
for i, column in enumerate(self.columns):
if name == column.name:
return i
return -1
def _customize_filters(self, filter):
"""Allows the user to customize the current set of table filters."""
filter_editor = TableFilterEditor(editor=self)
ui = filter_editor.edit_traits(parent=self.control)
if ui.result:
self.factory.filters = filter_editor.templates
self.filter = filter_editor.selected_filter
else:
self.setx(filter=filter)
def _update_filtering(self):
"""Update the filter summary and the filtered indices."""
items = self.items()
num_items = len(items)
f = self.filter
if f is None:
self._filtered_cache = None
self.filtered_indices = list(range(num_items))
self.filter_summary = "All %i items" % num_items
else:
if not callable(f):
f = f.filter
self._filtered_cache = fc = [f(item) for item in items]
self.filtered_indices = fi = [i for i, ok in enumerate(fc) if ok]
self.filter_summary = "%i of %i items" % (len(fi), num_items)
def _add_image(self, image_resource):
"""Adds a new image to the image map."""
image = image_resource.create_icon()
self.image_resources[image_resource] = image
self.images[image_resource.name] = image
return image
def _get_image(self, image):
"""Converts a user specified image to a QIcon."""
if isinstance(image, str):
self.image = image
image = self.image
if isinstance(image, ImageResource):
result = self.image_resources.get(image)
if result is not None:
return result
return self._add_image(image)
return self.images.get(image)
def _create_empty_menu(self):
"""Create a QMenu to display in empty space below the rows.
Returns a QMenu or None if no menu to display.
"""
if not self.factory.editable or self.factory.row_factory is None:
return None
empty_menu = QtGui.QMenu(self.table_view)
action = empty_menu.addAction("Add new item")
action.triggered.connect(self._on_context_append)
return empty_menu
def _create_header_menu(self):
"""Create a QMenu to display in the vertical header.
Returns a QMenu or None if no menu to display.
"""
header_menu = QtGui.QMenu(self.table_view)
if self.factory.editable:
if self.factory.row_factory is not None:
action = header_menu.addAction("Insert new item")
action.triggered.connect(self._on_context_insert)
if self.factory.deletable:
action = header_menu.addAction("Delete item")
action.triggered.connect(self._on_context_remove)
if self.factory.reorderable:
show_up = (self.header_row > 0)
show_down = (self.header_row < self.model.rowCount() - 1)
if not header_menu.isEmpty() and (show_up or show_down):
header_menu.addSeparator()
if show_up:
header_menu_up = header_menu.addAction("Move item up")
header_menu_up.triggered.connect(self._on_context_move_up)
if show_down:
header_menu_down = header_menu.addAction("Move item down")
header_menu_down.triggered.connect(self._on_context_move_down)
if header_menu.isEmpty():
return None
else:
return header_menu
# -- Trait Property getters/setters ---------------------------------------
@cached_property
def _get_selected_row(self):
"""Gets the selected row, or the first row if multiple rows are
selected."""
mode = self.factory.selection_mode
if mode.startswith("column"):
return None
elif mode == "row":
return self.selected
try:
if mode == "rows":
return self.selected[0]
elif mode == "cell":
return self.selected[0]
elif mode == "cells":
return self.selected[0][0]
except IndexError:
return None
@cached_property
def _get_selected_indices(self):
"""Gets the row,column indices which match the selected trait"""
selection_items = self.table_view.selectionModel().selection()
indices = self.model.mapSelectionToSource(selection_items).indexes()
if self.factory.selection_mode.startswith("row"):
indices = sorted(set(index.row() for index in indices))
elif self.factory.selection_mode.startswith("column"):
indices = sorted(set(index.column() for index in indices))
else:
indices = [(index.row(), index.column()) for index in indices]
if self.factory.selection_mode in {"rows", "columns", "cells"}:
return indices
elif len(indices) > 0:
return indices[0]
else:
return -1
def _set_selected_indices(self, indices):
if not isinstance(indices, list):
indices = [indices]
selected = []
if self.factory.selection_mode.startswith("row"):
for row in indices:
selected.append(self.value[row])
elif self.factory.selection_mode.startswith("column"):
for col in indices:
selected.append(self.columns[col].name)
else:
for row, col in indices:
selected.append((self.value[row], self.columns[col].name))
self.selected = selected
self.set_selection(self.selected, False)
# -- Trait Change Handlers ------------------------------------------------
def _filter_changed(self, old_filter, new_filter):
"""Handles the current filter being changed."""
if not self._no_notify:
if new_filter is customize_filter:
do_later(self._customize_filters, old_filter)
else:
self._update_filtering()
self.model.invalidate()
self.set_selection(self.selected)
def _update_columns(self):
"""Handle the column list being changed."""
self.table_view.setItemDelegate(TableDelegate(self.table_view))
for i, column in enumerate(self.columns):
if column.renderer:
self.table_view.setItemDelegateForColumn(i, column.renderer)
self.model.invalidate()
self.table_view.resizeColumnsToContents()
if self.auto_size:
self.table_view.resizeRowsToContents()
def _selected_changed(self, new):
"""Handle the selected row/column/cell being changed externally."""
if not self._no_notify:
self.set_selection(self.selected, notify=False)
def _update_filter_changed(self):
"""The filter has changed internally."""
self._filter_changed(self.filter, self.filter)
# -- Event Handlers -------------------------------------------------------
def _on_row_selection(self, added, removed):
"""Handle the row selection being changed."""
items = self.items()
indexes = self.table_view.selectionModel().selectedRows()
if len(indexes):
index = self.model.mapToSource(indexes[0])
selected = items[index.row()]
else:
selected = None
self.setx(selected=selected)
self.ui.evaluate(self.factory.on_select, self.selected)
def _on_rows_selection(self, added, removed):
"""Handle the rows selection being changed."""
items = self.items()
indexes = self.table_view.selectionModel().selectedRows()
selected = [
items[self.model.mapToSource(index).row()] for index in indexes
]
self.setx(selected=selected)
self.ui.evaluate(self.factory.on_select, self.selected)
def _on_column_selection(self, added, removed):
"""Handle the column selection being changed."""
indexes = self.table_view.selectionModel().selectedColumns()
if len(indexes):
index = self.model.mapToSource(indexes[0])
selected = self.columns[index.column()].name
else:
selected = ""
self.setx(selected=selected)
self.ui.evaluate(self.factory.on_select, self.selected)
def _on_columns_selection(self, added, removed):
"""Handle the columns selection being changed."""
indexes = self.table_view.selectionModel().selectedColumns()
selected = [
self.columns[self.model.mapToSource(index).column()].name
for index in indexes
]
self.setx(selected=selected)
self.ui.evaluate(self.factory.on_select, self.selected)
def _on_cell_selection(self, added, removed):
"""Handle the cell selection being changed."""
items = self.items()
indexes = self.table_view.selectionModel().selectedIndexes()
if len(indexes):
index = self.model.mapToSource(indexes[0])
obj = items[index.row()]
column_name = self.columns[index.column()].name
else:
obj = None
column_name = ""
selected = (obj, column_name)
self.setx(selected=selected)
self.ui.evaluate(self.factory.on_select, self.selected)
def _on_cells_selection(self, added, removed):
"""Handle the cells selection being changed."""
items = self.items()
indexes = self.table_view.selectionModel().selectedIndexes()
selected = []
for index in indexes:
index = self.model.mapToSource(index)
obj = items[index.row()]
column_name = self.columns[index.column()].name
selected.append((obj, column_name))
self.setx(selected=selected)
self.ui.evaluate(self.factory.on_select, self.selected)
def _on_click(self, index):
"""Handle a cell being clicked."""
index = self.model.mapToSource(index)
column = self.columns[index.column()]
obj = self.items()[index.row()]
# Fire the same event on the editor after mapping it to a model object
# and column name:
self.click = (obj, column)
# Invoke the column's click handler:
column.on_click(obj)
def _on_dclick(self, index):
"""Handle a cell being double clicked."""
index = self.model.mapToSource(index)
column = self.columns[index.column()]
obj = self.items()[index.row()]
# Fire the same event on the editor after mapping it to a model object
# and column name:
self.dclick = (obj, column)
# Invoke the column's double-click handler:
column.on_dclick(obj)
def _on_context_insert(self):
"""Handle 'insert item' being selected from the header context menu."""
self.model.insertRow(self.header_row)
def _on_context_append(self):
"""Handle 'add item' being selected from the empty space context
menu."""
self.model.insertRow(self.model.rowCount())
def _on_context_remove(self):
"""Handle 'remove item' being selected from the header context menu."""
self.model.removeRow(self.header_row)
def _on_context_move_up(self):
"""Handle 'move up' being selected from the header context menu."""
self.model.moveRow(self.header_row, self.header_row - 1)
def _on_context_move_down(self):
"""Handle 'move down' being selected from the header context menu."""
self.model.moveRow(self.header_row, self.header_row + 1)
# Define the SimpleEditor class.
SimpleEditor = TableEditor
# Define the ReadonlyEditor class.
ReadonlyEditor = TableEditor
# -------------------------------------------------------------------------
# Qt widgets that have been configured to behave as expected by Traits UI:
# -------------------------------------------------------------------------
class TableDelegate(QtGui.QStyledItemDelegate):
"""A QStyledItemDelegate which fetches Traits UI editors."""
def createEditor(self, parent, option, index):
"""Reimplemented to return the editor for a given index."""
model = index.model()
index = model.mapToSource(index)
table_editor = model._editor
column = table_editor.columns[index.column()]
obj = table_editor.items()[index.row()]
factory = column.get_editor(obj)
style = column.get_style(obj)
if factory is None:
return None
target, name = column.target_name(obj)
handler = default_handler()
if table_editor.ui.context is None:
ui = UI(handler=handler)
else:
context = table_editor.ui.context.copy()
context["table_editor_object"] = context["object"]
context["object"] = target
ui = UI(handler=handler, context=context)
# Create and initialize the editor
factory_method = getattr(factory, style + "_editor")
editor = factory_method(ui, target, name, "", parent)
editor.prepare(parent)
control = editor.control
control.setParent(parent)
# Required for QMouseEvents to propagate to the widget
control.setFocusPolicy(QtCore.Qt.FocusPolicy.StrongFocus)
# The table view's background will shine through unless the editor
# paints its own background
control.setAutoFillBackground(True)
# Make sure that editors are disposed of correctly
# will be disposed in closeEditor of the TableView
control._editor = editor
return control
def updateEditorGeometry(self, editor, option, index):
"""Update the editor's geometry."""
editor.setGeometry(option.rect)
def paint(self, painter, option, index):
self.initStyleOption(option, index)
if (option.state & QtGui.QStyle.StateFlag.State_Selected) and (
option.state & QtGui.QStyle.StateFlag.State_Active
):
factory = self.parent()._editor.factory
if factory.selection_bg_color is not None:
option.palette.setColor(
QtGui.QPalette.ColorRole.Highlight, factory.selection_bg_color_
)
if factory.selection_color is not None:
option.palette.setColor(
QtGui.QPalette.ColorRole.HighlightedText, factory.selection_color_
)
QtGui.QApplication.style().drawControl(
QtGui.QStyle.ControlElement.CE_ItemViewItem, option, painter, None
)
class TableView(QtGui.QTableView):
"""A QTableView configured to behave as expected by TraitsUI."""
_SELECTION_MAP = {
"row": (
QtGui.QAbstractItemView.SelectionBehavior.SelectRows,
QtGui.QAbstractItemView.SelectionMode.SingleSelection,
),
"rows": (
QtGui.QAbstractItemView.SelectionBehavior.SelectRows,
QtGui.QAbstractItemView.SelectionMode.ExtendedSelection,
),
"column": (
QtGui.QAbstractItemView.SelectionBehavior.SelectColumns,
QtGui.QAbstractItemView.SelectionMode.SingleSelection,
),
"columns": (
QtGui.QAbstractItemView.SelectionBehavior.SelectColumns,
QtGui.QAbstractItemView.SelectionMode.ExtendedSelection,
),
"cell": (
QtGui.QAbstractItemView.SelectionBehavior.SelectItems,
QtGui.QAbstractItemView.SelectionMode.SingleSelection,
),
"cells": (
QtGui.QAbstractItemView.SelectionBehavior.SelectItems,
QtGui.QAbstractItemView.SelectionMode.ExtendedSelection,
),
}
def __init__(self, editor):
"""Initialise the object."""
QtGui.QTableView.__init__(self)
self._initial_size = False
self._editor = editor
factory = editor.factory
# Configure the grid lines.
self.setShowGrid(factory.show_lines)
# Configure the selection behaviour.
self.setCornerButtonEnabled(False)
behav, mode = self._SELECTION_MAP[factory.selection_mode]
self.setSelectionBehavior(behav)
self.setSelectionMode(mode)
# Configure the editing behavior.
triggers = (
QtGui.QAbstractItemView.EditTrigger.DoubleClicked
| QtGui.QAbstractItemView.EditTrigger.SelectedClicked
)
if factory.edit_on_first_click and not factory.reorderable:
triggers |= QtGui.QAbstractItemView.EditTrigger.CurrentChanged
self.setEditTriggers(triggers)
# Configure the reordering and sorting behavior.
self.setDragEnabled(True)
self.viewport().setAcceptDrops(True)
self.setDropIndicatorShown(True)
if factory.reorderable:
self.setDragDropMode(QtGui.QAbstractItemView.DragDropMode.InternalMove)
if factory.sortable:
self.setSortingEnabled(True)
if factory._qt_stylesheet is not None:
self.setStyleSheet(factory._qt_stylesheet)
self.resizeColumnsToContents()
def setModel(self, model):
super().setModel(model)
self._update_header_sizing()
def contextMenuEvent(self, event):
"""Reimplemented to create context menus for cells and empty space."""
# Determine the logical indices of the cell where click occured
hheader, vheader = self.horizontalHeader(), self.verticalHeader()
position = event.globalPos()
row = vheader.logicalIndexAt(vheader.mapFromGlobal(position))
column = hheader.logicalIndexAt(hheader.mapFromGlobal(position))
# Map the logical row index to a real index for the source model
model = self.model()
row = model.mapToSource(model.index(row, 0)).row()
# Show a context menu for empty space at bottom of table...
editor = self._editor
if row == -1:
empty_menu = editor._create_empty_menu()
if empty_menu is not None:
event.accept()
empty_menu.exec_(position)
# ...or show a context menu for a cell.
elif column != -1:
obj = editor.items()[row]
column = editor.columns[column]
menu_manager = column.get_menu(obj)
if menu_manager is None:
menu_manager = editor.factory.menu
if menu_manager is not None:
event.accept()
selected = editor.selected
if not isinstance(selected, SequenceTypes):
selected = [selected]
if obj not in selected:
selected = [obj]
editor.set_menu_context(selected, obj, column)
menu = menu_manager.create_menu(self, controller=editor)
menu.exec_(position)
def eventFilter(self, obj, event):
"""Reimplemented to create context menu for the vertical header."""
vheader = self.verticalHeader()
if obj is vheader and event.type() == QtCore.QEvent.Type.ContextMenu:
position = event.globalPos()
editor = self._editor
row = vheader.logicalIndexAt(event.pos().y())
if row == -1:
empty_menu = editor._create_empty_menu()
if empty_menu is not None:
event.accept()
empty_menu.exec_(position)
else:
editor.header_row = row
header_menu = editor._create_header_menu()
if header_menu is not None:
event.accept()
header_menu.exec_(position)
return True
else:
return QtGui.QTableView.eventFilter(self, obj, event)
def resizeEvent(self, event):
"""Reimplemented to size the table columns when the size of the table
changes. Because the layout algorithm requires that the available
space be known, we have to wait until the UI that contains this table
gives it its initial size."""
QtGui.QTableView.resizeEvent(self, event)
if self._editor.auto_size:
self.resizeColumnsToContents()
self.resizeRowsToContents()
else:
parent = self.parent()
if (
not self._initial_size
and parent
and (self.isVisible() or isinstance(parent, QtGui.QMainWindow))
):
self._initial_size = True
if self._editor.auto_size:
self.resizeColumnsToContents()
self.resizeRowsToContents()
def sizeHint(self):
"""Reimplemented to define a better size hint for the width of the
TableEditor."""
size_hint = QtGui.QTableView.sizeHint(self)
# This method is sometimes called by Qt after the editor has been
# disposed but before this control has been deleted:
if self._editor.factory is None:
return size_hint
width = self.style().pixelMetric(
QtGui.QStyle.PixelMetric.PM_ScrollBarExtent, QtGui.QStyleOptionHeader(), self
)
for column in range(len(self._editor.columns)):
width += self.sizeHintForColumn(column)
size_hint.setWidth(width)
return size_hint
def sizeHintForColumn(self, column_index):
"""Reimplemented to support absolute width specification via
TableColumns and to improve the metric for autosizing columns."""
editor = self._editor
column = editor.columns[column_index]
requested_width = column.get_width()
# Autosize based on column contents and label width. Qt's default
# implementation of this function does content, we handle the label.
if requested_width < 1:
base_width = QtGui.QTableView.sizeHintForColumn(self, column_index)
# Determine what font to use in the calculation
font = column.get_text_font(None)
if font is None:
font = self.font()
font.setBold(True)
else:
font = QtGui.QFont(font)
# Determine the width of the column label
text = column.get_label()
# QFontMetrics.width() is deprecated and Qt docs suggest using
# horizontalAdvance() instead, but is only available since Qt 5.11
if QtCore.__version_info__ >= (5, 11):
width = QtGui.QFontMetrics(font).horizontalAdvance(text)
else:
width = QtGui.QFontMetrics(font).width(text)
# Add margin to the calculated width as appropriate
style = self.style()
option = QtGui.QStyleOptionHeader()
width += (
style.pixelMetric(
QtGui.QStyle.PixelMetric.PM_HeaderGripMargin, option, self
)
* 2
)
if editor.factory.sortable and not editor.factory.reorderable:
# Add size of sort indicator
width += style.pixelMetric(
QtGui.QStyle.PixelMetric.PM_HeaderMarkSize, option, self
)
# Add distance between sort indicator and text
width += style.pixelMetric(
QtGui.QStyle.PixelMetric.PM_HeaderMargin, option, self
)
return max(base_width, width)
# Or else set width absolutely
else:
return requested_width
def resizeColumnsToContents(self):
"""Support proportional column width specifications."""
# TODO: The proportional size specification approach found in the
# TableColumns is not entirely compatible with the ability to
# specify the resize_mode. Namely, there are combinations of
# specifications that are redundant, and others which are
# contradictory. Rework this method so that the various values
# for **width** have a well-defined, sensible meaning for each
# of the possible values of resize_mode.
editor = self._editor
available_space = self.viewport().width()
hheader = self.horizontalHeader()
# Compute sizes for columns with absolute or no size requests
proportional = []
for column_index in range(len(editor.columns)):
column = editor.columns[column_index]
requested_width = column.get_width()
if (
column.resize_mode in ("interactive", "stretch")
and 0 < requested_width <= 1.0
):
proportional.append((column_index, requested_width))
elif (
column.resize_mode == "interactive"
and requested_width < 0
and self._initial_size
):
# Keep previous size if initial sizing has been done
available_space -= hheader.sectionSize(column_index)
else:
base_width = hheader.sectionSizeHint(column_index)
width = max(base_width, self.sizeHintForColumn(column_index))
hheader.resizeSection(column_index, width)
available_space -= width
# Now use the remaining space for columns with proportional width
# requests
for column_index, percent in proportional:
base_width = hheader.sectionSizeHint(column_index)
width = max(base_width, int(percent * available_space))
hheader.resizeSection(column_index, width)
def closeEditor(self, control, hint):
# dispose traits editor associated with control if any
editor = getattr(control, "_editor", None)
if editor is not None:
editor.dispose()
delattr(control, "_editor")
return super().closeEditor(control, hint)
def _update_header_sizing(self):
"""Header section sizing can be done only after a valid model is set.
Otherwise results in segfault with Qt5.
"""
editor = self._editor
factory = editor.factory
# Configure the row headings.
vheader = self.verticalHeader()
set_resize_mode = set_qheader_section_resize_mode(vheader)
insertable = factory.row_factory is not None
if (
factory.editable and (insertable or factory.deletable)
) or factory.reorderable:
vheader.installEventFilter(self)
set_resize_mode(QtGui.QHeaderView.ResizeMode.ResizeToContents)
elif not factory.show_row_labels:
vheader.hide()
if factory.row_height > 0:
vheader.setDefaultSectionSize(factory.row_height)
self.setAlternatingRowColors(factory.alternate_bg_color)
self.setHorizontalScrollMode(QtGui.QAbstractItemView.ScrollMode.ScrollPerPixel)
# Configure the column headings.
# We detect if there are any stretchy sections at all; if not, then
# we make the last non-fixed-size column stretchy.
hheader = self.horizontalHeader()
set_resize_mode = set_qheader_section_resize_mode(hheader)
resize_mode_map = dict(
interactive=QtGui.QHeaderView.ResizeMode.Interactive,
fixed=QtGui.QHeaderView.ResizeMode.Fixed,
stretch=QtGui.QHeaderView.ResizeMode.Stretch,
resize_to_contents=QtGui.QHeaderView.ResizeMode.ResizeToContents,
)
stretchable_columns = []
for i, column in enumerate(editor.columns):
set_resize_mode(i, resize_mode_map[column.resize_mode])
if column.resize_mode in ("stretch", "interactive"):
stretchable_columns.append(i)
if not stretchable_columns:
# Use the behavior from before the "resize_mode" trait was added
# to TableColumn
hheader.setStretchLastSection(True)
else:
# hheader.setSectionResizeMode(
# stretchable_columns[-1], QtGui.QHeaderView.ResizeMode.Stretch)
hheader.setStretchLastSection(False)
if factory.show_column_labels:
hheader.setHighlightSections(False)
else:
hheader.hide()
# -------------------------------------------------------------------------
# Editor for configuring the filters available to a TableEditor:
# -------------------------------------------------------------------------
class TableFilterEditor(HasTraits):
"""An editor that manages table filters."""
# -------------------------------------------------------------------------
# Trait definitions:
# -------------------------------------------------------------------------
#: TableEditor this editor is associated with
editor = Instance(TableEditor)
#: The list of filters
filters = List(TableFilter)
#: The list of available templates from which filters can be created
templates = Property(List(TableFilter), observe="filters")
#: The currently selected filter template
selected_template = Instance(TableFilter)
#: The currently selected filter
selected_filter = Instance(TableFilter, allow_none=True)
#: The view to use for the current filter
selected_filter_view = Property(observe="selected_filter")
#: Buttons for add/removing filters
add_button = Button("New")
remove_button = Button("Delete")
# The default view for this editor
view = View(
Group(
Group(
Group(
Item("add_button", enabled_when="selected_template"),
Item(
"remove_button",
enabled_when="len(templates) > 1 and "
"selected_filter is not None",
),
orientation="horizontal",
show_labels=False,
),
Label("Base filter for new filters:"),
Item("selected_template", editor=EnumEditor(name="templates")),
Item(
"selected_filter",
style="custom",
editor=EnumEditor(name="filters", mode="list"),
),
show_labels=False,
),
Item(
"selected_filter",
width=0.75,
style="custom",
editor=InstanceEditor(view_name="selected_filter_view"),
),
id="TableFilterEditorSplit",
show_labels=False,
layout="split",
orientation="horizontal",
),
id="traitsui.qt.table_editor.TableFilterEditor",
buttons=["OK", "Cancel"],
kind="livemodal",
resizable=True,
width=800,
height=400,
title="Customize filters",
)
# -------------------------------------------------------------------------
# Private methods:
# -------------------------------------------------------------------------
# -- Trait Property getter/setters ----------------------------------------
@cached_property
def _get_selected_filter_view(self):
view = None
if self.selected_filter:
model = self.editor.model
index = model.mapToSource(model.index(0, 0))
if index.isValid():
obj = self.editor.items()[index.row()]
else:
obj = None
view = self.selected_filter.edit_view(obj)
return view
@cached_property
def _get_templates(self):
templates = [f for f in self.editor.factory.filters if f.template]
templates.extend(self.filters)
return templates
# -- Trait Change Handlers ------------------------------------------------
def _editor_changed(self):
self.filters = [
f.clone_traits()
for f in self.editor.factory.filters
if not f.template
]
self.selected_template = self.templates[0]
@observe('add_button')
def _create_and_select_new_filter(self, event):
"""Create a new filter based on the selected template and select it."""
new_filter = self.selected_template.clone_traits()
new_filter.template = False
new_filter.name = new_filter._name = "New filter"
self.filters.append(new_filter)
self.selected_filter = new_filter
@observe("remove_button")
def _delete_selected_filter(self, event):
"""Delete the currently selected filter."""
if self.selected_template == self.selected_filter:
self.selected_template = self.templates[0]
index = self.filters.index(self.selected_filter)
del self.filters[index]
if index < len(self.filters):
self.selected_filter = self.filters[index]
else:
self.selected_filter = None
@observe("selected_filter:name")
def _update_filter_list(self, event):
"""A hack to make the EnumEditor watching the list of filters refresh
their text when the name of the selected filter changes.
"""
filters = self.filters
self.filters = []
self.filters = filters
|