gis_set.py 31 KB

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