Viewprovider/it: Difference between revisions

From FreeCAD Documentation
(Created page with "== Introduzione ==")
(Created page with "Le classi viewprovider di solito includono {{incode|ViewProvider}} nel loro nome. Sono assegnati sull'attributo {{incode|ViewObject}} dell'oggetto di base.")
 
(8 intermediate revisions by 4 users not shown)
Line 3: Line 3:
== Introduzione ==
== Introduzione ==


[[Viewprovider|Viewproviders]] are classes that define the way objects will look like in the [[tree_view|tree view]] and the [[3D_view|3D view]], and how they will interact with certain graphical actions such as [[selection|selection]].
Le [[Viewprovider/it|Viewproviders]] sono classi che definiscono il modo in cui gli oggetti appariranno nella [[tree_view/it|Vista ad albero]] e nella [[3D_view/it|Vista 3D]], e come interagiranno con certe azioni grafiche come la [[Selection_methods/it|selezione]].


They complement the [[scripted_objects|scripted objects]]. While the base class of the scripted object defines its {{Emphasis|data}} [[property|properties]], the viewprovider defines it {{Emphasis|view}} [[property|properties]]. These view properties are not essential information of the object, as they only indicate superficial information like line width, line color, face color, etc. In a terminal only session, the viewprovider is not loaded because there will be no interface to manipulate those visible properties.
Completano gli [[scripted_objects/it|scripted objects]]. Mentre la classe base dell'oggetto con script definisce le suoe {{Emphasis|data}} [[property/it|proprietà]], il viewprovider definisce le {{Emphasis|view}} [[property/it|proprietà]]. Queste proprietà della vista non sono informazioni essenziali dell'oggetto, poiché indicano solo informazioni superficiali come larghezza della linea, colore della linea, colore del viso, ecc. In una sessione solo terminale, il viewprovider non viene caricato perché non ci sarà alcuna interfaccia per manipolare quelli visibili proprietà.


Like with data properties, view properties are accessible from the [[property_editor|property editor]].
Come per le proprietà dei dati, le proprietà della vista sono accessibili dall'[[property_editor/it|editor di proprietà]].


== Python view providers ==
== View providers di Python ==


The viewproviders classes usually include {{incode|ViewProvider}} in their name. They are assigned on the {{incode|ViewObject}} attribute of the base object.
Le classi viewprovider di solito includono {{incode|ViewProvider}} nel loro nome. Sono assegnati sull'attributo {{incode|ViewObject}} dell'oggetto di base.


In this example, we define two properties for the viewprovider, only if the properties don't already exist, and assign their default values. We also define the {{incode|onChanged}} method that runs every time a property changes. We need to test the property by name, and then we will call one of two methods that will do the actual work of updating the pattern or setting its size.
In this example, we define two properties for the viewprovider, only if the properties don't already exist, and assign their default values. We also define the {{incode|onChanged}} method that runs every time a property changes. We need to test the property by name, and then we will call one of two methods that will do the actual work of updating the pattern or setting its size.
Line 135: Line 135:


{{Powerdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}
{{clear}}

Latest revision as of 16:28, 19 November 2022

Other languages:

Introduzione

Le Viewproviders sono classi che definiscono il modo in cui gli oggetti appariranno nella Vista ad albero e nella Vista 3D, e come interagiranno con certe azioni grafiche come la selezione.

Completano gli scripted objects. Mentre la classe base dell'oggetto con script definisce le suoe data proprietà, il viewprovider definisce le view proprietà. Queste proprietà della vista non sono informazioni essenziali dell'oggetto, poiché indicano solo informazioni superficiali come larghezza della linea, colore della linea, colore del viso, ecc. In una sessione solo terminale, il viewprovider non viene caricato perché non ci sarà alcuna interfaccia per manipolare quelli visibili proprietà.

Come per le proprietà dei dati, le proprietà della vista sono accessibili dall'editor di proprietà.

View providers di Python

Le classi viewprovider di solito includono ViewProvider nel loro nome. Sono assegnati sull'attributo ViewObject dell'oggetto di base.

In this example, we define two properties for the viewprovider, only if the properties don't already exist, and assign their default values. We also define the onChanged method that runs every time a property changes. We need to test the property by name, and then we will call one of two methods that will do the actual work of updating the pattern or setting its size.

# views/view_custom.py
class ViewProviderCustom:
    """Viewprovider of the custom object."""

    def __init__(self, vobj):
        self.Object = vobj.Object

        self._set_properties(vobj)
        vobj.Proxy = self

    def _set_properties(self, vobj):
        if not hasattr(vobj, "Pattern"):
            vobj.addProperty("App::PropertyEnumeration",
                             "Pattern",
                             "Custom",
                             "Defines a hatch pattern for this object.")
            vobj.Pattern = ["None", "diagonals", "cross", "brick"]

        if not hasattr(vobj, "PatternSize"):
            vobj.addProperty("App::PropertyFloat",
                             "PatternSize",
                             "Custom",
                             "Defines the size of the hatch pattern.")
            vobj.PatternSize = 1

    def onChanged(self, vobj, prop):
        if prop in "Pattern":
            self._set_pattern(vobj.Pattern)
        if prop in "PatternSize":
            self._set_size(vobj.PatternSize)

    def _set_pattern(self, pattern):
        ...

    def _set_size(self, size):
        ...

The normal workflow is to first add the object proxy class, for example, CustomObject, and then the viewprovider, for example, ViewProviderCustom. The viewprovider can only be assigned when we have verified that the graphical interface is available, as otherwise the ViewObject attribute doesn't exist, and it will be an error to use this element as input for our class.

import FreeCAD as App
import objects.custom as custom
import views.view_custom as view_custom

doc = App.newDocument()
obj = doc.addObject("Part::FeaturePython", "Custom")

custom.CustomObject(obj)

if App.GuiUp:
    view_custom.ViewProviderCustom(obj.ViewObject)

Custom icons

By implementing the getIcon method, you can specify the icon that will be shown in the tree view in the upper part of the combo view.

The return value can be the full path to an icon.

import os
some_path = "/home/user/.FreeCAD/custom_icons"

class ViewProviderCustom:
    ...

    def getIcon(self):
        return os.path.join(some_path, "my_icon.svg")

The relative path to an icon inside a compiled resource file.

import MyModule_rc.py

class ViewProviderCustom:
    ...

    def getIcon(self):
        return ":/icons/my_icon.svg"

A raw XPM icon, which is essentially ASCII art.

import MyModule_rc.py

class ViewProviderCustom:
    ...

    def getIcon(self):
        return """
               /* XPM */
               static char *Some_icon_xpm[] = {
               /* columns rows colors chars-per-pixel */
               "16 16 3 1 ",
               "  c None",
               ". c #D71414",
               "+ c #AA1919",
               /* pixels */
               "                ",
               "  +          +  ",
               " +.+        +.+ ",
               "  +.+      +.+  ",
               "   +        +   ",
               "      ++++      ",
               "     +....+     ",
               "     +...++     ",
               "     +..+++     ",
               "     +.++.+     ",
               "      ++++      ",
               "   +        +   ",
               "  +.+      +.+  ",
               " +.+        +.+ ",
               "  +          +  ",
               "                "
               };
               """

See various examples in Custom icon in tree view.