# Copyright (c) 2011, Enthought, Ltd.
# Author: Pietro Berkes <pberkes@enthought.com>
# License: Apache license

"""Plot tools to save plots and copy their data inside to the clipboard.
"""

# Major library imports
import os.path

# Enthought library imports
from chaco.plot_graphics_context import PlotGraphicsContext
from chaco.tools.save_tool import SaveTool
from enable.base_tool import BaseTool
from traits.trait_types import Int, Any
from pyface.api import clipboard

import numpy as np

class SaveToolPlus(SaveTool):
    """Subclass of SaveTool that requests a filename and dpi before saving."""

    dpi = Int(300)

    def normal_key_pressed(self, event):
        """ Handles a key-press when the tool is in the 'normal' state.

        Saves an image of the plot if the keys pressed are Control and S.
        """

        if self.component is None: return

        if ((event.character == "s" or event.character == u'\x13')
            and event.control_down):
            # TODO: request name of file, dpi
            print 'triggered!!'

            if os.path.splitext(self.filename)[-1] == ".pdf":
                self._save_pdf()
            else:
                self._save_raster()
            event.handled = True

    def _save_raster(self):
        """ Saves an image of the component."""
        self.component.do_layout(force=True)
        # NOTE saving only works properly when dpi is a multiple of 72
        gc = PlotGraphicsContext((int(self.component.outer_width),
                                  int(self.component.outer_height)),
                                 dpi=np.ceil(self.dpi / 72.0)*72)
        gc.render_component(self.component)
        gc.save(self.filename)


class CopyDataToClipboardTool(BaseTool):
    """Tool that copies the plot's data to the clipboard."""

    # TODO allow apple-c key combination

    data = Any

    def normal_key_pressed(self, event):
        """ Handles a key-press when the tool is in the 'normal' state.

        Saves an image of the plot if the keys pressed are Control and S.
        """

        if self.component is None: return

        if ((event.character == "c" or event.character == u'\x03')
            and event.control_down):
            clipboard.data = repr(self.data)

            print 'triggered copy!!'

            event.handled = True

@@ -470,27 +470,755 @@
 ard%0A
-%0Aimport numpy as np
+from enthought.etsconfig.api import ETSConfig%0A%0Aimport numpy as np%0A%0A%0Adef _is_control_down(key_event):%0A    %22%22%22Return true if the Ctrl or Cmd key is down.%22%22%22%0A%0A    is_control_down = key_event.control_down%0A%0A    if ETSConfig.toolkit == 'wx':%0A        # workaround for the fact that wxPython does not return True in%0A        # KeyEvent.ContrlDown() when the Cmd key is pressed on a Mac,%0A        # which is not what wx does (see%0A        # http://docs.wxwidgets.org/2.9.2/classwx_keyboard_state.html)%0A%0A        # note that qt already does the right thing (i.e.,%0A        # control_down is true also for Mac's cmd key)%0A        is_control_down = (key_event.event.ControlDown()%0A                           or key_event.event.CmdDown())%0A%0A    return is_control_down%0A
 %0A%0Acl
@@ -1664,38 +1664,36 @@
             and 
-event.
+_is_
 control_down):%0A 
@@ -1680,32 +1680,39 @@
 _is_control_down
+(event)
 ):%0A            #
@@ -2538,50 +2538,8 @@
 %22%22%0A%0A
-    # TODO allow apple-c key combination%0A%0A
     
@@ -2877,22 +2877,20 @@
     and 
-event.
+_is_
 control_
@@ -2893,16 +2893,23 @@
 rol_down
+(event)
 ):%0A     

handle Cmd-C and Cmd-S in plot tools