Bläddra i källkod

wxGUI/modeler: fix loop item definition
fix condition objects


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

Martin Landa 11 år sedan
förälder
incheckning
0d47f3913a
3 ändrade filer med 49 tillägg och 30 borttagningar
  1. 15 10
      gui/wxpython/gmodeler/dialogs.py
  2. 6 4
      gui/wxpython/gmodeler/frame.py
  3. 28 16
      gui/wxpython/gmodeler/model.py

+ 15 - 10
gui/wxpython/gmodeler/dialogs.py

@@ -534,10 +534,9 @@ class ModelConditionDialog(ModelItemDialog):
         self.listBoxElse = wx.StaticBox(parent = self.panel, id = wx.ID_ANY,
                                         label=" %s " % _("List of items in 'else' block"))
         self.itemListElse = ItemCheckListCtrl(parent = self.panel,
-                                              window = self,
-                                              columns = [_("Name"),
+                                              columns = [_("Label"),
                                                          _("Command")],
-                                              shape = shape)
+                                              shape = shape, frame = parent)
         self.itemListElse.SetName('ElseBlockList')
         self.itemListElse.Populate(self.parent.GetModel().GetItems())
         
@@ -787,6 +786,8 @@ class ItemListCtrl(ModelListCtrl):
         self.disablePopup = disablePopup
 
         ModelListCtrl.__init__(self, parent, columns, frame, **kwargs)
+        self.itemIdMap = list()
+        
         self.SetColumnWidth(1, 100)
         self.SetColumnWidth(2, 65)
         
@@ -801,16 +802,17 @@ class ItemListCtrl(ModelListCtrl):
     def Populate(self, data):
         """!Populate the list"""
         self.itemDataMap = dict()
+        self.itemIdMap = list()
         
         if self.shape:
+            items = self.frame.GetModel().GetItems(objType=ModelAction)
             if isinstance(self.shape, ModelCondition):
-                if self.GetText() == 'ElseBlockList':
-                    shapeItems = map(lambda x: x.GetId(), self.shape.GetItems()['else'])
+                if self.GetLabel() == 'ElseBlockList':
+                    shapeItems = map(lambda x: x.GetId(), self.shape.GetItems(items)['else'])
                 else:
-                    shapeItems = map(lambda x: x.GetId(), self.shape.GetItems()['if'])
+                    shapeItems = map(lambda x: x.GetId(), self.shape.GetItems(items)['if'])
             else:
-                shapeItems = map(lambda x: x.GetId(),
-                                 self.shape.GetItems(self.frame.GetModel().GetItems(objType=ModelAction)))
+                shapeItems = map(lambda x: x.GetId(), self.shape.GetItems(items))
         else:
             shapeItems = list()
         
@@ -822,6 +824,8 @@ class ItemListCtrl(ModelListCtrl):
                     action == self.shape:
                 continue
             
+            self.itemIdMap.append(action.GetId())
+            
             if len(self.columns) == 2:
                 self.itemDataMap[i] = [action.GetLabel(),
                                        action.GetLog()]
@@ -993,12 +997,13 @@ class ItemCheckListCtrl(ItemListCtrl, listmix.CheckListCtrlMixin):
         """!Get list of selected actions"""
         ids = { 'checked'   : list(),
                 'unchecked' : list() }
+        
         # action ids start at 1
         for i in range(self.GetItemCount()):
             if self.IsChecked(i):
-                ids['checked'].append(i+1)
+                ids['checked'].append(self.itemIdMap[i])
             else:
-                ids['unchecked'].append(i+1)
+                ids['unchecked'].append(self.itemIdMap[i])
             
         return ids
 

+ 6 - 4
gui/wxpython/gmodeler/frame.py

@@ -959,11 +959,12 @@ class ModelFrame(wx.Frame):
 
     def DefineCondition(self, condition):
         """!Define if-else statement with given list of items"""
-        parent = condition
-        items = condition.GetItems()
+        items = condition.GetItems(self.model.GetItems(objType=ModelAction))
         if not items['if'] and not items['else']:
             return
         
+        parent = condition
+        
         # remove defined relations first
         for rel in condition.GetRelations():
             self.canvas.GetDiagram().RemoveShape(rel)
@@ -1136,11 +1137,12 @@ class ModelEvtHandler(ogl.ShapeEvtHandler):
                 alist = list()
                 for aId in ids['unchecked']:
                     action = model.GetItem(aId, objType=ModelAction)
-                    action.UnSetBlock(shape)
+                    if action:
+                        action.UnSetBlock(shape)
                 for aId in ids['checked']:
                     action = model.GetItem(aId, objType=ModelAction)
-                    action.SetBlock(shape)
                     if action:
+                        action.SetBlock(shape)
                         alist.append(aId)
                 shape.SetItems(alist)
                 self.frame.DefineLoop(shape)

+ 28 - 16
gui/wxpython/gmodeler/model.py

@@ -1407,17 +1407,7 @@ class ModelItem(ModelObject):
         """!Abstract class for loops and conditions"""
         ModelObject.__init__(self, id, label)
         self.parent  = parent
-        self.itemIds = list() # unordered
         
-    def GetItems(self, items):
-        """!Get sorted items by id"""
-        result = list()
-        for item in items:
-            if item.GetId() in self.itemIds:
-                result.append(item)
-        
-        return result
-
     def SetId(self, id):
         """!Set loop id"""
         self.id = id
@@ -1447,7 +1437,8 @@ class ModelLoop(ModelItem, ogl.RectangleShape):
     def __init__(self, parent, x, y, id=-1, idx=-1, width = None, height = None, label = '', items = []):
         """!Defines a loop"""
         ModelItem.__init__(self, parent, x, y, id, width, height, label, items)
-        
+        self.itemIds = list() # unordered        
+
         if not width:
             width = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'width'))
         if not height:
@@ -1488,7 +1479,16 @@ class ModelLoop(ModelItem, ogl.RectangleShape):
         
     def Update(self):
         self._setBrush()
+
+    def GetItems(self, items):
+        """!Get sorted items by id"""
+        result = list()
+        for item in items:
+            if item.GetId() in self.itemIds:
+                result.append(item)
         
+        return result
+
     def SetItems(self, items):
         """!Set items (id)"""
         self.itemIds = items
@@ -1500,10 +1500,11 @@ class ModelLoop(ModelItem, ogl.RectangleShape):
         ogl.RectangleShape.OnDraw(self, dc)
 
 class ModelCondition(ModelItem, ogl.PolygonShape):
-    def __init__(self, parent, x, y, id = -1, width = None, height = None, text = '',
+    def __init__(self, parent, x, y, id = -1, width = None, height = None, label = '',
                  items = { 'if' : [], 'else' : [] }):
         """!Defines a if-else condition"""
-        ModelItem.__init__(self, parent, x, y, id, width, height, text, items)
+        ModelItem.__init__(self, parent, x, y, id, width, height, label, items)
+        self.itemIds = {'if' : [], 'else': []}
         
         if not width:
             self.width = UserSettings.Get(group='modeler', key='if-else', subkey=('size', 'width'))
@@ -1527,8 +1528,8 @@ class ModelCondition(ModelItem, ogl.PolygonShape):
             self.SetX(x)
             self.SetY(y)
             self.SetPen(wx.BLACK_PEN)
-            if text:
-                self.AddText('(' + str(self.id) + ') ' + text)
+            if label:
+                self.AddText('(' + str(self.id) + ') ' + label)
             else:
                 self.AddText('(' + str(self.id) + ')')
 
@@ -1544,6 +1545,17 @@ class ModelCondition(ModelItem, ogl.PolygonShape):
         """!Get object height"""
         return self.height
 
+    def GetItems(self, items):
+        """!Get sorted items by id"""
+        result = {'if' : [], 'else': []}
+        for item in items:
+            if item.GetId() in self.itemIds['if']:
+                result['if'].append(item)
+            elif item.GetId() in self.itemIds['else']:
+                result['else'].append(item)
+        
+        return result
+
     def SetItems(self, items, branch = 'if'):
         """!Set items (id)
 
@@ -1551,7 +1563,7 @@ class ModelCondition(ModelItem, ogl.PolygonShape):
         @param branch 'if' / 'else'
         """
         if branch in ['if', 'else']:
-            self.items[branch] = items
+            self.itemIds[branch] = items
 
 class ProcessModelFile:
     """!Process GRASS model file (gxm)"""