# -*- coding: utf-8 -*-
"""
$Id$

Copyright 2011 Lars Kruse <devel@sumpfralle.de>

This file is part of pyMKcam.

pyMKcam 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.

pyMKcam 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 pyMKcam.  If not, see <http://www.gnu.org/licenses/>.
"""


import pymkcam.Plugins
import pymkcam.Gui.ControlsGTK
from pymkcam.Toolpath.Filters import toolpath_filter


class ToolParamRadius(pymkcam.Plugins.PluginBase):

    DEPENDS = ["Tools"]
    CATEGORIES = ["Tool", "Parameter"]

    def setup(self):
        self.control = pymkcam.Gui.ControlsGTK.InputNumber(lower=0, upper=999,
                digits=4, change_handler=lambda widget=None: \
                    self.core.emit_event("tool-changed"))
        self.control.set_conversion(set_conv=lambda value: value * 2.0,
                get_conv=lambda value: value / 2.0)
        self.core.get("register_parameter")("tool", "radius", self.control)
        self.core.register_ui("tool_size", "Tool Diameter",
                self.control.get_widget(), weight=10)
        return True

    def teardown(self):
        self.core.get("unregister_parameter")("tool", "radius")
        self.core.unregister_ui("tool_size", self.control.get_widget())


class ToolParamTorusRadius(pymkcam.Plugins.PluginBase):

    DEPENDS = ["Tools"]
    CATEGORIES = ["Tool", "Parameter"]

    def setup(self):
        self.control = pymkcam.Gui.ControlsGTK.InputNumber(lower=0, upper=999,
                digits=4, change_handler=lambda widget=None: \
                    self.core.emit_event( "tool-changed"))
        self.core.get("register_parameter")("tool", "torus_radius",
                self.control)
        self.core.register_ui("tool_size", "Torus Radius",
                self.control.get_widget(), weight=50)
        return True

    def teardown(self):
        self.core.unregister_ui("tool_size", self.control.get_widget())
        self.core.get("unregister_parameter")("tool", "torus_radius")


class ToolParamFeedrate(pymkcam.Plugins.PluginBase):

    DEPENDS = ["Tools"]
    CATEGORIES = ["Tool", "Parameter"]

    def setup(self):
        self.control = pymkcam.Gui.ControlsGTK.InputNumber(lower=0, upper=50000,
                digits=0, change_handler=lambda widget=None: \
                    self.core.emit_event("tool-changed"))
        self.core.get("register_parameter")("tool", "feedrate", self.control)
        self.core.register_ui("tool_speed", "Feedrate",
                self.control.get_widget(), weight=10)
        self.core.register_chain("toolpath_filters",
                self.get_toolpath_filters)
        return True

    def teardown(self):
        self.core.unregister_ui("tool_speed", self.control.get_widget())
        self.core.get("unregister_parameter")("tool", "feedrate")
        self.core.unregister_chain("toolpath_filters",
                self.get_toolpath_filters)

    @toolpath_filter("tool", "feedrate")
    def get_toolpath_filters(self, feedrate):
        return [pymkcam.Toolpath.Filters.MachineSetting("feedrate", feedrate)]


class ToolParamSpindleSpeed(pymkcam.Plugins.PluginBase):

    DEPENDS = ["Tools"]
    CATEGORIES = ["Tool", "Parameter"]

    def setup(self):
        self.control = pymkcam.Gui.ControlsGTK.InputNumber(lower=1, upper=500000,
                digits=0, change_handler=lambda widget=None: \
                    self.core.emit_event("tool-changed"))
        self.core.get("register_parameter")("tool", "spindle_speed",
                self.control)
        self.core.register_ui("tool_speed", "Spindle Speed",
                self.control.get_widget(), weight=50)
        self.core.register_chain("toolpath_filters",
                self.get_toolpath_filters)
        return True

    def teardown(self):
        self.core.unregister_ui("tool_speed", self.control.get_widget())
        self.core.get("unregister_parameter")("tool", "spindle_speed")
        self.core.unregister_chain("toolpath_filters",
                self.get_toolpath_filters)

    @toolpath_filter("tool", "spindle_speed")
    def get_toolpath_filters(self, spindle_speed):
        return [pymkcam.Toolpath.Filters.MachineSetting("spindle_speed",
                spindle_speed)]

