drag_rectangle2.py 4.94 KB
Newer Older
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
# draggable rectangle with the animation blit techniques; see
# http://www.scipy.org/Cookbook/Matplotlib/Animations
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy import interpolate
from pylab import *
import Tkinter as tk

listay = list()
drs = []
fig = plt.figure()
ax = fig.add_subplot(111)
recursivecontrol = 0

#######################################################################################################
# Add square

def add_square(x):
    global listay, drs, ax
    rect = ax.bar(x, 0.03, width=0.5) 
    for rec in rect:
        dr = DraggableRectangle(rec)
        dr.connect()
        drs.append(dr)
        listay.append(dr.rect.xy[1])
    ylim([0,1])
    xlim([0,20])

def remove_square():
    global listay, drs
    listay.pop()
    drs.pop()
    print listay, drs

#######################################################################################################


class DraggableRectangle:
    lock = None  # only one can be animated at a time
    def __init__(self, rect):
        self.rect = rect
        self.press = None
        self.background = None
        self.rect.set_y(0.1)

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.rect.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)
        self.cida = self.rect.figure.canvas.mpl_connect(
            'key_release_event', self.on_a)
        self.cidr = self.rect.figure.canvas.mpl_connect(
            'key_release_event', self.on_r)

    def on_a(self, event):
        if event.key=='a':
            if not event.inaxes: return
            add_square(float(event.xdata))
            print event.xdata


# NAO FUNCIONA (DELETA TUDO)
    def on_r(self, event):
        if event.key=='r':
            if not event.inaxes: return
            remove_square()

    def on_press(self, event):
        global drs, recursivecontrol
        k = 0
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.rect.axes: return 
        if DraggableRectangle.lock is not None: return
        contains, attrd = self.rect.contains(event)
        if not contains: return


#            if recursivecontrol == 1:
 #               recursivecontrol = 0
  #              return 1
  #          for rec in drs:
  #              if recursivecontrol == 0:
  #                  recursivecontrol = 1
  #                  k = k + rec.on_press(event)
  #          if k == (len(drs)):

#                recursivecontrol = 0
 #           return 1

          
        #print 'event contains', self.rect.xy
        x0, y0 = self.rect.xy
        self.press = x0, y0, event.xdata, event.ydata
        DraggableRectangle.lock = self

        # draw everything but the selected rectangle and store the pixel buffer
        canvas = self.rect.figure.canvas
        axes = self.rect.axes
        self.rect.set_animated(True)
        canvas.draw()
        self.background = canvas.copy_from_bbox(self.rect.axes.bbox)

        # now redraw just the rectangle
        axes.draw_artist(self.rect)

        # and blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_motion(self, event):
        'on motion we will move the rect if the mouse is over us'
        if DraggableRectangle.lock is not self: return
        if event.inaxes != self.rect.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.rect.set_x(x0+dx)
        self.rect.set_y(y0+dy)

        canvas = self.rect.figure.canvas
        axes = self.rect.axes
        # restore the background region
        canvas.restore_region(self.background)

        # redraw just the current rectangle
        axes.draw_artist(self.rect)

        # blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_release(self, event):
        'on release we reset the press data'
        if DraggableRectangle.lock is not self:
            return

        self.press = None
        DraggableRectangle.lock = None

        # turn off the rect animation property and reset the background
        self.rect.set_animated(False)
        self.background = None

        # redraw the full figure
        self.rect.figure.canvas.draw()

        for i in range(len(listay)):
            listay[i] = drs[i].rect.xy

        print listay

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
        self.rect.figure.canvas.mpl_disconnect(self.cida)
        self.rect.figure.canvas.mpl_disconnect(self.cidr)

add_square(2)

plt.show()