-Item stffix is stripped from item names
This commit is contained in:
parent
b3cf3ccff9
commit
7a6889adf5
@ -15,6 +15,7 @@ import gaphas
|
||||
from gaphor.core.modeling.coremodel import PackageableElement
|
||||
from gaphor.core.modeling.element import Id, RepositoryProtocol
|
||||
from gaphor.core.modeling.event import DiagramItemCreated
|
||||
from gaphor.core.modeling.presentation import Presentation
|
||||
from gaphor.core.modeling.stylesheet import StyleSheet
|
||||
from gaphor.core.styling import FontStyle, Style, StyleNode, TextAlign
|
||||
|
||||
@ -69,12 +70,21 @@ class DrawContext:
|
||||
dropzone: bool
|
||||
|
||||
|
||||
# From https://www.python.org/dev/peps/pep-0616/
|
||||
def removesuffix(self: str, suffix: str, /) -> str:
|
||||
# suffix='' should not call self[:-0].
|
||||
if suffix and self.endswith(suffix):
|
||||
return self[: -len(suffix)]
|
||||
else:
|
||||
return self[:]
|
||||
|
||||
|
||||
class StyledDiagram:
|
||||
def __init__(self, diagram: Diagram, view: Optional[gaphas.View] = None):
|
||||
self.diagram = diagram
|
||||
self.view = view
|
||||
|
||||
def local_name(self) -> str:
|
||||
def name(self) -> str:
|
||||
return "diagram"
|
||||
|
||||
def parent(self):
|
||||
@ -99,13 +109,14 @@ class StyledItem:
|
||||
pseudo-classes for the item (focus, hover, etc.)
|
||||
"""
|
||||
|
||||
def __init__(self, item: gaphas.Item, view: Optional[gaphas.View] = None):
|
||||
def __init__(self, item: Presentation, view: Optional[gaphas.View] = None):
|
||||
assert item.canvas
|
||||
self.item = item
|
||||
self.canvas = item.canvas
|
||||
self.view = view
|
||||
|
||||
def local_name(self) -> str:
|
||||
return type(self.item).__name__.lower()
|
||||
def name(self) -> str:
|
||||
return removesuffix(type(self.item).__name__, "Item").lower()
|
||||
|
||||
def parent(self) -> Union[StyledItem, StyledDiagram]:
|
||||
parent = self.canvas.get_parent(self.item)
|
||||
@ -254,7 +265,9 @@ class Diagram(PackageableElement):
|
||||
return self.create_as(type, str(uuid.uuid1()), parent, subject)
|
||||
|
||||
def create_as(self, type, id, parent=None, subject=None):
|
||||
if not (type and issubclass(type, gaphas.Item)):
|
||||
if not (
|
||||
type and issubclass(type, gaphas.Item) and issubclass(type, Presentation)
|
||||
):
|
||||
raise TypeError(
|
||||
f"Type {type} can not be added to a diagram as it is not a diagram item"
|
||||
)
|
||||
|
47
gaphor/core/modeling/tests/test_diagram_style.py
Normal file
47
gaphor/core/modeling/tests/test_diagram_style.py
Normal file
@ -0,0 +1,47 @@
|
||||
import gaphas
|
||||
import pytest
|
||||
|
||||
from gaphor.core.eventmanager import EventManager
|
||||
from gaphor.core.modeling import ElementFactory, Presentation
|
||||
from gaphor.core.modeling.diagram import Diagram, StyledDiagram, StyledItem
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def event_manager():
|
||||
return EventManager()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def element_factory(event_manager):
|
||||
return ElementFactory(event_manager)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def diagram(element_factory):
|
||||
diagram = element_factory.create(Diagram)
|
||||
yield diagram
|
||||
diagram.unlink()
|
||||
|
||||
|
||||
class DemoItem(Presentation, gaphas.Item):
|
||||
pass
|
||||
|
||||
|
||||
def test_name_does_not_have_item_suffix(diagram):
|
||||
item = diagram.create(DemoItem)
|
||||
node = StyledItem(item)
|
||||
|
||||
assert node.name() == "demo"
|
||||
|
||||
|
||||
def test_diagram_is_parent_of_item(diagram):
|
||||
item = diagram.create(DemoItem)
|
||||
node = StyledItem(item)
|
||||
|
||||
assert node.parent().name() == "diagram"
|
||||
|
||||
|
||||
def test_diagram_has_no_parent(diagram):
|
||||
node = StyledDiagram(diagram)
|
||||
|
||||
assert node.parent() is None
|
@ -30,7 +30,7 @@ from gaphor.core.styling.selectors import compile_selector_list
|
||||
|
||||
|
||||
class StyleNode(Protocol):
|
||||
def local_name(self) -> str:
|
||||
def name(self) -> str:
|
||||
...
|
||||
|
||||
def parent(self) -> Optional[StyleNode]:
|
||||
|
@ -43,8 +43,8 @@ def compile_compound_selector(selector: parser.CompoundSelector):
|
||||
|
||||
|
||||
@compile_node.register
|
||||
def compile_local_name_selector(selector: parser.LocalNameSelector):
|
||||
return lambda el: el.local_name() == selector.lower_local_name
|
||||
def compile_name_selector(selector: parser.LocalNameSelector):
|
||||
return lambda el: el.name() == selector.lower_local_name
|
||||
|
||||
|
||||
def ancestors(el):
|
||||
|
@ -4,8 +4,8 @@ from gaphor.core.styling import parse_style_sheet
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, local_name, parent=None, children=None, attributes={}, state=()):
|
||||
self._local_name = local_name
|
||||
def __init__(self, name, parent=None, children=None, attributes={}, state=()):
|
||||
self._name = name
|
||||
self._parent = parent
|
||||
self._children = children or []
|
||||
self._attributes = attributes
|
||||
@ -16,8 +16,8 @@ class Node:
|
||||
for c in self._children:
|
||||
c._parent = self
|
||||
|
||||
def local_name(self):
|
||||
return self._local_name
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
def parent(self):
|
||||
return self._parent
|
||||
@ -36,8 +36,8 @@ def test_node_test_object_parent_child():
|
||||
c = Node("child")
|
||||
p = Node("parent", children=[c])
|
||||
|
||||
assert c.local_name() == "child"
|
||||
assert p.local_name() == "parent"
|
||||
assert c.name() == "child"
|
||||
assert p.name() == "parent"
|
||||
assert c.parent() is p
|
||||
assert c in p.children()
|
||||
|
||||
@ -46,8 +46,8 @@ def test_node_test_object_child_parent():
|
||||
p = Node("parent")
|
||||
c = Node("child", parent=p)
|
||||
|
||||
assert c.local_name() == "child"
|
||||
assert p.local_name() == "parent"
|
||||
assert c.name() == "child"
|
||||
assert p.name() == "parent"
|
||||
assert c.parent() is p
|
||||
assert c in p.children()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user