diff --git a/FlatCAMDraw.py b/FlatCAMDraw.py index 2735568..a4c7959 100644 --- a/FlatCAMDraw.py +++ b/FlatCAMDraw.py @@ -704,6 +704,11 @@ class FlatCAMDraw(QtCore.QObject): self.tool_shape = self.app.plotcanvas.new_shape_collection() self.cursor = self.app.plotcanvas.new_cursor() + # Remove from scene + self.shapes.enabled = False + self.tool_shape.enabled = False + self.cursor.enabled = False + ## List of selected shapes. self.selected = [] @@ -756,12 +761,9 @@ class FlatCAMDraw(QtCore.QObject): self.snap_max_dist_entry.editingFinished.connect(lambda: entry2option("snap_max", self.snap_max_dist_entry)) def activate(self): - - print "activate" - parent = self.canvas.vispy_canvas.view.scene - self.shapes.parent = parent - self.tool_shape.parent = parent - self.cursor.parent = parent + self.shapes.enabled = True + self.tool_shape.enabled = True + self.cursor.enabled = True def connect_canvas_event_handlers(self): ## Canvas events @@ -822,11 +824,10 @@ class FlatCAMDraw(QtCore.QObject): self.drawing_toolbar.setDisabled(True) self.snap_toolbar.setDisabled(True) # TODO: Combine and move into tool - # Hide vispy visuals - if self.shapes.parent is not None: - self.shapes.parent = None - self.tool_shape.parent = None - self.cursor.parent = None + # Disable visuals + self.shapes.enabled = False + self.tool_shape.enabled = False + self.cursor.enabled = False # Show original geometry if self.fcgeometry: diff --git a/PlotCanvas.py b/PlotCanvas.py index 9194a93..e433aba 100644 --- a/PlotCanvas.py +++ b/PlotCanvas.py @@ -10,11 +10,10 @@ from PyQt4 import QtCore import logging from VisPyCanvas import VisPyCanvas -from VisPyVisuals import ShapeGroup, ShapeCollection, TextCollection, TextGroup -from vispy.scene.visuals import Markers, Text, InfiniteLine +from VisPyVisuals import ShapeGroup, ShapeCollection, TextCollection, TextGroup, Cursor +from vispy.scene.visuals import InfiniteLine import numpy as np from vispy.geometry import Rect -import multiprocessing log = logging.getLogger('base') @@ -58,10 +57,8 @@ class PlotCanvas(QtCore.QObject): parent=self.vispy_canvas.view.scene) self.shape_collection = self.new_shape_collection() - self.shape_collection.parent = self.vispy_canvas.view.scene - self.text_collection = self.new_text_collection() - self.text_collection.parent = self.vispy_canvas.view.scene + self.text_collection.enabled = False def vis_connect(self, event_name, callback): return getattr(self.vispy_canvas.events, event_name).connect(callback) @@ -80,28 +77,24 @@ class PlotCanvas(QtCore.QObject): :type center: list :return: None """ - self.vispy_canvas.view.camera.zoom(factor, center) def new_shape_group(self): return ShapeGroup(self.shape_collection) def new_shape_collection(self, **kwargs): - return ShapeCollection(**kwargs) + return ShapeCollection(parent=self.vispy_canvas.view.scene, **kwargs) def new_cursor(self): - m = Markers(pos=np.empty((0, 2))) - m.antialias = 0 - return m + c = Cursor(pos=np.empty((0, 2)), parent=self.vispy_canvas.view.scene) + c.antialias = 0 + return c def new_text_group(self): return TextGroup(self.text_collection) def new_text_collection(self, **kwargs): - return TextCollection(**kwargs) - - def new_text(self): - return Text(self.annotations) + return TextCollection(parent=self.vispy_canvas.view.scene, **kwargs) def fit_view(self, rect=None): if not rect: diff --git a/VisPyPatches.py b/VisPyPatches.py index be9c250..7e412eb 100644 --- a/VisPyPatches.py +++ b/VisPyPatches.py @@ -123,4 +123,4 @@ def apply_patches(): return NotImplementedError return major_frac, minor_frac, labels - Ticker._get_tick_frac_labels = _get_tick_frac_labels \ No newline at end of file + Ticker._get_tick_frac_labels = _get_tick_frac_labels diff --git a/VisPyVisuals.py b/VisPyVisuals.py index 6f87cc5..2c7a112 100644 --- a/VisPyVisuals.py +++ b/VisPyVisuals.py @@ -1,5 +1,5 @@ -from vispy.visuals import CompoundVisual, LineVisual, MeshVisual, TextVisual -from vispy.scene.visuals import create_visual_node +from vispy.visuals import CompoundVisual, LineVisual, MeshVisual, TextVisual, MarkersVisual +from vispy.scene.visuals import VisualNode, generate_docstring, visuals from vispy.gloo import set_state from vispy.color import Color from shapely.geometry import Polygon, LineString, LinearRing @@ -488,5 +488,64 @@ class TextCollectionVisual(TextVisual): self.__update() -ShapeCollection = create_visual_node(ShapeCollectionVisual) -TextCollection = create_visual_node(TextCollectionVisual) +# Add 'enabled' property to visual nodes +def create_fast_node(subclass): + # Create a new subclass of Node. + + # Decide on new class name + clsname = subclass.__name__ + if not (clsname.endswith('Visual') and + issubclass(subclass, visuals.BaseVisual)): + raise RuntimeError('Class "%s" must end with Visual, and must ' + 'subclass BaseVisual' % clsname) + clsname = clsname[:-6] + + # Generate new docstring based on visual docstring + try: + doc = generate_docstring(subclass, clsname) + except Exception: + # If parsing fails, just return the original Visual docstring + doc = subclass.__doc__ + + # New __init__ method + def __init__(self, *args, **kwargs): + parent = kwargs.pop('parent', None) + name = kwargs.pop('name', None) + self.name = name # to allow __str__ before Node.__init__ + self._visual_superclass = subclass + + # parent: property, + # _parent: attribute of Node class + # __parent: attribute of fast_node class + self.__parent = parent + self._enabled = False + + subclass.__init__(self, *args, **kwargs) + self.unfreeze() + VisualNode.__init__(self, parent=parent, name=name) + self.freeze() + + # Create new class + cls = type(clsname, (VisualNode, subclass), + {'__init__': __init__, '__doc__': doc}) + + # 'Enabled' property clears/restores 'parent' property of Node class + # Scene will be painted quicker than when using 'visible' property + def get_enabled(self): + return self._enabled + + def set_enabled(self, enabled): + if enabled: + self.parent = self.__parent # Restore parent + else: + if self.parent: # Store parent + self.__parent = self.parent + self.parent = None + + cls.enabled = property(get_enabled, set_enabled) + + return cls + +ShapeCollection = create_fast_node(ShapeCollectionVisual) +TextCollection = create_fast_node(TextCollectionVisual) +Cursor = create_fast_node(MarkersVisual)