gis_set.py 31 KB

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