Commit 2f6fbc1e authored by sumpfralle's avatar sumpfralle

register events and gtk widget signals via a global list -> cleaner setup/teardown for modules


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1171 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent d935f820
......@@ -136,14 +136,11 @@ class Bounds(pycam.Plugins.ListPluginBase):
self._edit_bounds_name)
self.gui.get_object("ModelDescriptionColumn").set_cell_data_func(
self.gui.get_object("ModelNameCell"), self._render_model_name)
self.core.register_event("bounds-selection-changed",
self._switch_bounds)
self.core.register_event("bounds-changed",
self._store_bounds_settings)
self.core.register_event("bounds-changed",
self._trigger_table_update)
self.core.register_event("model-list-changed",
self._update_model_list)
self._event_handlers = (("bounds-selection-changed", self._switch_bounds),
("bounds-changed", self._store_bounds_settings),
("bounds-changed", self._trigger_table_update),
("model-list-changed", self._update_model_list))
self.register_event_handlers(self._event_handlers)
self._trigger_table_update()
self._switch_bounds()
self._update_model_list()
......@@ -155,14 +152,7 @@ class Bounds(pycam.Plugins.ListPluginBase):
def teardown(self):
if self.gui:
self.core.unregister_ui("main", self.gui.get_object("BoundsBox"))
self.core.unregister_event("bounds-selection-changed",
self._switch_bounds)
self.core.unregister_event("bounds-changed",
self._store_bounds_settings)
self.core.unregister_event("bounds-changed",
self._trigger_table_update)
self.core.unregister_event("model-list-changed",
self._update_model_list)
self.unregister_event_handlers(self._event_handlers)
self.core.set("bounds", None)
return True
......
......@@ -46,38 +46,42 @@ class Clipboard(pycam.Plugins.PluginBase):
if self.gui:
import gtk
self._gtk = gtk
self._gtk_handlers = []
self.clipboard = self._gtk.clipboard_get()
self.core.set("clipboard-set", self._copy_text_to_clipboard)
self.clipboard.connect("owner-change", self._update_clipboard_widget)
self.core.register_event("model-selection-changed",
self._update_clipboard_widget)
self._gtk_handlers.append((self.clipboard, "owner-change",
self._update_clipboard_widget))
# menu item and shortcut
self.copy_action = self.gui.get_object("CopyModelToClipboard")
self.copy_action.connect("activate", self.copy_model_to_clipboard)
self._gtk_handlers.append((self.copy_action, "activate",
self.copy_model_to_clipboard))
self.register_gtk_accelerator("clipboard", self.copy_action,
"<Control>c", "CopyModelToClipboard")
self.core.register_ui("edit_menu", "CopyModelToClipboard",
self.copy_action, 20)
self.paste_action = self.gui.get_object("PasteModelFromClipboard")
self.paste_action.connect("activate", self.paste_model_from_clipboard)
self._gtk_handlers.append((self.paste_action, "activate",
self.paste_model_from_clipboard))
self.register_gtk_accelerator("clipboard", self.paste_action,
"<Control>v", "PasteModelFromClipboard")
self.core.register_ui("edit_menu", "PasteModelFromClipboard",
self.paste_action, 25)
self._event_handlers = (("model-selection-changed",
self._update_clipboard_widget), )
self.register_event_handlers(self._event_handlers)
self.register_gtk_handlers(self._gtk_handlers)
self._update_clipboard_widget()
return True
def teardown(self):
if self.gui:
self.core.unregister_event("model-selection-changed",
self._update_clipboard_widget)
self.unregister_event_handlers(self._event_handlers)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_gtk_accelerator("clipboard", self.copy_action)
self.core.unregister_ui("edit_menu", self.copy_action)
self.unregister_gtk_accelerator("clipboard", self.paste_action)
self.core.unregister_ui("edit_menu", self.paste_action)
self.core.set("clipboard-set", None)
# TODO: check if this disconnects the clipboard-change-handler
self.clipboard = None
def _get_exportable_models(self):
models = self.core.get("models").get_selected()
......
......@@ -38,11 +38,14 @@ class EMCToolExport(pycam.Plugins.PluginBase):
self.export_action = self.gui.get_object("ExportEMCToolDefinition")
self.register_gtk_accelerator("export", self.export_action, None,
"ExportEMCToolDefinition")
self.export_action.connect("activate", self.export_emc_tools)
self._gtk_handlers = ((self.export_action, "activate",
self.export_emc_tools), )
self.core.register_ui("export_menu", "ExportEMCToolDefinition",
self.export_action, 80)
self.core.register_event("tool-selection-changed",
self._update_emc_tool_button)
self._event_handlers = (("tool-selection-changed",
self._update_emc_tool_button), )
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_emc_tool_button()
return True
......@@ -50,8 +53,8 @@ class EMCToolExport(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("export_menu", self.export_emc_tools)
self.unregister_gtk_accelerator("fonts", self.export_emc_tools)
self.core.unregister_event("tool-selection-changed",
self._update_emc_tool_button)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_emc_tool_button(self, widget=None):
exportable = len(self.core.get("tools")) > 0
......
......@@ -41,48 +41,51 @@ class Fonts(pycam.Plugins.PluginBase):
def setup(self):
self._fonts_cache = pycam.Utils.FontCache.FontCache(get_font_dir(),
core=self.core)
self.core.add_item("fonts", lambda: self._fonts_cache)
self.core.set("fonts", self._fonts_cache)
# "font dialog" window
if self.gui:
import gtk
self._gtk = gtk
self.font_dialog_window = self.gui.get_object("FontDialog")
self.font_dialog_window.connect("delete-event",
self.toggle_font_dialog_window, False)
self.font_dialog_window.connect("destroy",
self.toggle_font_dialog_window, False)
self.gui.get_object("FontDialogCancel").connect("clicked",
self.toggle_font_dialog_window, False)
self.gui.get_object("FontDialogApply").connect("clicked",
self.import_from_font_dialog)
self.gui.get_object("FontDialogSave").connect("clicked",
self.export_from_font_dialog)
self.gui.get_object("FontDialogCopy").connect("clicked",
self.copy_font_dialog_to_clipboard)
self.gui.get_object("FontDialogInputBuffer").connect("changed",
self.update_font_dialog_preview)
self.gui.get_object("FontDialogPreview").connect("configure_event",
self.update_font_dialog_preview)
self.gui.get_object("FontDialogPreview").connect("expose_event",
self.update_font_dialog_preview)
window = self.font_dialog_window
hide_window = lambda *args: \
self.toggle_font_dialog_window(state=False)
self._gtk_handlers = [(window, "delete-event", hide_window),
(window, "destroy", hide_window),
(self.gui.get_object("FontDialogCancel"), "clicked",
hide_window),
(self.gui.get_object("FontDialogApply"), "clicked",
self.import_from_font_dialog),
(self.gui.get_object("FontDialogSave"), "clicked",
self.export_from_font_dialog),
(self.gui.get_object("FontDialogCopy"), "clicked",
self.copy_font_dialog_to_clipboard),
(self.gui.get_object("FontDialogInputBuffer"), "changed",
self.update_font_dialog_preview),
(self.gui.get_object("FontDialogPreview"),
"configure_event", self.update_font_dialog_preview),
(self.gui.get_object("FontDialogPreview"), "expose_event",
self.update_font_dialog_preview)]
for objname in ("FontSideSkewValue", "FontCharacterSpacingValue",
"FontLineSpacingValue"):
obj = self.gui.get_object(objname)
# set default value before connecting the change-handler
if objname != "FontSideSkewValue":
obj.set_value(1.0)
obj.connect("value-changed",
self.update_font_dialog_preview)
self._gtk_handlers.append((obj, "value-changed",
self.update_font_dialog_preview))
for objname in ("FontTextAlignLeft", "FontTextAlignCenter",
"FontTextAlignRight"):
self.gui.get_object(objname).connect("toggled",
self.update_font_dialog_preview)
self._gtk_handlers.append((self.gui.get_object(objname),
"toggled", self.update_font_dialog_preview))
# use global key accel map
self.font_dialog_window.add_accel_group(self.core.get("gtk-accel-group"))
self.font_dialog_window.add_accel_group(
self.core.get("gtk-accel-group"))
font_action = self.gui.get_object("ShowFontDialog")
self.register_gtk_accelerator("fonts", font_action,
"<Control><Shift>t", "ShowFontDialog")
font_action.connect("activate", self.toggle_font_dialog_window)
self._gtk_handlers.append((font_action, "activate",
self.toggle_font_dialog_window))
self.core.register_ui("edit_menu", "ShowFontDialogSeparator",
None, 55)
self.core.register_ui("edit_menu", "ShowFontDialog", font_action,
......@@ -91,6 +94,7 @@ class Fonts(pycam.Plugins.PluginBase):
self._font_dialog_window_visible = False
self._font_dialog_window_position = None
self.font_selector = None
self.register_gtk_handlers(self._gtk_handlers)
return True
def teardown(self):
......@@ -99,6 +103,7 @@ class Fonts(pycam.Plugins.PluginBase):
font_toggle = self.gui.get_object("ShowFontDialog")
self.core.unregister_ui("edit_menu", font_toggle)
self.unregister_gtk_accelerator("fonts", font_toggle)
self.unregister_gtk_handlers(self._gtk_handlers)
def toggle_font_dialog_window(self, widget=None, event=None, state=None):
# only "delete-event" uses four arguments
......
......@@ -39,31 +39,38 @@ class Log(pycam.Plugins.PluginBase):
self._gtk = gtk
# menu item and shortcut
log_action = self.gui.get_object("ToggleLogWindow")
log_action.connect("toggled", self.toggle_log_window)
self._gtk_handlers = []
self._gtk_handlers.append((log_action, "toggled",
self.toggle_log_window))
self.register_gtk_accelerator("log", log_action,
"<Control>l", "ToggleLogWindow")
self.core.register_ui("view_menu", "ToggleLogWindow", log_action, 100)
self.core.register_ui("view_menu", "ToggleLogWindow", log_action,
100)
# status bar
self.status_bar = self.gui.get_object("StatusBar")
self.gui.get_object("StatusBarEventBox").connect(
"button-press-event", self.toggle_log_window)
event_bar = self.gui.get_object("StatusBarEventBox")
self._gtk_handlers.append((event_bar, "button-press-event",
self.toggle_log_window))
event_bar.unparent()
self.core.register_ui("main_window", "Status", event_bar, 100)
# "log" window
self.log_window = self.gui.get_object("LogWindow")
self.log_window.set_default_size(500, 400)
self.log_window.connect("delete-event", self.toggle_log_window, False)
self.log_window.connect("destroy", self.toggle_log_window, False)
self.gui.get_object("LogWindowClose").connect("clicked", self.toggle_log_window, False)
self.gui.get_object("LogWindowClear").connect("clicked", self.clear_log_window)
self.gui.get_object("LogWindowCopyToClipboard").connect("clicked",
self.copy_log_to_clipboard)
hide_window = lambda *args: self.toggle_log_window(state=False)
self._gtk_handlers.extend([
(self.log_window, "delete-event", hide_window),
(self.log_window, "destroy", hide_window),
(self.gui.get_object("LogWindowClose"), "clicked", hide_window),
(self.gui.get_object("LogWindowClear"), "clicked",
self.clear_log_window),
(self.gui.get_object("LogWindowCopyToClipboard"), "clicked",
self.copy_log_to_clipboard)])
self.log_model = self.gui.get_object("LogWindowList")
# window state
self._log_window_position = None
# register a callback for the log window
pycam.Utils.log.add_hook(self.add_log_message)
self.register_gtk_handlers(self._gtk_handlers)
return True
def teardown(self):
......@@ -76,6 +83,7 @@ class Log(pycam.Plugins.PluginBase):
self.gui.get_object("StatusBarEventBox"))
self.core.unregister_ui("view_menu",
self.gui.get_object("ToggleLogWindow"))
self.unregister_gtk_handlers(self._gtk_handlers)
# TODO: disconnect the log handler
def add_log_message(self, title, message, record=None):
......
......@@ -44,14 +44,13 @@ class ModelExtrusion(pycam.Plugins.PluginBase):
if self.gui:
extrusion_frame = self.gui.get_object("ModelExtrusionFrame")
extrusion_frame.unparent()
self.core.register_event("model-change-after",
self._update_extrude_widgets)
self.core.register_event("model-selection-changed",
self._update_extrude_widgets)
self._gtk_handlers = ((self.gui.get_object("ExtrudeButton"),
"clicked", self._extrude_model), )
self._event_handlers = (
("model-change-after", self._update_extrude_widgets),
("model-selection-changed", self._update_extrude_widgets))
self.core.register_ui("model_handling", "Extrusion",
extrusion_frame, 5)
self.gui.get_object("ExtrudeButton").connect("clicked",
self._extrude_model)
self.gui.get_object("ExtrusionHeight").set_value(1)
self.gui.get_object("ExtrusionWidth").set_value(1)
self.gui.get_object("ExtrusionGrid").set_value(0.5)
......@@ -60,6 +59,8 @@ class ModelExtrusion(pycam.Plugins.PluginBase):
extrusion_model.append((row[0], row[1],
self.gui.get_object(row[2]).get_pixbuf()))
self.gui.get_object("ExtrusionTypeSelector").set_active(0)
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_extrude_widgets()
return True
......@@ -67,10 +68,8 @@ class ModelExtrusion(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelExtrusionFrame"))
self.core.unregister_event("model-change-after",
self._update_extrude_widgets)
self.core.unregister_event("model-selection-changed",
self._update_extrude_widgets)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _get_extrudable_models(self):
models = self.core.get("models").get_selected()
......
......@@ -35,19 +35,19 @@ class ModelPlaneMirror(pycam.Plugins.PluginBase):
mirror_box = self.gui.get_object("ModelMirrorBox")
mirror_box.unparent()
self.core.register_ui("model_handling", "Mirror", mirror_box, 0)
self.gui.get_object("PlaneMirrorButton").connect("clicked",
self._plane_mirror)
self.core.register_event("model-selection-changed",
self._update_plane_widgets)
self._gtk_handlers = ((self.gui.get_object("PlaneMirrorButton"),
"clicked", self._plane_mirror), )
self._event_handlers = (("model-selection-changed",
self._update_plane_widgets), )
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_plane_widgets()
return True
def teardown(self):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelMirrorBox"))
self.core.unregister_event("model-selection-changed",
self._update_plane_widgets)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_plane_widgets(self):
plane_widget = self.gui.get_object("ModelMirrorBox")
......
......@@ -35,14 +35,16 @@ class ModelPolygons(pycam.Plugins.PluginBase):
polygon_frame = self.gui.get_object("ModelPolygonFrame")
polygon_frame.unparent()
self.core.register_ui("model_handling", "Polygons", polygon_frame, 0)
self.core.register_event("model-change-after",
self._update_polygon_controls)
self.gui.get_object("ToggleModelDirectionButton").connect("clicked",
self._toggle_direction)
self.gui.get_object("DirectionsGuessButton").connect("clicked",
self._revise_directions)
self.core.register_event("model-selection-changed",
self._update_polygon_controls)
self._gtk_handlers = (
(self.gui.get_object("ToggleModelDirectionButton"), "clicked",
self._toggle_direction),
(self.gui.get_object("DirectionsGuessButton"), "clicked",
self._revise_directions))
self._event_handlers = (
("model-change-after", self._update_polygon_controls),
("model-selection-changed", self._update_polygon_controls))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_polygon_controls()
return True
......@@ -50,10 +52,8 @@ class ModelPolygons(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelPolygonFrame"))
self.core.unregister_event("model-change-after",
self._update_polygon_controls)
self.core.unregister_event("model-selection-changed",
self._update_polygon_controls)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _get_polygon_models(self):
models = []
......
......@@ -34,28 +34,36 @@ class ModelPosition(pycam.Plugins.PluginBase):
if self.gui:
position_box = self.gui.get_object("ModelPositionBox")
position_box.unparent()
self.core.register_ui("model_handling", "Position", position_box, -20)
shift_model_button = self.gui.get_object("ShiftModelButton")
shift_model_button.connect("clicked", self._shift_model)
align_model_button = self.gui.get_object("AlignPositionButton")
align_model_button.connect("clicked", self._align_model)
self._gtk_handlers = []
self.core.register_ui("model_handling", "Position", position_box,
-20)
shift_button = self.gui.get_object("ShiftModelButton")
self._gtk_handlers.append((shift_button, "clicked",
self._shift_model))
align_button = self.gui.get_object("AlignPositionButton")
self._gtk_handlers.append((align_button, "clicked",
self._align_model))
# grab default button for shift/align controls
for axis in "XYZ":
obj = self.gui.get_object("ShiftPosition%s" % axis)
obj.connect("focus-in-event", lambda widget, data: \
shift_model_button.grab_default())
obj.connect("focus-out-event", lambda widget, data: \
shift_model_button.get_toplevel().set_default(None))
self._gtk_handlers.extend((
(obj, "focus-in-event", lambda widget, data: \
shift_button.grab_default()),
(obj, "focus-out-event", lambda widget, data: \
shift_button.get_toplevel().set_default(None))))
for axis in "XYZ":
for name_template in ("AlignPosition%s", "AlignPosition%sMin",
"AlignPosition%sCenter", "AlignPosition%sMax"):
obj = self.gui.get_object("AlignPosition%s" % axis)
obj.connect("focus-in-event", lambda widget, data: \
align_model_button.grab_default())
obj.connect("focus-out-event", lambda widget, data: \
align_model_button.get_toplevel().set_default(None))
self.core.register_event("model-selection-changed",
self._update_position_widgets)
self._gtk_handlers.extend((
(obj, "focus-in-event", lambda widget, data: \
align_button.grab_default()),
(obj, "focus-out-event", lambda widget, data: \
align_button.get_toplevel().set_default(None))))
self._event_handlers = (("model-selection-changed",
self._update_position_widgets), )
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_position_widgets()
return True
......@@ -63,8 +71,8 @@ class ModelPosition(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelPositionBox"))
self.core.unregister_event("model-selection-changed",
self._update_position_widgets)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_position_widgets(self):
widget = self.gui.get_object("ModelPositionBox")
......
......@@ -38,12 +38,13 @@ class ModelProjection(pycam.Plugins.PluginBase):
projection_frame.unparent()
self.core.register_ui("model_handling", "Projection",
projection_frame, 10)
self.core.register_event("model-change-after",
self._update_controls)
self.gui.get_object("ProjectionButton").connect("clicked",
self._projection)
self.core.register_event("model-selection-changed",
self._update_controls)
self._gtk_handlers = ((self.gui.get_object("ProjectionButton"),
"clicked", self._projection), )
self._event_handlers = (
("model-change-after", self._update_controls),
("model-selection-changed", self._update_controls))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_controls()
return True
......@@ -51,10 +52,8 @@ class ModelProjection(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelProjectionFrame"))
self.core.unregister_event("model-selection-changed",
self._update_controls)
self.core.unregister_event("model-change-after",
self._update_controls)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _get_projectable_models(self):
models = self.core.get("models").get_selected()
......
......@@ -37,10 +37,12 @@ class ModelRotation(pycam.Plugins.PluginBase):
rotation_box.unparent()
self.core.register_ui("model_handling", "Rotation", rotation_box,
-10)
self.gui.get_object("RotateModelButton").connect("clicked",
self._rotate_model)
self.core.register_event("model-selection-changed",
self._update_controls)
self._gtk_handlers = ((self.gui.get_object("RotateModelButton"),
"clicked", self._rotate_model), )
self._event_handlers = (("model-selection-changed",
self._update_controls), )
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_controls()
return True
......@@ -48,8 +50,8 @@ class ModelRotation(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelRotationBox"))
self.core.unregister_event("model-selection-changed",
self._update_controls)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_controls(self):
widget = self.gui.get_object("ModelRotationBox")
......
......@@ -35,38 +35,29 @@ class ModelScaling(pycam.Plugins.PluginBase):
scale_box = self.gui.get_object("ModelScaleBox")
scale_box.unparent()
self.core.register_ui("model_handling", "Scale", scale_box, -5)
self.core.register_event("model-change-after",
self._update_scale_controls)
scale_percent = self.gui.get_object("ScalePercent")
scale_button = self.gui.get_object("ScaleModelButton")
scale_percent.set_value(100)
scale_percent.connect("focus-in-event",
lambda widget, data: scale_button.grab_default())
scale_percent.connect("focus-out-event", lambda widget, data: \
scale_box.get_toplevel().set_default(None))
scale_button.connect("clicked", self._scale_model)
# scale model to an axis dimension
self.gui.get_object("ScaleDimensionAxis").connect("changed",
lambda widget=None: self.core.emit_event(
"model-change-after"))
scale_dimension_button = self.gui.get_object("ScaleAllAxesButton")
scale_dimension_control = self.gui.get_object(
"ScaleDimensionControl")
scale_dimension_control.connect("focus-in-event",
lambda widget, data: scale_dimension_button.grab_default())
scale_dimension_control.connect("focus-out-event",
lambda widget, data: \
scale_box.get_toplevel().set_default(None))
scale_dimension_button.connect("clicked",
self._scale_model_axis_fit, True)
self.gui.get_object("ScaleSelectedAxisButton").connect("clicked",
self._scale_model_axis_fit, False)
self.gui.get_object("ScaleInchMM").connect("clicked",
self._scale_model, 100 * 25.4)
self.gui.get_object("ScaleMMInch").connect("clicked",
self._scale_model, 100 / 25.4)
self.core.register_event("model-selection-changed",
self._update_scale_controls)
self._gtk_handlers = []
self._gtk_handlers.extend((
(scale_percent, "focus-in-event", lambda widget, data: scale_button.grab_default()),
(scale_percent, "focus-out-event", lambda widget, data: scale_box.get_toplevel().set_default(None)),
(scale_button, "clicked", self._scale_model),
(self.gui.get_object("ScaleDimensionAxis"), "changed", lambda widget=None: self.core.emit_event( "model-change-after")),
(scale_dimension_control, "focus-in-event", lambda widget, data: scale_dimension_button.grab_default()),
(scale_dimension_control, "focus-out-event", lambda widget, data: scale_box.get_toplevel().set_default(None)),
(scale_dimension_button, "clicked", lambda widget: self._scale_model_axis_fit(proportionally=True)),
(self.gui.get_object("ScaleSelectedAxisButton"), "clicked", lambda widget: self._scale_model_axis_fit(proportionally=False)),
(self.gui.get_object("ScaleInchMM"), "clicked", lambda widget: self._scale_model(percent=(100 * 25.4))),
(self.gui.get_object("ScaleMMInch"), "clicked", lambda widget: self._scale_model(percent=(100 / 25.4)))))
self._event_handlers = (
("model-selection-changed", self._update_scale_controls),
("model-change-after", self._update_scale_controls))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_scale_controls()
return True
......@@ -74,10 +65,8 @@ class ModelScaling(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelScaleBox"))
self.core.unregister_event("model-change-after",
self._update_scale_controls)
self.core.unregister_event("model-selection-changed",
self._update_scale_controls)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_scale_controls(self):
models = self.core.get("models").get_selected()
......
......@@ -36,18 +36,11 @@ class ModelSupport(pycam.Plugins.PluginBase):
self._support_frame.unparent()
self.core.register_ui("model_handling", "Support",
self._support_frame, 0)
self.core.register_event("model-change-after",
self._support_model_changed)
self.core.register_event("bounds-changed",
self._support_model_changed)
self.core.register_event("model-selection-changed",
self._update_widgets)
self.core.register_event("support-model-changed",
self.update_support_model)
support_model_type_selector = self.gui.get_object(
"SupportGridTypesControl")
support_model_type_selector.connect("changed",
self._support_model_changed)
self._gtk_handlers = []
self._gtk_handlers.append((support_model_type_selector, "changed",
self._support_model_changed))
def add_support_type(obj, name):
types_model = support_model_type_selector.get_model()
types_model.append((obj, name))
......@@ -87,20 +80,30 @@ class ModelSupport(pycam.Plugins.PluginBase):
get_support_model_type,
set_support_model_type)
grid_thickness = self.gui.get_object("SupportGridThickness")
grid_thickness.connect("value-changed", self._support_model_changed)
self._gtk_handlers.append((grid_thickness, "value-changed",
self._support_model_changed))
self.core.add_item("support_grid_thickness",
grid_thickness.get_value, grid_thickness.set_value)
grid_height = self.gui.get_object("SupportGridHeight")
grid_height.connect("value-changed", self._support_model_changed)
self._gtk_handlers.append((grid_height, "value-changed",
self._support_model_changed))
self.core.add_item("support_grid_height",
grid_height.get_value, grid_height.set_value)
self._gtk_handlers.append((
self.gui.get_object("CreateSupportModel"), "clicked",
self._add_support_model))
# support grid defaults
self.core.set("support_grid_thickness", 0.5)
self.core.set("support_grid_height", 0.5)
self.core.set("support_grid_type", "none")
# prepare GUI
self.gui.get_object("CreateSupportModel").connect("clicked",
self._add_support_model)
# handlers
self._event_handlers = (
("model-change-after", self._support_model_changed),
("bounds-changed", self._support_model_changed),
("model-selection-changed", self._update_widgets),
("support-model-changed", self.update_support_model))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_widgets()
return True
......@@ -109,14 +112,8 @@ class ModelSupport(pycam.Plugins.PluginBase):
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelExtensionsFrame"))
self.core.unregister_ui("support_model_type_selector", "none")
self.core.unregister_event("model-change-after",
self._support_model_changed)
self.core.unregister_event("bounds-changed",
self._support_model_changed)
self.core.unregister_event("model-selection-changed",
self._update_widgets)
self.core.unregister_event("support-model-changed",
self.update_support_model)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_widgets(self):
if self.core.get("models").get_selected():
......
......@@ -34,36 +34,44 @@ class ModelSupportDistributed(pycam.Plugins.PluginBase):
if self.gui:
support_expander = self.gui.get_object("DistributedSupportExpander")
support_expander.unparent()
self._gtk_handlers = []
self.core.register_ui("support_model_type_selector",
"Distributed (edges)", "distributed_edges", weight=0)
self.core.register_ui("support_model_type_selector",
"Distributed (corners)", "distributed_corners", weight=10)
self.core.register_ui("support_model_settings", "Grid settings",
support_expander)
self.core.register_event("support-model-changed",
self.update_support_controls)
self.core.register_event("support-model-changed",
self.update_support_model)
grid_length = self.gui.get_object("SupportGridLength")
grid_length.connect("value-changed", self.update_support_model)
self._gtk_handlers.append((grid_length, "value-changed",
self.update_support_model))
self.core.add_item("support_grid_length",
grid_length.get_value, grid_length.set_value)
average_distance = self.gui.get_object("GridAverageDistance")
average_distance.connect("value-changed", self.update_support_model)
self._gtk_handlers.append((average_distance, "value-changed",
self.update_support_model))
self.core.add_item("support_grid_average_distance",
average_distance.get_value, average_distance.set_value)
minimum_bridges = self.gui.get_object("GridMinBridgesPerPolygon")
minimum_bridges.connect("value-changed", self.update_support_model)
self._gtk_handlers.append((minimum_bridges, "value-changed",
self.update_support_model))
self.core.add_item("support_grid_minimum_bridges",
minimum_bridges.get_value, minimum_bridges.set_value)
# TODO: remove these public settings
self.core.set("support_grid_average_distance", 30)
self.core.set("support_grid_minimum_bridges", 2)
self.core.set("support_grid_length", 5)
# register handlers
self._event_handlers = (
("support-model-changed", self.update_support_controls),
("support-model-changed", self.update_support_model))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
return True
def teardown(self):
if self.gui:
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
self.core.unregister_ui("support_model_type_selector",
"distributed_edges")
self.core.unregister_ui("support_model_type_selector",
......
......@@ -42,23 +42,25 @@ class ModelSupportGrid(pycam.Plugins.PluginBase):
"grid", weight=-10)
self.core.register_ui("support_model_settings", "Grid settings",
grid_box)
self.core.register_event("support-model-changed",
self.update_support_model)
support_model_changed = lambda widget=None: self.core.emit_event(
"support-model-changed")
self._gtk_handlers = []
# support grid
self.grid_adjustments_x = []
self.grid_adjustments_y = []
self.grid_adjustment_axis_x_last = True
self._block_manual_adjust_update = False
grid_distance_x = self.gui.get_object("SupportGridDistanceX")
grid_distance_x.connect("value-changed", support_model_changed)
self._gtk_handlers.append((grid_distance_x, "value-changed",
support_model_changed))
self.core.add_item("support_grid_distance_x",
grid_distance_x.get_value, grid_distance_x.set_value)
grid_distance_square = self.gui.get_object("SupportGridDistanceSquare")
grid_distance_square.connect("clicked", self.update_support_controls)
self._gtk_handlers.append((grid_distance_square, "clicked",
self.update_support_controls))
grid_distance_y = self.gui.get_object("SupportGridDistanceY")
grid_distance_y.connect("value-changed", support_model_changed)
self._gtk_handlers.append((grid_distance_y, "value-changed",
support_model_changed))
def get_support_grid_distance_y():
if grid_distance_square.get_active():
return self.core.get("support_grid_distance_x")
......@@ -67,39 +69,46 @@ class ModelSupportGrid(pycam.Plugins.PluginBase):
self.core.add_item("support_grid_distance_y",
get_support_grid_distance_y, grid_distance_y.set_value)
grid_offset_x = self.gui.get_object("SupportGridOffsetX")
grid_offset_x.connect("value-changed", support_model_changed)
self._gtk_handlers.append((grid_offset_x, "value-changed",
support_model_changed))
self.core.add_item("support_grid_offset_x",
grid_offset_x.get_value, grid_offset_x.set_value)
grid_offset_y = self.gui.get_object("SupportGridOffsetY")
grid_offset_y.connect("value-changed", support_model_changed)
self._gtk_handlers.append((grid_offset_y, "value-changed",
support_model_changed))
self.core.add_item("support_grid_offset_y",
grid_offset_y.get_value, grid_offset_y.set_value)
# manual grid adjustments
self.grid_adjustment_axis_x = self.gui.get_object("SupportGridPositionManualAxisX")
self.grid_adjustment_axis_x.connect("toggled",
self.switch_support_grid_manual_selector)
self.gui.get_object("SupportGridPositionManualResetOne").connect(
"clicked", self.reset_support_grid_manual, False)
self.gui.get_object("SupportGridPositionManualResetAll").connect(
"clicked", self.reset_support_grid_manual, True)
self.grid_adjustment_axis_x = self.gui.get_object(
"SupportGridPositionManualAxisX")
self._gtk_handlers.extend((
(self.grid_adjustment_axis_x, "toggled",
self.switch_support_grid_manual_selector),
(self.gui.get_object("SupportGridPositionManualResetOne"),
"clicked", lambda *args: \
self.reset_support_grid_manual(reset_all=False)),
(self.gui.get_object("SupportGridPositionManualResetAll"),
"clicked", lambda *args: \
self.reset_support_grid_manual(True))))
self.grid_adjustment_model = self.gui.get_object(
"SupportGridPositionManualList")
self.grid_adjustment_selector = self.gui.get_object(
"SupportGridPositionManualSelector")
self.grid_adjustment_selector.connect("changed",
self.switch_support_grid_manual_selector)
self._gtk_handlers.append((self.grid_adjustment_selector,
"changed", self.switch_support_grid_manual_selector))
self.grid_adjustment_value = self.gui.get_object(
"SupportGridPositionManualAdjustment")
self.grid_adjustment_value_control = self.gui.get_object(
"SupportGridPositionManualShiftControl")
self.grid_adjustment_value_control.set_update_policy(
gtk.UPDATE_DISCONTINUOUS)
self.grid_adjustment_value_control.connect("move-slider",
self.update_support_grid_manual_adjust)
self.grid_adjustment_value_control.connect("value-changed",
self.update_support_grid_manual_adjust)
self.gui.get_object("SupportGridPositionManualShiftControl2").connect(
"value-changed", self.update_support_grid_manual_adjust)
self._gtk_handlers.extend((
(self.grid_adjustment_value_control, "move-slider",
self.update_support_grid_manual_adjust),
(self.grid_adjustment_value_control, "value-changed",
self.update_support_grid_manual_adjust),
(self.gui.get_object("SupportGridPositionManualShiftControl2"),
"value-changed", self.update_support_grid_manual_adjust)))
def get_set_grid_adjustment_value(value=None):
if self.grid_adjustment_axis_x.get_active():
adjustments = self.grid_adjustments_x
......@@ -118,10 +127,14 @@ class ModelSupportGrid(pycam.Plugins.PluginBase):
# TODO: remove these public settings
self.core.add_item("support_grid_adjustment_value",
get_set_grid_adjustment_value, get_set_grid_adjustment_value)
self.core.register_event("support-model-changed",
self.update_support_controls)
grid_distance_square.set_active(True)
self.core.set("support_grid_distance_x", 10.0)
# handlers
self._event_handlers = ((
("support-model-changed", self.update_support_controls),
("support-model-changed", self.update_support_model)))
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
return True
def teardown(self):
......@@ -129,8 +142,8 @@ class ModelSupportGrid(pycam.Plugins.PluginBase):
self.core.unregister_ui("support_model_type_selector", "grid")
self.core.unregister_ui("support_model_settings",
self.gui.get_object("SupportModelGridBox"))
self.core.unregister_event("support-model-changed",
self.update_support_model)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def update_support_model(self, widget=None):
grid_type = self.core.get("support_model_type")
......
......@@ -35,10 +35,12 @@ class ModelSwapAxes(pycam.Plugins.PluginBase):
swap_box = self.gui.get_object("ModelSwapBox")
swap_box.unparent()
self.core.register_ui("model_handling", "Swap axes", swap_box, 0)
self.gui.get_object("SwapAxesButton").connect("clicked",
self._swap_axes)
self.core.register_event("model-selection-changed",
self._update_controls)
self._gtk_handlers = ((self.gui.get_object("SwapAxesButton"),
"clicked", self._swap_axes), )
self._event_handlers = (("model-selection-changed",
self._update_controls), )
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._update_controls()
return True
......@@ -46,8 +48,8 @@ class ModelSwapAxes(pycam.Plugins.PluginBase):
if self.gui:
self.core.unregister_ui("model_handling",
self.gui.get_object("ModelSwapBox"))
self.core.unregister_event("model-selection-changed",
self._update_controls)
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
def _update_controls(self):
box = self.gui.get_object("ModelSwapBox")
......
......@@ -61,17 +61,18 @@ class Models(pycam.Plugins.ListPluginBase):
(self.ACTION_CLEAR, "ModelDeleteAll")):
self.register_list_action_button(action, self._modelview,
self.gui.get_object(obj_name))
self.gui.get_object("ModelColorButton").connect("color-set",
self._set_colors_of_selected_models)
self.core.register_event("model-selection-changed",
self._get_colors_of_selected_models)
self._modelview.connect("row-activated",
self._list_action_toggle_custom, self.COLUMN_VISIBLE)
self._gtk_handlers = []
self._gtk_handlers.extend((
(self.gui.get_object("ModelColorButton"), "color-set",
self._set_colors_of_selected_models),
(self._modelview, "row-activated", lambda *args:
self._list_action_toggle_custom(
*(args + [self.COLUMN_VISIBLE]))),
(self.gui.get_object("ModelNameColumn"), "edited",
self._edit_model_name)))
self.gui.get_object("ModelVisibleColumn").set_cell_data_func(
self.gui.get_object("ModelVisibleSymbol"),
self._visualize_visible_state)
self.gui.get_object("ModelNameColumn").connect("edited",
self._edit_model_name)
self._treemodel = self.gui.get_object("ModelList")
self._treemodel.clear()
def update_model():
......@@ -93,9 +94,12 @@ class Models(pycam.Plugins.ListPluginBase):
self.core.emit_event("model-list-changed")
selection = self._modelview.get_selection()
selection.set_mode(self._gtk.SELECTION_MULTIPLE)
selection.connect("changed",
lambda widget, event: self.core.emit_event(event),
"model-selection-changed")
self._gtk_handlers.append((selection, "changed",
"model-selection-changed"))
self._event_handlers = (("model-selection-changed",
self._get_colors_of_selected_models), )
self.register_gtk_handlers(self._gtk_handlers)
self.register_event_handlers(self._event_handlers)
self._get_colors_of_selected_models()
self.register_model_update(update_model)
# update the model list
......@@ -105,10 +109,10 @@ class Models(pycam.Plugins.ListPluginBase):
def teardown(self):
if self.gui:
self.core.unregister_event("model-selection-changed",
self._get_colors_of_selected_models)
self.core.unregister_ui_section("model_handling")
self.core.unregister_ui("main", self.gui.get_object("ModelBox"))
self.unregister_gtk_handlers(self._gtk_handlers)
self.unregister_event_handlers(self._event_handlers)
self.core.set("models", None)
return True
......
......@@ -37,17 +37,16 @@ class OpenGLViewModel(pycam.Plugins.PluginBase):
import OpenGL.GL
self._gtk = gtk
self._GL = OpenGL.GL
self.core.register_event("visualize-items", self.draw_model)
self.core.register_event("model-changed",
lambda: self.core.emit_event("visual-item-updated"))
self.core.register_event("model-list-changed",
lambda: self.core.emit_event("visual-item-updated"))
self._event_handlers = (("visualize-items", self.draw_model),
("model-changed","visual-item-updated"),
("model-list-changed","visual-item-updated"))
self.register_event_handlers(self._event_handlers)
self.core.emit_event("visual-item-updated")
self._cache = {}
return True
def teardown(self):
self.core.unregister_event("visualize-items", self.draw_model)
self.unregister_event_handlers(self._event_handlers)
self.core.emit_event("visual-item-updated")
def _get_cache_key(self, model, *args, **kwargs):
......
......@@ -38,8 +38,10 @@ class ProgressBar(pycam.Plugins.PluginBase):
box = self.gui.get_object("ProgressBox")
box.unparent()
self.core.register_ui("main_window", "Progress", box, 50)
self.core.add_item("progress", lambda: ProgressGTK(self.core, self.gui))
show_progress_button = self.gui.get_object("ShowToolpathProgressButton")
self.core.add_item("progress", lambda: ProgressGTK(self.core,
self.gui, self.log))
show_progress_button = self.gui.get_object(
"ShowToolpathProgressButton")
# TODO: move this setting somewhere else or rename it
self.core.add_item("show_drill_progress",
show_progress_button.get_active,
......@@ -57,12 +59,13 @@ class ProgressGTK(object):
_PROGRESS_STACK = []
def __init__(self, core, gui):
def __init__(self, core, gui, log):
ProgressGTK._PROGRESS_STACK.append(self)
import gtk
self._finished = False
self._gtk = gtk
self._gui = gui
self.log = log
self.core = core
self._cancel_requested = False
self._start_time = 0
......@@ -75,7 +78,8 @@ class ProgressGTK(object):
self._cancel_button = self._gui.get_object("ProgressCancelButton")
self._cancel_button.connect("clicked", self.cancel)
self._progress_bar = self._gui.get_object("ProgressBar")
self._progress_button = self._gui.get_object("ShowToolpathProgressButton")
self._progress_button = self._gui.get_object(
"ShowToolpathProgressButton")
self._start_time = time.time()
self._progress_button.show()
self._last_text = None
......@@ -117,8 +121,8 @@ class ProgressGTK(object):
else:
text = "%d/%d" % (self._multi_counter, self._multi_maximum)
self._multi_widget.set_text(text)
self._multi_widget.set_fraction(
float(self._multi_counter) / self._multi_maximum)
self._multi_widget.set_fraction(min(1.0,
float(self._multi_counter) / self._multi_maximum))
def disable_cancel(self):
self._cancel_button.set_sensitive(False)
......@@ -152,7 +156,8 @@ class ProgressGTK(object):
self._finished = True
def __del__(self):
self.finish()
if not self._finished:
self.finish()
def update(self, text=None, percent=None):
if text:
......@@ -178,6 +183,7 @@ class ProgressGTK(object):
time_estimation_suffix = " remaining ..."
if self._progress_bar.get_fraction() > 0:
total_fraction = (self._progress_bar.get_fraction() + self._multi_counter) / max(1, self._multi_maximum)
total_fraction = max(0.0, min(total_fraction, 1.0))
eta_full = (time.time() - self._start_time) / total_fraction
if eta_full > 0:
eta_delta = eta_full - (time.time() - self._start_time)
......
......@@ -73,8 +73,34 @@ class PluginBase(object):
else:
self.log.debug("Failed to locate icon: %s" % self.ICONS[key])
self.ICONS[key] = None
self._lambda_handlers = {}
self.enabled = True
def __get_handler_func(self, func):
if isinstance(func, basestring):
if not func in self._lambda_handlers:
self._lambda_handlers[func] = lambda *args: \
self.core.emit_event(func)
return self._lambda_handlers[func]
else:
return func
def register_event_handlers(self, event_handlers):
for name, func in event_handlers:
self.core.register_event(name, self.__get_handler_func(func))
def register_gtk_handlers(self, gtk_widget_handlers):
for obj, signal, func in gtk_widget_handlers:
obj.connect(signal, self.__get_handler_func(func))
def unregister_event_handlers(self, event_handlers):
for name, func in event_handlers:
self.core.unregister_event(name, self.__get_handler_func(func))
def unregister_gtk_handlers(self, gtk_widget_handlers):
for obj, signal, func in gtk_widget_handlers:
obj.connect(signal, self.__get_handler_func(func))
def setup(self):
raise NotImplementedError(("Module %s (%s) does not implement " + \
"'setup'") % (self.name, __file__))
......
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