From b4c3a0d9633f1b46723ba1c6a14536959c840171 Mon Sep 17 00:00:00 2001 From: Roy-043 Date: Sat, 2 Dec 2023 13:11:08 +0100 Subject: [PATCH] Draft: update Draft_ApplyStyle The command now applies all style properties from `utils.get_default_annotation_style()` and `utils.get_default_shape_style()`. Additionally: minor improvements to gui_setstyle.py. --- src/Mod/Draft/Draft.py | 1 + src/Mod/Draft/draftguitools/gui_setstyle.py | 12 ++- src/Mod/Draft/draftguitools/gui_styles.py | 71 +++++----------- src/Mod/Draft/draftutils/gui_utils.py | 94 ++++++++++++++------- src/Mod/Draft/draftutils/utils.py | 40 +++++++-- 5 files changed, 122 insertions(+), 96 deletions(-) diff --git a/src/Mod/Draft/Draft.py b/src/Mod/Draft/Draft.py index f8360a0aa0..50f32c4bc6 100644 --- a/src/Mod/Draft/Draft.py +++ b/src/Mod/Draft/Draft.py @@ -107,6 +107,7 @@ from draftutils.gui_utils import (get3DView, removeHidden, remove_hidden, get_diffuse_color, + apply_current_style, formatObject, format_object, getSelection, diff --git a/src/Mod/Draft/draftguitools/gui_setstyle.py b/src/Mod/Draft/draftguitools/gui_setstyle.py index 241034e0ed..2f59b8a8d6 100644 --- a/src/Mod/Draft/draftguitools/gui_setstyle.py +++ b/src/Mod/Draft/draftguitools/gui_setstyle.py @@ -162,7 +162,7 @@ class Draft_SetStyle_TaskPanel: def setValues(self,preset): - # For compatibility with V0.21 and earlier some properties, if missing, revert to others: + # For compatibility with <= V0.21 some properties, if missing, reference others: # 'new' prop -> old prop # --------------------------- # PointColor -> LineColor @@ -261,12 +261,10 @@ class Draft_SetStyle_TaskPanel: if "PointSize" in properties: vobj.PointSize = self.form.PointSize.value() if "DrawStyle" in properties: - dstyles = ["Solid", "Dashed", "Dotted", "Dashdot"] - vobj.DrawStyle = dstyles[self.form.DrawStyle.currentIndex()] + vobj.DrawStyle = utils.DRAW_STYLES[self.form.DrawStyle.currentIndex()] if "DisplayMode" in properties: - dmodes = ["Flat Lines", "Shaded", "Wireframe", "Points"] - dm = dmodes[self.form.DisplayMode.currentIndex()] - if dm in vobj.getEnumerationsOfProperty("DisplayMode"): + dm = utils.DISPLAY_MODES[self.form.DisplayMode.currentIndex()] + if dm in vobj.listDisplayModes(): vobj.DisplayMode = dm else: # Annotations if "TextColor" in properties: @@ -282,7 +280,7 @@ class Draft_SetStyle_TaskPanel: if "LineWidth" in properties: vobj.LineWidth = self.form.AnnoLineWidth.value() if "ArrowType" in properties: - vobj.ArrowType = ["Dot", "Circle", "Arrow", "Tick", "Tick-2"][self.form.ArrowStyle.currentIndex()] + vobj.ArrowType = utils.ARROW_TYPES[self.form.ArrowStyle.currentIndex()] if "ArrowSize" in properties: vobj.ArrowSize = U.Quantity(self.form.ArrowSize.text()).Value if "ShowUnit" in properties: diff --git a/src/Mod/Draft/draftguitools/gui_styles.py b/src/Mod/Draft/draftguitools/gui_styles.py index f32930fdbc..1a622e8457 100644 --- a/src/Mod/Draft/draftguitools/gui_styles.py +++ b/src/Mod/Draft/draftguitools/gui_styles.py @@ -32,66 +32,41 @@ from PySide.QtCore import QT_TRANSLATE_NOOP import FreeCADGui as Gui -import draftguitools.gui_base_original as gui_base_original +from draftguitools import gui_base_original from draftutils.translate import translate - +from draftutils import groups class ApplyStyle(gui_base_original.Modifier): """Gui Command for the ApplyStyle tool.""" def GetResources(self): """Set icon, menu and tooltip.""" + return { + "Pixmap": "Draft_Apply", + "MenuText": QT_TRANSLATE_NOOP("Draft_ApplyStyle", "Apply current style"), + "ToolTip": QT_TRANSLATE_NOOP("Draft_ApplyStyle", "Applies the current style defined in the toolbar (line width and colors) to the selected objects and groups.") + } - return {'Pixmap': 'Draft_Apply', - 'MenuText': QT_TRANSLATE_NOOP("Draft_ApplyStyle", "Apply current style"), - 'ToolTip': QT_TRANSLATE_NOOP("Draft_ApplyStyle", "Applies the current style defined in the toolbar (line width and colors) to the selected objects and groups.")} + def IsActive(self): + return bool(Gui.ActiveDocument and Gui.Selection.getSelection()) def Activated(self): - """Execute when the command is called. - - Activate the specific BSpline tracker. - """ - super(ApplyStyle, self).Activated(name="Apply style") - if self.ui: - self.sel = Gui.Selection.getSelection() - if len(self.sel) > 0: - Gui.addModule("Draft") - _cmd_list = [] - for obj in self.sel: - # TODO: instead of `TypeId`, use `utils.get_type` - # to get the type of the object and apply different - # formatting information depending on the type of object. - # The groups may also be things like `App::Parts` - # or `Arch_BuildingParts`. - if obj.TypeId == "App::DocumentObjectGroup": - _cmd_list.extend(self.formatGroup(obj)) - else: - _cmd = 'Draft.formatObject' - _cmd += '(' - _cmd += 'FreeCAD.ActiveDocument.' + obj.Name - _cmd += ')' - _cmd_list.append(_cmd) - self.commit(translate("draft", "Change Style"), - _cmd_list) - self.finish() - - def formatGroup(self, group): - """Format a group instead of simple object.""" - Gui.addModule("Draft") - _cmd_list = [] - for obj in group.Group: - if obj.TypeId == "App::DocumentObjectGroup": - _cmd_list.extend(self.formatGroup(obj)) - else: - _cmd = 'Draft.formatObject' - _cmd += '(' - _cmd += 'FreeCAD.ActiveDocument.' + obj.Name - _cmd += ')' - _cmd_list.append(_cmd) - return _cmd_list + """Execute when the command is called.""" + super().Activated(name="Apply style") + objs = Gui.Selection.getSelection() + if objs: + objs = groups.get_group_contents(objs, addgroups=True, spaces=True, noarchchild=True) + Gui.addModule("Draft") + cmd_list = [ + "doc = FreeCAD.ActiveDocument", + "Draft.apply_current_style([" + ", ".join(["doc." + obj.Name for obj in objs]) + "])", + "doc.recompute()" + ] + self.commit(translate("draft", "Change Style"), cmd_list) + self.finish() -Gui.addCommand('Draft_ApplyStyle', ApplyStyle()) +Gui.addCommand("Draft_ApplyStyle", ApplyStyle()) ## @} diff --git a/src/Mod/Draft/draftutils/gui_utils.py b/src/Mod/Draft/draftutils/gui_utils.py index a518c32ae6..73b8b5044e 100644 --- a/src/Mod/Draft/draftutils/gui_utils.py +++ b/src/Mod/Draft/draftutils/gui_utils.py @@ -435,15 +435,46 @@ def get_diffuse_color(objs): return colors +def apply_current_style(objs): + """Apply the current style to one or more objects. + + Parameters + ---------- + objs: a single object or an iterable with objects. + """ + if not isinstance(objs, list): + objs = [objs] + anno_style = utils.get_default_annotation_style() + shape_style = utils.get_default_shape_style() + for obj in objs: + if not hasattr(obj, 'ViewObject'): + continue + vobj = obj.ViewObject + props = vobj.PropertiesList + style = anno_style if ("FontName" in props) else shape_style + for prop in props: + if prop in style: + if style[prop][0] == "index": + if style[prop][2] in vobj.getEnumerationsOfProperty(prop): + setattr(vobj, prop, style[prop][2]) + elif style[prop][0] == "color": + setattr(vobj, prop, style[prop][1] & 0xFFFFFF00) + else: + setattr(vobj, prop, style[prop][1]) + + def format_object(target, origin=None): """Apply visual properties to an object. This function only works if the graphical interface is available. - If construction mode is active the `origin` argument is ignored. - The `target` is then placed in the construction group and the `constr` - color is applied to its applicable color properties: - `TextColor`, `PointColor`, `LineColor`, and `ShapeColor`. + If origin is `None` and target is not an annotation, the DefaultDrawStyle + and DefaultDisplayMode preferences are applied. Else, the properties of + origin are applied to target. + + If construction mode is active target is then placed in the construction + group and the `constr` color is applied to its applicable color properties: + TextColor, PointColor, LineColor, and ShapeColor. Parameters ---------- @@ -465,34 +496,7 @@ def format_object(target, origin=None): return obrep = target.ViewObject obprops = obrep.PropertiesList - if "FontName" not in obprops: - if "DrawStyle" in obprops: - dstyles = ["Solid", "Dashed", "Dotted", "Dashdot"] - obrep.DrawStyle = dstyles[utils.getParam("DefaultDrawStyle", 0)] - if "DisplayMode" in obprops: - dmodes = ["Flat Lines", "Shaded", "Wireframe", "Points"] - dm = dmodes[utils.getParam("DefaultDisplayMode", 0)] - if dm in obrep.getEnumerationsOfProperty("DisplayMode"): - obrep.DisplayMode = dm - if Gui.draftToolBar.isConstructionMode(): - doc = App.ActiveDocument - col = Gui.draftToolBar.getDefaultColor("constr") + (0.0,) - grp = doc.getObject("Draft_Construction") - if not grp: - grp = doc.addObject("App::DocumentObjectGroup", "Draft_Construction") - grp.Label = utils.get_param("constructiongroupname", "Construction") - grp.addObject(target) - if "TextColor" in obprops: - obrep.TextColor = col - if "PointColor" in obprops: - obrep.PointColor = col - if "LineColor" in obprops: - obrep.LineColor = col - if "ShapeColor" in obprops: - obrep.ShapeColor = col - if hasattr(obrep, "Transparency"): - obrep.Transparency = 80 - elif origin and hasattr(origin, 'ViewObject'): + if origin and hasattr(origin, 'ViewObject'): matchrep = origin.ViewObject for p in matchrep.PropertiesList: if p not in ("DisplayMode", "BoundingBox", @@ -513,6 +517,32 @@ def format_object(target, origin=None): difcol = get_diffuse_color(origin) if difcol: obrep.DiffuseColor = difcol + elif "FontName" not in obprops: + # Apply 2 Draft style preferences, other style preferences are applied by Core. + if "DrawStyle" in obprops: + obrep.DrawStyle = utils.DRAW_STYLES[utils.getParam("DefaultDrawStyle", 0)] + if "DisplayMode" in obprops: + dm = utils.DISPLAY_MODES[utils.getParam("DefaultDisplayMode", 0)] + if dm in obrep.listDisplayModes(): + obrep.DisplayMode = dm + if Gui.draftToolBar.isConstructionMode(): + doc = App.ActiveDocument + col = Gui.draftToolBar.getDefaultColor("constr") + (0.0,) + grp = doc.getObject("Draft_Construction") + if not grp: + grp = doc.addObject("App::DocumentObjectGroup", "Draft_Construction") + grp.Label = utils.get_param("constructiongroupname", "Construction") + grp.addObject(target) + if "TextColor" in obprops: + obrep.TextColor = col + if "PointColor" in obprops: + obrep.PointColor = col + if "LineColor" in obprops: + obrep.LineColor = col + if "ShapeColor" in obprops: + obrep.ShapeColor = col + if hasattr(obrep, "Transparency"): + obrep.Transparency = 80 formatObject = format_object diff --git a/src/Mod/Draft/draftutils/utils.py b/src/Mod/Draft/draftutils/utils.py index 67ee641d70..cbf65c4734 100644 --- a/src/Mod/Draft/draftutils/utils.py +++ b/src/Mod/Draft/draftutils/utils.py @@ -53,7 +53,10 @@ if App.GuiUp: # The module is used to prevent complaints from code checkers (flake8) True if Draft_rc else False + ARROW_TYPES = ["Dot", "Circle", "Arrow", "Tick", "Tick-2"] +DISPLAY_MODES = ["Flat Lines", "Shaded", "Wireframe", "Points"] +DRAW_STYLES = ["Solid", "Dashed", "Dotted", "Dashdot"] arrowtypes = ARROW_TYPES @@ -61,24 +64,43 @@ def get_default_annotation_style(): param = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft") anno_scale = param.GetFloat("DraftAnnotationScale", 1) scale_mult = 1 / anno_scale if anno_scale > 0 else 1 + arrow_type_index = param.GetInt("dimsymbol", 0) return { - "ArrowSize": ("float", param.GetFloat("arrowsize", 20)), - "ArrowType": ("index", param.GetInt("dimsymbol", 0)), + "ArrowSize": ("float", param.GetFloat("arrowsize", 1)), + "ArrowType": ("index", arrow_type_index, ARROW_TYPES[arrow_type_index]), "Decimals": ("int", param.GetInt("dimPrecision", 2)), - "DimOvershoot": ("float", param.GetFloat("dimovershoot", 20)), - "ExtLines": ("float", param.GetFloat("extlines", 300)), - "ExtOvershoot": ("float", param.GetFloat("extovershoot", 20)), + "DimOvershoot": ("float", param.GetFloat("dimovershoot", 0)), + "ExtLines": ("float", param.GetFloat("extlines", -0.5)), + "ExtOvershoot": ("float", param.GetFloat("extovershoot", 2)), "FontName": ("font", param.GetString("textfont", "Sans")), - "FontSize": ("float", param.GetFloat("textheight", 100)), + "FontSize": ("float", param.GetFloat("textheight", 3.5)), "LineColor": ("color", param.GetUnsigned("DefaultAnnoLineColor", 255)), "LineSpacing": ("float", param.GetFloat("LineSpacing", 1)), - "LineWidth": ("int", param.GetInt("DefaultAnnoLineWidth", 1)), + "LineWidth": ("int", param.GetInt("DefaultAnnoLineWidth", 2)), "ScaleMultiplier": ("float", scale_mult), "ShowLine": ("bool", param.GetBool("DimShowLine", True)), "ShowUnit": ("bool", param.GetBool("showUnit", True)), "TextColor": ("color", param.GetUnsigned("DefaultTextColor", 255)), - "TextSpacing": ("float", param.GetFloat("dimspacing", 20)), - "UnitOverride": ("str", param.GetString("overrideUnit", "")), + "TextSpacing": ("float", param.GetFloat("dimspacing", 1)), + "UnitOverride": ("str", param.GetString("overrideUnit", "")) + } + + +def get_default_shape_style(): + # Uses the same format as get_default_annotation_style(). + param_draft = App.ParamGet("User parameter:BaseApp/Preferences/Mod/Draft") + param_view = App.ParamGet("User parameter:BaseApp/Preferences/View") + display_mode_index = param_draft.GetInt("DefaultDisplayMode", 0) + draw_style_index = param_draft.GetInt("DefaultDrawStyle", 0) + return { + "DisplayMode": ("index", display_mode_index, DISPLAY_MODES[display_mode_index]), + "DrawStyle": ("index", draw_style_index, DRAW_STYLES[draw_style_index]), + "LineColor": ("color", param_view.GetUnsigned("DefaultShapeLineColor", 255)), + "LineWidth": ("int", param_view.GetInt("DefaultShapeLineWidth", 2)), + "PointColor": ("color", param_view.GetUnsigned("DefaultShapeVertexColor", 255)), + "PointSize": ("int", param_view.GetInt("DefaultShapePointSize", 2)), + "ShapeColor": ("color", param_view.GetUnsigned("DefaultShapeColor", 3435973887)), + "Transparency": ("int", param_view.GetInt("DefaultShapeTransparency", 0)) }