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