[GRASS-SVN] r39120 - in grass/trunk/gui/wxpython: . gui_modules xml

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 11 03:50:42 EDT 2009


Author: martinl
Date: 2009-09-11 03:50:41 -0400 (Fri, 11 Sep 2009)
New Revision: 39120

Modified:
   grass/trunk/gui/wxpython/gis_set.py
   grass/trunk/gui/wxpython/gui_modules/gdialogs.py
   grass/trunk/gui/wxpython/gui_modules/gselect.py
   grass/trunk/gui/wxpython/gui_modules/utils.py
   grass/trunk/gui/wxpython/wxgui.py
   grass/trunk/gui/wxpython/xml/menudata.xml
Log:
wxGUI: add dialog to change location or mapset


Modified: grass/trunk/gui/wxpython/gis_set.py
===================================================================
--- grass/trunk/gui/wxpython/gis_set.py	2009-09-10 21:25:02 UTC (rev 39119)
+++ grass/trunk/gui/wxpython/gis_set.py	2009-09-11 07:50:41 UTC (rev 39120)
@@ -1,4 +1,4 @@
-"""
+"""!
 @package gis_set.py
 
 GRASS start-up screen.
@@ -517,17 +517,8 @@
 
     def UpdateLocations(self, dbase):
         """!Update list of locations"""
-        self.listOfLocations = []
-
-        for location in glob.glob(os.path.join(dbase, "*")):
-            try:
-                if os.path.join(location, "PERMANENT") in glob.glob(os.path.join(location, "*")):
-                    self.listOfLocations.append(os.path.basename(location))
-            except:
-                pass
-
-        utils.ListSortLower(self.listOfLocations)
-
+        self.listOfLocations = utils.GetListOfLocations(dbase)
+        
         self.lblocations.Clear()
         self.lblocations.InsertItems(self.listOfLocations, 0)
 
@@ -542,18 +533,9 @@
         """!Update list of mapsets"""
         self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
 
-        self.listOfMapsets = []
-        self.listOfMapsetsSelectable = []
-        
-        for mapset in glob.glob(os.path.join(self.gisdbase, location, "*")):
-            if os.path.isdir(mapset) and \
-                    os.path.isfile(os.path.join(self.gisdbase, location, mapset, "WIND")) and \
-                    os.path.basename(mapset) != 'PERMANENT':
-                self.listOfMapsets.append(os.path.basename(mapset))
-        
-        utils.ListSortLower(self.listOfMapsets)
-        self.listOfMapsets.insert(0, 'PERMANENT')
- 
+        self.listOfMapsetsSelectable = list()
+        self.listOfMapsets = utils.GetListOfMapsets(self.gisdbase, location)
+         
         self.lbmapsets.Clear()
 
         # disable mapset with denied permission

Modified: grass/trunk/gui/wxpython/gui_modules/gdialogs.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gdialogs.py	2009-09-10 21:25:02 UTC (rev 39119)
+++ grass/trunk/gui/wxpython/gui_modules/gdialogs.py	2009-09-11 07:50:41 UTC (rev 39120)
@@ -1,9 +1,12 @@
-"""
+"""!
 @package gdialogs.py
 
- at brief Common dialog used in wxGUI.
+ at brief Various dialogs used in wxGUI.
 
 List of classes:
+ - ElementDialog
+ - LocationDialog
+ - MapsetDialog
  - NewVectorDialog
  - SavedRegion
  - DecorationDialog
@@ -40,90 +43,195 @@
 import utils
 from preferences import globalSettings as UserSettings
 
-class NewVectorDialog(wx.Dialog):
-    """!Create new vector map layer"""
-    def __init__(self, parent, id, title, disableAdd=False, 
-                style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
+class ElementDialog(wx.Dialog):
+    """!General dialog to choose given element (location, mapset, vector map, etc.)"""
+    def __init__(self, parent, title, label, id = wx.ID_ANY,
+                 style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
+                 **kwargs):
+        
+        wx.Dialog.__init__(self, parent, id, title, style = style, **kwargs)
+        
+        self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
+        
+        self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
+        self.btnOK     = wx.Button(parent = self.panel, id = wx.ID_OK)
+        self.btnOK.SetDefault()
+        self.btnOK.Enable(False)
+        
+        self.label = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
+                                   label = label)
+        
+        self.element = None # must be defined 
+        
+        self.__Layout()
+        
+    def PostInit(self):
+        self.element.SetFocus()
+        self.element.Bind(wx.EVT_TEXT, self.OnElement)
+        
+    def OnElement(self, event):
+        """!Name for vector map layer given"""
+        if len(event.GetString()) > 0:
+            self.btnOK.Enable(True)
+        else:
+            self.btnOK.Enable(False)
+        
+    def __Layout(self):
+        """!Do layout"""
+        self.sizer = wx.BoxSizer(wx.VERTICAL)
+        
+        self.dataSizer = wx.BoxSizer(wx.VERTICAL)
+        self.dataSizer.Add(self.label, proportion=0,
+                           flag=wx.ALL, border=1)
+        
+        # buttons
+        btnSizer = wx.StdDialogButtonSizer()
+        btnSizer.AddButton(self.btnCancel)
+        btnSizer.AddButton(self.btnOK)
+        btnSizer.Realize()
+        
+        self.sizer.Add(item=self.dataSizer, proportion=1,
+                       flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
+        
+        self.sizer.Add(item=btnSizer, proportion=0,
+                       flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
+        
+    def GetElement(self):
+        """!Return (mapName, overwrite)"""
+        return self.element.GetValue()
+    
+class LocationDialog(ElementDialog):
+    """!Dialog used to select location"""
+    def __init__(self, parent, title = _("Select GRASS location and mapset"), id =  wx.ID_ANY):
+        ElementDialog.__init__(self, parent, title, label = _("Name of GRASS location:"))
 
-        wx.Dialog.__init__(self, parent, id, title, style=style)
+        self.element = gselect.LocationSelect(parent = self.panel, id = wx.ID_ANY,
+                                              size = globalvar.DIALOG_GSELECT_SIZE)
 
-        self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
+        self.element1 = gselect.MapsetSelect(parent = self.panel, id = wx.ID_ANY,
+                                             size = globalvar.DIALOG_GSELECT_SIZE)
+        self.element1.SetItems([])
+        
+        self.PostInit()
+        
+        self.__Layout()
+        self.SetMinSize(self.GetSize())
 
-        self.btnCancel = wx.Button(self.panel, wx.ID_CANCEL)
-        self.btnOK = wx.Button(self.panel, wx.ID_OK)
-        self.btnOK.SetDefault()
-        self.btnOK.Enable(False)
+    def __Layout(self):
+        """!Do layout"""
+        self.dataSizer.Add(self.element, proportion=0,
+                      flag=wx.EXPAND | wx.ALL, border=1)
+ 
+        self.dataSizer.Add(wx.StaticText(parent = self.panel, id = wx.ID_ANY,
+                                         label = _("Name of mapset:")), proportion=0,
+                           flag=wx.EXPAND | wx.ALL, border=1)
 
-        self.label = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
-                                   label=_("Name for new vector map:"))
-        self.mapName = gselect.Select(parent=self.panel, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
+        self.dataSizer.Add(self.element1, proportion=0,
+                      flag=wx.EXPAND | wx.ALL, border=1)
+       
+        self.panel.SetSizer(self.sizer)
+        self.sizer.Fit(self)
+
+    def OnElement(self, event):
+        """!Name for vector map layer given"""
+        location = event.GetString()
+        mapset   = self.element1.GetValue()
+        
+        if location:
+            dbase = grass.gisenv()['GISDBASE']
+            self.element1.SetItems(utils.GetListOfMapsets(dbase, location, selectable = True))
+            self.element1.SetSelection(0)
+            mapset = self.element1.GetStringSelection()
+        
+        if location and mapset:
+            self.btnOK.Enable(True)
+        else:
+            self.btnOK.Enable(False)
+
+    def GetValues(self):
+        """!Get location, mapset"""
+        return (self.GetElement(), self.element1.GetValue())
+    
+class MapsetDialog(ElementDialog):
+    """!Dialog used to select mapset"""
+    def __init__(self, parent, title = _("Select mapset in GRASS location"),
+                 location = None, id =  wx.ID_ANY):
+        ElementDialog.__init__(self, parent, title, label = _("Name of mapset:"))
+        if location:
+            self.SetTitle(self.GetTitle() + '<%s>' % location)
+        else:
+            self.SetTitle(self.GetTitle() + '<%s>' % grass.gisenv()['LOCATION_NAME'])
+        
+        self.element = gselect.MapsetSelect(parent = self.panel, id = wx.ID_ANY,
+                                            size = globalvar.DIALOG_GSELECT_SIZE)
+        
+        self.PostInit()
+        
+        self.__Layout()
+        self.SetMinSize(self.GetSize())
+
+    def __Layout(self):
+        """!Do layout"""
+        self.dataSizer.Add(self.element, proportion=0,
+                      flag=wx.EXPAND | wx.ALL, border=1)
+        
+        self.panel.SetSizer(self.sizer)
+        self.sizer.Fit(self)
+
+    def GetMapset(self):
+        return self.GetElement()
+    
+class NewVectorDialog(ElementDialog):
+    """!Dialog for creating new vector map"""
+    def __init__(self, parent, id, title, disableAdd=False, 
+                 style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER):
+        
+        ElementDialog.__init__(self, parent, title, label = _("Name for new vector map:"))
+        
+        self.element = gselect.Select(parent=self.panel, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
                                       type='vector', mapsets=[grass.gisenv()['MAPSET'],])
-        self.mapName.SetFocus()
         
-        self.table = wx.CheckBox(parent=self.panel, id=wx.ID_ANY,
-                                 label=_("Create attribute table"))
+        self.table = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
+                                 label = _("Create attribute table"))
         self.table.SetValue(True)
-
-        self.addbox = wx.CheckBox(parent=self.panel,
-                                  label=_('Add created map into layer tree'), style = wx.NO_BORDER)
+        
+        self.addbox = wx.CheckBox(parent = self.panel,
+                                  label = _('Add created map into layer tree'), style = wx.NO_BORDER)
         if disableAdd:
             self.addbox.SetValue(True)
             self.addbox.Enable(False)
         else:
-            self.addbox.SetValue(UserSettings.Get(group='cmd', key='addNewLayer', subkey='enabled'))
+            self.addbox.SetValue(UserSettings.Get(group = 'cmd', key = 'addNewLayer', subkey = 'enabled'))
         
-        self.mapName.Bind(wx.EVT_TEXT, self.OnMapName)
+        self.PostInit()
         
         self.__Layout()
-
         self.SetMinSize(self.GetSize())
-
+        
     def OnMapName(self, event):
         """!Name for vector map layer given"""
-        if len(event.GetString()) > 0:
-            self.btnOK.Enable(True)
-        else:
-            self.btnOK.Enable(False)
-
+        self.OnElement(event)
+        
     def __Layout(self):
         """!Do layout"""
-        sizer = wx.BoxSizer(wx.VERTICAL)
-
-        dataSizer = wx.BoxSizer(wx.VERTICAL)
-        dataSizer.Add(self.label, proportion=0,
-                      flag=wx.ALL, border=1)
-        dataSizer.Add(self.mapName, proportion=0,
+        self.dataSizer.Add(self.element, proportion=0,
                       flag=wx.EXPAND | wx.ALL, border=1)
         
-        dataSizer.Add(self.table, proportion=0,
+        self.dataSizer.Add(self.table, proportion=0,
                       flag=wx.EXPAND | wx.ALL, border=1)
-
-        dataSizer.AddSpacer(5)
         
-        dataSizer.Add(item=self.addbox, proportion=0,
+        self.dataSizer.AddSpacer(5)
+        
+        self.dataSizer.Add(item=self.addbox, proportion=0,
                       flag=wx.EXPAND | wx.ALL, border=1)
-                
-        # buttons
-        btnSizer = wx.StdDialogButtonSizer()
-        btnSizer.AddButton(self.btnCancel)
-        btnSizer.AddButton(self.btnOK)
-        btnSizer.Realize()
+        
+        self.panel.SetSizer(self.sizer)
+        self.sizer.Fit(self)
 
-        sizer.Add(item=dataSizer, proportion=1,
-                  flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
-
-        sizer.Add(item=btnSizer, proportion=0,
-                  flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
-       
-        self.panel.SetSizer(sizer)
-        sizer.Fit(self)
-
     def GetName(self):
         """!Return (mapName, overwrite)"""
-        mapName = self.mapName.GetValue().split('@', 1)[0]
-
-        return mapName
-    
+        return self.GetElement().split('@', 1)[0]
+            
 def CreateNewVector(parent, cmd, title=_('Create new vector map'),
                     exceptMap=None, log=None, disableAdd=False):
     """!Create new vector map layer

Modified: grass/trunk/gui/wxpython/gui_modules/gselect.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/gselect.py	2009-09-10 21:25:02 UTC (rev 39119)
+++ grass/trunk/gui/wxpython/gui_modules/gselect.py	2009-09-11 07:50:41 UTC (rev 39120)
@@ -12,6 +12,8 @@
  - DriverSelect
  - DatabaseSelect
  - ColumnSelect
+ - LocationSelect
+ - MapsetSelect
 
 (C) 2007-2009 by the GRASS Development Team This program is free
 software under the GNU General Public License (>=v2). Read the file
@@ -669,3 +671,39 @@
 
         self.SetItems(columns)
         
+class LocationSelect(wx.ComboBox):
+    """!Widget for selecting GRASS location"""
+    def __init__(self, parent, id = wx.ID_ANY, size = globalvar.DIALOG_COMBOBOX_SIZE, 
+                 gisdbase = None, **kwargs):
+        super(LocationSelect, self).__init__(parent, id, size = size, 
+                                             style = wx.CB_READONLY, **kwargs)
+
+        self.SetName("LocationSelect")
+        
+        if not gisdbase:
+            self.gisdbase = grass.gisenv()['GISDBASE']
+        else:
+            self.gisdbase = gisdbase
+
+        self.SetItems(utils.GetListOfLocations(self.gisdbase))
+
+class MapsetSelect(wx.ComboBox):
+    """!Widget for selecting GRASS mapset"""
+    def __init__(self, parent, id = wx.ID_ANY, size = globalvar.DIALOG_COMBOBOX_SIZE, 
+                 gisdbase = None, location = None, **kwargs):
+        super(MapsetSelect, self).__init__(parent, id, size = size, 
+                                           style = wx.CB_READONLY, **kwargs)
+        
+        self.SetName("MapsetSelect")
+        
+        if not gisdbase:
+            self.gisdbase = grass.gisenv()['GISDBASE']
+        else:
+            self.gisdbase = gisdbase
+        
+        if not location:
+            self.location = grass.gisenv()['LOCATION_NAME']
+        else:
+            self.location = location
+        
+        self.SetItems(utils.GetListOfMapsets(self.gisdbase, self.location, selectable = True)) # selectable

Modified: grass/trunk/gui/wxpython/gui_modules/utils.py
===================================================================
--- grass/trunk/gui/wxpython/gui_modules/utils.py	2009-09-10 21:25:02 UTC (rev 39119)
+++ grass/trunk/gui/wxpython/gui_modules/utils.py	2009-09-11 07:50:41 UTC (rev 39120)
@@ -1,27 +1,27 @@
-"""
+"""!
 @package utils.py
 
- at brief Misc utilities for GRASS wxPython GUI
+ at brief Misc utilities for wxGUI
 
-(C) 2007-2008 by the GRASS Development Team
+(C) 2007-2009 by the GRASS Development Team
 
-This program is free software under the GNU General Public
-License (>=v2). Read the file COPYING that comes with GRASS
-for details.
+This program is free software under the GNU General Public License
+(>=v2). Read the file COPYING that comes with GRASS for details.
 
- at author Martin Landa, Jachym Cepicky
-
- at date 2007-2008
+ at author Martin Landa <landa.martin gmail.com>
+ at author Jachym Cepicky
 """
 
 import os
 import sys
 import platform
 import string
+import glob
 
 import globalvar
 grassPath = os.path.join(globalvar.ETCDIR, "python")
 sys.path.append(grassPath)
+
 from grass.script import core as grass
 
 import gcmd
@@ -512,3 +512,58 @@
                 return (None, None)
     
     return (None, None)
+
+def GetListOfLocations(dbase):
+    """!Get list of GRASS locations in given dbase
+
+    @param dbase GRASS database path
+
+    @return list of locations (sorted)
+    """
+    listOfLocations = list()
+    
+    for location in glob.glob(os.path.join(dbase, "*")):
+        try:
+            if os.path.join(location, "PERMANENT") in glob.glob(os.path.join(location, "*")):
+                listOfLocations.append(os.path.basename(location))
+        except:
+            pass
+    
+    ListSortLower(listOfLocations)
+    
+    return listOfLocations
+
+def GetListOfMapsets(dbase, location, selectable = False):
+    """!Get list of mapsets in given GRASS location
+
+    @param dbase      GRASS database path
+    @param location   GRASS location
+    @param selectable True to get list of selectable mapsets, otherwise all
+
+    @return list of mapsets - sorted (PERMANENT first)
+    """
+    listOfMapsets = list()
+    
+    if selectable:
+        ret = gcmd.RunCommand('g.mapset',
+                              read = True,
+                              flags = 'l',
+                              location = location,
+                              gisdbase = dbase)
+        
+        if not ret:
+            return listOfMapsets
+            
+        for line in ret.rstrip().splitlines():
+            listOfMapsets += line.split(' ')
+    else:
+        for mapset in glob.glob(os.path.join(dbase, location, "*")):
+            if os.path.isdir(mapset) and \
+                    os.path.isfile(os.path.join(dbase, location, mapset, "WIND")) and \
+                    os.path.basename(mapset) != 'PERMANENT':
+                listOfMapsets.append(os.path.basename(mapset))
+    
+        ListSortLower(listOfMapsets)
+        listOfMapsets.insert(0, 'PERMANENT')
+    
+    return listOfMapsets

Modified: grass/trunk/gui/wxpython/wxgui.py
===================================================================
--- grass/trunk/gui/wxpython/wxgui.py	2009-09-10 21:25:02 UTC (rev 39119)
+++ grass/trunk/gui/wxpython/wxgui.py	2009-09-11 07:50:41 UTC (rev 39120)
@@ -467,6 +467,47 @@
             cmd = self.GetMenuCmd(event)
         menuform.GUI().ParseCommand(cmd, parentframe=self)
 
+    def OnChangeLocation(self, event):
+        """Change current location"""
+        dlg = gdialogs.LocationDialog(parent = self)
+        if dlg.ShowModal() == wx.ID_OK:
+            location, mapset = dlg.GetValues()
+            if location and mapset:
+                ret = gcmd.RunCommand("g.gisenv",
+                                      set = "LOCATION_NAME=%s" % location)
+                ret += gcmd.RunCommand("g.gisenv",
+                                       set = "MAPSET=%s" % mapset)
+                if ret > 0:
+                    wx.MessageBox(parent = self,
+                                  message = _("Unable to switch to location <%(loc)s> mapset <%(mapset)s>.") % \
+                                      { 'loc' : location, 'mapset' : mapset },
+                                  caption = _("Error"), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
+                else:
+                    # close workspace
+                    self.OnWorkspaceClose()
+                    self.OnWorkspaceNew()
+                    wx.MessageBox(parent = self,
+                                  message = _("Current location is <%(loc)s>.\n"
+                                              "Current mapset is <%(mapset)s>.") % \
+                                      { 'loc' : location, 'mapset' : mapset },
+                                  caption = _("Info"), style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
+                    
+    def OnChangeMapset(self, event):
+        """Change current mapset"""
+        dlg = gdialogs.MapsetDialog(parent = self)
+        if dlg.ShowModal() == wx.ID_OK:
+            mapset = dlg.GetMapset()
+            if mapset:
+                if gcmd.RunCommand("g.gisenv",
+                                   set = "MAPSET=%s" % mapset) != 0:
+                    wx.MessageBox(parent = self,
+                                  message = _("Unable to switch to mapset <%s>.") % mapset,
+                                  caption = _("Error"), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
+                else:
+                    wx.MessageBox(parent = self,
+                                  message = _("Current mapset is <%s>.") % mapset,
+                                  caption = _("Info"), style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
+        
     def OnNewVector(self, event):
         """!Create new vector map layer"""
         name, add = gdialogs.CreateNewVector(self, log = self.goutput,

Modified: grass/trunk/gui/wxpython/xml/menudata.xml
===================================================================
--- grass/trunk/gui/wxpython/xml/menudata.xml	2009-09-10 21:25:02 UTC (rev 39119)
+++ grass/trunk/gui/wxpython/xml/menudata.xml	2009-09-11 07:50:41 UTC (rev 39120)
@@ -682,6 +682,14 @@
 	      <handler>self.OnMapsets</handler>
 	    </menuitem>
 	    <menuitem>
+	      <label>User access</label>
+	      <help>Controls access to the current mapset for other users on the system.</help>
+	      <keywords>general</keywords>
+              <handler>self.OnMenuCmd</handler>
+	      <command>g.access</command>
+	    </menuitem>
+	    <separator />
+	    <menuitem>
 	      <label>Change working environment</label>
 	      <help>Change current mapset.</help>
 	      <keywords>general,settings</keywords>
@@ -689,12 +697,15 @@
 	      <command>g.mapset</command>
 	    </menuitem>
 	    <menuitem>
-	      <label>User access</label>
-	      <help>Controls access to the current mapset for other users on the system.</help>
-	      <keywords>general</keywords>
-              <handler>self.OnMenuCmd</handler>
-	      <command>g.access</command>
+	      <label>Change location and mapset</label>
+	      <help>Change current location and mapset (note that current workspace will be closed).</help>
+              <handler>self.OnChangeLocation</handler>
 	    </menuitem>
+	    <menuitem>
+	      <label>Change mapset</label>
+	      <help>Change current mapset.</help>
+              <handler>self.OnChangeMapset</handler>
+	    </menuitem>
 	    <separator />
 	    <menuitem>
 	      <label>Show settings</label>
@@ -746,6 +757,7 @@
 	    </menuitem>
 	  </items>
 	</menu>
+	<separator />
 	<menuitem>
 	  <label>Manage extensions</label>
 	  <help>Tool to maintain GRASS extensions in local GRASS installation.</help>



More information about the grass-commit mailing list