[GRASS-SVN] r53933 - in grass/trunk/gui/wxpython: core gmodeler gui_core lmgr

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 20 02:53:12 PST 2012


Author: wenzeslaus
Date: 2012-11-20 02:53:11 -0800 (Tue, 20 Nov 2012)
New Revision: 53933

Modified:
   grass/trunk/gui/wxpython/core/events.py
   grass/trunk/gui/wxpython/gmodeler/frame.py
   grass/trunk/gui/wxpython/gui_core/forms.py
   grass/trunk/gui/wxpython/gui_core/goutput.py
   grass/trunk/gui/wxpython/lmgr/frame.py
Log:
wxGUI/GConsole: decoupling LayerManager and TaskForm/ModuleForm (co-author: annakrat)

Modified: grass/trunk/gui/wxpython/core/events.py
===================================================================
--- grass/trunk/gui/wxpython/core/events.py	2012-11-20 08:38:00 UTC (rev 53932)
+++ grass/trunk/gui/wxpython/core/events.py	2012-11-20 10:53:11 UTC (rev 53933)
@@ -23,3 +23,8 @@
 # The message attribute contains the text of the message (plain text)
 gShowNotification, EVT_SHOW_NOTIFICATION = NewCommandEvent()
 
+
+# Occurs event when some map is created or updated by a module.
+# attributes: name: map name, ltype: map type,
+# add: if map should be added to layer tree (questionable attribute)
+gMapCreated, EVT_MAP_CREATED = NewCommandEvent()

Modified: grass/trunk/gui/wxpython/gmodeler/frame.py
===================================================================
--- grass/trunk/gui/wxpython/gmodeler/frame.py	2012-11-20 08:38:00 UTC (rev 53932)
+++ grass/trunk/gui/wxpython/gmodeler/frame.py	2012-11-20 10:53:11 UTC (rev 53933)
@@ -36,6 +36,7 @@
 from gui_core.widgets     import GNotebook
 from gui_core.goutput     import GConsole, \
     EVT_CMD_RUN, EVT_CMD_DONE, EVT_CMD_PREPARE, EVT_OUTPUT_TEXT
+from core.events          import EVT_MAP_CREATED
 from core.debug           import Debug
 from core.gcmd            import GMessage, GException, GWarning, GError, RunCommand
 from gui_core.dialogs     import GetImageHandlers
@@ -110,6 +111,7 @@
         self.Bind(EVT_CMD_RUN, self.OnCmdRun)
         self.Bind(EVT_CMD_DONE, self.OnCmdDone)
         self.Bind(EVT_CMD_PREPARE, self.OnCmdPrepare)
+        self.Bind(EVT_MAP_CREATED, self.OnMapCreated)
 
         self.notebook.AddPage(page = self.canvas, text=_('Model'), name = 'model')
         self.notebook.AddPage(page = self.itemPanel, text=_('Items'), name = 'items')
@@ -226,7 +228,15 @@
                 action.Update(running = True)
         except IndexError:
             pass
-        
+
+    def OnMapCreated(self, event):
+        """!Map was created but we don't want to add it to layer tree.
+        """
+        # remove this method if you want to add it to layer tree
+        # or see gui_core.forms.TaskFrame.OnMapCreated
+        event.add = False
+        event.Skip()
+
     def OnCloseWindow(self, event):
         """!Close window"""
         if self.modelChanged and \

Modified: grass/trunk/gui/wxpython/gui_core/forms.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/forms.py	2012-11-20 08:38:00 UTC (rev 53932)
+++ grass/trunk/gui/wxpython/gui_core/forms.py	2012-11-20 10:53:11 UTC (rev 53933)
@@ -90,6 +90,7 @@
 from core             import gcmd
 from core             import utils
 from core.settings    import UserSettings
+from core.events      import EVT_MAP_CREATED
 from gui_core.widgets import FloatValidator, GNotebook, FormNotebook, FormListbook
 
 wxUpdateDialog, EVT_DIALOG_UPDATE = NewEvent()
@@ -533,9 +534,10 @@
             guisizer.Add(item = self.closebox, proportion = 0,
                          flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
                          border = 5)
-        
+        # bindings
         self.Bind(wx.EVT_CLOSE,  self.OnCancel)
         self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
+        self.Bind(EVT_MAP_CREATED, self.OnMapCreated)
         
         # do layout
         # called automatically by SetSizer()
@@ -606,34 +608,34 @@
 
         @param returncode command's return code (0 for success)
         """
-        if not self.parent or returncode !=  0:
-            return
-        if self.parent.GetName() not in ('LayerTree', 'LayerManager'):
-            return
-        
-        if self.parent.GetName() == 'LayerTree':
-            display = self.parent.GetMapDisplay()
-        else: # Layer Manager
-            display = self.parent.GetLayerTree().GetMapDisplay()
-            
-        if not display or not display.IsAutoRendered():
-            return
-        
-        mapLayers = map(lambda x: x.GetName(),
-                        display.GetMap().GetListOfLayers(l_type = 'raster') +
-                        display.GetMap().GetListOfLayers(l_type = 'vector'))
-        
-        task = GUI(show = None).ParseCommand(cmd)
-        for p in task.get_options()['params']:
-            if p.get('prompt', '') not in ('raster', 'vector'):
-                continue
-            mapName = p.get('value', '')
-            if '@' not in mapName:
-                mapName = mapName + '@' + grass.gisenv()['MAPSET']
-            if mapName in mapLayers:
-                display.GetWindow().UpdateMap(render = True)
-                return
-        
+
+        if hasattr(self, "btn_cancel"):
+            self.btn_cancel.Enable(True)
+
+        if hasattr(self, "btn_clipboard"):
+            self.btn_clipboard.Enable(True)
+
+        if hasattr(self, "btn_help"):
+            self.btn_help.Enable(True)
+
+        if hasattr(self, "btn_run"):
+            self.btn_run.Enable(True)
+
+        if hasattr(self, "get_dcmd") and \
+                    self.get_dcmd is None and \
+                    hasattr(self, "closebox") and \
+                    self.closebox.IsChecked() and \
+                    (returncode == 0):
+                # was closed also when aborted but better is leave it open
+                wx.FutureCall(2000, self.Close)
+
+    def OnMapCreated(self, event):
+        if hasattr(self, "addbox") and self.addbox.IsChecked():
+            event.add = True
+        else:
+            event.add = False
+        event.Skip()
+    
     def OnOK(self, event):
         """!OK button pressed"""
         cmd = self.OnApply(event)

Modified: grass/trunk/gui/wxpython/gui_core/goutput.py
===================================================================
--- grass/trunk/gui/wxpython/gui_core/goutput.py	2012-11-20 08:38:00 UTC (rev 53932)
+++ grass/trunk/gui/wxpython/gui_core/goutput.py	2012-11-20 10:53:11 UTC (rev 53933)
@@ -39,11 +39,11 @@
 from core            import globalvar
 from core            import utils
 from core.gcmd       import CommandThread, GMessage, GError, GException, EncodeString
-from core.events     import gShowNotification
+from core.events     import gShowNotification, gMapCreated
 from gui_core.forms  import GUI
 from gui_core.prompt import GPromptSTC
 from core.debug      import Debug
-from core.settings   import UserSettings, GetDisplayVectSettings
+from core.settings   import UserSettings
 from gui_core.widgets import SearchModuleWidget, EVT_MODULE_SELECTED
 from core.modulesdata import ModulesData
 
@@ -866,112 +866,32 @@
         if event.cmd[0] == 'g.gisenv':
             Debug.SetLevel()
             self.Redirect()
-        
-        if self.frame.GetName() == "LayerManager":
-            self.btnCmdAbort.Enable(False)
-            if event.cmd[0] not in globalvar.grassCmd or \
-                    event.cmd[0] == 'r.mapcalc':
-                return
+
+        # do nothing when no map added
+        if event.returncode != 0 or event.aborted:
+            event.Skip()
+            return
+
+        # find which maps were created
+        try:
+            task = GUI(show = None).ParseCommand(event.cmd)
+        except GException, e:
+            print >> sys.stderr, e
+            task = None
+            return
             
-            tree = self.frame.GetLayerTree()
-            display = None
-            if tree:
-                display = tree.GetMapDisplay()
-            if not display or not display.IsAutoRendered():
-                return
-            mapLayers = map(lambda x: x.GetName(),
-                            display.GetMap().GetListOfLayers(l_type = 'raster') +
-                            display.GetMap().GetListOfLayers(l_type = 'vector'))
-            
-            try:
-                task = GUI(show = None).ParseCommand(event.cmd)
-            except GException, e:
-                print >> sys.stderr, e
-                task = None
-                return
-            
-            for p in task.get_options()['params']:
-                if p.get('prompt', '') not in ('raster', 'vector'):
-                    continue
-                mapName = p.get('value', '')
-                if '@' not in mapName:
-                    mapName = mapName + '@' + grass.gisenv()['MAPSET']
-                if mapName in mapLayers:
-                    display.GetWindow().UpdateMap(render = True)
-                    return
-        elif self.frame.GetName() == 'Modeler':
-            pass
-        else: # standalone dialogs
-            dialog = self.frame
-            if hasattr(dialog, "btn_abort"):
-                dialog.btn_abort.Enable(False)
-            
-            if hasattr(dialog, "btn_cancel"):
-                dialog.btn_cancel.Enable(True)
-            
-            if hasattr(dialog, "btn_clipboard"):
-                dialog.btn_clipboard.Enable(True)
-            
-            if hasattr(dialog, "btn_help"):
-                dialog.btn_help.Enable(True)
-            
-            if hasattr(dialog, "btn_run"):
-                dialog.btn_run.Enable(True)
-            
-            if event.returncode == 0 and not event.aborted:
-                try:
-                    winName = self.frame.parent.GetName()
-                except AttributeError:
-                    winName = ''
-                
-                if winName == 'LayerManager':
-                    mapTree = self.frame.parent.GetLayerTree()
-                elif winName == 'LayerTree':
-                    mapTree = self.frame.parent
-                elif winName: # GConsole
-                    mapTree = self.frame.parent.parent.GetLayerTree()
-                else:
-                    mapTree = None
-                
-                cmd = dialog.notebookpanel.createCmd(ignoreErrors = True)
-                if hasattr(dialog, "addbox") and dialog.addbox.IsChecked():
-                    # add created maps into layer tree
-                    for p in dialog.task.get_options()['params']:
-                        prompt = p.get('prompt', '')
-                        if prompt in ('raster', 'vector', '3d-raster') and \
-                                p.get('age', 'old') == 'new' and \
-                                p.get('value', None):
-                            name, found = utils.GetLayerNameFromCmd(cmd, fullyQualified = True,
-                                                                    param = p.get('name', ''))
-                            
-                            if mapTree.GetMap().GetListOfLayers(l_name = name):
-                                display = mapTree.GetMapDisplay()
-                                if display and display.IsAutoRendered():
-                                    display.GetWindow().UpdateMap(render = True)
-                                continue
-                            
-                            if prompt == 'raster':
-                                lcmd = ['d.rast',
-                                        'map=%s' % name]
-                            elif prompt == '3d-raster':
-                                lcmd = ['d.rast3d',
-                                        'map=%s' % name]
-                            else:
-                                defaultParams = GetDisplayVectSettings()
-                                lcmd = ['d.vect',
-                                        'map=%s' % name] + defaultParams
-                            mapTree.AddLayer(ltype = prompt,
-                                             lcmd = lcmd,
-                                             lname = name)
-            
-            if hasattr(dialog, "get_dcmd") and \
-                    dialog.get_dcmd is None and \
-                    hasattr(dialog, "closebox") and \
-                    dialog.closebox.IsChecked() and \
-                    (event.returncode == 0 or event.aborted):
-                self.cmdOutput.Update()
-                time.sleep(2)
-                dialog.Close()
+        for p in task.get_options()['params']:
+            prompt = p.get('prompt', '')
+            if prompt in ('raster', 'vector', '3d-raster') and \
+                    p.get('age', 'old') == 'new' and \
+                    p.get('value', None):
+                name = p.get('value')
+                if '@' not in name:
+                    name = name + '@' + grass.gisenv()['MAPSET']
+                mapEvent = gMapCreated(self.GetId(),
+                                       name = name, ltype = prompt, add = None)
+                wx.PostEvent(self, mapEvent)
+
         event.Skip()
 
     def OnProcessPendingOutputWindowEvents(self, event):

Modified: grass/trunk/gui/wxpython/lmgr/frame.py
===================================================================
--- grass/trunk/gui/wxpython/lmgr/frame.py	2012-11-20 08:38:00 UTC (rev 53932)
+++ grass/trunk/gui/wxpython/lmgr/frame.py	2012-11-20 10:53:11 UTC (rev 53933)
@@ -42,7 +42,7 @@
 from core.gcmd             import RunCommand, GError, GMessage
 from core.settings         import UserSettings, GetDisplayVectSettings
 from core.utils            import SetAddOnPath
-from core.events           import EVT_SHOW_NOTIFICATION
+from core.events           import EVT_SHOW_NOTIFICATION, EVT_MAP_CREATED
 from gui_core.preferences  import MapsetAccess, PreferencesDialog, EVT_SETTINGS_CHANGED
 from lmgr.layertree        import LayerTree, LMIcons
 from lmgr.menudata         import LayerManagerMenuData
@@ -157,6 +157,7 @@
         self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
         self.Bind(EVT_SHOW_NOTIFICATION,
                   lambda event: self.SetStatusText(event.message))
+        self.Bind(EVT_MAP_CREATED, self.OnMapCreated)
 
         # minimal frame size
         self.SetMinSize((globalvar.GM_WINDOW_SIZE[0], 400))
@@ -1552,8 +1553,14 @@
     def OnApplyMapLayers(self, event):
         self.AddMaps(mapLayers = event.mapLayers, ltype = event.ltype)
 
-    def AddMaps(self, mapLayers, ltype):
-        """!Add map layers to layer tree."""
+    def AddMaps(self, mapLayers, ltype, check = False):
+        """!Add map layers to layer tree.
+
+        @param mapLayers list of map names
+        @param ltype layer type ('rast', 'rast3d', 'vect')
+        @param check @c True if new layers should be checked in layer tree
+        @c False otherwise
+        """
         # start new map display if no display is available
         if not self.currentPage:
             self.NewDisplay()
@@ -1577,10 +1584,48 @@
             
             newItem = maptree.AddLayer(ltype = wxType,
                                        lname = layerName,
-                                       lchecked = False,
+                                       lchecked = check,
                                        lopacity = 1.0,
                                        lcmd = cmd,
                                        lgroup = None)
+
+    def OnMapCreated(self, event):
+        if event.add is None:
+            if UserSettings.Get(group = 'cmd',
+                                key = 'addNewLayer', subkey = 'enabled'):
+                self.AddOrUpdateMap(event.name, event.ltype)
+        elif event.add:
+            self.AddOrUpdateMap(event.name, event.ltype)
+
+    def AddOrUpdateMap(self, mapName, ltype):
+        """!Add map layer or update"""
+        # start new map display if no display is available
+
+        # TODO: standardize type identifiers
+        convertType = {'raster': 'rast',
+                       '3d-raster': 'rast3d',
+                       'vector': 'vect'}
+        try:
+            grassType = convertType[ltype]
+        except KeyError:
+            if ltype in convertType.values():
+                grassType = ltype
+            else:
+                GError(parent = self,
+                       message = _("Unsupported map layer type <%s>.") % ltype)
+                return
+
+        if not self.currentPage:
+            self.AddMaps([mapName], grassType, check = True)
+        else:
+            display = self.GetLayerTree().GetMapDisplay()
+            mapLayers = map(lambda x: x.GetName(),
+                            display.GetMap().GetListOfLayers(l_type = ltype))
+            if mapName in mapLayers:
+                display.GetWindow().UpdateMap(render = True)
+            else:
+                self.AddMaps([mapName], grassType, check = True)
+
     def OnAddRaster(self, event):
         """!Add raster map layer"""
         # start new map display if no display is available



More information about the grass-commit mailing list