From 34e7187639f5927614c7cccec486f94ad553468b Mon Sep 17 00:00:00 2001
From: "Michael D. M. Dryden" <mdryden@chem.utoronto.ca>
Date: Tue, 5 Aug 2014 17:34:14 -0400
Subject: [PATCH] Adds menu option to save plot.

---
 dstatInterface/interface/dstatinterface.glade |  6 +++-
 dstatInterface/interface/save.py              | 36 +++++++++++++++++++
 dstatInterface/interface_test.py              |  3 ++
 3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/dstatInterface/interface/dstatinterface.glade b/dstatInterface/interface/dstatinterface.glade
index b27fc3a..a608eaa 100644
--- a/dstatInterface/interface/dstatinterface.glade
+++ b/dstatInterface/interface/dstatinterface.glade
@@ -156,6 +156,7 @@
                         <property name="can_focus">False</property>
                         <property name="image">image3</property>
                         <property name="use_stock">False</property>
+                        <signal name="activate" handler="on_file_save_plot_activate" swapped="no"/>
                       </object>
                     </child>
                     <child>
@@ -456,14 +457,17 @@
                           <object class="GtkEntry" id="autosavename">
                             <property name="visible">True</property>
                             <property name="can_focus">True</property>
+                            <property name="has_tooltip">True</property>
                             <property name="max_length">32</property>
                             <property name="invisible_char">●</property>
                             <property name="text" translatable="yes">file</property>
-                            <property name="overwrite_mode">True</property>
+                            <property name="caps_lock_warning">False</property>
+                            <property name="primary_icon_stock">gtk-file</property>
                             <property name="primary_icon_activatable">False</property>
                             <property name="secondary_icon_activatable">False</property>
                             <property name="primary_icon_sensitive">True</property>
                             <property name="secondary_icon_sensitive">True</property>
+                            <property name="primary_icon_tooltip_text" translatable="yes">File name</property>
                           </object>
                           <packing>
                             <property name="expand">False</property>
diff --git a/dstatInterface/interface/save.py b/dstatInterface/interface/save.py
index 1f74c86..f472a98 100644
--- a/dstatInterface/interface/save.py
+++ b/dstatInterface/interface/save.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -*- coding: utf-8 -*-
 
 import gtk, io, os
 import numpy as np
@@ -34,6 +35,41 @@ def manSave(current_exp):
     elif response == gtk.RESPONSE_CANCEL:
         fcd.destroy()
 
+def plotSave(plot):
+    fcd = gtk.FileChooserDialog("Save Plot…", None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK))
+
+    filters = [gtk.FileFilter()]
+    filters[0].set_name("Portable Document Format (.pdf)")
+    filters[0].add_pattern("*.pdf")
+    filters.append(gtk.FileFilter())
+    filters[1].set_name("Portable Network Graphics (.png)")
+    filters[1].add_pattern("*.png")
+    
+    fcd.set_do_overwrite_confirmation(True)
+    for i in filters:
+        fcd.add_filter(i)
+    
+    response = fcd.run()
+    
+    if response == gtk.RESPONSE_OK:
+        path = fcd.get_filename()
+        print "Selected filepath: %s" % path
+        filter_selection = fcd.get_filter().get_name()
+        
+        if filter_selection.endswith("(.pdf)"):
+            if not path.endswith(".pdf"):
+                path += ".pdf"
+        
+        elif filter_selection.endswith("(.png)"):
+            if not path.endswith(".png"):
+                path += ".png"
+
+        plot.figure.savefig(path) #savefig determines format from file extension
+        fcd.destroy()
+    
+    elif response == gtk.RESPONSE_CANCEL:
+        fcd.destroy()
+
 def autoSave(current_exp, dir_button, name, expnumber):
     if name == "":
         name = "file"
diff --git a/dstatInterface/interface_test.py b/dstatInterface/interface_test.py
index e4b7b05..0b58a69 100644
--- a/dstatInterface/interface_test.py
+++ b/dstatInterface/interface_test.py
@@ -557,6 +557,9 @@ class main:
     def on_file_save_exp_activate(self, menuitem, data=None):
         if self.current_exp:
             save_inst = save.manSave(self.current_exp)
+    
+    def on_file_save_plot_activate(self, menuitem, data=None):
+            save_inst = save.plotSave(self.plot)
 
 
 if __name__ == "__main__":
-- 
GitLab