gis_set.py 32 KB

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