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
|
#------------------------------------------------------------------------------
#
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/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!
#
# Author: David C. Morrill
# Date: 07/01/2005
#
#------------------------------------------------------------------------------
""" Defines the filter object used to filter items displayed in a table editor.
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
from __future__ import absolute_import
from traits.api import (Any, Bool, Callable, Enum, Event, Expression, HasPrivateTraits,
Instance, List, Str, Trait)
from .editor_factory import EditorFactory
from .editors.api import EnumEditor
from .group import Group
from .include import Include
from .item import Item
from .menu import Action
from .table_column import ObjectColumn
from .view import View
#-------------------------------------------------------------------------------
# Trait definitions:
#-------------------------------------------------------------------------------
GenericTableFilterRuleOperation = Trait( '=', {
'=': 'eq',
'<>': 'ne',
'<': 'lt',
'<=': 'le',
'>': 'gt',
'>=': 'ge',
'contains': 'contains',
'starts with': 'starts_with',
'ends with': 'ends_with'
} )
#-------------------------------------------------------------------------------
# 'TableFilter' class:
#-------------------------------------------------------------------------------
class TableFilter ( HasPrivateTraits ):
""" Filter for items displayed in a table.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# UI name of this filter (so the user can identify it in the UI)
name = Str( 'Default filter' )
# Default name that can be automatically overridden
_name = Str( 'Default filter' )
# A user-readable description of what kind of object satisfies the filter
desc = Str( 'All items' )
# A callable function that returns whether the passed object is allowed
# by the filter
allowed = Callable( lambda object: True, transient = True )
# Is the filter a template (i.e., non-deletable, non-editable)?
template = Bool( False )
#---------------------------------------------------------------------------
# Class constants:
#---------------------------------------------------------------------------
# Traits that are ignored by the _anytrait_changed() handler
ignored_traits = [ '_name', 'template', 'desc' ]
#---------------------------------------------------------------------------
# Traits view definitions:
#---------------------------------------------------------------------------
traits_view = View(
'name{Filter name}', '_',
Include( 'filter_view' ),
title = 'Edit Filter',
width = 0.2,
buttons = [ 'OK',
'Cancel',
Action(
name = 'Help',
action = 'show_help',
defined_when = "ui.view_elements.content['filter_view']"
".help_id != ''"
)
]
)
searchable_view = View( [
[ Include( 'search_view' ), '|[]' ],
[ 'handler.status~', '|[]<>' ],
[ 'handler.find_next`Find the next matching item`',
'handler.find_previous`Find the previous matching item`',
'handler.select`Select all matching items`',
'handler.OK`Exit search`', '-<>' ],
'|<>' ],
title = 'Search for',
kind = 'livemodal',
width = 0.25 )
search_view = Group( Include( 'filter_view' ) )
filter_view = Group()
#---------------------------------------------------------------------------
# Returns whether a specified object meets the filter/search criteria:
# (Should normally be overridden)
#---------------------------------------------------------------------------
def filter ( self, object ):
""" Returns whether a specified object meets the filter or search
criteria.
"""
return self.allowed( object )
#---------------------------------------------------------------------------
# Returns a user readable description of what kind of object will
# satisfy the filter:
# (Should normally be overridden):
#---------------------------------------------------------------------------
def description ( self ):
""" Returns a user-readable description of what kind of object
satisfies the filter.
"""
return self.desc
#---------------------------------------------------------------------------
# Edits the contents of the filter:
#---------------------------------------------------------------------------
def edit ( self, object ):
""" Edits the contents of the filter.
"""
return self.edit_traits( view = self.edit_view( object ),
kind = 'livemodal' )
def edit_view ( self, object ):
""" Return a view to use for editing the filter.
The ''object'' parameter is a sample object for the table that the
filter will be applied to. It is supplied in case the filter needs to
extract data or metadata from the object. If the table is empty, the
''object'' argument is None.
"""
return None
#---------------------------------------------------------------------------
# 'object' interface:
#---------------------------------------------------------------------------
def __str__ ( self ):
return self.name
#---------------------------------------------------------------------------
# Event handlers:
#---------------------------------------------------------------------------
def _anytrait_changed ( self, name, old, new ):
if ((name not in self.ignored_traits) and
((self.name == self._name) or (self.name == ''))):
self.name = self._name = self.description()
#-------------------------------------------------------------------------------
# 'EvalTableFilter' class:
#-------------------------------------------------------------------------------
class EvalTableFilter ( TableFilter ):
""" A table filter based on evaluating an expression.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Override the standard **name** trait
name = 'Default evaluation filter'
# Python expression which will be applied to each table item
expression = Expression
#---------------------------------------------------------------------------
# Traits view definitions:
#---------------------------------------------------------------------------
filter_view = Group( 'expression' )
#---------------------------------------------------------------------------
# Returns whether a specified object meets the filter/search criteria:
# (Should normally be overridden)
#---------------------------------------------------------------------------
def filter ( self, object ):
""" Returns whether a specified object meets the filter or search
criteria.
"""
if self._traits is None:
self._traits = object.trait_names()
try:
return eval( self.expression_, globals(),
object.get( *self._traits ) )
except:
return False
#---------------------------------------------------------------------------
# Returns a user readable description of what kind of object will
# satisfy the filter:
# (Should normally be overridden):
#---------------------------------------------------------------------------
def description ( self ):
""" Returns a user readable description of what kind of object
satisfies the filter.
"""
return self.expression
#-------------------------------------------------------------------------------
# 'GenericTableFilterRule' class:
#-------------------------------------------------------------------------------
class GenericTableFilterRule ( HasPrivateTraits ):
""" A general rule used by a table filter.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Filter this rule is part of
filter = Instance( 'RuleTableFilter' )
# Is this rule enabled?
enabled = Bool( False )
# Is this rule an 'and' rule or an 'or' rule?
and_or = Enum( 'and', 'or' )
# EnumEditor used to edit the **name** trait:
name_editor = Instance( EditorFactory )
# Name of the object trait that this rule applies to
name = Str
# Operation to be applied in the rule
operation = GenericTableFilterRuleOperation
# Editor used to edit the **value** trait
value_editor = Instance( EditorFactory )
# Value to use in the operation when applying the rule to an object
value = Any
#---------------------------------------------------------------------------
# Class constants:
#---------------------------------------------------------------------------
# Traits that are ignored by the _anytrait_changed() handler
ignored_traits = [ 'filter', 'name_editor', 'value_editor' ]
#---------------------------------------------------------------------------
# Initializes the object:
#---------------------------------------------------------------------------
def __init__ ( self, **traits ):
super( GenericTableFilterRule, self ).__init__( **traits )
if self.name == '':
names = self.filter._trait_values.keys()
if len( names ) > 0:
names.sort()
self.name = names[0]
self.enabled = False
#---------------------------------------------------------------------------
# Handles the value of the 'name' trait changing:
#---------------------------------------------------------------------------
def _name_changed ( self, name ):
""" Handles a change to the value of the **name** trait.
"""
filter = self.filter
if (filter is not None) and (filter._object is not None):
self.value = filter._trait_values.get( name )
self.value_editor = filter._object.base_trait( name ).get_editor()
#---------------------------------------------------------------------------
# Event handlers:
#---------------------------------------------------------------------------
def _anytrait_changed ( self, name, old, new ):
if (name not in self.ignored_traits) and (self.filter is not None):
self.filter.modified = True
if name != 'enabled':
self.enabled = True
#---------------------------------------------------------------------------
# Clones a new object from this one, optionally copying only a specified
# set of traits:
#---------------------------------------------------------------------------
def clone_traits ( self, traits = None, memo = None, copy = None,
**metadata ):
""" Clones a new object from this one, optionally copying only a
specified set of traits."""
return super( GenericTableFilterRule, self ).clone_traits( traits,
memo, copy, **metadata ).set(
enabled = self.enabled,
name = self.name )
#---------------------------------------------------------------------------
# Returns a description of the filter:
#---------------------------------------------------------------------------
def description ( self ):
""" Returns a description of the filter.
"""
return '%s %s %s' % ( self.name, self.operation, self.value )
#---------------------------------------------------------------------------
# Returns whether the rule is true for a specified object:
#---------------------------------------------------------------------------
def is_true ( self, object ):
""" Returns whether the rule is true for a specified object.
"""
try:
value1 = getattr( object, self.name )
type1 = type( value1 )
value2 = self.value
if type1 is not type( value2 ):
value2 = type1( value2 )
return getattr( self, self.operation_ )( value1, value2 )
except:
return False
#---------------------------------------------------------------------------
# Implemenations of the various rule operations:
#---------------------------------------------------------------------------
def eq ( self, value1, value2 ):
return (value1 == value2)
def ne ( self, value1, value2 ):
return (value1 != value2)
def lt ( self, value1, value2 ):
return (value1 < value2)
def le ( self, value1, value2 ):
return (value1 <= value2)
def gt ( self, value1, value2 ):
return (value1 > value2)
def ge ( self, value1, value2 ):
return (value1 >= value2)
def contains ( self, value1, value2 ):
return (value1.lower().find( value2.lower() ) >= 0)
def starts_with ( self, value1, value2 ):
return (value1[ : len( value2 ) ].lower() == value2.lower())
def ends_with ( self, value1, value2 ):
return (value1[ -len( value2 ): ].lower() == value2.lower())
#-------------------------------------------------------------------------------
# 'GenericTableFilterRuleEnabledColumn' class:
#-------------------------------------------------------------------------------
class GenericTableFilterRuleEnabledColumn ( ObjectColumn ):
""" Table column that indicates whether a filter rule is enabled.
"""
#---------------------------------------------------------------------------
# Returns the value of the column for a specified object:
#---------------------------------------------------------------------------
def get_value ( self, object ):
""" Returns the traits editor of the column for a specified object.
"""
return [ '', '==>' ][ object.enabled ]
#-------------------------------------------------------------------------------
# 'GenericTableFilterRuleAndOrColumn' class:
#-------------------------------------------------------------------------------
class GenericTableFilterRuleAndOrColumn ( ObjectColumn ):
""" Table column that displays whether a filter rule is conjoining ('and')
or disjoining ('or').
"""
#---------------------------------------------------------------------------
# Returns the value of the column for a specified object:
#---------------------------------------------------------------------------
def get_value ( self, object ):
""" Returns the traits editor of the column for a specified object.
"""
if object.and_or == 'or':
return 'or'
return ''
#-------------------------------------------------------------------------------
# 'GenericTableFilterRuleNameColumn' class:
#-------------------------------------------------------------------------------
class GenericTableFilterRuleNameColumn ( ObjectColumn ):
""" Table column for the name of an object trait.
"""
#---------------------------------------------------------------------------
# Returns the traits editor of the column for a specified object:
#---------------------------------------------------------------------------
def get_editor ( self, object ):
""" Returns the traits editor of the column for a specified object.
"""
return object.name_editor
#-------------------------------------------------------------------------------
# 'GenericTableFilterRuleValueColumn' class:
#-------------------------------------------------------------------------------
class GenericTableFilterRuleValueColumn ( ObjectColumn ):
""" Table column for the value of an object trait.
"""
#---------------------------------------------------------------------------
# Returns the traits editor of the column for a specified object:
#---------------------------------------------------------------------------
def get_editor ( self, object ):
""" Returns the traits editor of the column for a specified object.
"""
return object.value_editor
#-------------------------------------------------------------------------------
# Defines the columns to display in the generic filter rule table:
#-------------------------------------------------------------------------------
# Columns to display in the table for generic filter rules.
generic_table_filter_rule_columns = [
GenericTableFilterRuleAndOrColumn( name = 'and_or', label = 'or' ),
GenericTableFilterRuleNameColumn( name = 'name' ),
ObjectColumn( name = 'operation' ),
GenericTableFilterRuleValueColumn( name = 'value' )
]
#-------------------------------------------------------------------------------
# 'RuleTableFilter' class:
#-------------------------------------------------------------------------------
class RuleTableFilter ( TableFilter ):
""" A table filter based on rules.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Overrides the default **name** trait
name = 'Default rule-based filter'
# List of the filter rules to be applied
rules = List( GenericTableFilterRule )
# Event fired when the contents of the filter have changed
modified = Event
# Persistence ID of the view
view_id = Str( 'traitsui.table_filter.RuleTableFilter' )
# Sample object that the filter will apply to
_object = Any
# Map of trait names and default values
_trait_values = Any
#---------------------------------------------------------------------------
# Traits view definitions:
#---------------------------------------------------------------------------
error_view = View(
Item( label = 'A menu or rule based filter can only be created for '
'tables with at least one entry'
),
title = 'Error Creating Filter',
kind = 'livemodal',
close_result = False,
buttons = [ 'Cancel' ]
)
#---------------------------------------------------------------------------
# Returns whether a specified object meets the filter/search criteria:
# (Should normally be overridden)
#---------------------------------------------------------------------------
def filter ( self, object ):
""" Returns whether a specified object meets the filter or search
criteria.
"""
is_first = is_true = True
for rule in self.rules:
if rule.and_or == 'or':
if is_true and (not is_first):
return True
is_true = True
if is_true:
is_true = rule.is_true( object )
is_first = False
return is_true
#---------------------------------------------------------------------------
# Returns a user readable description of what kind of object will
# satisfy the filter:
# (Should normally be overridden):
#---------------------------------------------------------------------------
def description ( self ):
""" Returns a user-readable description of the kind of object that
satisfies the filter.
"""
ors = []
ands = []
if len( self.rules ) > 0:
for rule in self.rules:
if rule.and_or == 'or':
if len( ands ) > 0:
ors.append( ' and '.join( ands ) )
ands = []
ands.append( rule.description() )
if len( ands ) > 0:
ors.append( ' and '.join( ands ) )
if len( ors ) == 1:
return ors[0]
if len( ors ) > 1:
return ' or '.join( [ '(%s)' % t for t in ors ] )
return super( RuleTableFilter, self ).description()
#---------------------------------------------------------------------------
# Edits the contents of the filter:
#---------------------------------------------------------------------------
def edit_view ( self, object ):
""" Return a view to use for editing the filter.
The ''object'' parameter is a sample object for the table that the
filter will be applied to. It is supplied in case the filter needs to
extract data or metadata from the object. If the table is empty, the
''object'' argument is None.
"""
self._object = object
if object is None:
return self.edit_traits( view = 'error_view' )
names = object.editable_traits()
self._trait_values = object.get( names )
return View(
[ [ 'name{Filter name}', '_' ],
[ Item( 'rules',
id = 'rules_table',
editor = self._get_table_editor( names ) ),
'|<>' ] ],
id = self.view_id,
title = 'Edit Filter',
kind = 'livemodal',
resizable = True,
buttons = [ 'OK', 'Cancel' ],
width = 0.4,
height = 0.3 )
#---------------------------------------------------------------------------
# Returns a table editor to use for editing the filter:
#---------------------------------------------------------------------------
def _get_table_editor ( self, names ):
""" Returns a table editor to use for editing the filter.
"""
from .api import TableEditor
return TableEditor( columns = generic_table_filter_rule_columns,
orientation = 'vertical',
deletable = True,
sortable = False,
configurable = False,
auto_size = False,
auto_add = True,
row_factory = GenericTableFilterRule,
row_factory_kw = {
'filter': self,
'name_editor': EnumEditor( values = names ) } )
#---------------------------------------------------------------------------
# Returns the state to be pickled (override of object):
#---------------------------------------------------------------------------
def __getstate__ ( self ):
""" Returns the state to be pickled.
This definition overrides **object**.
"""
dict = self.__dict__.copy()
if '_object' in dict:
del dict[ '_object' ]
del dict[ '_trait_values' ]
return dict
#---------------------------------------------------------------------------
# Handles the 'rules' trait being changed:
#---------------------------------------------------------------------------
def _rules_changed ( self, rules ):
""" Handles a change to the **rules** trait.
"""
for rule in rules:
rule.filter = self
#-------------------------------------------------------------------------------
# Defines the columns to display in the menu filter rule table:
#-------------------------------------------------------------------------------
# Columns to display in the table for menu filters.
menu_table_filter_rule_columns = [
GenericTableFilterRuleEnabledColumn( name = 'enabled', label = '' ),
GenericTableFilterRuleNameColumn( name = 'name' ),
ObjectColumn( name = 'operation' ),
GenericTableFilterRuleValueColumn( name = 'value' )
]
#-------------------------------------------------------------------------------
# 'MenuTableFilter' class:
#-------------------------------------------------------------------------------
class MenuTableFilter ( RuleTableFilter ):
""" A table filter based on a menu of rules.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Overrides the default **name** trait
name = 'Default menu-based filter'
# Overrides the persistence ID of the view
view_id = Str( 'traitsui.table_filter.MenuTableFilter' )
#---------------------------------------------------------------------------
# Returns whether a specified object meets the filter/search criteria:
# (Should normally be overridden)
#---------------------------------------------------------------------------
def filter ( self, object ):
""" Returns whether a specified object meets the filter or search
criteria.
"""
for rule in self.rules:
if rule.enabled and (not rule.is_true( object )):
return False
return True
#---------------------------------------------------------------------------
# Returns a user readable description of what kind of object will
# satisfy the filter:
# (Should normally be overridden):
#---------------------------------------------------------------------------
def description ( self ):
""" Returns a user8readable description of what kind of object
satisfies the filter.
"""
result = ' and '.join( [ rule.description() for rule in self.rules
if rule.enabled ] )
if result != '':
return result
return 'All items'
#---------------------------------------------------------------------------
# Returns a table editor to use for editing the filter:
#---------------------------------------------------------------------------
def _get_table_editor ( self, names ):
""" Returns a table editor to use for editing the filter.
"""
from .api import TableEditor
names = self._object.editable_traits()
name_editor = EnumEditor( values = names )
if len( self.rules ) == 0:
self.rules = [ GenericTableFilterRule(
filter = self,
name_editor = name_editor ).set(
name = name )
for name in names ]
for rule in self.rules:
rule.enabled = False
return TableEditor( columns = menu_table_filter_rule_columns,
orientation = 'vertical',
deletable = True,
sortable = False,
configurable = False,
auto_size = False,
auto_add = True,
row_factory = GenericTableFilterRule,
row_factory_kw = {
'filter': self,
'name_editor': name_editor } )
#-------------------------------------------------------------------------------
# Define some standard template filters:
#-------------------------------------------------------------------------------
EvalFilterTemplate = EvalTableFilter( name = 'Evaluation filter template',
template = True )
RuleFilterTemplate = RuleTableFilter( name = 'Rule-based filter template',
template = True )
MenuFilterTemplate = MenuTableFilter( name = 'Menu-based filter template',
template = True )
|