=== modified file 'gui/wxpython/gui_modules/mcalc_builder.py' --- gui/wxpython/gui_modules/mcalc_builder.py 2010-08-25 01:07:36 +0000 +++ gui/wxpython/gui_modules/mcalc_builder.py 2010-09-21 23:26:29 +0000 @@ -14,6 +14,8 @@ @author Michael Barton, Arizona State University @author Martin Landa """ +#TDOD: load expression functionaliy +#TODO: add to layer tree import os import sys @@ -23,9 +25,11 @@ if not os.getenv("GRASS_WXBUNDLED"): globalvar.CheckForWx() import wx +import wx.aui import gcmd import gselect +import gdialogs import menuform try: import subprocess @@ -136,6 +140,13 @@ self.btn_cmd = wx.Button(parent = self.panel, id = wx.ID_ANY, label = _("Command dialog")) self.btn_cmd.SetToolTipString(_('Open %s dialog') % self.cmd) + self.btn_save = wx.Button(parent = self.panel, id = wx.ID_ANY, + label = _("Save")) + self.btn_load = wx.Button(parent = self.panel, id = wx.ID_ANY, + label = _("Load")) + self.btn_load.SetToolTipString(_('Load %s from file') % _('Expression')) + + self.btn = dict() self.btn['pow'] = wx.Button(parent = self.panel, id = wx.ID_ANY, label = "^") @@ -216,6 +227,8 @@ self.overwrite = wx.CheckBox(parent = self.panel, id = wx.ID_ANY, label=_("Allow output files to overwrite existing files")) + self.addbox = wx.CheckBox(parent = self.panel, + label = _('Add created map into layer tree'), style = wx.NO_BORDER) self.overwrite.SetValue(UserSettings.Get(group='cmd', key='overwrite', subkey='enabled')) # @@ -229,7 +242,8 @@ self.btn_run.Bind(wx.EVT_BUTTON, self.OnMCalcRun) self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp) self.btn_cmd.Bind(wx.EVT_BUTTON, self.OnCmdDialog) - + self.btn_save.Bind(wx.EVT_BUTTON, self.OnSaveDialog) + self.btn_load.Bind(wx.EVT_BUTTON, self.OnLoadDialog) self.mapselect.Bind(wx.EVT_TEXT, self.OnSelect) self.function.Bind(wx.EVT_COMBOBOX, self.OnSelect) self.function.Bind(wx.EVT_TEXT_ENTER, self.OnSelect) @@ -298,6 +312,10 @@ flag = wx.ALL, border = 5) buttonSizer4.Add(item = self.btn_close, flag = wx.ALL, border = 5) + buttonSizer4.Add(item = self.btn_save, + flag = wx.ALL, border = 5) + buttonSizer4.Add(item = self.btn_load, + flag = wx.ALL, border = 5) buttonSizer4.Add(item = self.btn_run, flag = wx.ALL, border = 5) buttonSizer4.Add(item = self.btn_help, @@ -329,6 +347,11 @@ sizer.Add(item = self.overwrite, proportion = 0, flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5) + sizer.Add(item = self.addbox, proportion = 0, + flag = wx.EXPAND | wx.LEFT | wx.RIGHT, + border = 5) + #~ sizer.Add(item=self.addbox, proportion=0, + #~ flag=wx.EXPAND | wx.ALL, border=1) sizer.Add(item = buttonSizer4, proportion = 0, flag = wx.ALL | wx.ALIGN_RIGHT, border = 1) @@ -339,6 +362,16 @@ self.Fit() self.Layout() + #~ self.addbox = wx.CheckBox(parent = self.panel, + #~ label = _('Add created map into layer tree'), style = wx.NO_BORDER) + disableAdd=False + if disableAdd: + self.addbox.SetValue(True) + self.addbox.Enable(False) + else: + self.addbox.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled')) + + def AddMark(self,event): """!Sends operators to insertion method """ @@ -434,6 +467,25 @@ gcmd.RunCommand(self.cmd, expression = "%s=%s" % (name, mctxt), overwrite = overwrite) + + #~ if not self.addbox.IsChecked(): + #~ return + #~ + #~ maptree = self.parent.curr_page.maptree + #~ + #~ layer, output = self.list.GetLayers()[self.commandId] + #~ + #~ if '@' not in output: + #~ name = output + '@' + grass.gisenv()['MAPSET'] + #~ else: + #~ name = output + #~ # add imported layers into layer tree + #~ cmd = ['d.rast', 'map=%s' % name] + #~ if UserSettings.Get(group='cmd', key='rasterOpaque', subkey='enabled'): + #~ cmd.append('-n') + #~ item = maptree.AddLayer(ltype='raster', lname=name, lcmd=cmd) +#~ + #~ maptree.mapdisplay.MapWindow.ZoomToMap() def OnClear(self, event): """!Clears text area @@ -448,6 +500,95 @@ def OnClose(self,event): """!Close window""" self.Destroy() + + def OnSaveDialog(self, event): + """saves expresseion to file + """ + + mctxt = self.text_mcalc.GetValue().strip().replace("\n"," ") + mctxt = mctxt.replace(" " , "") + + #dialog + filetype = "Expression file (*.exp)|*.exp|Text file (*.txt)|*.txt" + ltype = [{ 'ext' : 'exp', 'type' : -1 }, + { 'ext' : 'tif', 'type' : wx.BITMAP_TYPE_TIF }] + + # get filename + dlg = wx.FileDialog(parent = self, + message = _("Choose a file name to save the expression"), + wildcard = filetype, + style=wx.SAVE | wx.FD_OVERWRITE_PROMPT) + if dlg.ShowModal() == wx.ID_OK: + path = dlg.GetPath() + if not path: + dlg.Destroy() + return + + base, ext = os.path.splitext(path) + fileType = ltype[dlg.GetFilterIndex()]['type'] + #extType = ltype[dlg.GetFilterIndex()]['ext'] + suffix = '_mcalc' + extType = 'exp' + + if ext != extType: + path = base + suffix + '.' + extType + + fobj = open(path,'w') + fobj.write(mctxt) + fobj.close() + #self.MapWindow.SaveToFile(path, fileType, + # width, height) + + dlg.Destroy() + + + + def OnLoadDialog(self, event): + """load expresseion from file + """ + + #mctxt = self.text_mcalc.GetValue().strip().replace("\n"," ") + #mctxt = mctxt.replace(" " , "") + + #dialog + filetype = "Expression file (*.exp)|*.exp|Text file (*.txt)|*.txt" + ltype = [{ 'ext' : 'exp', 'type' : -1 }, + { 'ext' : 'tif', 'type' : wx.BITMAP_TYPE_TIF }] + + # get filename + dlg = wx.FileDialog(parent = self, + message = _("Choose a file name to load the expression"), + wildcard = filetype, + style=wx.OPEN) + if dlg.ShowModal() == wx.ID_OK: + path = dlg.GetPath() + print path + if not path: + dlg.Destroy() + return + + base, ext = os.path.splitext(path) + fileType = ltype[dlg.GetFilterIndex()]['type'] + #extType = ltype[dlg.GetFilterIndex()]['ext'] + suffix = '_mcalc' + extType = 'exp' + + #if ext != extType: + # path = base + suffix + '.' + extType + + fobj = open(path,'r') + mctxt = fobj.read() + fobj.close() + print mctxt + self.text_mcalc.SetValue(mctxt) + self.text_mcalc.Update() + #self.MapWindow.SaveToFile(path, fileType, + # width, height) + + dlg.Destroy() + + + def OnCmdDialog(self, event): """!Shows command dialog"""