[GRASS-SVN] r48342 - in grass/trunk/gui/wxpython: . gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Sep 18 08:06:36 EDT 2011


Author: martinl
Date: 2011-09-18 05:06:36 -0700 (Sun, 18 Sep 2011)
New Revision: 48342

Modified:
   grass/trunk/gui/wxpython/gui_modules/gmodeler.py
   grass/trunk/gui/wxpython/wxgui.py
Log:
wxGUI/modeler: #1448 (run model menu item does not seem to work)
	       run model from the menu


Modified: grass/trunk/gui/wxpython/gui_modules/gmodeler.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gmodeler.py	2011-09-18 08:13:55 UTC (rev 48341)
+++ grass/trunk/gui/wxpython/gui_modules/gmodeler.py	2011-09-18 12:06:36 UTC (rev 48342)
@@ -408,14 +408,109 @@
         
         return errList
 
-    def Run(self, log, onDone):
-        """!Run model"""
-        for action in self.GetItems(objType = ModelAction):
-            if not action.IsEnabled():
+    def RunAction(self, item, params, log, onDone, statusbar = None):
+        """!Run given action
+
+        @param item action item
+        @param params parameters dict
+        @param log logging window
+        @param onDone on-done method
+        @param statusbar wx.StatusBar instance or None
+        """
+        name = item.GetName()
+        if name in params:
+            paramsOrig = item.GetParams(dcopy = True)
+            item.MergeParams(params[name])
+        
+        if statusbar:
+            statusbar.SetStatusText(_('Running model...'), 0)
+        log.RunCmd(command = item.GetLog(string = False),
+                   onDone = onDone)
+        
+        if name in params:
+            item.SetParams(paramsOrig)
+        
+    def Run(self, log, onDone, parent = None):
+        """!Run model
+
+        @param log logging window (see goutput.GMConsole)
+        @param onDone on-done method
+        @param parent window for messages or None
+        """
+        if self.GetNumItems() < 1:
+            GMessage(parent = parent,
+                     message = _('Model is empty. Nothing to run.'))
+            return
+        
+        statusbar = None
+        if isinstance(parent, wx.Frame):
+            statusbar = parent.GetStatusBar()
+        
+        # validation
+        if statusbar:
+            statusbar.SetStatusText(_('Validating model...'), 0)
+        errList = self.Validate()
+        if statusbar:
+            statusbar.SetStatusText('', 0)
+        if errList:
+            dlg = wx.MessageDialog(parent = parent,
+                                   message = _('Model is not valid. Do you want to '
+                                               'run the model anyway?\n\n%s') % '\n'.join(errList),
+                                   caption = _("Run model?"),
+                                   style = wx.YES_NO | wx.NO_DEFAULT |
+                                   wx.ICON_QUESTION | wx.CENTRE)
+            ret = dlg.ShowModal()
+            if ret != wx.ID_YES:
+                return
+        
+        # parametrization
+        params = self.Parameterize()
+        if params:
+            dlg = ModelParamDialog(parent = parent,
+                                   params = params)
+            dlg.CenterOnParent()
+            
+            ret = dlg.ShowModal()
+            if ret != wx.ID_OK:
+                dlg.Destroy()
+                return
+            
+            err = dlg.GetErrors()
+            if err:
+                GError(parent = self, message = unicode('\n'.join(err)))
+                return
+        
+        log.cmdThread.SetId(-1)
+        for item in self.GetItems():
+            if not item.IsEnabled():
                 continue
-            log.RunCmd(command = action.GetLog(string = False),
-                       onDone = onDone)
+            if isinstance(item, ModelAction):
+                if item.GetBlockId():
+                    continue
+                self.RunAction(item, params, log, onDone)
+            elif isinstance(item, ModelLoop):
+                cond = item.GetText()
+                # substitute variables in condition
+                variables = self.GetVariables()
+                for variable in variables:
+                    pattern = re.compile('%' + variable)
+                    if pattern.search(cond):
+                        value = variables[variable].get('value', '')
+                        vtype = variables[variable].get('type', 'string')
+                        if vtype == 'string':
+                            value = '"' + value + '"'
+                        cond = pattern.sub(value, cond)
+                # split condition
+                condVar, condText = re.split('\s*in\s*', cond)
+                
+                for action in item.GetItems():
+                    for vars()[condVar] in eval(condText):
+                        if isinstance(action, ModelAction):
+                            self.RunAction(action, params, log, onDone)
         
+        if params:
+            dlg.Destroy()
+        
     def DeleteIntermediateData(self, log):
         """!Detele intermediate data"""
         rast, vect, rast3d, msg = self.GetIntermediateData()
@@ -928,87 +1023,8 @@
         
     def OnRunModel(self, event):
         """!Run entire model"""
-        if self.model.GetNumItems() < 1:
-            GMessage(parent = self, 
-                     message = _('Model is empty. Nothing to run.'))
-            return
+        self.model.Run(self.goutput, self.OnDone, parent = self)
         
-        # validation
-        errList = self._validateModel()
-        if errList:
-            dlg = wx.MessageDialog(parent = self,
-                                   message = _('Model is not valid. Do you want to '
-                                               'run the model anyway?\n\n%s') % '\n'.join(errList),
-                                   caption=_("Run model?"),
-                                   style = wx.YES_NO | wx.NO_DEFAULT |
-                                   wx.ICON_QUESTION | wx.CENTRE)
-            ret = dlg.ShowModal()
-            if ret != wx.ID_YES:
-                return
-        
-        # parametrization
-        params = self.model.Parameterize()
-        if params:
-            dlg = ModelParamDialog(parent = self,
-                                   params = params)
-            dlg.CenterOnParent()
-            
-            ret = dlg.ShowModal()
-            if ret != wx.ID_OK:
-                dlg.Destroy()
-                return
-        
-            err = dlg.GetErrors()
-            if err:
-                GError(parent = self,
-                       message = unicode('\n'.join(err)))
-                return
-        
-        self.goutput.cmdThread.SetId(-1)
-        for item in self.model.GetItems():
-            if not item.IsEnabled():
-                continue
-            if isinstance(item, ModelAction):
-                if item.GetBlockId():
-                    continue
-                self._runAction(item, params)
-            elif isinstance(item, ModelLoop):
-                cond = item.GetText()
-                # substitute variables in condition
-                variables = self.model.GetVariables()
-                for variable in variables:
-                    pattern = re.compile('%' + variable)
-                    if pattern.search(cond):
-                        value = variables[variable].get('value', '')
-                        vtype = variables[variable].get('type', 'string')
-                        if vtype == 'string':
-                            value = '"' + value + '"'
-                        cond = pattern.sub(value, cond)
-                # split condition
-                condVar, condText = re.split('\s*in\s*', cond)
-                
-                for action in item.GetItems():
-                    for vars()[condVar] in eval(condText):
-                        if isinstance(action, ModelAction):
-                            self._runAction(action, params)
-        
-        if params:
-            dlg.Destroy()
-        
-    def _runAction(self, item, params):
-        """!Run given action"""
-        name = item.GetName()
-        if name in params:
-            paramsOrig = item.GetParams(dcopy = True)
-            item.MergeParams(params[name])
-            
-        self.SetStatusText(_('Running model...'), 0)
-        self.goutput.RunCmd(command = item.GetLog(string = False),
-                            onDone = self.OnDone)
-            
-        if name in params:
-            item.SetParams(paramsOrig)
-        
     def OnDone(self, cmd, returncode):
         """!Computation finished"""
         self.SetStatusText('', 0)
@@ -1020,8 +1036,11 @@
                      message = _('Model is empty. Nothing to validate.'))
             return
         
-        errList = self._validateModel()
         
+        self.SetStatusText(_('Validating model...'), 0)
+        errList = self.model.Validate()
+        self.SetStatusText('', 0)
+        
         if errList:
             GWarning(parent = self,
                      message = _('Model is not valid.\n\n%s') % '\n'.join(errList))
@@ -1133,16 +1152,6 @@
         
         self.SetStatusText(_("Model exported to <%s>") % filename)
 
-    def _validateModel(self):
-        """!Validate model"""
-        self.SetStatusText(_('Validating model...'), 0)
-        
-        errList = self.model.Validate()
-        
-        self.SetStatusText('', 0)
-        
-        return errList
-    
     def OnDefineRelation(self, event):
         """!Define relation between data and action items"""
         self.canvas.SetCursor(self.cursors["cross"])

Modified: grass/trunk/gui/wxpython/wxgui.py
===================================================================
--- grass/trunk/gui/wxpython/wxgui.py	2011-09-18 08:13:55 UTC (rev 48341)
+++ grass/trunk/gui/wxpython/wxgui.py	2011-09-18 12:06:36 UTC (rev 48342)
@@ -400,33 +400,21 @@
     def OnRunModel(self, event):
         """!Run model"""
         filename = ''
-        dlg = wx.FileDialog(parent = self, message=_("Choose model to run"),
+        dlg = wx.FileDialog(parent = self, message =_("Choose model to run"),
                             defaultDir = os.getcwd(),
-                            wildcard=_("GRASS Model File (*.gxm)|*.gxm"))
+                            wildcard = _("GRASS Model File (*.gxm)|*.gxm"))
         if dlg.ShowModal() == wx.ID_OK:
             filename = dlg.GetPath()
         
         if not filename:
+            dlg.Destroy()
             return
         
         self.model = gmodeler.Model()
         self.model.LoadModel(filename)
-        self.SetStatusText(_('Validating model...'), 0)
-        result =  self.model.Validate()
-        if result:
-            dlg = wx.MessageDialog(parent = self,
-                                   message = _('Model is not valid. Do you want to '
-                                               'run the model anyway?\n\n%s') % '\n'.join(errList),
-                                   caption=_("Run model?"),
-                                   style = wx.YES_NO | wx.NO_DEFAULT |
-                                   wx.ICON_QUESTION | wx.CENTRE)
-            ret = dlg.ShowModal()
-            if ret != wx.ID_YES:
-                return
+        self.model.Run(log = self.goutput, onDone = self.OnDone, parent = self)
         
-        self.SetStatusText(_('Running model...'), 0)
-        self.model.Run(log = self.goutput,
-                       onDone = self.OnDone)
+        dlg.Destroy()
         
     def OnMapsets(self, event):
         """!Launch mapset access dialog



More information about the grass-commit mailing list