Skip to content

Commit

Permalink
Center label when hostname change
Browse files Browse the repository at this point in the history
Fix #1404
  • Loading branch information
julien-duponchelle committed Aug 17, 2016
1 parent f82527e commit 5de27a9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 24 deletions.
29 changes: 21 additions & 8 deletions gns3server/controller/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from .compute import ComputeConflict
from ..utils.images import images_directories
from ..utils.qt import qt_font_to_style


import logging
Expand Down Expand Up @@ -54,13 +55,7 @@ def __init__(self, project, compute, name, node_id=None, node_type=None, **kwarg
self._compute = compute
self._node_type = node_type

self._label = {
"y": -25,
"text": "",
"style": "font-size: 10;font-familly: Verdana",
"x": -17,
"rotation": 0
}
self._label = None
self._name = None
self.name = name
self._console = None
Expand Down Expand Up @@ -106,7 +101,9 @@ def name(self):
def name(self, new_name):
self._name = self._project.update_node_name(self, new_name)
# The text in label need to be always the node name
self._label["text"] = self._name
if self.label and self._label["text"] != self._name:
self._label["text"] = self._name
self._label["x"] = None # Center text

@property
def node_type(self):
Expand Down Expand Up @@ -195,6 +192,22 @@ def symbol(self, val):
# If symbol is invalid we replace it by default
except (ValueError, OSError):
self.symbol = ":/symbols/computer.svg"
if self._label is None:
# Apply to label user style or default
try:
style = qt_font_to_style(
self._project.controller.settings["GraphicsView"]["default_label_font"],
self._project.controller.settings["GraphicsView"]["default_label_color"])
except KeyError:
style = "font-size: 10;font-familly: Verdana"

self._label = {
"y": round(self._height / 2 + 10) * -1,
"text": self._name,
"style": style,
"x": None, # None: mean the client should center it
"rotation": 0
}

@property
def label(self):
Expand Down
17 changes: 3 additions & 14 deletions gns3server/controller/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

from ..version import __version__
from ..schemas.topology import TOPOLOGY_SCHEMA
from ..utils.qt import qt_font_to_style


import logging
log = logging.getLogger(__name__)
Expand Down Expand Up @@ -403,20 +405,7 @@ def _convert_label(label):
"""
Convert a label from 1.X to the new format
"""
font_info = label["font"].split(",")
style = "font-family: {};font-size: {};".format(font_info[0], font_info[1])
if font_info[4] == "75":
style += "font-weight: bold;"
if font_info[5] == "1":
style += "font-style: italic;"
color = label["color"]

if len(color) == 9:
style += "fill: #" + color[-6:] + ";"
style += "fill-opacity: {};".format(round(1.0 / 255 * int(color[:3][-2:], base=16), 2))
else:
style += "fill: #" + color[-6:] + ";"
style += "fill-opacity: {};".format(1.0)
style = qt_font_to_style(label["font"], label["color"])
return {
"text": label["text"],
"rotation": 0,
Expand Down
4 changes: 2 additions & 2 deletions gns3server/schemas/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"type": "string"
},
"x": {
"description": "Relative X position of the label",
"type": "integer"
"description": "Relative X position of the label. If null center it",
"type": ["integer", "null"]
},
"y": {
"description": "Relative Y position of the label",
Expand Down
40 changes: 40 additions & 0 deletions gns3server/utils/qt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/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/>.

"""
Helper for conversion of Qt stuff
"""


def qt_font_to_style(font, color):
"""
Convert a Qt font to CSS style
"""
font_info = font.split(",")
style = "font-family: {};font-size: {};".format(font_info[0], font_info[1])
if font_info[4] == "75":
style += "font-weight: bold;"
if font_info[5] == "1":
style += "font-style: italic;"

if len(color) == 9:
style += "fill: #" + color[-6:] + ";"
style += "fill-opacity: {};".format(round(1.0 / 255 * int(color[:3][-2:], base=16), 2))
else:
style += "fill: #" + color[-6:] + ";"
style += "fill-opacity: {};".format(1.0)
return style
24 changes: 24 additions & 0 deletions tests/controller/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,35 @@ def test_symbol(node):
assert node.symbol == ":/symbols/dslam.svg"
assert node.width == 50
assert node.height == 53
assert node.label["x"] is None
assert node.label["y"] == -40

node.symbol = ":/symbols/cloud.svg"
assert node.symbol == ":/symbols/cloud.svg"
assert node.width == 159
assert node.height == 71

assert node.label["x"] is None
assert node.label["y"] == -40
assert node.label["style"] == "font-size: 10;font-familly: Verdana"


def test_label_with_default_label_font(node):
"""
If user has changed the font we need to have the node label using
the correct color
"""
node.project.controller.settings = {
"GraphicsView": {
"default_label_color": "#ff0000",
"default_label_font": "TypeWriter,10,-1,5,75,0,0,0,0,0"
}
}

node._label = None
node.symbol = ":/symbols/dslam.svg"
assert node.label["style"] == "font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #ff0000;fill-opacity: 1.0;"


def test_update(node, compute, project, async_run, controller):
response = MagicMock()
Expand Down

0 comments on commit 5de27a9

Please sign in to comment.