Skip to content

Commit

Permalink
Style is no longer exposed via Presentation
Browse files Browse the repository at this point in the history
* Require Gaphas 2.1.1 (master) code
* Everything styling related to gaphor.core.styling.
* Provide style data through update/draw contexts
* Style is requested via the diagram object
* Use Style type all around
  • Loading branch information
amolenaar committed Jul 5, 2020
1 parent 5088e99 commit 2cd08e4
Show file tree
Hide file tree
Showing 29 changed files with 495 additions and 529 deletions.
8 changes: 2 additions & 6 deletions gaphor/SysML/blocks/proxyport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
from gaphor.diagram.presentation import Named, postload_connect
from gaphor.diagram.shapes import (
Box,
DrawContext,
EditableText,
IconBox,
SizeContext,
Text,
TextAlign,
VerticalAlign,
Expand Down Expand Up @@ -111,9 +109,7 @@ def pre_update(self, context):
self._last_connected_side = side
self.update_shapes()

self.shape.size(SizeContext.from_context(context, self.style))
self.shape.size(context)

def draw(self, context):
self.shape.draw(
DrawContext.from_context(context, self.style), self.dimensions()
)
self.shape.draw(context, self.dimensions())
6 changes: 2 additions & 4 deletions gaphor/UML/actions/activitynodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from gaphor import UML
from gaphor.core.modeling import Presentation
from gaphor.diagram.presentation import ElementPresentation, Named
from gaphor.diagram.shapes import Box, DrawContext, EditableText, IconBox, Text, stroke
from gaphor.diagram.shapes import Box, EditableText, IconBox, Text, stroke
from gaphor.diagram.support import represents
from gaphor.UML.modelfactory import stereotypes_str

Expand Down Expand Up @@ -267,9 +267,7 @@ def _set_combined(self, value):
def draw(self, context):
h1, h2 = self.handles()
height = h2.pos.y - h1.pos.y
self.shape.draw(
DrawContext.from_context(context, self.style), Rectangle(0, 0, 1, height)
)
self.shape.draw(context, Rectangle(0, 0, 1, height))

def draw_fork_node(self, _box, context, _bounding_box):
"""
Expand Down
13 changes: 2 additions & 11 deletions gaphor/UML/actions/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@

from gaphor import UML
from gaphor.diagram.presentation import ElementPresentation, Named
from gaphor.diagram.shapes import (
Box,
SizeContext,
Text,
cairo_state,
draw_highlight,
stroke,
)
from gaphor.diagram.shapes import Box, Text, cairo_state, draw_highlight, stroke
from gaphor.diagram.support import represents
from gaphor.diagram.text import VerticalAlign
from gaphor.UML.modelfactory import stereotypes_str
Expand Down Expand Up @@ -63,9 +56,7 @@ def toplevel(self):
def pre_update(self, context):
assert self.canvas

self._header_size = self.shape.size(
SizeContext.from_context(context, self.style)
)
self._header_size = self.shape.size(context)

# get subpartitions
children: List[PartitionItem] = [
Expand Down
5 changes: 3 additions & 2 deletions gaphor/UML/actions/tests/test_flow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from gaphas.canvas import instant_cairo_context

import gaphor.UML as UML
from gaphor.diagram.shapes import DrawContext
from gaphor.core.modeling import DrawContext
from gaphor.core.styling import DEFAULT_STYLE
from gaphor.tests.testcase import TestCase
from gaphor.UML.actions.flow import FlowItem

Expand Down Expand Up @@ -41,11 +42,11 @@ def test_draw(self):
flow = self.create(FlowItem, UML.ControlFlow)
context = DrawContext(
cairo=instant_cairo_context(),
style=DEFAULT_STYLE,
hovered=True,
focused=True,
selected=True,
dropzone=False,
style={},
)

flow.draw(context)
21 changes: 11 additions & 10 deletions gaphor/UML/classes/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

from gaphor import UML
from gaphor.core.modeling import Presentation
from gaphor.core.styling import Style
from gaphor.diagram.presentation import LinePresentation, Named
from gaphor.diagram.shapes import (
Box,
DrawContext,
EditableText,
Text,
cairo_state,
combined_style,
draw_default_head,
draw_default_tail,
text_draw_focus_box,
Expand Down Expand Up @@ -200,13 +201,13 @@ def post_update(self, context):
self.draw_tail = draw_default_tail

# update relationship after self.set calls to avoid circural updates
size_context = super().post_update(context)
super().post_update(context)

# Calculate alignment of the head name and multiplicity
self._head_end.post_update(size_context, handles[0].pos, handles[1].pos)
self._head_end.post_update(context, handles[0].pos, handles[1].pos)

# Calculate alignment of the tail name and multiplicity
self._tail_end.post_update(size_context, handles[-1].pos, handles[-2].pos)
self._tail_end.post_update(context, handles[-1].pos, handles[-2].pos)

def point(self, pos):
"""
Expand All @@ -217,11 +218,11 @@ def point(self, pos):
)

def draw(self, context):
draw_context = super().draw(context)
self._head_end.draw(draw_context)
self._tail_end.draw(draw_context)
super().draw(context)
self._head_end.draw(context)
self._tail_end.draw(context)
if self._show_direction:
with cairo_state(draw_context.cairo) as cr:
with cairo_state(context.cairo) as cr:
cr.translate(*self._dir_pos)
cr.rotate(self._dir_angle)
cr.move_to(0, 0)
Expand Down Expand Up @@ -382,7 +383,7 @@ def __init__(self, owner, end=None):
self._name_layout = Layout("")
self._mult_layout = Layout("")

self._inline_style = {"font-size": 10}
self._inline_style: Style = {"font-size": 10}

name_bounds = property(lambda s: s._name_bounds)

Expand Down Expand Up @@ -417,7 +418,7 @@ def post_update(self, context, p1, p2):
multiplicity label. p1 is the line end and p2 is the last
but one point of the line.
"""
style = {**context.style, **self._inline_style}
style = combined_style(context.style, self._inline_style)
ofs = 5

name_dx = 0.0
Expand Down
5 changes: 3 additions & 2 deletions gaphor/UML/classes/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from gaphas.canvas import instant_cairo_context

from gaphor import UML
from gaphor.diagram.shapes import SizeContext
from gaphor.core.modeling import UpdateContext
from gaphor.core.styling import DEFAULT_STYLE
from gaphor.tests.testcase import TestCase
from gaphor.UML.classes.klass import ClassItem

Expand All @@ -15,7 +16,7 @@ def compartments(item):


def context():
return SizeContext(cairo=instant_cairo_context(), style={})
return UpdateContext(style=DEFAULT_STYLE)


class ClassTestCase(TestCase):
Expand Down
6 changes: 2 additions & 4 deletions gaphor/UML/interactions/executionspecification.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from gaphor import UML
from gaphor.core.modeling import Presentation
from gaphor.diagram.presentation import postload_connect
from gaphor.diagram.shapes import Box, DrawContext, draw_border
from gaphor.diagram.shapes import Box, draw_border
from gaphor.diagram.support import represents


Expand Down Expand Up @@ -85,9 +85,7 @@ def dimensions(self):
return Rectangle(pt.x - d / 2, pt.y, d, y1=pb.y)

def draw(self, context):
self.shape.draw(
DrawContext.from_context(context, self.style), self.dimensions()
)
self.shape.draw(context, self.dimensions())

def point(self, pos):
return distance_rectangle_point(self.dimensions(), pos)
Expand Down
5 changes: 3 additions & 2 deletions gaphor/UML/interactions/tests/test_executionspecification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from gaphas.canvas import Canvas, instant_cairo_context

from gaphor import UML
from gaphor.diagram.shapes import DrawContext
from gaphor.core.modeling import DrawContext
from gaphor.core.styling import DEFAULT_STYLE
from gaphor.diagram.tests.fixtures import allow, connect, disconnect
from gaphor.UML.interactions.executionspecification import ExecutionSpecificationItem
from gaphor.UML.interactions.lifeline import LifelineItem
Expand All @@ -25,11 +26,11 @@ def test_draw_on_canvas(diagram):
exec_spec.draw(
DrawContext(
cairo=cr,
style=DEFAULT_STYLE,
selected=False,
focused=False,
hovered=False,
dropzone=False,
style={},
)
)

Expand Down
5 changes: 3 additions & 2 deletions gaphor/core/modeling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
NamedElement,
PackageableElement,
)
from gaphor.core.modeling.diagram import Diagram
from gaphor.core.modeling.diagram import Diagram, DrawContext, UpdateContext
from gaphor.core.modeling.elementfactory import ElementFactory
from gaphor.core.modeling.event import *
from gaphor.core.modeling.presentation import Presentation, StyleSheet
from gaphor.core.modeling.presentation import Presentation
from gaphor.core.modeling.stylesheet import StyleSheet
60 changes: 55 additions & 5 deletions gaphor/core/modeling/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,64 @@

import logging
import uuid
from typing import TYPE_CHECKING
from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional

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.stylesheet import StyleSheet
from gaphor.core.styling.properties import DEFAULT_STYLE, Style

if TYPE_CHECKING:
from cairo import Context as CairoContext
from gaphor.core.modeling.properties import relation_one
from gaphor.UML import Package

log = logging.getLogger(__name__)


@dataclass(frozen=True)
class UpdateContext:
"""
Context used when updating items (Presentation's).
Style contains the base style, no style alterations due to view state
(focused, hovered, etc.).
"""

style: Style


@dataclass(frozen=True)
class DrawContext:
"""
Special context for draw()'ing the item. The draw-context contains
stuff like the cairo context and flags like selected and
focused.
"""

cairo: CairoContext
style: Style
selected: bool
focused: bool
hovered: bool
dropzone: bool


class DiagramCanvas(gaphas.Canvas):
"""DiagramCanvas extends the gaphas.Canvas class. Updates to the canvas
can be blocked by setting the block_updates property to true. A save
function can be applied to all root canvas items. Canvas items can be
selected with an optional expression filter."""

def __init__(self, diagram):
def __init__(self, diagram, create_update_context):
"""Initialize the diagram canvas with the supplied diagram. By default,
updates are not blocked."""

super().__init__()
super().__init__(create_update_context)
self._diagram = diagram
self._block_updates = False

Expand Down Expand Up @@ -92,12 +125,29 @@ class Diagram(PackageableElement):

package: relation_one[Package]

def __init__(self, id, model):
def __init__(
self, id: Optional[Id] = None, model: Optional[RepositoryProtocol] = None
):
"""Initialize the diagram with an optional id and element model.
The diagram also has a canvas."""

super().__init__(id, model)
self.canvas = DiagramCanvas(self)
self.canvas = DiagramCanvas(self, self._create_update_context)

@property
def styleSheet(self) -> Optional[StyleSheet]:
return next(self._model.select(StyleSheet), None) if self._model else None

def item_style(self, item) -> Style:
styleSheet = self.styleSheet
return (
{**DEFAULT_STYLE, **styleSheet.item_style(item)} # type: ignore[misc]
if styleSheet
else DEFAULT_STYLE
)

def _create_update_context(self, item):
return UpdateContext(style=self.item_style(item))

def save(self, save_func):
"""Apply the supplied save function to this diagram and the canvas."""
Expand Down

0 comments on commit 2cd08e4

Please sign in to comment.