Draft: Fix inconsistent properties of Draft annotations
This commit is contained in:
@@ -108,6 +108,9 @@ import DraftVecUtils
|
||||
import DraftGeomUtils
|
||||
import draftutils.utils as utils
|
||||
|
||||
from draftutils.messages import _wrn
|
||||
from draftutils.translate import translate
|
||||
|
||||
from draftobjects.draft_annotation import DraftAnnotation
|
||||
|
||||
|
||||
@@ -122,7 +125,7 @@ class DimensionBase(DraftAnnotation):
|
||||
"""
|
||||
|
||||
def __init__(self, obj, tp="Dimension"):
|
||||
super(DimensionBase, self).__init__(obj, tp)
|
||||
super().__init__(obj, tp)
|
||||
self.set_properties(obj)
|
||||
obj.Proxy = self
|
||||
|
||||
@@ -195,11 +198,23 @@ class DimensionBase(DraftAnnotation):
|
||||
|
||||
def onDocumentRestored(self, obj):
|
||||
"""Execute code when the document is restored.
|
||||
|
||||
It calls the parent class to add missing annotation properties.
|
||||
"""
|
||||
super(DimensionBase, self).onDocumentRestored(obj)
|
||||
super().onDocumentRestored(obj)
|
||||
|
||||
if not hasattr(obj, "ViewObject"):
|
||||
return
|
||||
vobj = obj.ViewObject
|
||||
if not vobj:
|
||||
return
|
||||
if hasattr(vobj, "TextColor"):
|
||||
return
|
||||
|
||||
self.update_properties_0v21(obj, vobj)
|
||||
|
||||
def update_properties_0v21(self, obj, vobj):
|
||||
vobj.Proxy.set_text_properties(vobj, vobj.PropertiesList)
|
||||
vobj.TextColor = vobj.LineColor
|
||||
_wrn("v0.21, " + obj.Label + ", " + translate("draft", "added view property 'TextColor'"))
|
||||
|
||||
class LinearDimension(DimensionBase):
|
||||
"""The linear dimension object.
|
||||
@@ -212,13 +227,12 @@ class LinearDimension(DimensionBase):
|
||||
"""
|
||||
|
||||
def __init__(self, obj):
|
||||
super(LinearDimension, self).__init__(obj, "LinearDimension")
|
||||
super(LinearDimension, self).set_properties(obj)
|
||||
self.set_properties(obj)
|
||||
obj.Proxy = self
|
||||
super().__init__(obj, "LinearDimension")
|
||||
|
||||
def set_properties(self, obj):
|
||||
"""Set basic properties only if they don't exist."""
|
||||
super().set_properties(obj)
|
||||
|
||||
properties = obj.PropertiesList
|
||||
|
||||
if "Start" not in properties:
|
||||
@@ -290,13 +304,6 @@ class LinearDimension(DimensionBase):
|
||||
_tip)
|
||||
obj.Diameter = False
|
||||
|
||||
def onDocumentRestored(self, obj):
|
||||
"""Execute code when the document is restored.
|
||||
|
||||
It calls the parent class to add missing dimension properties.
|
||||
"""
|
||||
super(LinearDimension, self).onDocumentRestored(obj)
|
||||
|
||||
def onChanged(self, obj, prop):
|
||||
"""Execute when a property is changed.
|
||||
|
||||
@@ -486,17 +493,12 @@ class AngularDimension(DimensionBase):
|
||||
"""
|
||||
|
||||
def __init__(self, obj):
|
||||
super(AngularDimension, self).__init__(obj, "AngularDimension")
|
||||
super(AngularDimension, self).set_properties(obj)
|
||||
self.set_properties(obj)
|
||||
obj.Proxy = self
|
||||
|
||||
# Inherited properties from the parent class
|
||||
obj.Normal = App.Vector(0, 0, 1)
|
||||
obj.Dimline = App.Vector(0, 1, 0)
|
||||
super().__init__(obj, "AngularDimension")
|
||||
|
||||
def set_properties(self, obj):
|
||||
"""Set basic properties only if they don't exist."""
|
||||
super().set_properties(obj)
|
||||
|
||||
properties = obj.PropertiesList
|
||||
|
||||
if "FirstAngle" not in properties:
|
||||
@@ -550,13 +552,6 @@ class AngularDimension(DimensionBase):
|
||||
_tip)
|
||||
obj.Angle = 0
|
||||
|
||||
def onDocumentRestored(self, obj):
|
||||
"""Execute code when the document is restored.
|
||||
|
||||
It calls the parent class to add missing dimension properties.
|
||||
"""
|
||||
super(AngularDimension, self).onDocumentRestored(obj)
|
||||
|
||||
def execute(self, obj):
|
||||
"""Execute when the object is created or recomputed.
|
||||
|
||||
|
||||
@@ -65,44 +65,27 @@ class DraftAnnotation(object):
|
||||
Check if new properties are present after the object is restored
|
||||
in order to migrate older objects.
|
||||
"""
|
||||
if hasattr(obj, "ViewObject") and obj.ViewObject:
|
||||
vobj = obj.ViewObject
|
||||
self.add_missing_properties_0v19(obj, vobj)
|
||||
if not hasattr(obj, "ViewObject"):
|
||||
return
|
||||
vobj = obj.ViewObject
|
||||
if not vobj:
|
||||
return
|
||||
if hasattr(vobj, "ScaleMultiplier") and hasattr(vobj, "AnnotationStyle"):
|
||||
return
|
||||
|
||||
self.add_missing_properties_0v19(obj, vobj)
|
||||
|
||||
def add_missing_properties_0v19(self, obj, vobj):
|
||||
"""Provide missing annotation properties, if they don't exist."""
|
||||
vproperties = vobj.PropertiesList
|
||||
|
||||
if 'ScaleMultiplier' not in vproperties:
|
||||
_tip = QT_TRANSLATE_NOOP("App::Property",
|
||||
"General scaling factor that affects "
|
||||
"the annotation consistently\n"
|
||||
"because it scales the text, "
|
||||
"and the line decorations, if any,\n"
|
||||
"in the same proportion.")
|
||||
vobj.addProperty("App::PropertyFloat",
|
||||
"ScaleMultiplier",
|
||||
"Annotation",
|
||||
_tip)
|
||||
vobj.ScaleMultiplier = 1.00
|
||||
|
||||
_wrn("v0.19, " + obj.Label + ", " + translate("draft","added view property 'ScaleMultiplier'"))
|
||||
|
||||
if 'AnnotationStyle' not in vproperties:
|
||||
_tip = QT_TRANSLATE_NOOP("App::Property","Annotation style to apply to this object.\nWhen using a saved style some of the view properties will become read-only;\nthey will only be editable by changing the style through the 'Annotation style editor' tool.")
|
||||
vobj.addProperty("App::PropertyEnumeration",
|
||||
"AnnotationStyle",
|
||||
"Annotation",
|
||||
_tip)
|
||||
styles = []
|
||||
for key in obj.Document.Meta.keys():
|
||||
if key.startswith("Draft_Style_"):
|
||||
styles.append(key[12:])
|
||||
|
||||
vobj.AnnotationStyle = [""] + styles
|
||||
|
||||
_info = "added view property 'AnnotationStyle'"
|
||||
_wrn("v0.19, " + obj.Label + ", " + translate("draft","added view property 'ScaleMultiplier'"))
|
||||
"""Provide missing annotation properties."""
|
||||
multiplier = None
|
||||
if not hasattr(vobj, "ScaleMultiplier"):
|
||||
multiplier = 1.00
|
||||
_wrn("v0.19, " + obj.Label + ", " + translate("draft", "added view property 'ScaleMultiplier'"))
|
||||
if not hasattr(vobj, "AnnotationStyle"):
|
||||
_wrn("v0.19, " + obj.Label + ", " + translate("draft", "added view property 'AnnotationStyle'"))
|
||||
vobj.Proxy.set_annotation_properties(vobj, vobj.PropertiesList)
|
||||
if multiplier is not None:
|
||||
vobj.ScaleMultiplier = multiplier
|
||||
|
||||
def __getstate__(self):
|
||||
"""Return a tuple of objects to save or None.
|
||||
|
||||
@@ -33,8 +33,11 @@
|
||||
from PySide.QtCore import QT_TRANSLATE_NOOP
|
||||
|
||||
import FreeCAD as App
|
||||
|
||||
from FreeCAD import Units as U
|
||||
|
||||
from draftutils.messages import _wrn
|
||||
from draftutils.translate import translate
|
||||
|
||||
from draftobjects.draft_annotation import DraftAnnotation
|
||||
|
||||
|
||||
@@ -42,7 +45,7 @@ class Label(DraftAnnotation):
|
||||
"""The Draft Label object."""
|
||||
|
||||
def __init__(self, obj):
|
||||
super(Label, self).__init__(obj, "Label")
|
||||
super().__init__(obj, "Label")
|
||||
self.set_properties(obj)
|
||||
obj.Proxy = self
|
||||
|
||||
@@ -221,14 +224,38 @@ class Label(DraftAnnotation):
|
||||
|
||||
def onDocumentRestored(self, obj):
|
||||
"""Execute code when the document is restored.
|
||||
|
||||
It calls the parent class to add missing annotation properties.
|
||||
"""
|
||||
super(Label, self).onDocumentRestored(obj)
|
||||
super().onDocumentRestored(obj)
|
||||
|
||||
if not hasattr(obj, "ViewObject"):
|
||||
return
|
||||
vobj = obj.ViewObject
|
||||
if not vobj:
|
||||
return
|
||||
if hasattr(vobj, "FontName") and hasattr(vobj, "FontSize"):
|
||||
return
|
||||
|
||||
self.update_properties_0v21(obj, vobj)
|
||||
|
||||
def update_properties_0v21(self, obj, vobj):
|
||||
old_fontname = vobj.TextFont
|
||||
old_fontsize = vobj.TextSize
|
||||
vobj.removeProperty("TextFont")
|
||||
vobj.removeProperty("TextSize")
|
||||
|
||||
vobj.Proxy.set_text_properties(vobj, vobj.PropertiesList)
|
||||
vobj.FontName = old_fontname
|
||||
vobj.FontSize = old_fontsize
|
||||
# The DisplayMode is updated automatically but the new values are
|
||||
# switched: "2D text" becomes "World" and "3D text" becomes "Screen".
|
||||
# It should be the other way around:
|
||||
vobj.DisplayMode = "World" if vobj.DisplayMode == "Screen" else "Screen"
|
||||
_wrn("v0.21, " + obj.Label + ", " + translate("draft", "updated view property 'TextFont' to 'FontName'"))
|
||||
_wrn("v0.21, " + obj.Label + ", " + translate("draft", "updated view property 'TextSize' to 'FontSize'"))
|
||||
|
||||
def onChanged(self, obj, prop):
|
||||
"""Execute when a property is changed."""
|
||||
super(Label, self).onChanged(obj, prop)
|
||||
super().onChanged(obj, prop)
|
||||
self.show_and_hide(obj, prop)
|
||||
|
||||
def show_and_hide(self, obj, prop):
|
||||
|
||||
@@ -39,7 +39,7 @@ class Text(DraftAnnotation):
|
||||
"""The Draft Text object."""
|
||||
|
||||
def __init__(self, obj):
|
||||
super(Text, self).__init__(obj, "Text")
|
||||
super().__init__(obj, "Text")
|
||||
self.set_properties(obj)
|
||||
obj.Proxy = self
|
||||
|
||||
@@ -69,13 +69,6 @@ class Text(DraftAnnotation):
|
||||
_tip)
|
||||
obj.Text = []
|
||||
|
||||
def onDocumentRestored(self, obj):
|
||||
"""Execute code when the document is restored.
|
||||
|
||||
It calls the parent class to add missing annotation properties.
|
||||
"""
|
||||
super(Text, self).onDocumentRestored(obj)
|
||||
|
||||
|
||||
# Alias for compatibility with v0.18 and earlier
|
||||
DraftText = Text
|
||||
|
||||
Reference in New Issue
Block a user