Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Css always have a stylesheet #361

Merged
merged 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions gaphor/UML/classes/tests/test_classconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ def test_connect(self):
self.connect(asc, asc.tail, c2)
assert asc.subject is not None

# Diagram, Class *2, Property *2, Association
self.assertEqual(6, len(list(self.element_factory.select())))
# Diagram, Class *2, Property *2, Association, StyleSheet
self.assertEqual(7, len(list(self.element_factory.select())))
assert asc.head_end.subject is not None
assert asc.tail_end.subject is not None

Expand Down
44 changes: 32 additions & 12 deletions gaphor/core/modeling/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import logging
import textwrap
import uuid
from dataclasses import dataclass
from typing import TYPE_CHECKING, Iterator, Optional, Sequence, Union
Expand All @@ -17,7 +18,7 @@
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
from gaphor.core.styling import Style, StyleNode

if TYPE_CHECKING:
from cairo import Context as CairoContext
Expand All @@ -34,14 +35,33 @@
"color": (0, 0, 0, 1),
"font-family": "sans",
"font-size": 14,
"font-style": FontStyle.NORMAL,
"highlight-color": (0, 0, 1, 0.4),
"line-width": 2,
"padding": (0, 0, 0, 0),
"text-align": TextAlign.CENTER,
}


DEFAULT_STYLE_SHEET = textwrap.dedent(
"""\
diagram {
background-color: white;
line-style: normal;
/* line-style: sloppy 0.3; */
}

diagram * {
background-color: transparent;
color: black;
font-family: sans;
font-size: 14;
highlight-color: rgba(0, 0, 255, 0.4);
line-width: 2;
padding: 0;
}
"""
)


@dataclass(frozen=True)
class UpdateContext:
"""
Expand Down Expand Up @@ -232,18 +252,18 @@ def __init__(
@property
def styleSheet(self) -> Optional[StyleSheet]:
model = self.model
return next(model.select(StyleSheet), None)
style_sheet = next(model.select(StyleSheet), None)
if not style_sheet:
style_sheet = self.model.create(StyleSheet)
style_sheet.styleSheet = DEFAULT_STYLE_SHEET
return style_sheet

def style(self, node: StyleNode) -> Style:
style_sheet = self.styleSheet
return (
{
**FALLBACK_STYLE, # type: ignore[misc]
**style_sheet.match(node),
}
if style_sheet
else FALLBACK_STYLE
)
return {
**FALLBACK_STYLE, # type: ignore[misc]
**style_sheet.match(node),
}

def save(self, save_func):
"""Apply the supplied save function to this diagram and the canvas."""
Expand Down
4 changes: 2 additions & 2 deletions gaphor/core/modeling/tests/test_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_diagram_stylesheet(element_factory):
assert diagram.styleSheet is styleSheet


def test_diagram_stylesheet_is_absent(element_factory):
def test_diagram_stylesheet_is_always_there(element_factory):
diagram = element_factory.create(Diagram)

assert diagram.styleSheet is None
assert diagram.styleSheet is not None
4 changes: 2 additions & 2 deletions gaphor/diagram/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def size(self, context: UpdateContext):
style = combined_style(context.style, self._inline_style)
min_w = style.get("min-width", 0)
min_h = style.get("min-height", 0)
text_align = style["text-align"]
text_align = style.get("text-align", TextAlign.CENTER)
padding_top, padding_right, padding_bottom, padding_left = style["padding"]

layout = self._layout
Expand Down Expand Up @@ -323,7 +323,7 @@ def draw(self, context: DrawContext, bounding_box: Rectangle):
style = combined_style(context.style, self._inline_style)

text_box = self.text_box(style, bounding_box)
text_align = style["text-align"]
text_align = style.get("text-align", TextAlign.CENTER)
focus_box = self.focus_box
x, y = focus_box_pos(text_box, (focus_box.width, focus_box.height), text_align)
focus_box.x = x
Expand Down
2 changes: 1 addition & 1 deletion gaphor/diagram/tests/test_copypaste.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_copy_item_when_subject_has_been_removed(diagram, element_factory):
cls.unlink() # normally handled by the sanitizer service

assert len(diagram.canvas.get_all_items()) == 0
assert len(element_factory.lselect()) == 2
assert len(element_factory.lselect()) == 3
assert not element_factory.lookup(orig_cls_id)

print(buffer)
Expand Down
3 changes: 2 additions & 1 deletion gaphor/plugins/diagramexport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from gaphor.abc import ActionProvider, Service
from gaphor.core import action, gettext
from gaphor.core.modeling.diagram import StyledDiagram
from gaphor.diagram.painter import ItemPainter
from gaphor.ui.filedialog import FileDialog
from gaphor.ui.questiondialog import QuestionDialog
Expand Down Expand Up @@ -64,7 +65,7 @@ def save_dialog(self, diagram, title, ext):
return filename

def update_painters(self, view, diagram):
style = diagram.style(diagram)
style = diagram.style(StyledDiagram(diagram))

sloppiness = style.get("line-style", 0.0)

Expand Down
2 changes: 1 addition & 1 deletion gaphor/storage/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def update_status_queue(_n=[0]):
)
yield from _load_attributes_and_references(elements, update_status_queue)

for d in factory.select(Diagram):
for d in factory.lselect(Diagram):
canvas = d.canvas
# update_now() is implicitly called when lock is released
canvas.block_updates = False
Expand Down
4 changes: 2 additions & 2 deletions gaphor/storage/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_load_uml_2(self):
data = self.save()
self.load(data)

assert len(self.element_factory.lselect()) == 5
assert len(self.element_factory.lselect()) == 6
assert len(self.element_factory.lselect(UML.Package)) == 1
assert len(self.element_factory.lselect(UML.Diagram)) == 1
d = self.element_factory.lselect(UML.Diagram)[0]
Expand Down Expand Up @@ -173,7 +173,7 @@ def test_save_and_load_model_with_relationships(self):
data = self.save()
self.load(data)

assert len(self.element_factory.lselect()) == 4
assert len(self.element_factory.lselect()) == 5
assert len(self.element_factory.lselect(UML.Package)) == 1
assert len(self.element_factory.lselect(UML.Diagram)) == 1
d = self.element_factory.lselect(UML.Diagram)[0]
Expand Down
51 changes: 42 additions & 9 deletions gaphor/ui/elementeditor.glade
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,14 @@ Tool selection only works from the diagram. If a tool does not get selected, cli
<property name="margin_left">6</property>
<property name="margin_right">6</property>
<property name="margin_top">12</property>
<property name="margin_bottom">6</property>
<property name="margin_bottom">12</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Style sheet</property>
<property name="label" translatable="yes">Preferences</property>
<property name="xalign">0</property>
<attributes>
<attribute name="weight" value="bold"/>
Expand All @@ -356,24 +356,24 @@ Tool selection only works from the diagram. If a tool does not get selected, cli
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Enable style sheet</property>
<property name="label" translatable="yes">Reset tool automatically</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSwitch" id="enable-style-sheet">
<object class="GtkSwitch">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="action_name">win.enable-style-sheet</property>
<property name="action_name">win.reset-tool-after-create</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
Expand All @@ -384,6 +384,23 @@ Tool selection only works from the diagram. If a tool does not get selected, cli
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_top">6</property>
<property name="label" translatable="yes">Style sheet</property>
<property name="xalign">0</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
Expand All @@ -401,7 +418,23 @@ Tool selection only works from the diagram. If a tool does not get selected, cli
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">🔗 &lt;a href="https://gaphor.readthedocs.io/en/latest/style_sheets.html"&gt;Style sheets in Gaphor&lt;/a&gt;</property>
<property name="use_markup">True</property>
<property name="wrap">True</property>
<property name="max_width_chars">28</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
Expand Down