-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteract.py
169 lines (148 loc) · 6.18 KB
/
Interact.py
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
class GaussAdjuster:
# Messages to show during the lifetime of the adjuster
messages = {
"left_point" : "Seleccione el punto izquierdo",
"right_point" : "Seleccione el punto derecho",
"ready" : "Listo para hacer el ajuste"
}
# Initialization of object, set variables needed to calculate adjustment and
# bind THIS object to the "button_press_event" event, making this object the
# "event handler"
def __init__(self, figure):
self.figure = figure
self.left_point = None
self.right_point = None
self.center_point = None
self.message = GaussAdjuster.messages["left_point"]
print self.message
self.ready = False
self.connection_id = self.figure.canvas.mpl_connect('button_press_event', self)
# this function is called whenever the event connected on __init__ is called
def __call__(self, event):
if self.left_point == None:
self.left_point = {"x": event.xdata, "y" : event.ydata}
self.message = GaussAdjuster.messages["right_point"]
print self.message
elif self.right_point == None:
self.right_point = {"x": event.xdata, "y" : event.ydata}
self.message = GaussAdjuster.messages["ready"]
print self.message
# elif self.center_point == None:
# self.center_point = {"x": event.xdata, "y" : event.ydata}
# self.message = GaussAdjuster.messages["ready"]
# print self.message
if self.validates():
#all data is correct, ready to make adjustment
self.ready = True
self.figure.canvas.mpl_disconnect(self.connection_id)
self.makeAdjustment()
else:
#user entered invalid data, reset internal state
self.reset()
# returns true or false, set validation rules here
def validates(self):
# TODO: return messages for each validation
if self.left_point["x"] >= self.right_point["x"]:
return False
# elif self.center_point["x"] <= self.left_point["x"] or self.center_point["x"] >= self.right_point["x"]:
# return False
else:
return True
# makes gaussian adjustment
def makeAdjustment(self):
message = "Ajuste hecho con: \n lp(x:%f, y:%.4E) \n rp(x:%f, y:%.4E)"
params = (self.left_point["x"], self.left_point["y"], self.right_point["x"], self.right_point["y"])
print message % params
# resets internal state
def reset(self):
self.left_point = None
self.right_point = None
# self.center_point = None
self.message = GaussAdjuster.messages["left_point"]
self.ready = False
#TODO: ver si es necesario
self.connection_id = self.figure.canvas.mpl_connect('button_press_event', self)
class LineAdjuster:
# Messages used throught the lifespan of the adjuster
messages = {
"initial" : "Listo para comenzar seleccion de puntos",
"lp" : "Seleccione el punto izquierdo",
"rp" : "Seleccione el punto derecho",
"p_defined" : "Punto %s definido en (x:%f, y:%.5E)",
"val_init" : "Validando los datos ingresados...",
"val_fail" : "Puntos definidos inconsistentes, vuelva a ingresarlos",
"val_success" : "Datos ingresados correctos"
}
# Initialization method
def __init__(self, figure):
self.figure = figure
self.lp = None
self.rp = None
self.message = LineAdjuster.messages["initial"]
self.ready = False
self.connection_id = None
# Event handling method, only works when object is attached
def __call__(self,event):
if not self.isAttached(): return
#if event.inaxes != self.line.axes: return
e_point = { "x": event.xdata , "y" : event.ydata }
if self.lp == None:
#Set lp
#self.bindPoint(self.lp, e_point)
self.lp = e_point
print LineAdjuster.messages["p_defined"] % ("lp", self.lp["x"], self.lp["y"])
self.message = LineAdjuster.messages["rp"]
print self.message
return
elif self.rp == None:
#Set rp
#self.bindPoint(self.rp, e_point)
self.rp = e_point
print LineAdjuster.messages["p_defined"] % ("rp", self.rp["x"], self.rp["y"])
self.message = LineAdjuster.messages["val_init"]
print self.message
#both points set, validate consitency
if self.validates():
print LineAdjuster.messages["val_success"]
self.setReady()
self.detach()
self.adjust()
else:
print LineAdjuster.messages["val_fail"]
self.reset()
self.message = LineAdjuster.messages["lp"]
print self.message
# Method to capture point definitions (mouse clicks)
def startPointSelection(self):
self.message = LineAdjuster.messages["lp"]
print self.message
self.attach()
# Method to bind selected point to the adjuster
def bindPoint(self, point, event_point):
point = event_point
# Method to "attach" to a given event, button_press_event is default
def attach(self, event_name = "button_press_event"):
self.connection_id = self.figure.canvas.mpl_connect(event_name, self)
# Method to "detach" from the event that this instance is connected to
def detach(self):
self.figure.canvas.mpl_disconnect(self.connection_id)
self.connection_id == None
# Return true of false if this instance is attached to a event
def isAttached(self):
return self.connection_id != None
# Method to check consitency of the adjuster internal state
def validates(self):
if self.lp["x"] > self.rp["x"]:
return False
else:
return True
# Method to reset adjuster internal state
def reset(self):
self.lp = None
self.rp = None
self.message = LineAdjuster.messages["initial"]
self.ready = False
def setReady(self):
self.ready = True
def adjust(self):
print " "