gns3-server/gns3server/controller/shape.py

131 lines
3.4 KiB
Python
Raw Normal View History

2016-06-20 16:45:31 +00:00
#!/usr/bin/env python
#
# Copyright (C) 2016 GNS3 Technologies Inc.
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
import uuid
2016-06-21 07:49:16 +00:00
class Shape:
2016-06-20 16:45:31 +00:00
"""
2016-06-21 07:49:16 +00:00
Shape are visual element not used by the network emulation. Like
2016-06-20 16:45:31 +00:00
text, images, rectangle... They are pure SVG elements.
"""
2016-06-22 16:14:14 +00:00
2016-06-21 17:39:00 +00:00
def __init__(self, project, shape_id=None, svg="<svg></svg>", x=0, y=0, z=0, rotation=0):
self.svg = svg
2016-06-20 16:45:31 +00:00
self._project = project
2016-06-21 07:49:16 +00:00
if shape_id is None:
2016-06-20 16:45:31 +00:00
self._id = str(uuid.uuid4())
else:
2016-06-21 07:49:16 +00:00
self._id = shape_id
2016-06-20 16:45:31 +00:00
self._x = x
self._y = y
self._z = z
2016-06-21 17:39:00 +00:00
self._rotation = rotation
2016-06-20 16:45:31 +00:00
@property
def id(self):
return self._id
@property
def svg(self):
return self._svg
@svg.setter
def svg(self, value):
self._svg = value
@property
def x(self):
return self._x
@x.setter
def x(self, val):
self._x = val
@property
def y(self):
return self._y
@y.setter
def y(self, val):
self._y = val
@property
def z(self):
return self._z
@z.setter
def z(self, val):
self._z = val
2016-06-21 17:39:00 +00:00
@property
def rotation(self):
return self._rotation
@rotation.setter
def rotation(self, val):
self._rotation = val
2016-06-20 16:45:31 +00:00
@asyncio.coroutine
def update(self, **kwargs):
"""
Update the shape
2016-06-20 16:45:31 +00:00
:param kwargs: Shape properties
2016-06-20 16:45:31 +00:00
"""
# Update node properties with additional elements
svg_changed = False
2016-06-20 16:45:31 +00:00
for prop in kwargs:
if getattr(self, prop) != kwargs[prop]:
if prop == "svg":
# To avoid spamming client with large data we don't send the svg if the SVG didn't change
svg_changed = True
2016-06-20 16:45:31 +00:00
setattr(self, prop, kwargs[prop])
data = self.__json__()
if not svg_changed:
del data["svg"]
self._project.controller.notification.emit("shape.updated", data)
2016-06-20 16:45:31 +00:00
self._project.dump()
def __json__(self, topology_dump=False):
"""
:param topology_dump: Filter to keep only properties require for saving on disk
"""
if topology_dump:
return {
2016-06-21 07:49:16 +00:00
"shape_id": self._id,
2016-06-20 16:45:31 +00:00
"x": self._x,
"y": self._y,
"z": self._z,
2016-06-21 17:39:00 +00:00
"rotation": self._rotation,
"svg": self._svg
2016-06-20 16:45:31 +00:00
}
return {
"project_id": self._project.id,
2016-06-21 07:49:16 +00:00
"shape_id": self._id,
2016-06-20 16:45:31 +00:00
"x": self._x,
"y": self._y,
"z": self._z,
2016-06-21 17:39:00 +00:00
"rotation": self._rotation,
"svg": self._svg
2016-06-20 16:45:31 +00:00
}
def __repr__(self):
2016-06-21 07:49:16 +00:00
return "<gns3server.controller.Shape {}>".format(self._id)