gis_set.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. """!
  2. @package gis_set.py
  3. GRASS start-up screen.
  4. Initialization module for wxPython GRASS GUI.
  5. Location/mapset management (selection, creation, etc.).
  6. Classes:
  7. - GRASSStartup
  8. - StartUp
  9. (C) 2006-2010 by the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. @author Michael Barton and Jachym Cepicky (original author)
  13. @author Martin Landa <landa.martin gmail.com> (various updates)
  14. """
  15. import os
  16. import sys
  17. import glob
  18. import shutil
  19. import copy
  20. import platform
  21. ### i18N
  22. import gettext
  23. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  24. from gui_modules import globalvar
  25. if not os.getenv("GRASS_WXBUNDLED"):
  26. globalvar.CheckForWx()
  27. import gui_modules.goutput
  28. from gui_modules.ghelp import HelpFrame
  29. from gui_modules.gcmd import GMessage
  30. import wx
  31. import wx.html
  32. import wx.lib.rcsizer as rcs
  33. import wx.lib.filebrowsebutton as filebrowse
  34. import wx.lib.mixins.listctrl as listmix
  35. import wx.lib.scrolledpanel as scrolled
  36. class GRASSStartup(wx.Frame):
  37. """!GRASS start-up screen"""
  38. def __init__(self, parent=None, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE):
  39. #
  40. # GRASS variables
  41. #
  42. self.gisbase = os.getenv("GISBASE")
  43. self.grassrc = self._read_grassrc()
  44. self.gisdbase = self.GetRCValue("GISDBASE")
  45. #
  46. # list of locations/mapsets
  47. #
  48. self.listOfLocations = []
  49. self.listOfMapsets = []
  50. self.listOfMapsetsSelectable = []
  51. wx.Frame.__init__(self, parent=parent, id=id, style=style)
  52. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  53. self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  54. # i18N
  55. import gettext
  56. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  57. #
  58. # graphical elements
  59. #
  60. # image
  61. try:
  62. name = os.path.join(globalvar.ETCDIR, "gui", "images", "startup_banner.png")
  63. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
  64. wx.Bitmap(name=name,
  65. type=wx.BITMAP_TYPE_PNG))
  66. except:
  67. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
  68. # labels
  69. ### crashes when LOCATION doesn't exist
  70. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  71. grassVersion = versionFile.readline().split(' ')[0].rstrip('\n')
  72. versionFile.close()
  73. self.select_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  74. label=" %s " % _("Choose project location and mapset"))
  75. self.manage_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  76. label=" %s " % _("Manage"))
  77. self.lwelcome = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  78. label=_("Welcome to GRASS GIS %s\n"
  79. "The world's leading open source GIS") % grassVersion,
  80. style=wx.ALIGN_CENTRE)
  81. self.ltitle = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  82. label=_("Select an existing project location and mapset\n"
  83. "or define a new location"),
  84. style=wx.ALIGN_CENTRE)
  85. self.ldbase = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  86. label=_("GIS Data Directory:"))
  87. self.llocation = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  88. label=_("Project location\n(projection/coordinate system)"),
  89. style=wx.ALIGN_CENTRE)
  90. self.lmapset = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  91. label=_("Accessible mapsets\n(directories of GIS files)"),
  92. style=wx.ALIGN_CENTRE)
  93. self.lcreate = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  94. label=_("Create new mapset\nin selected location"),
  95. style=wx.ALIGN_CENTRE)
  96. self.ldefine = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  97. label=_("Define new location"),
  98. style=wx.ALIGN_CENTRE)
  99. self.lmanageloc = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  100. label=_("Rename/delete selected\nmapset or location"),
  101. style=wx.ALIGN_CENTRE)
  102. # buttons
  103. self.bstart = wx.Button(parent=self.panel, id=wx.ID_ANY,
  104. label=_("Start GRASS"))
  105. self.bstart.SetDefault()
  106. self.bexit = wx.Button(parent=self.panel, id=wx.ID_EXIT)
  107. self.bstart.SetMinSize((180, self.bexit.GetSize()[1]))
  108. self.bhelp = wx.Button(parent=self.panel, id=wx.ID_HELP)
  109. self.bbrowse = wx.Button(parent=self.panel, id=wx.ID_ANY,
  110. label=_("Browse"))
  111. self.bmapset = wx.Button(parent=self.panel, id=wx.ID_ANY,
  112. label=_("Create mapset"))
  113. self.bwizard = wx.Button(parent=self.panel, id=wx.ID_ANY,
  114. label=_("Location wizard"))
  115. self.manageloc = wx.Choice(parent=self.panel, id=wx.ID_ANY,
  116. choices=[_('Rename mapset'), _('Rename location'),
  117. _('Delete mapset'), _('Delete location')])
  118. self.manageloc.SetSelection(0)
  119. # textinputs
  120. self.tgisdbase = wx.TextCtrl(parent=self.panel, id=wx.ID_ANY, value="", size=(300, -1),
  121. style=wx.TE_PROCESS_ENTER)
  122. # Locations
  123. self.lpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  124. self.lblocations = GListBox(parent=self.lpanel,
  125. id=wx.ID_ANY, size=(180, 200),
  126. choices=self.listOfLocations)
  127. self.lblocations.SetColumnWidth(0, 180)
  128. # TODO: sort; but keep PERMANENT on top of list
  129. # Mapsets
  130. self.mpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  131. self.lbmapsets = GListBox(parent=self.mpanel,
  132. id=wx.ID_ANY, size=(180, 200),
  133. choices=self.listOfMapsets)
  134. self.lbmapsets.SetColumnWidth(0, 180)
  135. # layout & properties
  136. self._set_properties()
  137. self._do_layout()
  138. # events
  139. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  140. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  141. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  142. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  143. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  144. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  145. self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
  146. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  147. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  148. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
  149. self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  150. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  151. def _set_properties(self):
  152. """!Set frame properties"""
  153. self.SetTitle(_("Welcome to GRASS GIS"))
  154. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
  155. wx.BITMAP_TYPE_ICO))
  156. self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
  157. self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  158. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  159. self.bstart.SetToolTipString(_("Enter GRASS session"))
  160. self.bstart.Enable(False)
  161. self.bmapset.Enable(False)
  162. self.manageloc.Enable(False)
  163. # set database
  164. if not self.gisdbase:
  165. # sets an initial path for gisdbase if nothing in GISRC
  166. if os.path.isdir(os.getenv("HOME")):
  167. self.gisdbase = os.getenv("HOME")
  168. else:
  169. self.gisdbase = os.getcwd()
  170. try:
  171. self.tgisdbase.SetValue(self.gisdbase)
  172. except UnicodeDecodeError:
  173. wx.MessageBox(parent = self, caption = _("Error"),
  174. message = _("Unable to set GRASS database. "
  175. "Check your locale settings."),
  176. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  177. self.OnSetDatabase(None)
  178. location = self.GetRCValue("LOCATION_NAME")
  179. if location == "<UNKNOWN>" or \
  180. not os.path.isdir(os.path.join(self.gisdbase, location)):
  181. location = None
  182. if location:
  183. # list of locations
  184. self.UpdateLocations(self.gisdbase)
  185. try:
  186. self.lblocations.SetSelection(self.listOfLocations.index(location),
  187. force=True)
  188. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  189. except ValueError:
  190. print >> sys.stderr, _("ERROR: Location <%s> not found") % \
  191. (utils.UnicodeString(location))
  192. # list of mapsets
  193. self.UpdateMapsets(os.path.join(self.gisdbase, location))
  194. mapset = self.GetRCValue("MAPSET")
  195. if mapset:
  196. try:
  197. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  198. force=True)
  199. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  200. except ValueError:
  201. self.lbmapsets.Clear()
  202. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % \
  203. (utils.UnicodeString(mapset))
  204. def _do_layout(self):
  205. label_style = wx.ADJUST_MINSIZE | wx.ALIGN_CENTER_HORIZONTAL
  206. sizer = wx.BoxSizer(wx.VERTICAL)
  207. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  208. location_sizer = wx.FlexGridSizer(rows=1, cols=2, vgap=4, hgap=4)
  209. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  210. select_sizer = wx.FlexGridSizer(rows=2, cols=2, vgap=4, hgap=4)
  211. manage_boxsizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  212. manage_sizer = wx.BoxSizer(wx.VERTICAL)
  213. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  214. # gis data directory
  215. dbase_sizer.Add(item=self.ldbase, proportion=0,
  216. flag=wx.ALIGN_CENTER_VERTICAL |
  217. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  218. border=3)
  219. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  220. flag=wx.ALIGN_CENTER_VERTICAL |
  221. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  222. border=3)
  223. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  224. flag=wx.ALIGN_CENTER_VERTICAL |
  225. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  226. border=3)
  227. # select sizer
  228. select_sizer.Add(item=self.llocation, proportion=0,
  229. flag=label_style | wx.ALL,
  230. border=3)
  231. select_sizer.Add(item=self.lmapset, proportion=0,
  232. flag=label_style | wx.ALL,
  233. border=3)
  234. select_sizer.Add(item=self.lpanel, proportion=0,
  235. flag=wx.ADJUST_MINSIZE |
  236. wx.ALIGN_CENTER_VERTICAL |
  237. wx.ALIGN_CENTER_HORIZONTAL)
  238. select_sizer.Add(item=self.mpanel, proportion=0,
  239. flag=wx.ADJUST_MINSIZE |
  240. wx.ALIGN_CENTER_VERTICAL |
  241. wx.ALIGN_CENTER_HORIZONTAL)
  242. select_boxsizer.Add(item=select_sizer, proportion=0)
  243. # define new location and mapset
  244. manage_sizer.Add(item=self.ldefine, proportion=0,
  245. flag=label_style | wx.ALL,
  246. border=3)
  247. manage_sizer.Add(item=self.bwizard, proportion=0,
  248. flag=label_style | wx.BOTTOM,
  249. border=5)
  250. manage_sizer.Add(item=self.lcreate, proportion=0,
  251. flag=label_style | wx.ALL,
  252. border=3)
  253. manage_sizer.Add(item=self.bmapset, proportion=0,
  254. flag=label_style | wx.BOTTOM,
  255. border=5)
  256. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  257. flag=label_style | wx.ALL,
  258. border=3)
  259. manage_sizer.Add(item=self.manageloc, proportion=0,
  260. flag=label_style | wx.BOTTOM,
  261. border=5)
  262. manage_boxsizer.Add(item=manage_sizer, proportion=0)
  263. # location sizer
  264. location_sizer.Add(item=select_boxsizer, proportion=0,
  265. flag=wx.ADJUST_MINSIZE |
  266. wx.ALIGN_CENTER_VERTICAL |
  267. wx.ALIGN_CENTER_HORIZONTAL |
  268. wx.RIGHT | wx.LEFT | wx.EXPAND,
  269. border=3) # GISDBASE setting
  270. location_sizer.Add(item=manage_boxsizer, proportion=0,
  271. flag=wx.ADJUST_MINSIZE |
  272. wx.ALIGN_TOP |
  273. wx.ALIGN_CENTER_HORIZONTAL |
  274. wx.RIGHT | wx.EXPAND,
  275. border=3)
  276. # buttons
  277. btns_sizer.Add(item=self.bstart, proportion=0,
  278. flag=wx.ALIGN_CENTER_HORIZONTAL |
  279. wx.ALIGN_CENTER_VERTICAL |
  280. wx.ALL,
  281. border=5)
  282. btns_sizer.Add(item=self.bexit, proportion=0,
  283. flag=wx.ALIGN_CENTER_HORIZONTAL |
  284. wx.ALIGN_CENTER_VERTICAL |
  285. wx.ALL,
  286. border=5)
  287. btns_sizer.Add(item=self.bhelp, proportion=0,
  288. flag=wx.ALIGN_CENTER_HORIZONTAL |
  289. wx.ALIGN_CENTER_VERTICAL |
  290. wx.ALL,
  291. border=5)
  292. # main sizer
  293. sizer.Add(item=self.hbitmap,
  294. proportion=0,
  295. flag=wx.ALIGN_CENTER_VERTICAL |
  296. wx.ALIGN_CENTER_HORIZONTAL |
  297. wx.ALL,
  298. border=3) # image
  299. sizer.Add(item=self.lwelcome, # welcome message
  300. proportion=0,
  301. flag=wx.ALIGN_CENTER_VERTICAL |
  302. wx.ALIGN_CENTER_HORIZONTAL |
  303. wx.BOTTOM,
  304. border=5)
  305. sizer.Add(item=self.ltitle, # title
  306. proportion=0,
  307. flag=wx.ALIGN_CENTER_VERTICAL |
  308. wx.ALIGN_CENTER_HORIZONTAL)
  309. sizer.Add(item=dbase_sizer, proportion=0,
  310. flag=wx.ALIGN_CENTER_HORIZONTAL |
  311. wx.RIGHT | wx.LEFT,
  312. border=1) # GISDBASE setting
  313. sizer.Add(item=location_sizer, proportion=1,
  314. flag=wx.ALIGN_CENTER_VERTICAL |
  315. wx.ALIGN_CENTER_HORIZONTAL |
  316. wx.RIGHT | wx.LEFT,
  317. border=1)
  318. sizer.Add(item=btns_sizer, proportion=0,
  319. flag=wx.ALIGN_CENTER_VERTICAL |
  320. wx.ALIGN_CENTER_HORIZONTAL |
  321. wx.RIGHT | wx.LEFT,
  322. border=1)
  323. self.panel.SetAutoLayout(True)
  324. self.panel.SetSizer(sizer)
  325. sizer.Fit(self.panel)
  326. sizer.SetSizeHints(self)
  327. self.Layout()
  328. def _read_grassrc(self):
  329. """!Read variables from $HOME/.grass7/rc file
  330. """
  331. grassrc = {}
  332. gisrc = os.getenv("GISRC")
  333. if gisrc and os.path.isfile(gisrc):
  334. try:
  335. rc = open(gisrc, "r")
  336. for line in rc.readlines():
  337. key, val = line.split(":", 1)
  338. grassrc[key.strip()] = val.strip()
  339. finally:
  340. rc.close()
  341. return grassrc
  342. def GetRCValue(self, value):
  343. "Return GRASS variable (read from GISRC)"""
  344. if self.grassrc.has_key(value):
  345. return self.grassrc[value]
  346. else:
  347. return None
  348. def OnWizard(self, event):
  349. """!Location wizard started"""
  350. from gui_modules import location_wizard
  351. gWizard = location_wizard.LocationWizard(parent = self,
  352. grassdatabase = self.tgisdbase.GetValue())
  353. if gWizard.location != None:
  354. self.OnSetDatabase(event)
  355. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  356. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  357. self.lbmapsets.SetSelection(0)
  358. def OnManageLoc(self, event):
  359. """!Location management choice control handler
  360. """
  361. sel = event.GetSelection()
  362. if sel == 0:
  363. self.RenameMapset()
  364. elif sel == 1:
  365. self.RenameLocation()
  366. elif sel == 2:
  367. self.DeleteMapset()
  368. elif sel == 3:
  369. self.DeleteLocation()
  370. event.Skip()
  371. def RenameMapset(self):
  372. """!Rename selected mapset
  373. """
  374. location = utils.UnicodeString(self.listOfLocations[self.lblocations.GetSelection()])
  375. mapset = utils.UnicodeString(self.listOfMapsets[self.lbmapsets.GetSelection()])
  376. if mapset == 'PERMANENT':
  377. GMessage(parent = self,
  378. message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  379. 'This mapset cannot be renamed.'),
  380. msgType = 'info')
  381. return
  382. dlg = wx.TextEntryDialog(parent=self,
  383. message=_('Current name: %s\n\nEnter new name:') % mapset,
  384. caption=_('Rename selected mapset'))
  385. if dlg.ShowModal() == wx.ID_OK:
  386. newmapset = dlg.GetValue()
  387. if newmapset == mapset:
  388. dlg.Destroy()
  389. return
  390. if newmapset in self.listOfMapsets:
  391. wx.MessageBox(parent = self,
  392. caption = _('Message'),
  393. message=_('Unable to rename mapset.\n\n'
  394. 'Mapset <%s> already exists in location.') % newmapset,
  395. style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  396. else:
  397. try:
  398. os.rename(os.path.join(self.gisdbase, location, mapset),
  399. os.path.join(self.gisdbase, location, newmapset))
  400. self.OnSelectLocation(None)
  401. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  402. except StandardError, e:
  403. wx.MessageBox(parent = self,
  404. caption = _('Error'),
  405. message=_('Unable to rename mapset.\n\n%s') % e,
  406. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  407. dlg.Destroy()
  408. def RenameLocation(self):
  409. """!Rename selected location
  410. """
  411. location = utils.UnicodeString(self.listOfLocations[self.lblocations.GetSelection()])
  412. dlg = wx.TextEntryDialog(parent=self,
  413. message=_('Current name: %s\n\nEnter new name:') % location,
  414. caption=_('Rename selected location'))
  415. if dlg.ShowModal() == wx.ID_OK:
  416. newlocation = dlg.GetValue()
  417. if newlocation == location:
  418. dlg.Destroy()
  419. return
  420. if newlocation in self.listOfLocations:
  421. wx.MessageBox(parent = self,
  422. caption = _('Message'),
  423. message=_('Unable to rename location.\n\n'
  424. 'Location <%s> already exists in GRASS database.') % newlocation,
  425. style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  426. else:
  427. try:
  428. os.rename(os.path.join(self.gisdbase, location),
  429. os.path.join(self.gisdbase, newlocation))
  430. self.UpdateLocations(self.gisdbase)
  431. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  432. self.UpdateMapsets(newlocation)
  433. except StandardError, e:
  434. wx.MessageBox(parent = self,
  435. caption = _('Error'),
  436. message=_('Unable to rename location.\n\n%s') % e,
  437. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  438. dlg.Destroy()
  439. def DeleteMapset(self):
  440. """!Delete selected mapset
  441. """
  442. location = self.listOfLocations[self.lblocations.GetSelection()]
  443. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  444. if mapset == 'PERMANENT':
  445. GMessage(parent = self,
  446. message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  447. 'This mapset cannot be deleted.'),
  448. msgType = 'info')
  449. return
  450. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting mapset <%(mapset)s> "
  451. "from location <%(location)s>?\n\n"
  452. "ALL MAPS included in this mapset will be "
  453. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  454. 'location' : location},
  455. caption=_("Delete selected mapset"),
  456. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  457. if dlg.ShowModal() == wx.ID_YES:
  458. try:
  459. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  460. self.OnSelectLocation(None)
  461. self.lbmapsets.SetSelection(0)
  462. except:
  463. wx.MessageBox(message=_('Unable to delete mapset'))
  464. dlg.Destroy()
  465. def DeleteLocation(self):
  466. """
  467. Delete selected location
  468. """
  469. location = self.listOfLocations[self.lblocations.GetSelection()]
  470. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting "
  471. "location <%s>?\n\n"
  472. "ALL MAPS included in this location will be "
  473. "PERMANENTLY DELETED!") % (location),
  474. caption=_("Delete selected location"),
  475. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  476. if dlg.ShowModal() == wx.ID_YES:
  477. try:
  478. shutil.rmtree(os.path.join(self.gisdbase, location))
  479. self.UpdateLocations(self.gisdbase)
  480. self.lblocations.SetSelection(0)
  481. self.OnSelectLocation(None)
  482. self.lbmapsets.SetSelection(0)
  483. except:
  484. wx.MessageBox(message=_('Unable to delete location'))
  485. dlg.Destroy()
  486. def UpdateLocations(self, dbase):
  487. """!Update list of locations"""
  488. try:
  489. self.listOfLocations = utils.GetListOfLocations(dbase)
  490. except UnicodeEncodeError:
  491. wx.MessageBox(parent = self, caption = _("Error"),
  492. message = _("Unable to set GRASS database. "
  493. "Check your locale settings."),
  494. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  495. self.lblocations.Clear()
  496. self.lblocations.InsertItems(self.listOfLocations, 0)
  497. if len(self.listOfLocations) > 0:
  498. self.lblocations.SetSelection(0)
  499. else:
  500. self.lblocations.SetSelection(wx.NOT_FOUND)
  501. return self.listOfLocations
  502. def UpdateMapsets(self, location):
  503. """!Update list of mapsets"""
  504. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  505. self.listOfMapsetsSelectable = list()
  506. self.listOfMapsets = utils.GetListOfMapsets(self.gisdbase, location)
  507. self.lbmapsets.Clear()
  508. # disable mapset with denied permission
  509. locationName = os.path.basename(location)
  510. try:
  511. ret = gcmd.RunCommand('g.mapset',
  512. read = True,
  513. flags = 'l',
  514. location = locationName,
  515. gisdbase = self.gisdbase)
  516. if not ret:
  517. raise gcmd.CmdError("")
  518. for line in ret.splitlines():
  519. self.listOfMapsetsSelectable += line.split(' ')
  520. except:
  521. gcmd.RunCommand("g.gisenv",
  522. set= "GISDBASE=%s" % self.gisdbase)
  523. gcmd.RunCommand("g.gisenv",
  524. set = "LOCATION_NAME=%s" % locationName)
  525. gcmd.RunCommand("g.gisenv",
  526. set = "MAPSET=PERMANENT")
  527. # first run only
  528. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  529. disabled = []
  530. idx = 0
  531. for mapset in self.listOfMapsets:
  532. mapset = utils.UnicodeString(mapset)
  533. if mapset not in self.listOfMapsetsSelectable or \
  534. os.path.isfile(os.path.join(self.gisdbase,
  535. locationName,
  536. mapset, ".gislock")):
  537. disabled.append(idx)
  538. idx += 1
  539. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  540. return self.listOfMapsets
  541. def OnSelectLocation(self, event):
  542. """!Location selected"""
  543. if event:
  544. self.lblocations.SetSelection(event.GetIndex())
  545. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  546. self.UpdateMapsets(os.path.join(self.gisdbase,
  547. self.listOfLocations[self.lblocations.GetSelection()]))
  548. else:
  549. self.listOfMapsets = []
  550. disabled = []
  551. idx = 0
  552. try:
  553. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  554. except IndexError:
  555. locationName = ''
  556. for mapset in self.listOfMapsets:
  557. if mapset not in self.listOfMapsetsSelectable or \
  558. os.path.isfile(os.path.join(self.gisdbase,
  559. locationName,
  560. mapset, ".gislock")):
  561. disabled.append(idx)
  562. idx += 1
  563. self.lbmapsets.Clear()
  564. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  565. if len(self.listOfMapsets) > 0:
  566. self.lbmapsets.SetSelection(0)
  567. if locationName:
  568. # enable start button when location and mapset is selected
  569. self.bstart.Enable()
  570. self.bmapset.Enable()
  571. self.manageloc.Enable()
  572. else:
  573. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  574. self.bstart.Enable(False)
  575. self.bmapset.Enable(False)
  576. self.manageloc.Enable(False)
  577. def OnSelectMapset(self, event):
  578. """!Mapset selected"""
  579. self.lbmapsets.SetSelection(event.GetIndex())
  580. if event.GetText() not in self.listOfMapsetsSelectable:
  581. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  582. else:
  583. self.FormerMapsetSelection = event.GetIndex()
  584. event.Skip()
  585. def OnSetDatabase(self, event):
  586. """!Database set"""
  587. self.gisdbase = self.tgisdbase.GetValue()
  588. self.UpdateLocations(self.gisdbase)
  589. self.OnSelectLocation(None)
  590. def OnBrowse(self, event):
  591. """'Browse' button clicked"""
  592. grassdata = None
  593. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  594. style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  595. if dlg.ShowModal() == wx.ID_OK:
  596. self.gisdbase = dlg.GetPath()
  597. self.tgisdbase.SetValue(self.gisdbase)
  598. self.OnSetDatabase(event)
  599. dlg.Destroy()
  600. def OnCreateMapset(self,event):
  601. """!Create new mapset"""
  602. self.gisdbase = self.tgisdbase.GetValue()
  603. location = self.listOfLocations[self.lblocations.GetSelection()]
  604. dlg = wx.TextEntryDialog(parent=self,
  605. message=_('Enter name for new mapset:'),
  606. caption=_('Create new mapset'))
  607. if dlg.ShowModal() == wx.ID_OK:
  608. mapset = dlg.GetValue()
  609. try:
  610. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  611. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  612. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  613. os.path.join(self.gisdbase, location, mapset))
  614. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  615. self.OnSelectLocation(None)
  616. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  617. except StandardError, e:
  618. dlg = wx.MessageDialog(parent=self, message=_("Unable to create new mapset: %s") % e,
  619. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  620. dlg.ShowModal()
  621. dlg.Destroy()
  622. return False
  623. return True
  624. def OnStart(self, event):
  625. """'Start GRASS' button clicked"""
  626. gcmd.RunCommand("g.gisenv",
  627. set = "GISDBASE=%s" % \
  628. self.tgisdbase.GetValue())
  629. gcmd.RunCommand("g.gisenv",
  630. set = "LOCATION_NAME=%s" % \
  631. self.listOfLocations[self.lblocations.GetSelection()])
  632. gcmd.RunCommand("g.gisenv",
  633. set = "MAPSET=%s" % \
  634. self.listOfMapsets[self.lbmapsets.GetSelection()])
  635. self.Destroy()
  636. sys.exit(0)
  637. def OnExit(self, event):
  638. """'Exit' button clicked"""
  639. self.Destroy()
  640. sys.exit(2)
  641. def OnHelp(self, event):
  642. """'Help' button clicked"""
  643. # help text in lib/init/helptext.html
  644. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  645. helpFrame = HelpFrame(parent=self, id=wx.ID_ANY,
  646. title=_("GRASS Quickstart"),
  647. size=(640, 480),
  648. file=file)
  649. helpFrame.Show(True)
  650. event.Skip()
  651. def OnCloseWindow(self, event):
  652. """!Close window event"""
  653. event.Skip()
  654. sys.exit(2)
  655. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  656. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  657. non-selectable items (e.g. mapsets with denied permission)"""
  658. def __init__(self, parent, id, size,
  659. choices, disabled=[]):
  660. wx.ListCtrl.__init__(self, parent, id, size=size,
  661. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  662. wx.BORDER_SUNKEN)
  663. listmix.ListCtrlAutoWidthMixin.__init__(self)
  664. self.InsertColumn(0, '')
  665. self.selected = wx.NOT_FOUND
  666. self.__LoadData(choices, disabled)
  667. def __LoadData(self, choices, disabled=[]):
  668. """
  669. Load data into list
  670. @param choices list of item
  671. @param disabled list of indeces of non-selectable items
  672. """
  673. idx = 0
  674. for item in choices:
  675. index = self.InsertStringItem(sys.maxint, item)
  676. self.SetStringItem(index, 0, item)
  677. if idx in disabled:
  678. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  679. idx += 1
  680. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  681. def Clear(self):
  682. self.DeleteAllItems()
  683. def InsertItems(self, choices, pos, disabled=[]):
  684. self.__LoadData(choices, disabled)
  685. def SetSelection(self, item, force = False):
  686. if item != wx.NOT_FOUND and \
  687. (platform.system() != 'Windows' or force):
  688. ### Windows -> FIXME
  689. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  690. self.selected = item
  691. def GetSelection(self):
  692. return self.selected
  693. class StartUp(wx.App):
  694. """!Start-up application"""
  695. def OnInit(self):
  696. wx.InitAllImageHandlers()
  697. StartUp = GRASSStartup()
  698. StartUp.CenterOnScreen()
  699. self.SetTopWindow(StartUp)
  700. StartUp.Show()
  701. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  702. wx.MessageBox(parent=StartUp,
  703. caption=_('Starting GRASS for the first time'),
  704. message=_('GRASS needs a directory in which to store its data. '
  705. 'Create one now if you have not already done so. '
  706. 'A popular choice is "grassdata", located in '
  707. 'your home directory.'),
  708. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  709. StartUp.OnBrowse(None)
  710. return 1
  711. if __name__ == "__main__":
  712. if os.getenv("GISBASE") is None:
  713. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  714. else:
  715. import gettext
  716. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  717. import gui_modules.gcmd as gcmd
  718. import gui_modules.utils as utils
  719. GRASSStartUp = StartUp(0)
  720. GRASSStartUp.MainLoop()