core-extra/coretk/coretk/graph/shape.py

125 lines
3.7 KiB
Python
Raw Normal View History

2019-11-27 21:15:04 +00:00
"""
class for shapes
"""
2019-12-03 00:05:10 +00:00
from coretk.dialogs.shapemod import ShapeDialog
2019-11-28 00:39:48 +00:00
from coretk.images import ImageEnum
ABOVE_COMPONENT = ["gridline", "edge", "linkinfo", "antenna", "node", "nodename"]
2019-11-27 21:15:04 +00:00
2019-12-06 17:03:21 +00:00
class AnnotationData:
2019-12-05 18:12:31 +00:00
def __init__(
self,
2019-12-06 17:03:21 +00:00
text="",
font="Arial",
font_size=12,
text_color="#000000",
fill_color="#CFCFFF",
border_color="#000000",
border_width=0,
2019-12-05 18:12:31 +00:00
bold=0,
italic=0,
underline=0,
):
2019-12-06 17:03:21 +00:00
self.text = text
self.font = font
self.font_size = font_size
self.text_color = text_color
self.fill_color = fill_color
self.border_color = border_color
self.border_width = border_width
self.bold = bold
self.italic = italic
self.underline = underline
2019-12-04 01:17:45 +00:00
2019-11-27 21:15:04 +00:00
class Shape:
2019-12-05 18:12:31 +00:00
def __init__(
self,
app,
canvas,
top_x=None,
top_y=None,
coords=None,
data=None,
shape_type=None,
):
2019-11-27 21:15:04 +00:00
self.app = app
self.canvas = canvas
2019-12-05 18:12:31 +00:00
if data is None:
self.x0 = top_x
self.y0 = top_y
self.created = False
self.text_id = None
2019-12-06 17:03:21 +00:00
self.shape_data = AnnotationData()
2019-12-05 18:12:31 +00:00
canvas.delete(canvas.find_withtag("selectednodes"))
annotation_type = self.canvas.annotation_type
if annotation_type == ImageEnum.OVAL:
self.id = canvas.create_oval(
top_x, top_y, top_x, top_y, tags="shape", dash="-"
)
elif annotation_type == ImageEnum.RECTANGLE:
self.id = canvas.create_rectangle(
top_x, top_y, top_x, top_y, tags="shape", dash="-"
)
else:
x0, y0, x1, y1 = coords
self.x0 = x0
self.y0 = y0
self.created = True
if shape_type == "oval":
self.id = self.canvas.create_oval(
x0,
y0,
x1,
y1,
tags="shape",
fill=data.fill_color,
outline=data.border_color,
width=data.border_width,
)
elif shape_type == "rectangle":
self.id = self.canvas.create_rectangle(
x0,
y0,
x1,
y1,
tags="shape",
fill=data.fill_color,
outline=data.border_color,
width=data.border_width,
)
_x = (x0 + x1) / 2
2019-12-05 21:39:09 +00:00
_y = y0 + 1.5 * data.font_size
2019-12-05 18:12:31 +00:00
self.text_id = self.canvas.create_text(
2019-12-05 21:39:09 +00:00
_x, _y, tags="shapetext", text=data.text, fill=data.text_color
2019-12-05 18:12:31 +00:00
)
self.shape_data = data
2019-11-28 00:39:48 +00:00
self.cursor_x = None
self.cursor_y = None
def shape_motion(self, x1, y1):
self.canvas.coords(self.id, self.x0, self.y0, x1, y1)
def shape_complete(self, x, y):
for component in ABOVE_COMPONENT:
self.canvas.tag_raise(component)
2019-12-04 01:17:45 +00:00
s = ShapeDialog(self.app, self.app, self)
2019-12-03 00:05:10 +00:00
s.show()
2019-11-28 00:39:48 +00:00
def motion(self, event, delta_x=None, delta_y=None):
if event is not None:
delta_x = event.x - self.cursor_x
delta_y = event.y - self.cursor_y
self.cursor_x = event.x
self.cursor_y = event.y
self.canvas.move(self.id, delta_x, delta_y)
self.canvas.object_drag(self.id, delta_x, delta_y)
if self.text_id is not None:
self.canvas.move(self.text_id, delta_x, delta_y)
def delete(self):
self.canvas.delete(self.id)
self.canvas.delete(self.text_id)