Commit ceddbba3 authored by sumpfralle's avatar sumpfralle

improved consistency of input/output conversion for control widgets


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1149 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 9f494566
......@@ -30,15 +30,20 @@ _log = pycam.Utils.log.get_logger()
def _input_conversion(func):
def _input_conversion_wrapper(self, value):
if hasattr(self, "_input_converter") and self._input_converter:
value = self._input_converter(value)
return func(self, value)
if value is None:
new_value = None
elif hasattr(self, "_input_converter") and self._input_converter:
new_value = self._input_converter(value)
else:
new_value = value
return func(self, new_value)
return _input_conversion_wrapper
def _output_conversion(func):
def _output_conversion_wrapper(self):
result = func(self)
if hasattr(self, "_output_converter") and self._output_converter:
if not (result is None) and hasattr(self, "_output_converter") and \
self._output_converter:
result = self._output_converter(result)
return result
return _output_conversion_wrapper
......@@ -59,6 +64,11 @@ class InputBaseClass(object):
self._input_converter = set_conv
self._output_converter = get_conv
@_input_conversion
def _get_input_conversion_result(self, value):
# a simple dummy replicating the behaviour of _input_conversion
return value
class InputNumber(InputBaseClass):
......@@ -89,7 +99,8 @@ class InputChoice(InputBaseClass):
g_type = self._get_column_type(choices, force_type=force_type)
self.model = gtk.ListStore(gobject.TYPE_STRING, g_type)
for label, value in choices:
self.model.append((label, value))
self.model.append((label,
self._get_input_conversion_result(value)))
renderer = gtk.CellRendererText()
self.control = gtk.ComboBox(self.model)
self.control.pack_start(renderer)
......@@ -136,6 +147,8 @@ class InputChoice(InputBaseClass):
if row[1] == value:
self.control.set_active(index)
break
else:
_log.debug("Unknown value: %s" % str(value))
def update_choices(self, choices):
# TODO: selection restore does not work currently; there seems to be a glitch during "delete model"
......@@ -146,7 +159,8 @@ class InputChoice(InputBaseClass):
break
else:
# this choice is new
self.model.insert(choice_index, (label, value))
self.model.insert(choice_index, (label,
self._get_input_conversion_result(value)))
continue
# the current choice is preceded by some obsolete items
while index > choice_index:
......@@ -170,7 +184,8 @@ class InputTable(InputChoice):
g_type = self._get_column_type(choices, force_type=force_type)
self.model = gtk.ListStore(gobject.TYPE_STRING, g_type)
for label, value in choices:
self.model.append((label, value))
self.model.append((label,
self._get_input_conversion_result(value)))
renderer = gtk.CellRendererText()
self.control = gtk.ScrolledWindow()
self._treeview = gtk.TreeView(self.model)
......@@ -188,6 +203,10 @@ class InputTable(InputChoice):
if change_handler:
self._selection.connect("changed", change_handler)
def _get_input_conversion_result(self, value):
# handle a list instead of single items
return super(InputTable, self)._get_input_conversion_result([value])[0]
@_output_conversion
def get_value(self):
model, rows = self._selection.get_selected_rows()
......
......@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Plugins
import pycam.Gui.ControlsGTK
import pycam.Toolpath.MotionGrid
class PathParamOverlap(pycam.Plugins.PluginBase):
......@@ -86,9 +87,9 @@ class PathParamMillingStyle(pycam.Plugins.PluginBase):
def setup(self):
input_control = pycam.Gui.ControlsGTK.InputChoice(
(("ignore", "ignore"),
("climb / down", "climb"),
("conventional / up", "conventional")),
(("ignore", pycam.Toolpath.MotionGrid.MILLING_STYLE_IGNORE),
("climb / down", pycam.Toolpath.MotionGrid.MILLING_STYLE_CLIMB),
("conventional / up", pycam.Toolpath.MotionGrid.MILLING_STYLE_CONVENTIONAL)),
change_handler=lambda widget=None: self.core.emit_event(
"process-changed"))
self.core.get("register_parameter")("process", "pathgenerator",
......@@ -106,7 +107,9 @@ class PathParamGridDirection(pycam.Plugins.PluginBase):
def setup(self):
input_control = pycam.Gui.ControlsGTK.InputChoice(
(("x", "x"), ("y", "y"), ("xy", "xy")),
(("x", pycam.Toolpath.MotionGrid.GRID_DIRECTION_X),
("y", pycam.Toolpath.MotionGrid.GRID_DIRECTION_Y),
("xy", pycam.Toolpath.MotionGrid.GRID_DIRECTION_XY)),
change_handler=lambda widget=None: self.core.emit_event(
"process-changed"))
self.core.get("register_parameter")("process", "pathgenerator",
......@@ -166,7 +169,7 @@ class PathParamTraceModel(pycam.Plugins.PluginBase):
models = self.core.get("models")
for model in models:
if hasattr(model, "get_polygons"):
choices.append((models.get_attr(model, "name"), id(model)))
choices.append((models.get_attr(model, "name"), model))
self.input_control.update_choices(choices)
def teardown(self):
......
......@@ -62,7 +62,7 @@ class TaskParamCollisionModels(pycam.Plugins.PluginBase):
models = self.core.get("models")
for model in models:
if hasattr(model, "triangles"):
choices.append((models.get_attr(model, "name"), id(model)))
choices.append((models.get_attr(model, "name"), model))
self.input_control.update_choices(choices)
......@@ -74,6 +74,9 @@ class TaskParamTool(pycam.Plugins.PluginBase):
self.input_control = pycam.Gui.ControlsGTK.InputChoice([],
force_type=long, change_handler=lambda widget=None: \
self.core.emit_event("task-changed"))
self.input_control.set_conversion(
get_conv=lambda ref: ([tool for tool in self.core.get("tools") if id(tool) == ref] + [None])[0],
set_conv=lambda tool: id(tool))
self.core.get("register_parameter")("task", "components", "tool",
"Tool", self.input_control, weight=10)
self.core.register_event("tool-list-changed", self._update_tools)
......@@ -86,7 +89,7 @@ class TaskParamTool(pycam.Plugins.PluginBase):
choices = []
tools = self.core.get("tools")
for tool in tools:
choices.append((tools.get_attr(tool, "name"), id(tool)))
choices.append((tools.get_attr(tool, "name"), tool))
self.input_control.update_choices(choices)
......@@ -98,6 +101,9 @@ class TaskParamProcess(pycam.Plugins.PluginBase):
self.input_control = pycam.Gui.ControlsGTK.InputChoice([],
force_type=long, change_handler=lambda widget=None: \
self.core.emit_event("task-changed"))
self.input_control.set_conversion(
get_conv=lambda ref: ([process for process in self.core.get("processes") if id(process) == ref] + [None])[0],
set_conv=lambda process: id(process))
self.core.get("register_parameter")("task", "components", "process",
"Process", self.input_control, weight=20)
self.core.register_event("process-list-changed", self._update_processes)
......@@ -110,7 +116,7 @@ class TaskParamProcess(pycam.Plugins.PluginBase):
choices = []
processes = self.core.get("processes")
for process in processes:
choices.append((processes.get_attr(process, "name"), id(process)))
choices.append((processes.get_attr(process, "name"), process))
self.input_control.update_choices(choices)
......@@ -122,6 +128,9 @@ class TaskParamBounds(pycam.Plugins.PluginBase):
self.input_control = pycam.Gui.ControlsGTK.InputChoice([],
force_type=long, change_handler=lambda widget=None: \
self.core.emit_event("task-changed"))
self.input_control.set_conversion(
get_conv=lambda ref: ([bounds for bounds in self.core.get("bounds") if id(bounds) == ref] + [None])[0],
set_conv=lambda bounds: id(bounds))
self.core.get("register_parameter")("task", "components", "bounds",
"Bounds", self.input_control, weight=30)
self.core.register_event("bounds-list-changed", self._update_bounds)
......@@ -134,6 +143,6 @@ class TaskParamBounds(pycam.Plugins.PluginBase):
choices = []
bounds = self.core.get("bounds")
for bound in bounds:
choices.append((bounds.get_attr(bound, "name"), id(bound)))
choices.append((bounds.get_attr(bound, "name"), bound))
self.input_control.update_choices(choices)
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