Commit f88a37a5 authored by sumpfralle's avatar sumpfralle

simplified xml state handling further


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1200 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 90b37076
......@@ -100,13 +100,15 @@ def draw_complete_model_view(settings):
if settings.get("show_simulation"):
obj = settings.get("simulation_object")
if not obj is None:
GL.glColor4f(*settings.get("color_material"))
color = settings.get("color_material")
GL.glColor4f(color["red"], color["green"], color["blue"], color["alpha"])
# we need to wait until the color change is active
GL.glFinish()
obj.to_OpenGL()
# draw the support grid
if False and settings.get("show_support_grid") and settings.get("current_support_model"):
GL.glColor4f(*settings.get("color_support_grid"))
color = settings.get("color_support_grid")
GL.glColor4f(color["red"], color["green"], color["blue"], color["alpha"])
# we need to wait until the color change is active
GL.glFinish()
settings.get("current_support_model").to_OpenGL()
......@@ -122,7 +124,8 @@ def draw_complete_model_view(settings):
if settings.get("show_drill"):
cutter = settings.get("cutter")
if not cutter is None:
GL.glColor4f(*settings.get("color_cutter"))
color = settings.get("color_cutter")
GL.glColor4f(color["red"], color["green"], color["blue"], color["alpha"])
GL.glFinish()
cutter.to_OpenGL()
if settings.get("show_drill_progress") \
......
......@@ -74,15 +74,15 @@ PREFERENCES_DEFAULTS = {
"show_toolpath": True,
"show_drill": False,
"show_directions": False,
"color_background": (0.0, 0.0, 0.0, 1.0),
"color_model": (0.5, 0.5, 1.0, 1.0),
"color_support_grid": (0.8, 0.8, 0.3, 1.0),
"color_bounding_box": (0.3, 0.3, 0.3, 1.0),
"color_cutter": (1.0, 0.2, 0.2, 1.0),
"color_toolpath_cut": (1.0, 0.5, 0.5, 1.0),
"color_toolpath_return": (0.9, 1.0, 0.1, 0.4),
"color_material": (1.0, 0.5, 0.0, 1.0),
"color_grid": (0.75, 1.0, 0.7, 0.55),
"color_background": {"red": 0.0, "green": 0.0, "blue": 0.0, "alpha": 1.0},
"color_model": {"red": 0.5, "green": 0.5, "blue": 1.0, "alpha": 1.0},
"color_support_grid": {"red": 0.8, "green": 0.8, "blue": 0.3, "alpha": 1.0},
"color_bounding_box": {"red": 0.3, "green": 0.3, "blue": 0.3, "alpha": 1.0},
"color_cutter": {"red": 1.0, "green": 0.2, "blue": 0.2, "alpha": 1.0},
"color_toolpath_cut": {"red": 1.0, "green": 0.5, "blue": 0.5, "alpha": 1.0},
"color_toolpath_return": {"red": 0.9, "green": 1.0, "blue": 0.1, "alpha": 0.4},
"color_material": {"red": 1.0, "green": 0.5, "blue": 0.0, "alpha": 1.0},
"color_grid": {"red": 0.75, "green": 1.0, "blue": 0.7, "alpha": 0.55},
"view_light": True,
"view_shadow": True,
"view_polygon": True,
......@@ -303,20 +303,25 @@ class EventCore(pycam.Gui.Settings.Settings):
def dump_state(self):
result = []
self.call_chain("state_dump", result)
for plugin in self.get("plugin-manager").get_plugins():
if plugin.enabled:
plugin.dump_state(result)
root = ET.Element("pycam")
for match, element in result:
chain = match.split("/")
if not hasattr(element, "findtext"):
# not an instance of ET.Element
element = pycam.Utils.xml_handling.get_xml(element, chain[-1])
parent = root
if match:
chain = match.split("/")
for component in chain:
for component in chain[:-1]:
if parent.find(component):
parent = parent.find(component)
else:
item = ET.SubElement(parent, component)
parent = item
parent.append(element)
return os.linesep.join(pycam.Utils.xml_handling.get_xml_lines(parent))
return os.linesep.join(pycam.Utils.xml_handling.get_xml_lines(root))
def reset_state(self):
pass
......
......@@ -20,8 +20,6 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import xml.etree.ElementTree as ET
import pycam.Plugins
# TODO: move Toolpath.Bounds here?
import pycam.Toolpath
......@@ -145,13 +143,13 @@ class Bounds(pycam.Plugins.ListPluginBase):
self._switch_bounds()
self._update_model_list()
self._event_handlers.append(("bounds-changed", "visual-item-updated"))
self.core.register_chain("state_dump", self.dump_state)
self.register_event_handlers(self._event_handlers)
self.register_state_item("bounds-list", self)
return True
def teardown(self):
self.clear_state_items()
if self.gui:
self.core.unregister_chain("state_dump", self.dump_state)
self.core.unregister_ui("main", self.gui.get_object("BoundsBox"))
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
......@@ -390,12 +388,6 @@ class Bounds(pycam.Plugins.ListPluginBase):
new_text:
self._treemodel[path][self.COLUMN_NAME] = new_text
def dump_state(self, result):
root = ET.Element("bounds-list")
for bounds in self:
root.append(bounds.get_xml())
result.append((None, root))
class BoundsDict(pycam.Plugins.ObjectWithAttributes):
......
......@@ -20,8 +20,6 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import xml.etree.ElementTree as ET
import pycam.Plugins
import pycam.Gui.ControlsGTK
......@@ -112,20 +110,16 @@ class GCodeSafetyHeight(pycam.Plugins.PluginBase):
self.safety_height.get_widget(), weight=20)
self.core.add_item("gcode_safety_height",
self.safety_height.get_value, self.safety_height.set_value)
self.core.register_chain("state_dump", self.dump_state)
self.register_state_item("settings/gcode/gcode_safety_height",
self.safety_height.get_value, self.safety_height.set_value)
return True
def teardown(self):
self.core.unregister_chain("state_dump", self.dump_state)
self.clear_state_items()
self.core.add_item("gcode_safety_height", lambda value: None,
lambda: None)
self.safety_height.destroy()
def dump_state(self, result):
item = ET.Element("gcode_safety_height")
item.text = "%f" % self.core.get("gcode_safety_height")
result.append(("settings/gcode", item))
class GCodeFilenameExtension(pycam.Plugins.PluginBase):
......@@ -167,28 +161,24 @@ class GCodeStepWidth(pycam.Plugins.PluginBase):
control = pycam.Gui.ControlsGTK.InputNumber(digits=8, start=0.0001,
increment=0.00005)
# TODO: this should be done via parameter groups based on postprocessors
name = "gcode_minimum_step_%s" % key
control.get_widget().show()
self.core.add_item("gcode_minimum_step_%s" % key,
control.get_value, control.set_value)
self.core.add_item(name, control.get_value, control.set_value)
self.core.register_ui("gcode_step_width", key.upper(),
control.get_widget(), weight="xyz".index(key))
self.register_state_item("settings/gcode/%s" % name,
control.get_value, control.set_value)
self.controls.append(control)
return True
def teardown(self):
self.clear_state_items()
while self.controls:
self.core.unregister_ui("gcode_step_width", self.controls.pop())
for key in "xyz":
self.core.add_item("gcode_minimum_step_%s" % key, lambda: None,
lambda value: None)
def dump_state(self, result):
for key in "xyz":
item = ET.Element("gcode_minimum_step_%s" % key)
item.text = "%f" % self.core.get("gcode_minimum_step_%s" % key)
result.append(("settings/gcode", item))
class GCodeSpindle(pycam.Plugins.PluginBase):
DEPENDS = ["GCodePreferences"]
......
......@@ -87,7 +87,8 @@ class OpenGLViewBounds(pycam.Plugins.PluginBase):
if self.core.get("view_light"):
GL.glDisable(GL.GL_LIGHTING)
# lower rectangle
GL.glColor4f(*self.core.get("color_bounding_box"))
color = self.core.get("color_bounding_box")
GL.glColor4f(color["red"], color["green"], color["blue"], color["alpha"])
GL.glFinish()
GL.glBegin(GL.GL_LINES)
# all combinations of neighbouring corners
......
......@@ -87,7 +87,8 @@ class OpenGLViewGrid(pycam.Plugins.PluginBase):
self.gui.get_object("MinorGridSizeLabel").set_text(
"%g%s" % (minor_distance, unit))
line_counter = int(math.ceil(grid_size / minor_distance))
GL.glColor4f(*self.core.get("color_grid"))
color = self.core.get("color_grid")
GL.glColor4f(color["red"], color["green"], color["blue"], color["alpha"])
GL.glFinish()
is_light = GL.glIsEnabled(GL.GL_LIGHTING)
GL.glDisable(GL.GL_LIGHTING)
......
......@@ -90,9 +90,11 @@ class OpenGLViewToolpath(pycam.Plugins.PluginBase):
if last_rapid != rapid:
GL.glEnd()
if rapid:
GL.glColor4f(*color_rapid)
GL.glColor4f(color_rapid["red"], color_rapid["green"],
color_rapid["blue"], color_rapid["alpha"])
else:
GL.glColor4f(*color_cut)
GL.glColor4f(color_cut["red"], color_cut["green"],
color_cut["blue"], color_cut["alpha"])
# we need to wait until the color change is active
GL.glFinish()
GL.glBegin(GL.GL_LINE_STRIP)
......
......@@ -32,7 +32,6 @@ except (ImportError, RuntimeError):
import gtk
import math
import xml.etree.ElementTree as ET
from pycam.Gui.OpenGLTools import draw_complete_model_view
from pycam.Geometry.Point import Point
......@@ -213,17 +212,23 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
# handlers
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self.core.register_chain("state_dump", self.dump_state)
# show the window - the handlers _must_ be registered before "show"
self.area.show()
toggle_3d.set_active(True)
# refresh display
self.core.emit_event("visual-item-updated")
def get_get_set_functions(name):
get_func = lambda: self.core.get(name)
set_func = lambda value: self.core.set(name, value)
return get_func, set_func
for name in ("view_light", "view_shadow", "view_polygon",
"view_perspective", "drill_progress_max_fps"):
self.register_state_item("settings/view/opengl/%s" % name,
*get_get_set_functions(name))
return True
def teardown(self):
if self.gui:
self.core.unregister_chain("state_dump", self.dump_state)
self.core.unregister_ui("preferences",
self.gui.get_object("OpenGLPrefTab"))
toggle_3d = self.gui.get_object("Toggle3DView")
......@@ -243,23 +248,7 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
# the area will be created during setup again
self.container.remove(self.area)
self.area = None
def dump_state(self, result):
# register all visible items ("show_model", ...) and OpenGL settings
for name in self._display_items.keys() + ["view_light", "view_shadow",
"view_polygon", "view_perspective", "drill_progress_max_fps"]:
item = ET.Element(name)
item.text = repr(bool(self.core.get(name)))
result.append(("settings/items", item))
# register all colors
for name in self._color_settings:
item = ET.Element(name)
color = self.core.get(name)
for index, color_key in enumerate(("red", "green", "blue",
"alpha")):
sub = ET.SubElement(item, color_key)
sub.text = str(color[index])
result.append(("settings/colors", item))
self.clear_state_items()
def update_view(self, widget=None, data=None):
if self.is_visible:
......@@ -287,15 +276,20 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
menu_item = action.create_menu_item()
widgets = (checkbox, tool_item, menu_item)
self._display_items[name] = {"name": name, "label": label,
"weight": weight, "widgets": widgets}
"weight": weight, "widgets": widgets, "action": action}
self.core.add_item(name, action.get_active, action.set_active)
self._rebuild_display_items()
# add this item to the state handler
self.register_state_item("settings/view/items/%s" % name,
action.get_active, action.set_active)
def unregister_display_item(self, name):
if not name in self._display_items:
self.log.debug("Failed to unregister unknown display item: %s" % \
name)
return
action = self._display_items[name]["action"]
self.unregister_state_item(name, action.get_active, action.set_active)
del self._display_items[name]
self._rebuild_display_items()
......@@ -321,40 +315,38 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
return
# color selectors
def get_color_wrapper(obj):
def gtk_color_to_float():
def gtk_color_to_dict():
gtk_color = obj.get_color()
alpha = obj.get_alpha()
return (gtk_color.red / GTK_COLOR_MAX,
gtk_color.green / GTK_COLOR_MAX,
gtk_color.blue / GTK_COLOR_MAX,
alpha / GTK_COLOR_MAX)
return gtk_color_to_float
return {"red": gtk_color.red / GTK_COLOR_MAX,
"green": gtk_color.green / GTK_COLOR_MAX,
"blue": gtk_color.blue / GTK_COLOR_MAX,
"alpha": alpha / GTK_COLOR_MAX}
return gtk_color_to_dict
def set_color_wrapper(obj):
def set_gtk_color_by_float(components):
# use alpha if it was given
if len(components) == 3:
alpha = 1.0
else:
alpha = components[3]
red, green, blue = components[:3]
obj.set_color(gtk.gdk.Color(int(red * GTK_COLOR_MAX),
int(green * GTK_COLOR_MAX), int(blue * GTK_COLOR_MAX)))
obj.set_alpha(int(alpha * GTK_COLOR_MAX))
return set_gtk_color_by_float
def set_gtk_color_by_dict(color):
obj.set_color(gtk.gdk.Color(int(color["red"] * GTK_COLOR_MAX),
int(color["green"] * GTK_COLOR_MAX),
int(color["blue"] * GTK_COLOR_MAX)))
obj.set_alpha(int(color["alpha"] * GTK_COLOR_MAX))
return set_gtk_color_by_dict
widget = gtk.ColorButton()
widget.set_use_alpha(True)
wrappers = (get_color_wrapper(widget), set_color_wrapper(widget))
self._color_settings[name] = {"name": name, "label": label,
"weight": weight, "widget": widget}
"weight": weight, "widget": widget, "wrappers": wrappers}
widget.connect("color-set", lambda widget: \
self.core.emit_event("visual-item-updated"))
self.core.add_item(name, get_color_wrapper(widget),
set_color_wrapper(widget))
self.core.add_item(name, *wrappers)
self.register_state_item("settings/view/colors/%s" % name, *wrappers)
self._rebuild_color_settings()
def unregister_color_setting(self, name):
if not name in self._color_settings:
self.log.debug("Failed to unregister unknown color item: %s" % name)
return
wrappers = self._color_settings[name]
self.unregister_state_item(name, *wrappers)
del self._color_settings[name]
self._rebuild_color_settings()
......@@ -487,7 +479,7 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
GL.glMatrixMode(GL.GL_MODELVIEW)
# clear the background with the configured color
bg_col = self.core.get("color_background")
GL.glClearColor(bg_col[0], bg_col[1], bg_col[2], 0.0)
GL.glClearColor(bg_col["red"], bg_col["green"], bg_col["blue"], 0.0)
GL.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT)
result = func(self, *args, **kwargs)
self.camera.position_camera()
......@@ -515,7 +507,7 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
# use vertex normals for smooth rendering
GL.glShadeModel(GL.GL_SMOOTH)
bg_col = self.core.get("color_background")
GL.glClearColor(bg_col[0], bg_col[1], bg_col[2], 0.0)
GL.glClearColor(bg_col["red"], bg_col["green"], bg_col["blue"], 0.0)
GL.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST)
GL.glMatrixMode(GL.GL_MODELVIEW)
# enable blending/transparency (alpha) for colors
......@@ -538,8 +530,9 @@ class OpenGLWindow(pycam.Plugins.PluginBase):
#GL.glEnable(GL.GL_MULTISAMPLE_ARB)
GL.glEnable(GL.GL_POLYGON_OFFSET_FILL)
GL.glPolygonOffset(1.0, 1.0)
GL.glMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE,
self.core.get("color_model") )
col = self.core.get("color_model")
col = (col["red"], col["green"], col["blue"], col["alpha"])
GL.glMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE, col)
GL.glMaterial(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR,
(1.0, 1.0, 1.0, 1.0))
GL.glMaterial(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, (100.0))
......
......@@ -20,8 +20,6 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import xml.etree.ElementTree as ET
import pycam.Plugins
......@@ -107,10 +105,11 @@ class Processes(pycam.Plugins.ListPluginBase):
("process-strategy-changed", self._store_process_settings))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self.core.register_chain("state_dump", self.dump_state)
self.register_state_item("processes", self)
return True
def teardown(self):
self.clear_state_items()
if self.gui:
self.core.unregister_chain("state_dump", self.dump_state)
self.core.unregister_ui("main", self.gui.get_object("ProcessBox"))
......@@ -263,12 +262,6 @@ class Processes(pycam.Plugins.ListPluginBase):
self.append(new_process)
self.select(new_process)
def dump_state(self, result):
root = ET.Element("processes")
for process in self:
root.append(process.get_xml())
result.append((None, root))
class ProcessEntity(pycam.Plugins.ObjectWithAttributes):
......
......@@ -21,7 +21,6 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import time
import xml.etree.ElementTree as ET
import pycam.Plugins
import pycam.Utils
......@@ -132,12 +131,12 @@ class Tasks(pycam.Plugins.ListPluginBase):
self._update_widgets()
self._update_table()
self._task_switch()
self.core.register_chain("state_dump", self.dump_state)
self.register_state_item("tasks", self)
self.core.set("tasks", self)
return True
def teardown(self):
self.core.unregister_chain("state_dump", self.dump_state)
self.clear_state_items()
if self.gui:
self.core.unregister_ui("main", self.gui.get_object("TaskBox"))
self.core.unregister_ui("task_parameters",
......@@ -358,12 +357,6 @@ class Tasks(pycam.Plugins.ListPluginBase):
progress.finish()
return result
def dump_state(self, result):
root = ET.Element("tasks")
for task in self:
root.append(task.get_xml())
result.append((None, root))
class TaskEntity(pycam.Plugins.ObjectWithAttributes):
......
......@@ -20,8 +20,6 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import xml.etree.ElementTree as ET
import pycam.Plugins
......@@ -126,12 +124,12 @@ class Tools(pycam.Plugins.ListPluginBase):
self.register_event_handlers(self._event_handlers)
self._update_widgets()
self._tool_switch()
self.core.register_chain("state_dump", self.dump_state)
self.register_state_item("tools", self)
return True
def teardown(self):
self.clear_state_items()
if self.gui:
self.core.unregister_chain("state_dump", self.dump_state)
self.core.unregister_ui("main", self.gui.get_object("ToolBox"))
self.core.unregister_ui_section("tool_speed")
self.core.unregister_ui_section("tool_size")
......@@ -286,12 +284,6 @@ class Tools(pycam.Plugins.ListPluginBase):
self.append(new_tool)
self.select(new_tool)
def dump_state(self, result):
root = ET.Element("tools")
for tool in self:
root.append(tool.get_xml())
result.append((None, root))
class ToolEntity(pycam.Plugins.ObjectWithAttributes):
......
......@@ -20,8 +20,6 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import xml.etree.ElementTree as ET
import pycam.Plugins
......@@ -60,22 +58,18 @@ class Units(pycam.Plugins.PluginBase):
(self.unit_change_window, "delete_event",
self.change_unit_apply, False)))
self.register_gtk_handlers(self._gtk_handlers)
self.core.register_chain("state_dump", self.dump_state)
self.register_state_item("settings/unit", lambda: self.core.get("unit"),
lambda value: self.core.set("unit", value))
return True
def teardown(self):
self.core.clear_state_items()
if self.gui:
self.core.unregister_chain("state_dump", self.dump_state)
self.core.unregister_ui("preferences_general",
self.gui.get_object("UnitPrefBox"))
self.unregister_gtk_handlers(self._gtk_handlers)
# TODO: reset setting "unit" back to a default value?
def dump_state(self, result):
item = ET.Element("unit")
item.text = self.core.get("unit")
result.append(("settings", item))
def change_unit_init(self, widget=None):
new_unit = self.gui.get_object("unit_control").get_active_text()
if self._last_unit is None:
......
......@@ -77,6 +77,34 @@ class PluginBase(object):
self._func_cache = {}
self._gtk_handler_id_cache = []
self.enabled = True
self._state_items = []
def register_state_item(self, path, get_func, set_func=None):
group = (path, get_func, set_func)
if group in self._state_items:
self.log.debug("Trying to register a state item twice: %s" % \
path)
else:
self._state_items.append(group)
def clear_state_items(self):
self._state_items = []
def unregister_state_item(self, path, get_func, set_func=None):
group = (path, get_func, set_func)
if group in self._state_items:
self._state_items.remove(group)
else:
self.log.debug("Trying to unregister an unknown state item: %s" % \
path)
def dump_state(self, result):
for path, get_func, set_func in self._state_items:
if callable(get_func):
value = get_func()
else:
value = get_func
result.append((path, value))
def __get_handler_func(self, func, params=None):
if params is None:
......@@ -495,9 +523,3 @@ class ObjectWithAttributes(dict):
self.update(params)
self.node_key = key
def get_xml(self, name=None):
if name is None:
name = self.node_key
return pycam.Utils.xml_handling.get_xml(self, name=name,
ignore_self=True)
......@@ -24,22 +24,25 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import xml.etree.ElementTree as ET
def get_xml(item, name="value", ignore_self=False):
if not ignore_self and hasattr(item, "get_xml"):
return item.get_xml(name=name)
elif isinstance(item, (list, tuple, set)):
leaf = ET.Element("list", name=name)
def get_xml(item, name=None):
if name is None:
if hasattr(item, "node_key"):
name = item.node_key
else:
name = "value"
if isinstance(item, (list, tuple, set)):
leaf = ET.Element(name)
for single in item:
leaf.append(get_xml(single))
return leaf
elif isinstance(item, dict):
leaf = ET.Element("dict", name=name)
leaf = ET.Element(name)
for key, value in item.iteritems():
leaf.append(get_xml(value, name=key))
return leaf
else:
leaf = ET.Element(name)
leaf.text=repr(item)
leaf.text = repr(item)
return leaf
def get_xml_lines(item):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment