File: drag_box.py

package info (click to toggle)
python-enable 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,280 kB
  • ctags: 13,899
  • sloc: cpp: 48,447; python: 28,502; ansic: 9,004; makefile: 315; sh: 44
file content (135 lines) | stat: -rw-r--r-- 3,801 bytes parent folder | download | duplicates (3)
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
#-----------------------------------------------------------------------------
#
#  Copyright (c) 2005, Enthought, Inc.
#  All rights reserved.
#
#  Author: Scott Swarts <swarts@enthought.com>
#
#-----------------------------------------------------------------------------

"""A drag drawn box
"""

# Standard library imports.

# Major packages.

# Enthought library imports
from enable.primitives.api import Box
from enable.enable_traits import Pointer
from traits.api import Event, Float, Trait, Tuple

# Application specific imports.

# Local imports.

##############################################################################
# class 'DragBox'
##############################################################################

class DragBox(Box):
    """A drag drawn box
    """

    ##########################################################################
    # Traits
    ##########################################################################

    ### 'DragBox' interface ############################################

    # Event fired when complete:
    complete = Event

    # Constraints on size:
    x_bounds = Trait(None, None, Tuple(Float, Float))
    y_bounds = Trait(None, None, Tuple(Float, Float))

    #### Pointers. ####

    # Pointer for the complete state:
    complete_pointer = Pointer('cross')

    # Pointer for the drawing state:
    drawing_pointer = Pointer('cross')

    # Pointer for the normal state:
    normal_pointer = Pointer('cross')

    #### Private traits

    # Position of the left down:
    start_x = Float
    start_y = Float

    ##########################################################################
    # 'object' interface
    ##########################################################################

    ##########################################################################
    # 'Component' interface
    ##########################################################################

    #### 'normal' state ######################################################

    def normal_left_down ( self, event ):
        """ Handle the left button down in the 'normal' state. """

        self.event_state = 'drawing'
        self.pointer = self.drawing_pointer

        self.start_x = event.x
        self.start_y = event.y
        self._set_bounds(event)

        return

    def normal_mouse_move (self, event):
        """ Handle the mouse moving in the 'normal' state. """

        self.pointer = self.normal_pointer

        return

    #### 'drawing' state #####################################################

    def drawing_mouse_move(self, event):
        """ Handle the mouse moving in the 'drawing' state. """

        self._set_bounds(event)


    def drawing_left_up(self, event):
        """ Handle the left mouse button coming up in the 'drawing' state. """

        self.event_state = 'complete'
        self.pointer = self.complete_pointer

        self.complete = True
        self._set_bounds(event)

        return


    ##########################################################################
    # Private interface
    ##########################################################################

    def _set_bounds(self, event):
        """
        Sets the bounds based on start_x, start_y, the event position
        and any constrants.
        """
        if self.x_bounds is not None:
            x, dx = self.x_bounds
        else:
            x = min(self.start_x, event.x)
            dx = abs(event.x-self.start_x)
        if self.y_bounds is not None:
            y, dy = self.y_bounds
        else:
            y = min(self.start_y, event.y)
            dy = abs(event.y-self.start_y)
        self.bounds = (x, y, dx, dy)


#### EOF ######################################################################