[GRASS-SVN] r44069 - grass/branches/develbranch_6/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 28 12:49:21 EDT 2010


Author: martinl
Date: 2010-10-28 09:49:21 -0700 (Thu, 28 Oct 2010)
New Revision: 44069

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/mcalc_builder.py
Log:
wxGUI/mcalc: load/save expression (based on Tim Michelsen's patch #669)
(merge r44068 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/mcalc_builder.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/mcalc_builder.py	2010-10-28 16:43:17 UTC (rev 44068)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/mcalc_builder.py	2010-10-28 16:49:21 UTC (rev 44069)
@@ -13,6 +13,7 @@
 
 @author Michael Barton, Arizona State University
 @author Martin Landa <landa.martin gmail.com>
+ at author Tim Michelsen (load/save expression)
 """
 
 import os
@@ -134,7 +135,12 @@
         self.btn_run = wx.Button(parent = self.panel, id = wx.ID_ANY, label = _("&Run"))
         self.btn_run.SetDefault()
         self.btn_close = wx.Button(parent = self.panel, id = wx.ID_CLOSE)
-
+        self.btn_save = wx.Button(parent = self.panel, id = wx.ID_SAVE)
+        self.btn_save.SetToolTipString(_('Save expression to file'))
+        self.btn_load = wx.Button(parent = self.panel, id = wx.ID_ANY,
+                                  label = _("&Load"))
+        self.btn_load.SetToolTipString(_('Load expression from file'))
+        
         self.btn = dict()        
         self.btn['pow'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "^")
         self.btn['pow'].SetToolTipString(_('exponent'))
@@ -228,6 +234,8 @@
         self.btn_clear.Bind(wx.EVT_BUTTON, self.OnClear)
         self.btn_run.Bind(wx.EVT_BUTTON, self.OnMCalcRun)
         self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp)
+        self.btn_save.Bind(wx.EVT_BUTTON, self.OnSaveExpression)
+        self.btn_load.Bind(wx.EVT_BUTTON, self.OnLoadExpression)
         
         self.mapselect.Bind(wx.EVT_TEXT, self.OnSelect)
         self.function.Bind(wx.EVT_COMBOBOX, self.OnSelect)
@@ -291,11 +299,17 @@
                          span = (1,1), flag = wx.ALIGN_RIGHT)
         
         buttonSizer4 = wx.BoxSizer(wx.HORIZONTAL)
-        buttonSizer4.Add(item = self.btn_close,
+        buttonSizer4.AddSpacer(10)
+        buttonSizer4.Add(item = self.btn_load,
                          flag = wx.ALL, border = 5)
+        buttonSizer4.Add(item = self.btn_save,
+                         flag = wx.ALL, border = 5)                         
+        buttonSizer4.AddSpacer(10)
+        buttonSizer4.Add(item = self.btn_help,
+                         flag = wx.ALL, border = 5)
         buttonSizer4.Add(item = self.btn_run,
                          flag = wx.ALL, border = 5)
-        buttonSizer4.Add(item = self.btn_help,
+        buttonSizer4.Add(item = self.btn_close,
                          flag = wx.ALL, border = 5)
         
         operatorSizer.Add(item = buttonSizer1, proportion = 0,
@@ -436,6 +450,62 @@
         if display and display.IsAutoRendered():
             display.GetWindow().UpdateMap(render = True)
         
+    def OnSaveExpression(self, event):
+        """!Saves expression to file
+        """
+        mctxt = self.newmaptxt.GetValue() + ' = ' + self.text_mcalc.GetValue() + os.linesep
+        
+        #dialog
+        dlg = wx.FileDialog(parent = self,
+                            message = _("Choose a file name to save the expression"),
+                            wildcard = _("Expression file (*)|*"),
+                            style = wx.SAVE | wx.FD_OVERWRITE_PROMPT)
+        if dlg.ShowModal() == wx.ID_OK:
+            path = dlg.GetPath()
+            if not path:
+                dlg.Destroy()
+                return
+            
+            try:
+                fobj = open(path, 'w')
+                fobj.write(mctxt)
+            finally:
+                fobj.close()
+        
+        dlg.Destroy()
+
+    def OnLoadExpression(self, event):
+        """!Load expression from file
+        """
+        dlg = wx.FileDialog(parent = self,
+                            message = _("Choose a file name to load the expression"),
+                            wildcard = _("Expression file (*)|*"),
+                            style = wx.OPEN)
+        if dlg.ShowModal() == wx.ID_OK:
+            path = dlg.GetPath()
+            if not path:
+                dlg.Destroy()
+                return
+
+            try:
+                fobj = open(path,'r')
+                mctxt = fobj.read()
+            finally:
+                fobj.close()
+            
+            try:
+                result, exp = mctxt.split('=', 1)
+            except ValueError:
+                result = ''
+                exp = mctxt
+            
+            self.newmaptxt.SetValue(result.strip())
+            self.text_mcalc.SetValue(exp.strip())
+            self.text_mcalc.SetFocus()
+            self.text_mcalc.SetInsertionPointEnd()
+        
+        dlg.Destroy()
+                
     def OnClear(self, event):
         """!Clears text area
         """



More information about the grass-commit mailing list