location.py
Go to the documentation of this file.
00001 # -*- Python -*-
00002 # -*- coding: utf-8 -*-
00003 
00004 '''rtsprofile
00005 
00006 Copyright (C) 2009-2010
00007     Geoffrey Biggs
00008     RT-Synthesis Research Group
00009     Intelligent Systems Research Institute,
00010     National Institute of Advanced Industrial Science and Technology (AIST),
00011     Japan
00012     All rights reserved.
00013 Licensed under the Eclipse Public License -v 1.0 (EPL)
00014 http://www.opensource.org/licenses/eclipse-1.0.txt
00015 
00016 File: direction.py
00017 
00018 Location object storing the position of components in a graphical view.
00019 
00020 '''
00021 
00022 __version__ = '$Revision: $'
00023 # $Source$
00024 
00025 
00026 from rtsprofile import RTS_EXT_NS, RTS_EXT_NS_S
00027 from rtsprofile import direction as dir
00028 from rtsprofile.utils import validate_attribute
00029 
00030 
00031 ##############################################################################
00032 ## Location object
00033 
00034 class Location(object):
00035     '''Stores the location of a component in a graphical view.'''
00036 
00037     def __init__(self, x=0, y=0, height=0, width=0, direction=dir.DOWN):
00038         '''Constructor.
00039 
00040         @param x X position of the top-left of the component.
00041         @type x int
00042         @param y Y position of the top-left of the component.
00043         @type y int
00044         @param height Height of the component.
00045         @type height int
00046         @param width Width of the component.
00047         @type width int
00048         @param direction Direction the component faces.
00049         @type direction direction.const_type
00050 
00051         '''
00052         validate_attribute(x, 'Location.x',
00053                            expected_type=int, required=False)
00054         self._x = x
00055         validate_attribute(y, 'Location.y',
00056                            expected_type=int, required=False)
00057         self._y = y
00058         validate_attribute(height, 'Location.height',
00059                            expected_type=int, required=False)
00060         self._height = height
00061         validate_attribute(width, 'Location.width',
00062                            expected_type=int, required=False)
00063         self._width = width
00064         validate_attribute(direction, 'Location.direction',
00065                            expected_type=dir.const_type, required=False)
00066         self._direction = direction
00067 
00068     def __str__(self):
00069         return 'Position: {0}, {1}\nSize: {2}x{3}\nDirection: {4}'.format(\
00070                 self.x, self.y, self.width, self.height,
00071                 dir.to_string(self.direction))
00072 
00073     @property
00074     def x(self):
00075         '''The X position of the component in a graphical tool.'''
00076         return self._x
00077 
00078     @x.setter
00079     def x(self, x):
00080         validate_attribute(x, 'Location.x',
00081                            expected_type=int, required=False)
00082         self._x = x
00083 
00084     @property
00085     def y(self):
00086         '''The Y position of the component in a graphical tool.'''
00087         return self._y
00088 
00089     @y.setter
00090     def y(self, y):
00091         validate_attribute(y, 'Location.y',
00092                            expected_type=int, required=False)
00093         self._y = y
00094 
00095     @property
00096     def height(self):
00097         '''The height of the component in a graphical tool.'''
00098         return self._height
00099 
00100     @height.setter
00101     def height(self, height):
00102         validate_attribute(height, 'Location.height',
00103                            expected_type=int, required=False)
00104         self._height = height
00105 
00106     @property
00107     def width(self):
00108         '''The width of the component in a graphical tool.
00109 
00110         A value of -1 for this property indicates that the width should be as
00111         wide as is necessary.
00112 
00113         '''
00114         return self._width
00115 
00116     @width.setter
00117     def width(self, width):
00118         validate_attribute(width, 'Location.width',
00119                            expected_type=int, required=False)
00120         self._width = width
00121 
00122     @property
00123     def direction(self):
00124         '''The direction of the component in a graphical tool.
00125 
00126         A value of -1 for this property indicates that the height should be as
00127         wide as is necessary.
00128 
00129         '''
00130         return self._direction
00131 
00132     @direction.setter
00133     def direction(self, direction):
00134         validate_attribute(direction, 'Location.direction',
00135                            expected_type=dir.const_type, required=False)
00136         self._direction = direction
00137 
00138     def parse_xml_node(self, node):
00139         '''Parse an xml.dom Node object representing a location into this
00140         object.
00141 
00142         '''
00143         self.x = int(node.getAttributeNS(RTS_EXT_NS, 'x'))
00144         self.y = int(node.getAttributeNS(RTS_EXT_NS, 'y'))
00145         self.height = int(node.getAttributeNS(RTS_EXT_NS, 'height'))
00146         self.width = int(node.getAttributeNS(RTS_EXT_NS, 'width'))
00147         self.direction = dir.from_string(node.getAttributeNS(RTS_EXT_NS,
00148                 'direction'))
00149         return self
00150 
00151     def parse_yaml(self, y):
00152         '''Parse a YAML specification of a location into this object.'''
00153         self.x = int(y['x'])
00154         self.y = int(y['y'])
00155         self.height = int(y['height'])
00156         self.width = int(y['width'])
00157         self.direction = dir.from_string(y['direction'])
00158         return self
00159 
00160     def save_xml(self, doc, element):
00161         '''Save this location into an xml.dom.Element object.'''
00162         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'x', str(self.x))
00163         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'y', str(self.y))
00164         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'height',
00165                                str(self.height))
00166         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'width',
00167                                str(self.width))
00168         element.setAttributeNS(RTS_EXT_NS, RTS_EXT_NS_S + 'direction',
00169                                dir.to_string(self.direction))
00170 
00171     def to_dict(self):
00172         '''Save this location into a dictionary.'''
00173         return {'x': self.x,
00174                 'y': self.y,
00175                 'height': self.height,
00176                 'width': self.width,
00177                 'direction': dir.to_string(self.direction)}
00178 
00179 
00180 # vim: tw=79
00181 


rtsprofile
Author(s): Geoffrey Biggs
autogenerated on Sun Mar 26 2017 03:39:43