~ubuntu-branches/ubuntu/utopic/python-chaco/utopic

« back to all changes in this revision

Viewing changes to enthought/chaco/tools/range_selection.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 19:03:54 UTC
  • mfrom: (7.2.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110406190354-rwd55l2ezjecfo41
Tags: 3.4.0-2
d/rules: fix pyshared directory path (Closes: #621116)

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from numpy import array
5
5
 
6
6
# Enthought library imports
7
 
from enthought.traits.api import Any, Array, Bool, Enum, Event, Float, Int, List, \
8
 
                             Property, Trait, Tuple
 
7
from enthought.traits.api import Any, Array, Bool, Enum, Event, Float, Int, Instance, List, \
 
8
                             Property, Str, Trait, Tuple
 
9
from enthought.enable.api import KeySpec
9
10
 
10
11
# Chaco imports
11
12
from enthought.chaco.api import AbstractController
25
26
    # and fires change-events as the user is dragging.
26
27
    selection = Property
27
28
    
 
29
    selection_mode = Enum("set", "append")
 
30
    
28
31
    # This event is fired whenever the user completes the selection, or when a
29
32
    # finalized selection gets modified.  The value of the event is the data
30
33
    # space range.
31
34
    selection_completed = Event
32
35
 
 
36
    # The name of the metadata on the datasource that we will write self.selection to
 
37
    metadata_name = Str("selections")
 
38
 
 
39
    # Either "set" or "append", depending on whether self.append_key was held down
 
40
    selection_mode_metadata_name = Str("selection_mode")
 
41
    
 
42
    # The name of the metadata on the datasource that we will set to a numpy
 
43
    # boolean array for masking the datasource's data
 
44
    mask_metadata_name = Str("selection_masks")
 
45
   
33
46
    # The possible event states of this selection tool (overrides 
34
47
    # enable.Interactor).
35
48
    #
86
99
    # Allow the left button begin a selection?
87
100
    left_button_selects = Bool(False)
88
101
    
 
102
    # Disable all left-mouse button interactions?
 
103
    disable_left_mouse = Bool(False)
 
104
 
89
105
    # Allow the tool to be put into the deselected state via mouse clicks
90
106
    allow_deselection = Bool(True)
 
107
 
 
108
    # The minimum span, in pixels, of a selection region.  Any attempt to
 
109
    # select a region smaller than this will be treated as a deselection.
 
110
    minimum_selection = Int(5)
 
111
    
 
112
    # The key which, if held down while the mouse is being dragged, will 
 
113
    # indicate that the selection should be appended to an existing selection
 
114
    # as opposed to overwriting it.
 
115
    append_key = Instance(KeySpec, args=(None, "control"))
91
116
    
92
117
    #------------------------------------------------------------------------
93
118
    # Private traits
165
190
        
166
191
        Otherwise, the selection becomes deselected.
167
192
        """
 
193
        if self.disable_left_mouse:
 
194
            return
 
195
 
168
196
        screen_bounds = self._get_selection_screencoords()
169
197
        if screen_bounds is None:
170
198
            self.deselect(event)
224
252
                    self.event_state = "selecting"
225
253
                    self._drag_edge = "low"
226
254
                    self.selecting_mouse_move(event)
227
 
                elif self.allow_deselection:
228
 
                    self.deselect(event)
 
255
                #elif self.allow_deselection:
 
256
                #    self.deselect(event)
229
257
                else:
230
258
                    # Treat this as a combination deselect + right down
231
259
                    self.deselect(event)
281
309
        
282
310
        Switches the tool to the 'selected' state.
283
311
        """
 
312
        if self.disable_left_mouse:
 
313
            return
 
314
 
284
315
        self.event_state = "selected"
285
316
        self.selection_completed = self.selection
286
317
        event.handled = True
367
398
        mapped_pos = self.mapper.map_data(pos)
368
399
        self.selection = (mapped_pos, mapped_pos)
369
400
        self._set_sizing_cursor(event)
 
401
        self._down_point = array([event.x, event.y])
370
402
        self.event_state = "selecting"
 
403
        if self.append_key is not None and self.append_key.match(event):
 
404
            print "append key matched"
 
405
            self.selection_mode = "append"
 
406
        else:
 
407
            self.selection_mode = "set"
371
408
        self.selecting_mouse_move(event)
372
409
        return
373
410
 
410
447
        return
411
448
 
412
449
    def selecting_button_up(self, event):
413
 
        self.event_state = "selected"
414
 
 
415
 
        # Fire the "completed" event
416
 
        self.selection_completed = self.selection
417
 
        event.handled = True
 
450
        # Check to see if the selection region is bigger than the minimum
 
451
        event.window.set_pointer("arrow")
 
452
        start = self._down_point[self.axis_index]
 
453
        end = self._get_axis_coord(event)
 
454
        if self.minimum_selection > abs(start - end):
 
455
            self.deselect(event)
 
456
            event.handled = True
 
457
 
 
458
        else:
 
459
            self.event_state = "selected"
 
460
 
 
461
            # Fire the "completed" event
 
462
            self.selection_completed = self.selection
 
463
            event.handled = True
418
464
        return
419
465
 
420
466
    def selecting_right_up(self, event):
431
477
        
432
478
        Switches the tool to the 'selected' state.
433
479
        """
 
480
        if self.disable_left_mouse:
 
481
            return
434
482
        self.selecting_button_up(event)
435
483
    
436
484
    def selecting_mouse_leave(self, event):
512
560
        return
513
561
 
514
562
    def _get_selection(self):
515
 
        selection = getattr(self.plot, self.axis).metadata["selections"]
 
563
        selection = getattr(self.plot, self.axis).metadata[self.metadata_name]
516
564
        return selection
517
565
    
518
566
    def _set_selection(self, val):
523
571
 
524
572
        if datasource is not None:
525
573
        
 
574
            mdname = self.metadata_name
 
575
 
526
576
            # Set the selection range on the datasource
527
 
            datasource.metadata["selections"] = val
528
 
            datasource.metadata_changed = {"selections": val}
 
577
            datasource.metadata[mdname] = val
 
578
            datasource.metadata_changed = {mdname: val}
529
579
            
530
580
            # Set the selection mask on the datasource
531
581
            selection_masks = \
532
 
                datasource.metadata.setdefault("selection_masks", [])
 
582
                datasource.metadata.setdefault(self.mask_metadata_name, [])
533
583
            for index in range(len(selection_masks)):
534
584
                if id(selection_masks[index]) == id(self._selection_mask):
535
585
                    del selection_masks[index]
536
586
                    break
537
587
 
 
588
            # Set the selection mode on the datasource
 
589
            datasource.metadata[self.selection_mode_metadata_name] = self.selection_mode
 
590
                
538
591
            if val is not None:
539
592
                low, high = val
540
593
                data_pts = datasource.get_data()
541
594
                new_mask = (data_pts>=low) & (data_pts<=high)
542
595
                selection_masks.append(new_mask)
543
596
                self._selection_mask = new_mask
544
 
            datasource.metadata_changed = {"selection_masks": val}
 
597
            datasource.metadata_changed = {self.mask_metadata_name: val}
545
598
            
546
599
        self.trait_property_changed("selection", oldval, val)
547
600