Made Element.min_(width|height) work by adding some special code to HandleTool.
This commit is contained in:
parent
23116c9e7c
commit
07bc1c2083
2
demo.py
2
demo.py
@ -255,6 +255,8 @@ create_window(c)
|
||||
# Add stuff to the canvas:
|
||||
|
||||
b=MyBox()
|
||||
b.min_width = 20
|
||||
b.min_height = 30
|
||||
print 'box', b
|
||||
b.matrix=(1.0, 0.0, 0.0, 1, 20,20)
|
||||
b.width=b.height = 40
|
||||
|
@ -173,6 +173,8 @@ class Element(Item):
|
||||
>>> b._handles[SE].x
|
||||
Variable(20, 30)
|
||||
"""
|
||||
if width < self.min_width:
|
||||
width = self.min_width
|
||||
h = self._handles
|
||||
h[SE].x = h[NW].x + width
|
||||
|
||||
@ -191,11 +193,16 @@ class Element(Item):
|
||||
>>> b.height = 20
|
||||
>>> b.height
|
||||
20.0
|
||||
>>> b.height = 2
|
||||
>>> b.height
|
||||
10.0
|
||||
>>> b._handles[NW].y
|
||||
Variable(0, 30)
|
||||
>>> b._handles[SE].y
|
||||
Variable(20, 30)
|
||||
"""
|
||||
if height < self.min_height:
|
||||
height = self.min_height
|
||||
h = self._handles
|
||||
h[SE].y = h[NW].y + height
|
||||
|
||||
@ -279,11 +286,6 @@ class Element(Item):
|
||||
"""Make sure handles do not overlap during movement.
|
||||
"""
|
||||
pass
|
||||
#h = self._handles
|
||||
#if float(h[NW].x) > float(h[NE].x):
|
||||
# h[NE].x = h[NW].x
|
||||
#if float(h[NW].y) > float(h[SW].y):
|
||||
# h[SW].y = h[NW].y
|
||||
|
||||
def update(self, context):
|
||||
"""Do nothing dureing update.
|
||||
|
@ -26,6 +26,7 @@ __version__ = "$Revision$"
|
||||
import cairo
|
||||
import gtk
|
||||
from canvas import Context
|
||||
from item import Element
|
||||
from geometry import Rectangle
|
||||
|
||||
DEBUG_TOOL = False
|
||||
@ -316,6 +317,34 @@ class HandleTool(Tool):
|
||||
return item, h
|
||||
return None, None
|
||||
|
||||
def move(self, view, item, handle, x, y):
|
||||
"""Move the handle to position (x,y).
|
||||
This version already has some special behavior implemented for
|
||||
gaphas.item.Element's. The min_width and min_height properties of
|
||||
Element's are used to restrict the handles from overlapping each other.
|
||||
"""
|
||||
# Special behavior for Elements:
|
||||
if isinstance(item, Element):
|
||||
index = list(item.handles()).index(handle)
|
||||
opposite = item.handles()[(index + 2) % 4]
|
||||
|
||||
if index == 0 or index == 3:
|
||||
if opposite.x - x < item.min_width:
|
||||
x = opposite.x - item.min_width
|
||||
else:
|
||||
if x - opposite.x < item.min_width:
|
||||
x = opposite.x + item.min_width
|
||||
|
||||
if index == 0 or index == 1:
|
||||
if opposite.y - y < item.min_height:
|
||||
y = opposite.y - item.min_height
|
||||
else:
|
||||
if y - opposite.y < item.min_height:
|
||||
y = opposite.y + item.min_height
|
||||
|
||||
handle.x = x
|
||||
handle.y = y
|
||||
|
||||
def glue(self, view, item, handle, wx, wy):
|
||||
"""find an item near @handle that @item can connect to.
|
||||
"""
|
||||
@ -378,10 +407,10 @@ class HandleTool(Tool):
|
||||
|
||||
# Calculate the distance the item has to be moved
|
||||
wx, wy = view.transform_point_c2w(event.x, event.y)
|
||||
|
||||
x, y = view.canvas.get_matrix_w2i(item).transform_point(wx, wy)
|
||||
handle.x = x
|
||||
handle.y = y
|
||||
|
||||
# Do the actual move:
|
||||
self.move(view, item, handle, x, y)
|
||||
|
||||
item.request_update()
|
||||
item.canvas.update_matrices()
|
||||
|
Loading…
x
Reference in New Issue
Block a user