Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
sortvisu.ArrayItem Class Reference

List of all members.

Public Member Functions

def __init__
def delete
def mouse_down
def mouse_move
def mouse_up
def setindex
def swapwith
def compareto
def position
def nearestindex

Public Attributes

 array
 index
 value
 canvas
 item_id
 lastx
 lasty
 origx
 origy

Detailed Description

Definition at line 194 of file sortvisu.py.


Constructor & Destructor Documentation

def sortvisu.ArrayItem.__init__ (   self,
  array,
  index,
  value 
)

Definition at line 196 of file sortvisu.py.

00196 
00197     def __init__(self, array, index, value):
00198         self.array = array
00199         self.index = index
00200         self.value = value
00201         self.canvas = array.canvas
00202         x1, y1, x2, y2 = self.position()
00203         self.item_id = array.canvas.create_rectangle(x1, y1, x2, y2,
00204             fill='red', outline='black', width=1)
00205         self.canvas.tag_bind(self.item_id, '<Button-1>', self.mouse_down)
00206         self.canvas.tag_bind(self.item_id, '<Button1-Motion>', self.mouse_move)
00207         self.canvas.tag_bind(self.item_id, '<ButtonRelease-1>', self.mouse_up)

Here is the caller graph for this function:


Member Function Documentation

def sortvisu.ArrayItem.compareto (   self,
  other 
)

Definition at line 301 of file sortvisu.py.

00301 
00302     def compareto(self, other):
00303         myfill = self.canvas.itemcget(self.item_id, 'fill')
00304         otherfill = self.canvas.itemcget(other.item_id, 'fill')
00305         if self.value < other.value:
00306             myflash = 'white'
00307             otherflash = 'black'
00308             outcome = -1
00309         elif self.value > other.value:
00310             myflash = 'black'
00311             otherflash = 'white'
00312             outcome = 1
00313         else:
00314             myflash = otherflash = 'grey'
00315             outcome = 0
00316         try:
00317             self.canvas.itemconfig(self.item_id, fill=myflash)
00318             self.canvas.itemconfig(other.item_id, fill=otherflash)
00319             self.array.wait(500)
00320         finally:
00321             self.canvas.itemconfig(self.item_id, fill=myfill)
00322             self.canvas.itemconfig(other.item_id, fill=otherfill)
00323         return outcome

def sortvisu.ArrayItem.delete (   self)

Definition at line 208 of file sortvisu.py.

00208 
00209     def delete(self):
00210         item_id = self.item_id
00211         self.array = None
00212         self.item_id = None
00213         self.canvas.delete(item_id)

def sortvisu.ArrayItem.mouse_down (   self,
  event 
)

Definition at line 214 of file sortvisu.py.

00214 
00215     def mouse_down(self, event):
00216         self.lastx = event.x
00217         self.lasty = event.y
00218         self.origx = event.x
00219         self.origy = event.y
00220         self.canvas.tag_raise(self.item_id)

def sortvisu.ArrayItem.mouse_move (   self,
  event 
)

Definition at line 221 of file sortvisu.py.

00221 
00222     def mouse_move(self, event):
00223         self.canvas.move(self.item_id,
00224                          event.x - self.lastx, event.y - self.lasty)
00225         self.lastx = event.x
00226         self.lasty = event.y

def sortvisu.ArrayItem.mouse_up (   self,
  event 
)

Definition at line 227 of file sortvisu.py.

00227 
00228     def mouse_up(self, event):
00229         i = self.nearestindex(event.x)
00230         if i >= self.array.getsize():
00231             i = self.array.getsize() - 1
00232         if i < 0:
00233             i = 0
00234         other = self.array.items[i]
00235         here = self.index
00236         self.array.items[here], self.array.items[i] = other, self
00237         self.index = i
00238         x1, y1, x2, y2 = self.position()
00239         self.canvas.coords(self.item_id, (x1, y1, x2, y2))
00240         other.setindex(here)

Here is the call graph for this function:

def sortvisu.ArrayItem.nearestindex (   self,
  x 
)

Definition at line 331 of file sortvisu.py.

00331 
00332     def nearestindex(self, x):
00333         return int(round(float(x)/XGRID)) - 1
00334 
00335 
00336 # Subroutines that don't need an object

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 324 of file sortvisu.py.

00324 
00325     def position(self):
00326         x1 = (self.index+1)*XGRID - WIDTH//2
00327         x2 = x1+WIDTH
00328         y2 = (self.array.maxvalue+1)*YGRID
00329         y1 = y2 - (self.value)*YGRID
00330         return x1, y1, x2, y2

Here is the caller graph for this function:

def sortvisu.ArrayItem.setindex (   self,
  index 
)

Definition at line 241 of file sortvisu.py.

00241 
00242     def setindex(self, index):
00243         nsteps = steps(self.index, index)
00244         if not nsteps: return
00245         if self.array.speed == "fastest":
00246             nsteps = 0
00247         oldpts = self.position()
00248         self.index = index
00249         newpts = self.position()
00250         trajectory = interpolate(oldpts, newpts, nsteps)
00251         self.canvas.tag_raise(self.item_id)
00252         for pts in trajectory:
00253             self.canvas.coords(self.item_id, pts)
00254             self.array.wait(50)

Here is the call graph for this function:

def sortvisu.ArrayItem.swapwith (   self,
  other 
)

Definition at line 255 of file sortvisu.py.

00255 
00256     def swapwith(self, other):
00257         nsteps = steps(self.index, other.index)
00258         if not nsteps: return
00259         if self.array.speed == "fastest":
00260             nsteps = 0
00261         myoldpts = self.position()
00262         otheroldpts = other.position()
00263         self.index, other.index = other.index, self.index
00264         mynewpts = self.position()
00265         othernewpts = other.position()
00266         myfill = self.canvas.itemcget(self.item_id, 'fill')
00267         otherfill = self.canvas.itemcget(other.item_id, 'fill')
00268         self.canvas.itemconfig(self.item_id, fill='green')
00269         self.canvas.itemconfig(other.item_id, fill='yellow')
00270         self.array.master.update()
00271         if self.array.speed == "single-step":
00272             self.canvas.coords(self.item_id, mynewpts)
00273             self.canvas.coords(other.item_id, othernewpts)
00274             self.array.master.update()
00275             self.canvas.itemconfig(self.item_id, fill=myfill)
00276             self.canvas.itemconfig(other.item_id, fill=otherfill)
00277             self.array.wait(0)
00278             return
00279         mytrajectory = interpolate(myoldpts, mynewpts, nsteps)
00280         othertrajectory = interpolate(otheroldpts, othernewpts, nsteps)
00281         if self.value > other.value:
00282             self.canvas.tag_raise(self.item_id)
00283             self.canvas.tag_raise(other.item_id)
00284         else:
00285             self.canvas.tag_raise(other.item_id)
00286             self.canvas.tag_raise(self.item_id)
00287         try:
00288             for i in range(len(mytrajectory)):
00289                 mypts = mytrajectory[i]
00290                 otherpts = othertrajectory[i]
00291                 self.canvas.coords(self.item_id, mypts)
00292                 self.canvas.coords(other.item_id, otherpts)
00293                 self.array.wait(50)
00294         finally:
00295             mypts = mytrajectory[-1]
00296             otherpts = othertrajectory[-1]
00297             self.canvas.coords(self.item_id, mypts)
00298             self.canvas.coords(other.item_id, otherpts)
00299             self.canvas.itemconfig(self.item_id, fill=myfill)
00300             self.canvas.itemconfig(other.item_id, fill=otherfill)

Here is the call graph for this function:


Member Data Documentation

Definition at line 197 of file sortvisu.py.

Definition at line 200 of file sortvisu.py.

Definition at line 198 of file sortvisu.py.

Definition at line 202 of file sortvisu.py.

Definition at line 215 of file sortvisu.py.

Definition at line 216 of file sortvisu.py.

Definition at line 217 of file sortvisu.py.

Definition at line 218 of file sortvisu.py.

Definition at line 199 of file sortvisu.py.


The documentation for this class was generated from the following file: