AnchorPoint.py :  » UML » Python-UML-Tool » pyut-1.4.0 » src » MiniOgl » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » UML » Python UML Tool 
Python UML Tool » pyut 1.4.0 » src » MiniOgl » AnchorPoint.py
#!/usr/bin/env python
#
# Copyright 2002, Laurent Burgbacher, Eivd.
# Visit http://www.eivd.ch
#
# This file is part of MiniOgl.
#
# MiniOgl is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# MiniOgl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MiniOgl; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

__author__    = "Laurent Burgbacher, lb@alawa.ch, Eivd"
__copyright__ = "Copyright 2002, Laurent Burgbacher, Eivd"
__license__   = "Released under the terms of the GNU General Public Licence V2"
__date__      = "2002-10-15"
__version__   = "$Id: AnchorPoint.py,v 1.10 2006/02/04 22:01:01 dutoitc Exp $"

from __future__ import division
from LinePoint import LinePoint
#import wx

__all__ = ["AnchorPoint"]

class AnchorPoint(LinePoint):
    """
    This is a point which begins or ends a line.
    It is often anchored to a parent shape, but that's not mandatory.

    Exported methods:
    -----------------

    __init__(self, x, y, parent=None)
        Constructor.
    SetStayInside(self, state)
        If True, the point will stay inside the bounds of its parent shape.
    GetStayInside(self)
        Return True if the point stays inside the bounds of its parent shape.
    SetStayOnBorder(self, state)
        If True, the point will stay on the border of its parent shape.
    GetStayOnBorder(self)
        Return True if the point stays on the border of its parent shape.
    SetPosition(self, x, y)
        Change the position of the anchor point, if it's draggable.
    stayInside(low, length, value)
        Return the nearest value in [low, low+length].
    stickToBorder(ox, oy, width, height, x, y)
        Return (x, y) on the square (ox, oy, ox+width, oy+height) by
    Detach(self)
        Detach the line and all its line points, including src and dst.

    @author Laurent Burgbacher <lb@alawa.ch>
    """
    def __init__(self, x, y, parent=None):
        """
        Constructor.

        @param double x, y : position of the point
        @param Shape parent : parent shape
        """
        #print ">>>AnchorPoint.init"
        LinePoint.__init__(self, x, y, parent)
        self._protected = True # protected by default
        self.SetDraggable(False)
        self._stayInside = True
        self._stayOnBorder = True

    #>------------------------------------------------------------------------

    def SetStayInside(self, state):
        """
        If True, the point will stay inside the bounds of its parent shape.

        @param boolean state
        """
        self._stayInside = state

    #>------------------------------------------------------------------------

    def GetStayInside(self):
        """
        Return True if the point stays inside the bounds of its parent shape.

        @return boolean
        """
        return self._stayInside

    #>------------------------------------------------------------------------

    def SetStayOnBorder(self, state):
        """
        If True, the point will stay on the border of its parent shape.

        @param boolean state
        """
        self._stayOnBorder = state

    #>------------------------------------------------------------------------

    def GetStayOnBorder(self):
        """
        Return True if the point stays on the border of its parent shape.

        @return boolean
        """
        return self._stayOnBorder

    #>------------------------------------------------------------------------

    def SetPosition(self, x, y):
        """
        Change the position of the anchor point, if it's draggable.

        @param double x, y : new position in diagram coordinates
        """
                
        def stayInside(low, length, value):
            """
            Return the nearest value in [low, low+length].
            """
            if value < low:
                value = low
            elif value > low + length:
                value = low + length
            return value

        def stickToBorder(ox, oy, width, height, x, y):
            """
            Return (x, y) on the square (ox, oy, ox+width, oy+height) by
            placing (x, y) on the nearest border.
            """
            left = x - ox
            right = ox + width - x
            up = y - oy
            down = oy + height - y
            choice = {
                left :  lambda x, y: (ox, y),
                right : lambda x, y: (ox + width, y),
                up :    lambda x, y: (x, oy),
                down :  lambda x, y: (x, oy + height),
            }
            lesser = min(left, right, up, down)
            return choice[lesser](x, y)

        if self._draggable:
            if self._parent is not None:
                
                topLeftX, topLeftY = self._parent.GetTopLeft()
                width, height = self._parent.GetSize()
                width = abs(width) - 1
                height = abs(height) - 1
                if self._stayInside or self._stayOnBorder:
                    x = stayInside(topLeftX, width, x)
                    y = stayInside(topLeftY, height, y)
                    if self._stayOnBorder:
                        x, y = stickToBorder(topLeftX, topLeftY, width, height, x, y)
                self._x, self._y = self.ConvertCoordToRelative(x, y)
            else:
                self._x = x
                self._y = y

            #added by P. Dabrowski <przemek.dabrowski@destroy-display.com> (12.11.2005)
            #updates the model of the anchor point (MVC pattern)
            if self.HasDiagramFrame():
                self.UpdateModel()
                
    #>------------------------------------------------------------------ 

    def Detach(self):
        """
        Detach the line and all its line points, including src and dst.
        Also remove self from the parent.
        """
        LinePoint.Detach(self)
        parent = self.GetParent()
        if parent:
            parent.RemoveAnchor(self)

    #>-------------------------------------------------------------------
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.