1 # Copyright 2022 highstreet technologies GmbH
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 Module for an abstract class called "Top".
18 This calls should be inherited for common functions
20 from lxml import etree
25 The abstract "SVG" class adds common functions
32 __label: str = "my label"
35 FONTSIZE: int = 10 # see svg.style.css file
41 def __init__(self, tapi_object, center_x: int, center_y: int) -> None:
42 self.__center_x = center_x
43 self.__center_y = center_y
44 self.__tapi_object = tapi_object
45 self.__label = tapi_object.name().upper()
46 self.__width = 4 * self.FONTSIZE # default for 1 CEP == cep.width()
47 self.__height = 2 * self.FONTSIZE
50 def center_x(self) -> int:
52 Getter for x coordinate of the SVG center position
53 :return Center x coordinate in pixel
55 return self.__center_x
57 def center_y(self) -> int:
59 Getter for y coordinate of the SVG center position
60 :return Center y coordinate in pixel
62 return self.__center_y
64 def width(self) -> int:
66 Getter for width of the SVG Element
67 :return Width in pixel
71 def height(self) -> int:
73 Getter for height of the SVG Element
74 :return Height in pixel
78 def label(self) -> str:
80 Getter for the short label as displayed of the SVG Element
81 :return Label of the TAPI object
85 def tapi_object(self) -> int:
87 Getter for the given TAPI object
88 :return Python TAPI object
90 return self.__tapi_object
92 def type_name(self) -> int:
94 Getter for the given TAPI object type name
95 :return TAPI object type name
97 return type(self.tapi_object()).__name__
99 def svg_group(self) -> etree.Element:
101 Mothod generating the root SVG Element for the TAPI object
102 :return SVG Element as root for the TAPI object
104 group: etree.Element = etree.Element("g")
105 group.attrib["class"] = " ".join(["node", self.type_name()])
106 title = etree.Element("title")
107 title.text = "\n".join(
110 "id: " + self.tapi_object().identifier(),
111 "name: " + self.tapi_object().name()
117 def svg_main(self) -> etree.Element:
119 Mothod generating the main SVG Element shaping the TAPI object
120 :return SVG Element as main representations for the TAPI object
122 main = etree.Element("ellipse")
123 main.attrib['cx'] = str(self.center_x())
124 main.attrib['cy'] = str(self.center_y())
125 main.attrib['rx'] = str(int(self.width()/2))
126 main.attrib['ry'] = str(int(self.height()/2))
127 main.attrib['class'] = " ".join([self.type_name()])
130 def svg_label(self) -> etree.Element:
132 Mothod generating the SVG Element of the label of the TAPI object
133 :return SVG Element for the label of the TAPI object
135 label = etree.Element('text')
136 label.attrib['x'] = str(self.center_x())
137 # +4px for font-size 14px (think of chars like 'gjy')
138 label.attrib['y'] = str(self.center_y() + 4)
139 label.text = self.label()
142 def svg_center(self) -> etree.Element:
144 Mothod generating the SVG Element of the label of the TAPI object
145 :return SVG Element for the label of the TAPI object
147 dot = etree.Element('circle')
148 dot.attrib['cx'] = str(self.center_x())
149 dot.attrib['cy'] = str(self.center_y())
150 dot.attrib['r'] = "2"
151 dot.attrib['class'] = "dot"
154 def svg_element(self) -> etree.Element:
156 Method generating a SVG Element representing the TAPI Object
157 :return A SVG group element including the main shape and a label
159 group: etree.Element = self.svg_group()
160 group.attrib['id'] = self.tapi_object().identifier()
161 group.append(self.svg_main())
162 group.append(self.svg_label())
163 # group.append(self.svg_center())