瀏覽代碼

wxGUI/modeler: https://trac.osgeo.org/grass/ticket/1448 (run model menu item does not seem to work)
run model from the menu


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@48342 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 13 年之前
父節點
當前提交
1574d27e73
共有 2 個文件被更改,包括 111 次插入114 次删除
  1. 106 97
      gui/wxpython/gui_modules/gmodeler.py
  2. 5 17
      gui/wxpython/wxgui.py

+ 106 - 97
gui/wxpython/gui_modules/gmodeler.py

@@ -408,13 +408,108 @@ class Model(object):
         
         
         return errList
         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
                 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):
     def DeleteIntermediateData(self, log):
         """!Detele intermediate data"""
         """!Detele intermediate data"""
@@ -928,86 +1023,7 @@ class ModelFrame(wx.Frame):
         
         
     def OnRunModel(self, event):
     def OnRunModel(self, event):
         """!Run entire model"""
         """!Run entire model"""
-        if self.model.GetNumItems() < 1:
-            GMessage(parent = self, 
-                     message = _('Model is empty. Nothing to run.'))
-            return
-        
-        # 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)
+        self.model.Run(self.goutput, self.OnDone, parent = self)
         
         
     def OnDone(self, cmd, returncode):
     def OnDone(self, cmd, returncode):
         """!Computation finished"""
         """!Computation finished"""
@@ -1020,7 +1036,10 @@ class ModelFrame(wx.Frame):
                      message = _('Model is empty. Nothing to validate.'))
                      message = _('Model is empty. Nothing to validate.'))
             return
             return
         
         
-        errList = self._validateModel()
+        
+        self.SetStatusText(_('Validating model...'), 0)
+        errList = self.model.Validate()
+        self.SetStatusText('', 0)
         
         
         if errList:
         if errList:
             GWarning(parent = self,
             GWarning(parent = self,
@@ -1133,16 +1152,6 @@ class ModelFrame(wx.Frame):
         
         
         self.SetStatusText(_("Model exported to <%s>") % filename)
         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):
     def OnDefineRelation(self, event):
         """!Define relation between data and action items"""
         """!Define relation between data and action items"""
         self.canvas.SetCursor(self.cursors["cross"])
         self.canvas.SetCursor(self.cursors["cross"])

+ 5 - 17
gui/wxpython/wxgui.py

@@ -400,33 +400,21 @@ class GMFrame(wx.Frame):
     def OnRunModel(self, event):
     def OnRunModel(self, event):
         """!Run model"""
         """!Run model"""
         filename = ''
         filename = ''
-        dlg = wx.FileDialog(parent = self, message=_("Choose model to run"),
+        dlg = wx.FileDialog(parent = self, message =_("Choose model to run"),
                             defaultDir = os.getcwd(),
                             defaultDir = os.getcwd(),
-                            wildcard=_("GRASS Model File (*.gxm)|*.gxm"))
+                            wildcard = _("GRASS Model File (*.gxm)|*.gxm"))
         if dlg.ShowModal() == wx.ID_OK:
         if dlg.ShowModal() == wx.ID_OK:
             filename = dlg.GetPath()
             filename = dlg.GetPath()
         
         
         if not filename:
         if not filename:
+            dlg.Destroy()
             return
             return
         
         
         self.model = gmodeler.Model()
         self.model = gmodeler.Model()
         self.model.LoadModel(filename)
         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):
     def OnMapsets(self, event):
         """!Launch mapset access dialog
         """!Launch mapset access dialog