Draft: Improve layer functions

The current make_layer function has a `None` default for the shape color and the line color. With that value the current preference is used. This, and how the function is called, results in some confusing behaviors:
* Newly created layers will only use 2 values from the preferences when they might use 5. The latter makes more sense for the end-user IMO.
* Layers created during DXF import (for example) will have a different shape color depending on the current preferences.
* The make_layer function may reapply colors that have already been set by the view provider.

To solve this all view property related function parameter have been changed to a not None value. If a None value is supplied the view property as set by the view provider is not changed. The Layer Manager has been updated accordingly.
I realize that calling a function with 6 None values is not very convenient, but think it is the solution that is least likely to break other exiting code.

Additionally:
* Removed the makeLayer function. Layers were introduced in V0.19 when the naming scheme was changed to "make_*". Maybe it was created by mistake, or before the actual renaming operation started, but it is safe to remove it now.
* Removed overly verbose messages.
* gui_layers.py had a missing import (result of a previous V0.22 PR): `from draftutils import utils`.
This commit is contained in:
Roy-043
2024-01-20 11:17:34 +01:00
committed by Chris Hennes
parent 8c3ff5d3e9
commit 5b98a762d1
5 changed files with 83 additions and 105 deletions

View File

@@ -379,8 +379,7 @@ if App.GuiUp:
from draftobjects.layer import (Layer,
_VisGroup)
from draftmake.make_layer import (make_layer,
makeLayer)
from draftmake.make_layer import make_layer
if App.GuiUp:
from draftviewproviders.view_layer import (ViewProviderLayer,

View File

@@ -37,6 +37,7 @@ import Draft
import Draft_rc
from draftguitools import gui_base
from draftutils import params
from draftutils import utils
from draftutils.translate import translate
# The module is used to prevent complaints from code checkers (flake8)
@@ -76,8 +77,8 @@ class Layer(gui_base.GuiCommandSimplest):
self.doc.openTransaction("Create Layer")
Gui.addModule("Draft")
Gui.doCommand('_layer_ = Draft.make_layer()')
Gui.doCommand('FreeCAD.ActiveDocument.recompute()')
Gui.doCommand("_layer_ = Draft.make_layer(name=None, line_color=None, shape_color=None, line_width=None, draw_style=None, transparency=None)")
Gui.doCommand("FreeCAD.ActiveDocument.recompute()")
self.doc.commitTransaction()
@@ -170,7 +171,8 @@ class LayerManager:
if not changed:
FreeCAD.ActiveDocument.openTransaction("Layers change")
changed = True
obj = Draft.make_layer()
obj = Draft.make_layer(name=None, line_color=None, shape_color=None,
line_width=None, draw_style=None, transparency=None)
# visibility
checked = True if self.model.item(row,0).checkState() == QtCore.Qt.Checked else False
@@ -303,7 +305,7 @@ class LayerManager:
nameItem = QtGui.QStandardItem(translate("Draft", "New Layer"))
widthItem = QtGui.QStandardItem()
widthItem.setData(params.get_param_view("DefaultShapeLineWidth"), QtCore.Qt.DisplayRole)
styleItem = QtGui.QStandardItem("Solid")
styleItem = QtGui.QStandardItem(utils.DRAW_STYLES[params.get_param("DefaultDrawStyle")])
lineColorItem = QtGui.QStandardItem()
lineColorItem.setData(
utils.get_rgba_tuple(params.get_param_view("DefaultShapeLineColor"))[:3],
@@ -315,7 +317,10 @@ class LayerManager:
QtCore.Qt.UserRole
)
transparencyItem = QtGui.QStandardItem()
transparencyItem.setData(0, QtCore.Qt.DisplayRole)
transparencyItem.setData(
params.get_param_view("DefaultShapeTransparency"),
QtCore.Qt.DisplayRole
)
linePrintColorItem = QtGui.QStandardItem()
linePrintColorItem.setData(
utils.get_rgba_tuple(params.get_param("DefaultPrintColor"))[:3],
@@ -428,7 +433,7 @@ if FreeCAD.GuiUp:
editor.setMaximum(99)
elif index.column() == 3: # Line style
editor = QtGui.QComboBox(parent)
editor.addItems(["Solid","Dashed","Dotted","Dashdot"])
editor.addItems(utils.DRAW_STYLES)
elif index.column() == 4: # Line color
editor = QtGui.QLineEdit(parent)
self.first = True
@@ -452,7 +457,7 @@ if FreeCAD.GuiUp:
elif index.column() == 2: # Line width
editor.setValue(index.data())
elif index.column() == 3: # Line style
editor.setCurrentIndex(["Solid","Dashed","Dotted","Dashdot"].index(index.data()))
editor.setCurrentIndex(utils.DRAW_STYLES.index(index.data()))
elif index.column() == 4: # Line color
editor.setText(str(index.data(QtCore.Qt.UserRole)))
if self.first:
@@ -486,7 +491,7 @@ if FreeCAD.GuiUp:
elif index.column() == 2: # Line width
model.setData(index,editor.value())
elif index.column() == 3: # Line style
model.setData(index,["Solid","Dashed","Dotted","Dashdot"][editor.currentIndex()])
model.setData(index,utils.DRAW_STYLES[editor.currentIndex()])
elif index.column() == 4: # Line color
model.setData(index,eval(editor.text()),QtCore.Qt.UserRole)
model.itemFromIndex(index).setIcon(getColorIcon(eval(editor.text())))

View File

@@ -30,7 +30,6 @@
# @{
import FreeCAD as App
from draftobjects.layer import Layer, LayerContainer
from draftutils import params
from draftutils import utils
from draftutils.messages import _msg, _err
from draftutils.translate import translate
@@ -79,53 +78,52 @@ def getLayerContainer():
def make_layer(name=None,
line_color=None, shape_color=None,
line_color=(0.0, 0.0, 0.0), # does not match default DefaultShapeLineColor
shape_color=(0.8, 0.8, 0.8), # matches default DefaultShapeColor
line_width=2.0,
draw_style="Solid", transparency=0):
draw_style="Solid",
transparency=0):
"""Create a Layer object in the active document.
If a layer container named `'LayerContainer'` does not exist,
it is created with this name.
If a layer container named `'LayerContainer'` does not exist, it is created.
A layer controls the view properties of the objects inside the layer,
so all parameters except for `name` only apply if the graphical interface
A layer controls the view properties of the objects inside the layer.
All parameters except for `name` only apply if the graphical interface
is up.
All parameters that control view properties can be set to `None`. Their
value, as set by the view provider (matching the current preferences), is
then not changed.
Parameters
----------
name: str, optional
It is used to set the layer's `Label` (user editable).
It defaults to `None`, in which case the `Label`
is set to `'Layer'` or to its translation in the current language.
name: str or `None`, optional
It defaults to `None`.
It is used to set the layer's `Label`. If it is `None` the `Label` is
set to `'Layer'` or to its translation in the current language.
line_color: tuple, optional
It defaults to `None`, in which case it uses the value of the parameter
`User parameter:BaseApp/Preferences/View/DefaultShapeLineColor`.
If it is given, it should be a tuple of three
floating point values from 0.0 to 1.0.
line_color: tuple or `None`, optional
It defaults to `(0.0, 0.0, 0.0)`.
If it is given, it should be a tuple of three floating point values
from 0.0 to 1.0.
shape_color: tuple, optional
It defaults to `None`, in which case it uses the value of the parameter
`User parameter:BaseApp/Preferences/View/DefaultShapeColor`.
If it is given, it should be a tuple of three
floating point values from 0.0 to 1.0.
shape_color: tuple or `None`, optional
It defaults to `(0.8, 0.8, 0.8)`.
If it is given, it should be a tuple of three floating point values
from 0.0 to 1.0.
line_width: float, optional
line_width: float or `None`, optional
It defaults to 2.0.
It determines the width of the edges of the objects contained
in the layer.
It determines the width of the edges of the objects contained in the layer.
draw_style: str, optional
draw_style: str or `None`, optional
It defaults to `'Solid'`.
It determines the style of the edges of the objects contained
in the layer.
If it is given, it should be 'Solid', 'Dashed', 'Dotted',
or 'Dashdot'.
It determines the style of the edges of the objects contained in the layer.
If it is given, it should be 'Solid', 'Dashed', 'Dotted' or 'Dashdot'.
transparency: int, optional
transparency: int or `None`, optional
It defaults to 0.
It should be an integer value from 0 (completely opaque)
to 100 (completely transparent).
It should be an integer from 0 to 100.
Return
------
@@ -139,15 +137,13 @@ def make_layer(name=None,
If there is a problem it will return `None`.
"""
_name = "make_layer"
utils.print_header(_name, translate("draft","Layer"))
found, doc = utils.find_doc(App.activeDocument())
if not found:
_err(translate("draft","No active document. Aborting."))
return None
if name:
_msg("name: {}".format(name))
if name is not None:
try:
utils.type_check([(name, str)], name=_name)
except TypeError:
@@ -156,8 +152,7 @@ def make_layer(name=None,
else:
name = translate("draft", "Layer")
if line_color:
_msg("line_color: {}".format(line_color))
if line_color is not None:
try:
utils.type_check([(line_color, tuple)], name=_name)
except TypeError:
@@ -167,14 +162,8 @@ def make_layer(name=None,
if not all(isinstance(color, (int, float)) for color in line_color):
_err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
return None
else:
c = params.get_param_view("DefaultShapeLineColor")
line_color = (((c >> 24) & 0xFF) / 255,
((c >> 16) & 0xFF) / 255,
((c >> 8) & 0xFF) / 255)
if shape_color:
_msg("shape_color: {}".format(shape_color))
if shape_color is not None:
try:
utils.type_check([(shape_color, tuple)], name=_name)
except TypeError:
@@ -184,38 +173,33 @@ def make_layer(name=None,
if not all(isinstance(color, (int, float)) for color in shape_color):
_err(translate("draft","Wrong input: must be a tuple of three floats 0.0 to 1.0."))
return None
else:
c = params.get_param_view("DefaultShapeColor")
shape_color = (((c >> 24) & 0xFF) / 255,
((c >> 16) & 0xFF) / 255,
((c >> 8) & 0xFF) / 255)
_msg("line_width: {}".format(line_width))
try:
utils.type_check([(line_width, (int, float))], name=_name)
line_width = float(abs(line_width))
except TypeError:
_err(translate("draft","Wrong input: must be a number."))
return None
if line_width is not None:
try:
utils.type_check([(line_width, (int, float))], name=_name)
line_width = float(abs(line_width))
except TypeError:
_err(translate("draft","Wrong input: must be a number."))
return None
_msg("draw_style: {}".format(draw_style))
try:
utils.type_check([(draw_style, str)], name=_name)
except TypeError:
_err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
return None
if draw_style is not None:
try:
utils.type_check([(draw_style, str)], name=_name)
except TypeError:
_err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
return None
if draw_style not in ('Solid', 'Dashed', 'Dotted', 'Dashdot'):
_err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
return None
if draw_style not in ('Solid', 'Dashed', 'Dotted', 'Dashdot'):
_err(translate("draft","Wrong input: must be 'Solid', 'Dashed', 'Dotted', or 'Dashdot'."))
return None
_msg("transparency: {}".format(transparency))
try:
utils.type_check([(transparency, (int, float))], name=_name)
transparency = int(abs(transparency))
except TypeError:
_err(translate("draft","Wrong input: must be a number between 0 and 100."))
return None
if transparency is not None:
try:
utils.type_check([(transparency, (int, float))], name=_name)
transparency = int(abs(transparency))
except TypeError:
_err(translate("draft","Wrong input: must be a number between 0 and 100."))
return None
new_obj = doc.addObject("App::FeaturePython", "Layer")
Layer(new_obj)
@@ -224,32 +208,20 @@ def make_layer(name=None,
if App.GuiUp:
ViewProviderLayer(new_obj.ViewObject)
new_obj.ViewObject.LineColor = line_color
new_obj.ViewObject.ShapeColor = shape_color
new_obj.ViewObject.LineWidth = line_width
new_obj.ViewObject.DrawStyle = draw_style
new_obj.ViewObject.Transparency = transparency
if line_color is not None:
new_obj.ViewObject.LineColor = line_color
if shape_color is not None:
new_obj.ViewObject.ShapeColor = shape_color
if line_width is not None:
new_obj.ViewObject.LineWidth = line_width
if draw_style is not None:
new_obj.ViewObject.DrawStyle = draw_style
if transparency is not None:
new_obj.ViewObject.Transparency = transparency
container = get_layer_container()
container.addObject(new_obj)
return new_obj
def makeLayer(name=None, linecolor=None, drawstyle=None,
shapecolor=None, transparency=None):
"""Create a Layer. DEPRECATED. Use 'make_layer'."""
utils.use_instead("make_layer")
if not drawstyle:
drawstyle = "Solid"
if not transparency:
transparency = 0
return make_layer(name,
line_color=linecolor, shape_color=shapecolor,
draw_style=drawstyle, transparency=transparency)
## @}

View File

@@ -586,6 +586,7 @@ def _create_objects(doc=None,
line_color=(0.33, 0.0, 0.49),
shape_color=(0.56, 0.89, 0.56),
line_width=4,
draw_style="Solid",
transparency=50)
box = doc.addObject("Part::Box", "Box")
box.Length = 200

View File

@@ -557,7 +557,8 @@ class ViewProviderLayerContainer:
doc = App.ActiveDocument
doc.openTransaction(translate("draft", "Add new layer"))
Draft.make_layer()
Draft.make_layer(name=None, line_color=None, shape_color=None,
line_width=None, draw_style=None, transparency=None)
doc.recompute()
doc.commitTransaction()