[GRASS-SVN] r42750 - in grass/branches/develbranch_6/gui/wxpython: gui_modules xml

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 9 17:35:44 EDT 2010


Author: martinl
Date: 2010-07-09 21:35:44 +0000 (Fri, 09 Jul 2010)
New Revision: 42750

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py
   grass/branches/develbranch_6/gui/wxpython/gui_modules/preferences.py
   grass/branches/develbranch_6/gui/wxpython/xml/menudata_modeler.xml
Log:
wxGUI/modeler: conditions implemented (initial)
(merge r42749 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py	2010-07-09 21:02:13 UTC (rev 42749)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py	2010-07-09 21:35:44 UTC (rev 42750)
@@ -25,6 +25,7 @@
  - ModelLoopDialog
  - ActionPanel
  - ActionListCtrl
+ - ModelCondition
 
 (C) 2010 by the GRASS Development Team
 This program is free software under the GNU General Public License
@@ -304,7 +305,7 @@
                                  actions = alist,
                                  id = loop['id'])
             
-            for action in loopItem.GetActions():
+            for action in loopItem.GetItems():
                 action.SetLoop(loopItem)
             
             self.AddItem(loopItem)
@@ -895,7 +896,7 @@
                 # split condition
                 condVar, condText = re.split('\s*in\s*', cond)
                 
-                for action in item.GetActions():
+                for action in item.GetItems():
                     for vars()[condVar] in eval(condText):
                         self._runAction(action, params)
         
@@ -1101,14 +1102,28 @@
             fd.write('    pass\n')
         
         fd.write("\ndef main():\n")
+        indent = 4
         for item in self.model.GetItems():
             if isinstance(item, ModelAction):
-                self._writePythonAction(fd, item)
+                if item.GetLoopId():
+                    continue
+                self._writePythonAction(fd, item, indent)
             elif isinstance(item, ModelLoop):
-                # for action in item.GetActions():
-                #     fd.write('for %s:\n' % item.GetText())
-                #     self._writePythonAction(fd, action)
-                pass
+                # substitute conditiond
+                variables = self.model.GetVariables()
+                cond = item.GetText()
+                for variable in variables:
+                    pattern= re.compile('%' + variable)
+                    if pattern.search(cond):
+                        value = variables[variable].get('value', '')
+                        if variables[variable].get('type', 'string') == 'string':
+                            value = '"' + value + '"'
+                        cond = pattern.sub(value, cond)
+                fd.write('%sfor %s:\n' % (' ' * indent, cond))
+                indent += 4
+                for action in item.GetItems():
+                    self._writePythonAction(fd, action, indent)
+                indent -= 4
         
         fd.write("\n    return 0\n")
         
@@ -1120,14 +1135,14 @@
     sys.exit(main())
 """)
         
-    def _writePythonAction(self, fd, item):
+    def _writePythonAction(self, fd, item, indent):
         task = menuform.GUI().ParseCommand(cmd = item.GetLog(string = False),
                                            show = None)
         opts = task.get_options()
         flags = ''
         params = list()
-        strcmd = "    grass.run_command("
-        indent = len(strcmd)
+        strcmd = "%sgrass.run_command(" % (' ' * indent)
+        cmdIndent = len(strcmd)
         for f in opts['flags']:
             if f.get('value', False) == True:
                 name = f.get('name', '')
@@ -1148,12 +1163,12 @@
         
         fd.write(strcmd + '"%s"' % task.get_name())
         if flags:
-            fd.write(",\n%sflags = '%s'" % (' ' * indent, flags))
+            fd.write(",\n%sflags = '%s'" % (' ' * cmdIndent, flags))
         if len(params) > 0:
             fd.write(",\n")
             for opt in params[:-1]:
-                fd.write("%s%s,\n" % (' ' * indent, opt))
-            fd.write("%s%s)\n" % (' ' * indent, params[-1]))
+                fd.write("%s%s,\n" % (' ' * cmdIndent, opt))
+            fd.write("%s%s)\n" % (' ' * cmdIndent, params[-1]))
         else:
             fd.write(")\n")
         
@@ -1178,14 +1193,29 @@
         self.ModelChanged()
         
         width, height = self.canvas.GetSize()
-        loop = ModelLoop(self, x = width/2, y = height/2)
+        loop = ModelLoop(self, x = width/2, y = height/2,
+                         id = len(self.model.GetActions()) + 1)
         self.canvas.diagram.AddShape(loop)
         loop.Show(True)
         
         self._addEvent(loop)
         
         self.canvas.Refresh()
-
+        
+    def OnDefineCondition(self, event):
+        """!Define new condition in the model"""
+        self.ModelChanged()
+        
+        width, height = self.canvas.GetSize()
+        cond = ModelCondition(self, x = width/2, y = height/2,
+                              id = len(self.model.GetActions()) + 1)
+        self.canvas.diagram.AddShape(cond)
+        loop.Show(True)
+        
+        self._addEvent(cond)
+        
+        self.canvas.Refresh()
+    
     def OnAddAction(self, event):
         """!Add action to model"""
         if self.searchDialog is None:
@@ -1467,17 +1497,17 @@
     def DefineLoop(self, loop):
         """!Define loop with given list of actions"""
         parent = loop
-        actions = loop.GetActions()
+        actions = loop.GetItems()
         if not actions:
             return
         
-        for action in loop.GetActions():
+        for action in loop.GetItems():
             rel = ModelRelation(parent, action)
             self.AddLine(rel)
             parent = action
         
         # close loop
-        action = loop.GetActions()[-1]
+        action = loop.GetItems()[-1]
         rel = ModelRelation(action, loop)
         self.AddLine(rel)
         dx = loop.GetWidth() / 2 + 50
@@ -2089,7 +2119,7 @@
                 alist = list()
                 for aId in dlg.GetActions():
                     alist.append(self.frame.GetModel().GetAction(aId))
-                shape.SetActions(alist)
+                shape.SetItems(alist)
                 self.frame.DefineLoop(shape)
             self.frame.GetCanvas().Refresh()
             
@@ -2829,7 +2859,7 @@
         if text:
             self.fd.write('%s<condition>%s</condition>\n' %
                           (' ' * self.indent, self._filterValue(text)))
-        for action in loop.GetActions():
+        for action in loop.GetItems():
             self.fd.write('%s<loop-action>%d</loop-action>\n' %
                           (' ' * self.indent, action.GetId()))
         self.indent -= 4
@@ -3631,7 +3661,7 @@
         menu.Destroy()
         
 class ModelLoop(ModelObject, ogl.RectangleShape):
-    def __init__(self, parent, x, y, id = -1, width = None, height = None, text = None, actions = []):
+    def __init__(self, parent, x, y, id = -1, width = None, height = None, text = None, items = []):
         """!Defines a loop"""
         ModelObject.__init__(self, id)
         
@@ -3641,7 +3671,7 @@
             width = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'width'))
         if not height:
             height = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'height'))
-        self.actions = actions # list of actions in the loop
+        self.items = items # list of items in the loop
         
         if self.parent.GetCanvas():
             ogl.RectangleShape.__init__(self, width, height)
@@ -3651,23 +3681,26 @@
             self.SetY(y)
             self.SetPen(wx.BLACK_PEN)
             self.SetCornerRadius(100)
-            self.AddText('(' + str(self.id) + ') ' + text)
+            if text:
+                self.AddText('(' + str(self.id) + ') ' + text)
+            else:
+                self.AddText('(' + str(self.id) + ')')
         
     def GetText(self):
         """!Get loop text"""
         return self.text
 
-    def GetActions(self):
-        """!Get actions (id)"""
-        return self.actions
+    def GetItems(self):
+        """!Get items (id)"""
+        return self.items
 
     def SetId(self, id):
         """!Set loop id"""
         self.id = id
 
-    def SetActions(self, actions):
-        """!Set actions (id)"""
-        self.actions = actions
+    def SetItems(self, items):
+        """!Set items (id)"""
+        self.items = items
 
     def SetText(self, cond):
         """!Set loop text (condition)"""
@@ -3691,7 +3724,7 @@
         self.condText = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY,
                                     value = shape.GetText())
         self.listBox = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
-                                    label=" %s " % _("List of actions in loop"))
+                                    label=" %s " % _("List of items in loop"))
         self.actionList = ActionCheckListCtrl(parent = self.panel,
                                               columns = [_("ID"), _("Name"),
                                                          _("Command")])
@@ -3954,7 +3987,35 @@
     def OnBeginEdit(self, event):
         """!Disable editing"""
         event.Veto()
+
+class ModelCondition(ModelObject, ogl.PolygonShape):
+    def __init__(self, parent, x, y, id = -1, width = None, height = None, text = None, items = []):
+        """!Defines a condition"""
+        ModelObject.__init__(self, id)
         
+        self.parent  = parent
+        self.text    = text
+        if not width:
+            width = UserSettings.Get(group='modeler', key='condition', subkey=('size', 'width'))
+        if not height:
+            height = UserSettings.Get(group='modeler', key='condition', subkey=('size', 'height'))
+        self.items = items # list of items in the loop
+        
+        if self.parent.GetCanvas():
+            ogl.PolygonShape.__init__(self)
+            points = [(x, y - height / 2),
+                      (x + width / 2, y),
+                      (x, y + height / 2),
+                      (x - width / 2, y)]
+            self.Create(points)
+            
+            self.SetCanvas(self.parent)
+            self.SetPen(wx.BLACK_PEN)
+            if text:
+                self.AddText('(' + str(self.id) + ') ' + text)
+            else:
+                self.AddText('(' + str(self.id) + ')')
+        
 def main():
     app = wx.PySimpleApp()
     wx.InitAllImageHandlers()

Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/preferences.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/preferences.py	2010-07-09 21:02:13 UTC (rev 42749)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/preferences.py	2010-07-09 21:35:44 UTC (rev 42750)
@@ -567,6 +567,12 @@
                         'height' : 50,
                         },
                     },
+                'condition' : {
+                    'size' : {
+                        'width' : 100,
+                        'height' : 50,
+                        },
+                    },
                 },
             }
         

Modified: grass/branches/develbranch_6/gui/wxpython/xml/menudata_modeler.xml
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/xml/menudata_modeler.xml	2010-07-09 21:02:13 UTC (rev 42749)
+++ grass/branches/develbranch_6/gui/wxpython/xml/menudata_modeler.xml	2010-07-09 21:35:44 UTC (rev 42750)
@@ -41,6 +41,7 @@
 	  <label>Export to Python</label>
 	  <help>Export model to Python script</help>
 	  <handler>OnExportPython</handler>
+	  <shortcut>Ctrl+E</shortcut>
 	</menuitem>
 	<separator />
 	<menuitem>
@@ -83,11 +84,17 @@
 	</menuitem>
 	<menuitem>
 	  <label>Add loop</label>
-	  <help>Adds loop to model</help>
+	  <help>Adds loop (for) to model</help>
 	  <handler>OnDefineLoop</handler>
 	  <shortcut>Ctrl+L</shortcut>
 	</menuitem>
 	<menuitem>
+	  <label>Add condition</label>
+	  <help>Adds condition (if/else) to model</help>
+	  <handler>OnDefineCondition</handler>
+	  <shortcut>Ctrl+I</shortcut>
+	</menuitem>
+	<menuitem>
 	  <label>Remove item</label>
 	  <help>Remove action/data from model</help>
 	  <handler>OnRemoveItem</handler>



More information about the grass-commit mailing list