[4972de2] | 1 | import plottables |
---|
| 2 | from BaseInteractor import _BaseInteractor |
---|
| 3 | |
---|
| 4 | class PointInteractor(_BaseInteractor): |
---|
| 5 | |
---|
| 6 | def __init__(self,base,axes,color='black', zorder=3, id=''): |
---|
| 7 | _BaseInteractor.__init__(self, base, axes, color=color) |
---|
| 8 | self.zorder = zorder |
---|
| 9 | self.id = id |
---|
[a17ffdf] | 10 | self.color=color |
---|
[4972de2] | 11 | self.colorlist = ['b','g','r','c','m','y'] |
---|
| 12 | self.symbollist = ['o','x','^','v','<','>','+','s','d','D','h','H','p'] |
---|
| 13 | self.marker = None |
---|
| 14 | self.marker2 = None |
---|
| 15 | self._button_down = False |
---|
| 16 | self._context_menu = False |
---|
| 17 | self._dragged = False |
---|
[7fef474] | 18 | self.connect_markers([self.axes]) |
---|
| 19 | |
---|
[4972de2] | 20 | |
---|
| 21 | def _color(self,c): |
---|
| 22 | """Return a particular colour""" |
---|
| 23 | return self.colorlist[c%len(self.colorlist)] |
---|
| 24 | |
---|
| 25 | def _symbol(self,s): |
---|
| 26 | """Return a particular symbol""" |
---|
| 27 | return self.symbollist[s%len(self.symbollist)] |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | def points(self,x,y,dx=None,dy=None,color=0,symbol=0,label=None): |
---|
| 31 | |
---|
| 32 | if not self.marker==None: |
---|
| 33 | self.base.connect.clear([self.marker]) |
---|
| 34 | |
---|
| 35 | self.color = self._color(color) |
---|
| 36 | |
---|
| 37 | # Convert tuple (lo,hi) to array [(x-lo),(hi-x)] |
---|
| 38 | if dx != None and type(dx) == type(()): |
---|
| 39 | dx = nx.vstack((x-dx[0],dx[1]-x)).transpose() |
---|
| 40 | if dy != None and type(dy) == type(()): |
---|
| 41 | dy = nx.vstack((y-dy[0],dy[1]-y)).transpose() |
---|
| 42 | |
---|
| 43 | if dx==None and dy==None: |
---|
| 44 | self.marker = self.axes.plot(x,y,color=self.color, |
---|
| 45 | marker=self._symbol(symbol),linestyle='',label=label, |
---|
| 46 | zorder=self.zorder)[0] |
---|
| 47 | else: |
---|
| 48 | self.marker = self.axes.errorbar(x, y, yerr=dy, xerr=None, |
---|
| 49 | ecolor=self.color, |
---|
| 50 | color=self.color, |
---|
| 51 | capsize=2,linestyle='', barsabove=False, |
---|
| 52 | #mec=self.color, mfc=self.color, |
---|
| 53 | marker=self._symbol(symbol), |
---|
| 54 | lolims=False, uplims=False, |
---|
| 55 | xlolims=False, xuplims=False,label=label, |
---|
| 56 | zorder=self.zorder)[0] |
---|
| 57 | |
---|
| 58 | self.connect_markers([self.marker]) |
---|
| 59 | self.update() |
---|
| 60 | |
---|
| 61 | def curve(self, x, y, dy=None, color=0, symbol=0, label=None): |
---|
| 62 | |
---|
| 63 | if not self.marker==None: |
---|
| 64 | self.base.connect.clear([self.marker]) |
---|
| 65 | |
---|
| 66 | self.color = self._color(color) |
---|
| 67 | |
---|
| 68 | self.marker = self.axes.plot(x,y,color=self.color,marker='',linestyle='-',label=label)[0] |
---|
| 69 | |
---|
| 70 | self.connect_markers([self.marker]) |
---|
| 71 | self.update() |
---|
| 72 | |
---|
| 73 | def connect_markers(self,markers): |
---|
| 74 | """ |
---|
| 75 | Connect markers to callbacks |
---|
| 76 | """ |
---|
| 77 | for h in markers: |
---|
| 78 | connect = self.base.connect |
---|
| 79 | connect('enter', h, self._on_enter) |
---|
| 80 | connect('leave', h, self._on_leave) |
---|
| 81 | connect('click', h, self._on_click) |
---|
| 82 | connect('release', h, self._on_release) |
---|
| 83 | #connect('drag', h, self._on_drag) |
---|
| 84 | connect('key', h, self.onKey) |
---|
| 85 | |
---|
| 86 | def clear(self): |
---|
| 87 | print "plottable_interactor.clear()" |
---|
| 88 | |
---|
| 89 | def _on_click(self, evt): |
---|
[7fef474] | 90 | """ |
---|
| 91 | Called when a mouse button is clicked |
---|
| 92 | from within the boundaries of an artist. |
---|
| 93 | """ |
---|
| 94 | if self._context_menu==True: |
---|
| 95 | self._context_menu = False |
---|
| 96 | evt.artist = self.marker |
---|
| 97 | self._on_leave(evt) |
---|
| 98 | |
---|
[4972de2] | 99 | |
---|
| 100 | def _on_release(self, evt): |
---|
[7fef474] | 101 | """ |
---|
| 102 | Called when a mouse button is released |
---|
| 103 | within the boundaries of an artist |
---|
| 104 | """ |
---|
[4972de2] | 105 | # Check to see whether we are about to pop |
---|
| 106 | # the context menu up |
---|
| 107 | if evt.button==3: |
---|
| 108 | self._context_menu = True |
---|
| 109 | |
---|
| 110 | def _on_enter(self, evt): |
---|
[7fef474] | 111 | """ |
---|
| 112 | Called when we are entering the boundaries |
---|
| 113 | of an artist. |
---|
| 114 | """ |
---|
| 115 | if not evt.artist.__class__.__name__=="Subplot": |
---|
[4972de2] | 116 | |
---|
[7fef474] | 117 | self.base.plottable_selected(self.id) |
---|
| 118 | evt.artist.set_color('y') |
---|
[4972de2] | 119 | if hasattr(evt.artist, "set_facecolor"): |
---|
[7fef474] | 120 | evt.artist.set_facecolor('y') |
---|
| 121 | if hasattr(evt.artist, "set_edgecolor"): |
---|
| 122 | evt.artist.set_edgecolor('y') |
---|
[4972de2] | 123 | self.axes.figure.canvas.draw_idle() |
---|
[7fef474] | 124 | |
---|
| 125 | def _on_leave(self, evt): |
---|
| 126 | """ |
---|
| 127 | Called when we are leaving the boundaries |
---|
| 128 | of an artist. |
---|
| 129 | """ |
---|
| 130 | if not evt.artist.__class__.__name__=="Subplot": |
---|
| 131 | if self._context_menu==False: |
---|
| 132 | self.base.plottable_selected(None) |
---|
| 133 | evt.artist.set_color(self.color) |
---|
| 134 | if hasattr(evt.artist, "set_facecolor"): |
---|
| 135 | evt.artist.set_facecolor(self.color) |
---|
| 136 | if hasattr(evt.artist, "set_edgecolor"): |
---|
| 137 | evt.artist.set_edgecolor(self.color) |
---|
| 138 | self.axes.figure.canvas.draw_idle() |
---|
[4972de2] | 139 | |
---|
| 140 | def update(self): |
---|
| 141 | """ |
---|
| 142 | Update |
---|
| 143 | """ |
---|
[a17ffdf] | 144 | pass |
---|
[4972de2] | 145 | |
---|
| 146 | |
---|