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

Label wrapping #233

Merged
merged 3 commits into from
Dec 22, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions eddy/core/items/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ def __init__(self, template='', movable=True, editable=True, parent=None):
self.setFont(Font(weight=Font.Light))
self.setText(self.template)
self.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.setToolTip(template)
self.wrapLabel()

self.customFont = False

Expand All @@ -306,7 +308,7 @@ def setCustomFont(self, font):
if not font==self.font():
npos = self.pos()
self.setFont(font)
self.setAlignment(self.alignment())
self.wrapLabel()
self.setPos(npos)
self.customFont = True

Expand All @@ -327,7 +329,7 @@ def sceneEvent(self, event: QtCore.QEvent) -> bool:
# UPDATE THE DOCUMENT FONT AND ADJUST ITEM SIZE AND POSITION
npos = self.pos()
self.setFont(nfont)
self.setAlignment(self.alignment())
self.wrapLabel()
self.setPos(npos)
# CASCADE THE EVENT TO EACH CHILD ITEM
for item in self.childItems():
Expand Down Expand Up @@ -380,12 +382,12 @@ def focusOutEvent(self, focusEvent):

self.focusInData = None
self.setSelectedText(False)
self.setAlignment(self.alignment())
self.wrapLabel()
self.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.diagram.setMode(DiagramMode.Idle)
self.diagram.sgnUpdated.emit()

self.setAlignment(self.alignment())
self.wrapLabel()
mnamici marked this conversation as resolved.
Show resolved Hide resolved
self.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
super().focusOutEvent(focusEvent)

Expand Down Expand Up @@ -525,8 +527,6 @@ def setAlignment(self, alignment):
:type alignment: int
"""
self._alignment = alignment
self.setTextWidth(-1)
self.setTextWidth(self.boundingRect().width())
format_ = QtGui.QTextBlockFormat()
format_.setAlignment(alignment)
cursor = self.textCursor()
Expand Down Expand Up @@ -602,6 +602,14 @@ def width(self):
"""
return self.boundingRect().width()

def wrapLabel(self):
"""
Wrap label into node.
"""
self.setTextWidth(-1)
self.setTextWidth(self.boundingRect().width())
self.setAlignment(self.alignment())

def __repr__(self):
"""
Returns repr(self).
Expand Down
29 changes: 28 additions & 1 deletion eddy/core/items/nodes/common/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@
##########################################################################


from PyQt5 import QtCore
from math import ceil

from PyQt5 import (
QtCore,
QtGui,
)

from eddy.core.commands.labels import CommandLabelChange
from eddy.core.datatypes.graphol import Item
from eddy.core.datatypes.misc import DiagramMode
from eddy.core.functions.misc import isEmpty
from eddy.core.items.common import AbstractLabel
Expand All @@ -45,6 +51,9 @@ class NodeLabel(AbstractLabel):
"""
This class implements the label to be attached to the graphol nodes.
"""
MinWidth = 60.0 # Minimum label width (in pixels)
MinHeight = 40.0 # Minimum label height (in pixels)

def __init__(self, template='', pos=None, movable=True, editable=True, parent=None):
"""
Initialize the label.
Expand Down Expand Up @@ -100,6 +109,24 @@ def updatePos(self, moved=False):
if not moved:
self.setPos(self.defaultPos())

def wrapLabel(self):
"""
Wrap label into node.
"""
super().wrapLabel()
if self.parentItem() and self.parentItem().type() in [Item.ConceptNode,
Item.IndividualNode,
Item.ValueDomainNode,
Item.LiteralNode,
Item.FacetNode]:
width = max(self.MinWidth, self.parentItem().width())
height = max(self.MinHeight, self.parentItem().height())
fm = QtGui.QFontMetrics(self.font())
lineWidth = fm.width(self.template) / ceil(fm.width(self.template) / width)
maxWidth = int(lineWidth * (height // fm.lineSpacing()))
self.setText(fm.elidedText(self.template, QtCore.Qt.TextElideMode.ElideRight, maxWidth))
self.setTextWidth(width)
self.setAlignment(self.alignment())

class PredicateLabel(NodeLabel):
"""
Expand Down
1 change: 1 addition & 0 deletions eddy/core/items/nodes/concept.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def resize(self, mousePos):
self.polygon.setGeometry(polygon)

self.updateNode(selected=True, handle=self.mp_Handle, anchors=(self.mp_Data, D))
self.label.wrapLabel()
self.updateTextPos(moved=moved)

def setIdentity(self, identity):
Expand Down
1 change: 1 addition & 0 deletions eddy/core/items/nodes/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ def resize(self, mousePos):
self.polygon.setGeometry(polygon)

self.updateNode(selected=True, handle=self.mp_Handle, anchors=(self.mp_Data, D))
self.label.wrapLabel()
self.updateTextPos(moved=moved)

def setIdentity(self, identity):
Expand Down
1 change: 1 addition & 0 deletions eddy/core/items/nodes/literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ def resize(self, mousePos):
self.polygon.setGeometry(polygon)

self.updateNode(selected=True, handle=self.mp_Handle, anchors=(self.mp_Data, D))
self.label.wrapLabel()
self.updateTextPos(moved=moved)

def setIdentity(self, identity):
Expand Down
1 change: 1 addition & 0 deletions eddy/core/items/nodes/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ def resize(self, mousePos):
self.polygon.setGeometry(polygon)

self.updateNode(selected=True, handle=self.mp_Handle, anchors=(self.mp_Data, D))
self.label.wrapLabel()
self.updateTextPos(moved=moved)

def setAsymmetric(self, asymmetric):
Expand Down