[GRASS-SVN] r42349 - grass/branches/develbranch_6/gui/wxpython/gui_modules

svn_grass at osgeo.org svn_grass at osgeo.org
Tue May 25 11:57:35 EDT 2010


Author: martinl
Date: 2010-05-25 11:57:35 -0400 (Tue, 25 May 2010)
New Revision: 42349

Modified:
   grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py
Log:
wxGUI/modeler: save properties in gxm
(merge r42348 from trunk)


Modified: grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py
===================================================================
--- grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py	2010-05-25 15:54:46 UTC (rev 42348)
+++ grass/branches/develbranch_6/gui/wxpython/gui_modules/gmodeler.py	2010-05-25 15:57:35 UTC (rev 42349)
@@ -68,8 +68,12 @@
 class Model(object):
     """!Class representing the model"""
     def __init__(self, canvas = None):
-        self.actions = list()    # list of recorded actions
-        self.data    = list()    # list of recorded data items
+        self.actions    = list()    # list of recorded actions
+        self.data       = list()    # list of recorded data items
+        # model properties
+        self.properties = { 'name'        : '',
+                            'description' : '',
+                            'author'      : getpass.getuser() }
         
         self.canvas  = canvas
         
@@ -80,7 +84,11 @@
     def GetActions(self):
         """!Return list of actions"""
         return self.actions
-
+    
+    def GetProperties(self):
+        """!Get model properties"""
+        return self.properties
+    
     def GetData(self):
         """!Return list of data"""
         return self.data
@@ -137,6 +145,9 @@
         except StandardError, e:
             raise GError(e)
         
+        # load properties
+        self.properties = gxmXml.properties
+        
         # load model.GetActions()
         for action in gxmXml.actions:
             actionItem = ModelAction(parent = self, 
@@ -159,7 +170,7 @@
                     break
             actionItem.SetValid(valid)
         
-        # load data & connections
+        # load data & relations
         for data in gxmXml.data:
             dataItem = ModelData(parent = self, 
                                  x = data['pos'][0],
@@ -307,7 +318,6 @@
         self.baseTitle = title
         self.modelFile = None    # loaded model
         self.modelChanged = False
-        self.properties = None
         
         self.cursors = {
             "default" : wx.StockCursor(wx.CURSOR_ARROW),
@@ -412,13 +422,9 @@
         """!Model properties dialog"""
         dlg = PropertiesDialog(parent = self)
         dlg.CentreOnParent()
-        if self.properties:
-            dlg.Init(self.properties)
-        else:
-            dlg.Init({ 'name' : '',
-                       'desc' : '',
-                       'author' : getpass.getuser() })
+        dlg.Init(self.model.GetProperties())
         if dlg.ShowModal() == wx.ID_OK:
+            self.ModelChanged()
             self.properties = dlg.GetValues()
             for action in self.model.GetActions():
                 action.GetTask().set_flag('overwrite', self.properties['overwrite'])
@@ -782,9 +788,9 @@
         if self.properties:
             properties = self.properties
         else:
-            properties = { 'name' : _("Graphical modeler script"),
-                           'desc' : _("Script generated by wxGUI Graphical Modeler"),
-                           'author' : getpass.getuser() }
+            properties = { 'name'        : _("Graphical modeler script"),
+                           'description' : _("Script generated by wxGUI Graphical Modeler"),
+                           'author'      : getpass.getuser() }
         
         fd.write(
 r"""#!/usr/bin/env python
@@ -802,7 +808,7 @@
 #############################################################################
 """ % (properties['name'],
        properties['author'],
-       properties['desc'],
+       properties['description'],
        time.asctime()))
         
         fd.write(
@@ -916,6 +922,8 @@
         # add action to canvas
         width, height = self.canvas.GetSize()
         action = ModelAction(self, cmd = cmd, x = width/2, y = height/2)
+        action.GetTask().set_flag('overwrite', self.properties['overwrite'])
+        
         self.canvas.diagram.AddShape(action)
         action.Show(True)
 
@@ -1117,7 +1125,8 @@
         try:
             WriteModelFile(fd = tmpfile,
                            actions = self.model.GetActions(),
-                           data = self.model.GetData())
+                           data = self.model.GetData(),
+                           properties = self.properties)
         except StandardError:
             GMessage(parent = self,
                      message = _("Writing current settings to model file failed."))
@@ -1797,11 +1806,13 @@
         """
         self.tree = tree
         self.root = self.tree.getroot()
-
+        
         # list of actions, data
+        self.properties = dict()
         self.actions = list()
         self.data    = list()
         
+        self._processProperties()
         self._processActions()
         self._processData()
         
@@ -1826,6 +1837,28 @@
         
         return default
     
+    def _processProperties(self):
+        """!Process model properties"""
+        node = self.root.find('properties')
+        if node is None:
+            return
+        self._processProperty(node, 'name')
+        self._processProperty(node, 'description')
+        self._processProperty(node, 'author')
+        
+        for f in node.findall('flag'):
+            name = f.get('name', '')
+            if name == 'overwrite':
+                self.properties['overwrite'] = True
+        
+    def _processProperty(self, pnode, name):
+        """!Process given property"""
+        node = pnode.find(name)
+        if node is not None:
+            self.properties[name] = node.text
+        else:
+            self.properties[name] = ''
+            
     def _processActions(self):
         """!Process model file"""
         for action in self.root.findall('action'):
@@ -1948,10 +1981,11 @@
     
 class WriteModelFile:
     """!Generic class for writing model file"""
-    def __init__(self, fd, actions, data):
+    def __init__(self, fd, actions, data, properties):
         self.fd      = fd
         self.actions = actions
         self.data    = data
+        self.properties = properties
         
         self.indent = 0
         
@@ -1983,6 +2017,20 @@
         """!Write actions"""
         id = 1
         self.indent += 4
+        self.fd.write('%s<properties>\n' % (' ' * self.indent))
+        self.indent += 4
+        if self.properties['name']:
+            self.fd.write('%s<name>%s</name>\n' % (' ' * self.indent, self.properties['name']))
+        if self.properties['description']:
+            self.fd.write('%s<description>%s</description>\n' % (' ' * self.indent, self.properties['description']))
+        if self.properties['author']:
+            self.fd.write('%s<author>%s</author>\n' % (' ' * self.indent, self.properties['author']))
+        
+        if self.properties.has_key('overwrite') and \
+                self.properties['overwrite']:
+            self.fd.write('%s<flag name="overwrite" />\n' % (' ' * self.indent))
+        self.indent -= 4
+        self.fd.write('%s</properties>\n' % (' ' * self.indent))
         for action in self.actions:
             action.SetId(id)
             self.fd.write('%s<action id="%d" name="%s" pos="%d,%d" size="%d,%d">\n' % \
@@ -2059,8 +2107,8 @@
                         self.fd.write('%s<point>\n' % (' ' * self.indent))
                         self.indent += 4
                         x, y = point.Get()
-                        self.fd.write('%s<x>%f</x>\n' % (' ' * self.indent, x))
-                        self.fd.write('%s<y>%f</y>\n' % (' ' * self.indent, y))
+                        self.fd.write('%s<x>%d</x>\n' % (' ' * self.indent, int(x)))
+                        self.fd.write('%s<y>%d</y>\n' % (' ' * self.indent, int(y)))
                         self.indent -= 4
                         self.fd.write('%s</point>\n' % (' ' * self.indent))
                     self.indent -= 4
@@ -2437,14 +2485,14 @@
     def GetValues(self):
         """!Get values"""
         return { 'name'   : self.name.GetValue(),
-                 'desc'   : self.desc.GetValue(),
+                 'description'   : self.desc.GetValue(),
                  'author' : self.author.GetValue(),
                  'overwrite' : self.overwrite.IsChecked() }
 
     def Init(self, prop):
         """!Initialize dialog"""
         self.name.SetValue(prop['name'])
-        self.desc.SetValue(prop['desc'])
+        self.desc.SetValue(prop['description'])
         self.author.SetValue(prop['author'])
         if prop.has_key('overwrite'):
             self.overwrite.SetValue(prop['overwrite'])



More information about the grass-commit mailing list