Commit 989d52e8 authored by sumpfralle's avatar sumpfralle

moved tool handling to a separate plugin


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1116 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent c2f9b155
...@@ -82,6 +82,10 @@ ...@@ -82,6 +82,10 @@
<property name="position">0</property> <property name="position">0</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child> <child>
<object class="GtkVButtonBox" id="vbuttonbox1"> <object class="GtkVButtonBox" id="vbuttonbox1">
<property name="visible">True</property> <property name="visible">True</property>
...@@ -93,7 +97,6 @@ ...@@ -93,7 +97,6 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="tooltip_text" translatable="yes">Remove the selected models</property>
<property name="use_stock">True</property> <property name="use_stock">True</property>
</object> </object>
<packing> <packing>
...@@ -108,7 +111,6 @@ ...@@ -108,7 +111,6 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="tooltip_text" translatable="yes">Remove all models</property>
<property name="use_stock">True</property> <property name="use_stock">True</property>
</object> </object>
<packing> <packing>
...@@ -122,7 +124,6 @@ ...@@ -122,7 +124,6 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
<property name="tooltip_text" translatable="yes">Change the color of the selected models</property>
<property name="use_alpha">True</property> <property name="use_alpha">True</property>
<property name="color">#000000000000</property> <property name="color">#000000000000</property>
</object> </object>
...@@ -160,6 +161,11 @@ ...@@ -160,6 +161,11 @@
<property name="position">4</property> <property name="position">4</property>
</packing> </packing>
</child> </child>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child> <child>
<object class="GtkLinkButton" id="ModelTransformationsHelp"> <object class="GtkLinkButton" id="ModelTransformationsHelp">
<property name="label">gtk-help</property> <property name="label">gtk-help</property>
...@@ -173,7 +179,7 @@ ...@@ -173,7 +179,7 @@
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">False</property> <property name="fill">False</property>
<property name="position">5</property> <property name="position">1</property>
</packing> </packing>
</child> </child>
</object> </object>
...@@ -184,7 +190,7 @@ ...@@ -184,7 +190,7 @@
</child> </child>
</object> </object>
<packing> <packing>
<property name="resize">False</property> <property name="resize">True</property>
<property name="shrink">False</property> <property name="shrink">False</property>
</packing> </packing>
</child> </child>
...@@ -195,7 +201,7 @@ ...@@ -195,7 +201,7 @@
<property name="tab_pos">left</property> <property name="tab_pos">left</property>
</object> </object>
<packing> <packing>
<property name="resize">True</property> <property name="resize">False</property>
<property name="shrink">False</property> <property name="shrink">False</property>
</packing> </packing>
</child> </child>
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
This diff is collapsed.
...@@ -32,7 +32,7 @@ class Models(pycam.Plugins.ListPluginBase): ...@@ -32,7 +32,7 @@ class Models(pycam.Plugins.ListPluginBase):
UI_FILE = "models.ui" UI_FILE = "models.ui"
COLUMN_ID, COLUMN_NAME, COLUMN_VISIBLE, COLUMN_COLOR, COLUMN_ALPHA = range(5) COLUMN_ID, COLUMN_NAME, COLUMN_VISIBLE, COLUMN_COLOR, COLUMN_ALPHA = range(5)
ATTRIBUTE_MAP = {"name": COLUMN_NAME, "visible": COLUMN_VISIBLE, LIST_ATTRIBUTE_MAP = {"name": COLUMN_NAME, "visible": COLUMN_VISIBLE,
"color": COLUMN_COLOR, "alpha": COLUMN_ALPHA} "color": COLUMN_COLOR, "alpha": COLUMN_ALPHA}
ICONS = {"visible": "visible.svg", "hidden": "visible_off.svg"} ICONS = {"visible": "visible.svg", "hidden": "visible_off.svg"}
# TODO: move this to the preferences dialog # TODO: move this to the preferences dialog
...@@ -75,7 +75,7 @@ class Models(pycam.Plugins.ListPluginBase): ...@@ -75,7 +75,7 @@ class Models(pycam.Plugins.ListPluginBase):
selection.connect("changed", selection.connect("changed",
lambda widget, event: self.core.emit_event(event), lambda widget, event: self.core.emit_event(event),
"model-selection-changed") "model-selection-changed")
selection.set_mode(gtk.SELECTION_MULTIPLE) selection.set_mode(self._gtk.SELECTION_MULTIPLE)
self._treemodel = self.gui.get_object("ModelList") self._treemodel = self.gui.get_object("ModelList")
self._treemodel.clear() self._treemodel.clear()
def update_model(): def update_model():
...@@ -96,30 +96,9 @@ class Models(pycam.Plugins.ListPluginBase): ...@@ -96,30 +96,9 @@ class Models(pycam.Plugins.ListPluginBase):
int(self.DEFAULT_COLOR[3] * _GTK_COLOR_MAX))) int(self.DEFAULT_COLOR[3] * _GTK_COLOR_MAX)))
self._get_colors_of_selected_models() self._get_colors_of_selected_models()
self.register_model_update(update_model) self.register_model_update(update_model)
self.core.add_item("models", lambda: self) self.core.set("models", self)
return True return True
def get_attr(self, model, attr):
return self.__get_set_attr(model, attr, write=False)
def set_attr(self, model, attr, value):
return self.__get_set_attr(model, attr, value=value, write=True)
def __get_set_attr(self, model, attr, value=None, write=True):
if attr in self.ATTRIBUTE_MAP:
col = self.ATTRIBUTE_MAP[attr]
for index in range(len(self)):
if self._treemodel[index][self.COLUMN_ID] == id(model):
if write:
self._treemodel[index][col] = value
return
else:
return self._treemodel[index][col]
raise IndexError("Model not found: %s" % str(model))
else:
raise KeyError("Attribute '%s' is not part of this list: %s" % \
(attr, ", ".join(self.ATTRIBUTE_MAP.keys())))
def _get_colors_of_selected_models(self, widget=None): def _get_colors_of_selected_models(self, widget=None):
color_button = self.gui.get_object("ModelColorButton") color_button = self.gui.get_object("ModelColorButton")
models = self.get_selected() models = self.get_selected()
...@@ -144,7 +123,8 @@ class Models(pycam.Plugins.ListPluginBase): ...@@ -144,7 +123,8 @@ class Models(pycam.Plugins.ListPluginBase):
def _edit_model_name(self, cell, path, new_text): def _edit_model_name(self, cell, path, new_text):
path = int(path) path = int(path)
if new_text != self._treemodel[path][self.COLUMN_NAME]: if (new_text != self._treemodel[path][self.COLUMN_NAME]) and \
new_text:
self._treemodel[path][self.COLUMN_NAME] = new_text self._treemodel[path][self.COLUMN_NAME] = new_text
def _visualize_visible_state(self, column, cell, model, m_iter): def _visualize_visible_state(self, column, cell, model, m_iter):
......
# -*- coding: utf-8 -*-
"""
$Id$
Copyright 2011 Lars Kruse <devel@sumpfralle.de>
This file is part of PyCAM.
PyCAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PyCAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Plugins
from pycam.Cutters.CylindricalCutter import CylindricalCutter
from pycam.Cutters.SphericalCutter import SphericalCutter
from pycam.Cutters.ToroidalCutter import ToroidalCutter
class Tools(pycam.Plugins.ListPluginBase):
UI_FILE = "tools.ui"
COLUMN_REF, COLUMN_ID, COLUMN_NAME = range(3)
LIST_ATTRIBUTE_MAP = {"id": COLUMN_ID, "name": COLUMN_NAME}
SHAPE_MAP = {CylindricalCutter: ("CylindricalCutter", "Flat end"),
SphericalCutter: ("SphericalCutter", "Ball nose"),
ToroidalCutter: ("ToroidalCutter", "Bull nose")}
def setup(self):
if self.gui:
tool_frame = self.gui.get_object("ToolBox")
tool_frame.unparent()
self.core.register_ui("main", "Tools", tool_frame, weight=10)
self._modelview = self.gui.get_object("ToolEditorTable")
for action, obj_name in ((self.ACTION_UP, "ToolMoveUp"),
(self.ACTION_DOWN, "ToolMoveDown"),
(self.ACTION_DELETE, "ToolDelete")):
self.register_list_action_button(action, self._modelview,
self.gui.get_object(obj_name))
self.gui.get_object("ToolNew").connect("clicked", self._tool_new)
selection = self._modelview.get_selection()
selection.connect("changed",
lambda widget, event: self.core.emit_event(event),
"tool-selection-changed")
cell = self.gui.get_object("ToolTableShapeCell")
self.gui.get_object("ToolTableShapeColumn").set_cell_data_func(
cell, self._visualize_tool_size)
self.gui.get_object("ToolTableIDCell").connect("edited",
self._edit_tool_id)
self.gui.get_object("ToolTableNameCell").connect("edited",
self._edit_tool_name)
self._treemodel = self.gui.get_object("ToolList")
self._treemodel.clear()
def update_model():
if not hasattr(self, "_model_cache"):
self._model_cache = {}
cache = self._model_cache
for row in self._treemodel:
cache[row[self.COLUMN_ID]] = list(row)
self._treemodel.clear()
for index, item in enumerate(self):
if id(item) in cache:
self._treemodel.append(cache[id(item)])
else:
self._treemodel.append((id(item), index + 1,
"Tool #%d" % index))
self.register_model_update(update_model)
# drill settings
self._detail_handlers = []
for objname in ("ToolDiameterControl", "TorusDiameterControl"):
obj = self.gui.get_object(objname)
handler = obj.connect("value-changed",
lambda *args: self.core.emit_event(args[-1]),
"tool-changed")
self._detail_handlers.append((obj, handler))
for objname in ("SphericalCutter", "CylindricalCutter",
"ToroidalCutter"):
obj = self.gui.get_object(objname)
handler = obj.connect("toggled",
lambda *args: self.core.emit_event(args[-1]),
"tool-changed")
self._detail_handlers.append((obj, handler))
self.core.register_event("tool-selection-changed",
self._tool_change)
self.core.register_event("tool-changed",
self._update_tool_controls)
self._update_tool_controls()
self.core.set("tools", self)
return True
def teardown(self):
if self.gui:
self.core.unregister_ui("main", self.gui.get_object("ToolBox"))
self.core.set("tools", None)
return True
def get_selected(self, index=False):
return self._get_selected(self._modelview, index=index)
def select(self, tool):
if tool in self:
selection = self._modelview.get_selection()
index = self.index(tool)
selection.unselect_all()
selection.select_path((index,))
def _visualize_tool_size(self, column, cell, model, m_iter):
path = model.get_path(m_iter)
tool = self[path[0]]
for cutter_class, (objname, desc) in self.SHAPE_MAP.iteritems():
if isinstance(tool, cutter_class):
break
text = "%s (%g%s)" % (desc, 2 * tool.radius, self.core.get("unit"))
cell.set_property("text", text)
def _edit_tool_name(self, cell, path, new_text):
path = int(path)
if (new_text != self._treemodel[path][self.COLUMN_NAME]) and \
new_text:
self._treemodel[path][self.COLUMN_NAME] = new_text
def _edit_tool_id(self, cell, path, new_text):
path = int(path)
try:
new_value = int(new_text)
except ValueError:
return
if str(new_value) != self._treemodel[path][self.COLUMN_ID]:
self._treemodel[path][self.COLUMN_ID] = new_value
def _update_tool_controls(self):
tool_index = self.get_selected(index=True)
if tool_index is None:
self.gui.get_object("ToolSettingsControlsBox").hide()
return
# disable the toroidal radius if the toroidal cutter is not enabled
if self.gui.get_object("ToroidalCutter").get_active():
self.gui.get_object("TorusDiameterControl").show()
self.gui.get_object("TorusDiameterLabel").show()
else:
self.gui.get_object("TorusDiameterControl").hide()
self.gui.get_object("TorusDiameterLabel").hide()
for objname, default_value in (("ToolDiameterControl", 1.0),
("TorusDiameterControl", 0.25)):
obj = self.gui.get_object(objname)
if obj.get_value() == 0:
# set the value to the configured minimum
obj.set_value(default_value)
# update the tool object
for cutter_class, (objname, desc) in self.SHAPE_MAP.iteritems():
if self.gui.get_object(objname).get_active():
break
radius = 0.5 * self.gui.get_object("ToolDiameterControl").get_value()
if cutter_class is ToroidalCutter:
args = [0.5 * self.gui.get_object("TorusDiameterControl").get_value()]
else:
args = []
new_tool = cutter_class(radius, *args)
self.pop(tool_index)
self.insert(tool_index, new_tool)
self.select(new_tool)
def _tool_change(self, widget=None, data=None):
tool = self.get_selected()
control_box = self.gui.get_object("ToolSettingsControlsBox")
if not tool:
control_box.hide()
else:
for obj, handler in self._detail_handlers:
obj.handler_block(handler)
# cutter shapes
for cutter_class, (objname, desc) in self.SHAPE_MAP.iteritems():
if isinstance(tool, cutter_class):
self.gui.get_object(objname).set_active(True)
# radius -> diameter
self.gui.get_object("ToolDiameterControl").set_value(2 * tool.radius)
torus_control = self.gui.get_object("TorusDiameterControl")
torus_label = self.gui.get_object("TorusDiameterLabel")
if hasattr(tool, "minorradius"):
torus_control.set_value(2 * tool.minorradius)
torus_control.show()
torus_label.show()
else:
torus_control.set_value(0.25)
torus_control.hide()
torus_label.hide()
for obj, handler in self._detail_handlers:
obj.handler_unblock(handler)
control_box.show()
def _tool_new(self, *args):
current_tool_index = self.get_selected(index=True)
if current_tool_index is None:
current_tool_index = 0
new_tool = CylindricalCutter(1.0)
self.insert(current_tool_index, new_tool)
self.select(new_tool)
...@@ -150,13 +150,14 @@ class PluginManager(object): ...@@ -150,13 +150,14 @@ class PluginManager(object):
class ListPluginBase(PluginBase, list): class ListPluginBase(PluginBase, list):
ACTION_UP, ACTION_DOWN, ACTION_DELETE, ACTION_CLEAR = range(4) ACTION_UP, ACTION_DOWN, ACTION_DELETE, ACTION_CLEAR = range(4)
LIST_ATTRIBUTE_MAP = {}
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ListPluginBase, self).__init__(*args, **kwargs) super(ListPluginBase, self).__init__(*args, **kwargs)
self._update_model_funcs = [] self._update_model_funcs = []
def get_function(func_name): def get_function(func_name):
return lambda *args, **kwargs: self._change_wrapper(func_name, *args, **kwargs) return lambda *args, **kwargs: self._change_wrapper(func_name, *args, **kwargs)
for name in "append", "insert", "pop", "reverse", "sort": for name in ("append", "insert", "pop", "reverse", "sort"):
setattr(self, name, get_function(name)) setattr(self, name, get_function(name))
def _change_wrapper(self, func_name, *args, **kwargs): def _change_wrapper(self, func_name, *args, **kwargs):
...@@ -279,3 +280,24 @@ class ListPluginBase(PluginBase, list): ...@@ -279,3 +280,24 @@ class ListPluginBase(PluginBase, list):
modelview, action, button) modelview, action, button)
button.connect("clicked", self._list_action, modelview, action) button.connect("clicked", self._list_action, modelview, action)
def get_attr(self, model, attr):
return self.__get_set_attr(model, attr, write=False)
def set_attr(self, model, attr, value):
return self.__get_set_attr(model, attr, value=value, write=True)
def __get_set_attr(self, model, attr, value=None, write=True):
if attr in self.LIST_ATTRIBUTE_MAP:
col = self.LIST_ATTRIBUTE_MAP[attr]
for index in range(len(self)):
if self._treemodel[index][self.COLUMN_ID] == id(model):
if write:
self._treemodel[index][col] = value
return
else:
return self._treemodel[index][col]
raise IndexError("Model not found: %s" % str(model))
else:
raise KeyError("Attribute '%s' is not part of this list: %s" % \
(attr, ", ".join(self.LIST_ATTRIBUTE_MAP.keys())))
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