gis_set.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  50. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  51. # i18N
  52. import gettext
  53. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  54. #
  55. # graphical elements
  56. #
  57. # image
  58. try:
  59. name = os.path.join(globalvar.ETCDIR, "gui", "images", "startup_banner.png")
  60. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
  61. wx.Bitmap(name=name,
  62. type=wx.BITMAP_TYPE_PNG))
  63. except:
  64. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
  65. # labels
  66. ### crashes when LOCATION doesn't exist
  67. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  68. grassVersion = versionFile.readline().split(' ')[0].rstrip('\n')
  69. versionFile.close()
  70. self.select_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  71. label=" %s " % _("Choose project location and mapset"))
  72. self.manage_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  73. label=" %s " % _("Manage"))
  74. self.lwelcome = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  75. label=_("Welcome to GRASS GIS %s\n"
  76. "The world's leading open source GIS") % grassVersion,
  77. style=wx.ALIGN_CENTRE)
  78. self.ltitle = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  79. label=_("Select an existing project location and mapset\n"
  80. "or define a new location"),
  81. style=wx.ALIGN_CENTRE)
  82. self.ldbase = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  83. label=_("GIS Data Directory:"))
  84. self.llocation = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  85. label=_("Project location\n(projection/coordinate system)"),
  86. style=wx.ALIGN_CENTRE)
  87. self.lmapset = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  88. label=_("Accessible mapsets\n(directories of GIS files)"),
  89. style=wx.ALIGN_CENTRE)
  90. self.lcreate = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  91. label=_("Create new mapset\nin selected location"),
  92. style=wx.ALIGN_CENTRE)
  93. self.ldefine = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  94. label=_("Define new location"),
  95. style=wx.ALIGN_CENTRE)
  96. self.lmanageloc = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  97. label=_("Rename/delete selected\nmapset or location"),
  98. style=wx.ALIGN_CENTRE)
  99. # buttons
  100. self.bstart = wx.Button(parent=self.panel, id=wx.ID_ANY,
  101. label=_("Start GRASS"))
  102. self.bstart.SetDefault()
  103. self.bexit = wx.Button(parent=self.panel, id=wx.ID_EXIT)
  104. self.bstart.SetMinSize((180, self.bexit.GetSize()[1]))
  105. self.bhelp = wx.Button(parent=self.panel, id=wx.ID_HELP)
  106. self.bbrowse = wx.Button(parent=self.panel, id=wx.ID_ANY,
  107. label=_("Browse"))
  108. self.bmapset = wx.Button(parent=self.panel, id=wx.ID_ANY,
  109. label=_("Create mapset"))
  110. self.bwizard = wx.Button(parent=self.panel, id=wx.ID_ANY,
  111. label=_("Location wizard"))
  112. self.manageloc = wx.Choice(parent=self.panel, id=wx.ID_ANY,
  113. choices=[_('Rename mapset'), _('Rename location'),
  114. _('Delete mapset'), _('Delete location')])
  115. self.manageloc.SetSelection(0)
  116. # textinputs
  117. self.tgisdbase = wx.TextCtrl(parent=self.panel, id=wx.ID_ANY, value="", size=(300, -1),
  118. style=wx.TE_PROCESS_ENTER)
  119. # Locations
  120. self.lpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  121. self.lblocations = GListBox(parent=self.lpanel,
  122. id=wx.ID_ANY, size=(180, 200),
  123. choices=self.listOfLocations)
  124. self.lblocations.SetColumnWidth(0, 180)
  125. # TODO: sort; but keep PERMANENT on top of list
  126. # Mapsets
  127. self.mpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  128. self.lbmapsets = GListBox(parent=self.mpanel,
  129. id=wx.ID_ANY, size=(180, 200),
  130. choices=self.listOfMapsets)
  131. self.lbmapsets.SetColumnWidth(0, 180)
  132. # layout & properties
  133. self._set_properties()
  134. self._do_layout()
  135. # events
  136. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  137. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  138. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  139. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  140. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  141. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  142. self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
  143. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  144. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  145. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
  146. self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  147. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  148. def _set_properties(self):
  149. """!Set frame properties"""
  150. self.SetTitle(_("Welcome to GRASS GIS"))
  151. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
  152. wx.BITMAP_TYPE_ICO))
  153. self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
  154. self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  155. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  156. self.bstart.SetToolTipString(_("Enter GRASS session"))
  157. self.bstart.Enable(False)
  158. self.bmapset.Enable(False)
  159. self.manageloc.Enable(False)
  160. # set database
  161. if not self.gisdbase:
  162. # sets an initial path for gisdbase if nothing in GISRC
  163. if os.path.isdir(os.getenv("HOME")):
  164. self.gisdbase = os.getenv("HOME")
  165. else:
  166. self.gisdbase = os.getcwd()
  167. try:
  168. self.tgisdbase.SetValue(self.gisdbase)
  169. except UnicodeDecodeError:
  170. wx.MessageBox(parent = self, caption = _("Error"),
  171. message = _("Unable to set GRASS database. "
  172. "Check your locale settings."),
  173. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  174. self.OnSetDatabase(None)
  175. location = self.GetRCValue("LOCATION_NAME")
  176. if location == "<UNKNOWN>" or \
  177. not os.path.isdir(os.path.join(self.gisdbase, location)):
  178. location = None
  179. if location:
  180. # list of locations
  181. self.UpdateLocations(self.gisdbase)
  182. try:
  183. self.lblocations.SetSelection(self.listOfLocations.index(location),
  184. force=True)
  185. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  186. except ValueError:
  187. print >> sys.stderr, _("ERROR: Location <%s> not found") % \
  188. (location)
  189. # list of mapsets
  190. self.UpdateMapsets(os.path.join(self.gisdbase,location))
  191. mapset = self.GetRCValue("MAPSET")
  192. if mapset:
  193. try:
  194. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  195. force=True)
  196. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  197. except ValueError:
  198. self.lbmapsets.Clear()
  199. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % \
  200. (mapset)
  201. # self.bstart.Enable(True)
  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. if mapset not in self.listOfMapsetsSelectable or \
  498. os.path.isfile(os.path.join(self.gisdbase,
  499. locationName,
  500. mapset, ".gislock")):
  501. disabled.append(idx)
  502. idx += 1
  503. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  504. return self.listOfMapsets
  505. def OnSelectLocation(self, event):
  506. """!Location selected"""
  507. if event:
  508. self.lblocations.SetSelection(event.GetIndex())
  509. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  510. self.UpdateMapsets(os.path.join(self.gisdbase,
  511. self.listOfLocations[self.lblocations.GetSelection()]))
  512. else:
  513. self.listOfMapsets = []
  514. disabled = []
  515. idx = 0
  516. try:
  517. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  518. except IndexError:
  519. locationName = ''
  520. for mapset in self.listOfMapsets:
  521. if mapset not in self.listOfMapsetsSelectable or \
  522. os.path.isfile(os.path.join(self.gisdbase,
  523. locationName,
  524. mapset, ".gislock")):
  525. disabled.append(idx)
  526. idx += 1
  527. self.lbmapsets.Clear()
  528. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  529. if len(self.listOfMapsets) > 0:
  530. self.lbmapsets.SetSelection(0)
  531. if locationName:
  532. # enable start button when location and mapset is selected
  533. self.bstart.Enable()
  534. self.bmapset.Enable()
  535. self.manageloc.Enable()
  536. else:
  537. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  538. self.bstart.Enable(False)
  539. self.bmapset.Enable(False)
  540. self.manageloc.Enable(False)
  541. def OnSelectMapset(self, event):
  542. """!Mapset selected"""
  543. self.lbmapsets.SetSelection(event.GetIndex())
  544. if event.GetText() not in self.listOfMapsetsSelectable:
  545. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  546. else:
  547. self.FormerMapsetSelection = event.GetIndex()
  548. event.Skip()
  549. def OnSetDatabase(self, event):
  550. """!Database set"""
  551. self.gisdbase = self.tgisdbase.GetValue()
  552. self.UpdateLocations(self.gisdbase)
  553. self.OnSelectLocation(None)
  554. def OnBrowse(self, event):
  555. """'Browse' button clicked"""
  556. grassdata = None
  557. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  558. style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  559. if dlg.ShowModal() == wx.ID_OK:
  560. self.gisdbase = dlg.GetPath()
  561. self.tgisdbase.SetValue(self.gisdbase)
  562. self.OnSetDatabase(event)
  563. dlg.Destroy()
  564. def OnCreateMapset(self,event):
  565. """!Create new mapset"""
  566. self.gisdbase = self.tgisdbase.GetValue()
  567. location = self.listOfLocations[self.lblocations.GetSelection()]
  568. dlg = wx.TextEntryDialog(parent=self,
  569. message=_('Enter name for new mapset:'),
  570. caption=_('Create new mapset'))
  571. if dlg.ShowModal() == wx.ID_OK:
  572. mapset = dlg.GetValue()
  573. try:
  574. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  575. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  576. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  577. os.path.join(self.gisdbase, location, mapset))
  578. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  579. self.OnSelectLocation(None)
  580. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  581. except StandardError, e:
  582. dlg = wx.MessageDialog(parent=self, message=_("Unable to create new mapset: %s") % e,
  583. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  584. dlg.ShowModal()
  585. dlg.Destroy()
  586. return False
  587. return True
  588. def OnStart(self, event):
  589. """'Start GRASS' button clicked"""
  590. gcmd.RunCommand("g.gisenv",
  591. set = "GISDBASE=%s" % \
  592. self.tgisdbase.GetValue())
  593. gcmd.RunCommand("g.gisenv",
  594. set = "LOCATION_NAME=%s" % \
  595. self.listOfLocations[self.lblocations.GetSelection()])
  596. gcmd.RunCommand("g.gisenv",
  597. set = "MAPSET=%s" % \
  598. self.listOfMapsets[self.lbmapsets.GetSelection()])
  599. self.Destroy()
  600. sys.exit(0)
  601. def OnExit(self, event):
  602. """'Exit' button clicked"""
  603. self.Destroy()
  604. sys.exit (2)
  605. def OnHelp(self, event):
  606. """'Help' button clicked"""
  607. # help text in lib/init/helptext.html
  608. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  609. helpFrame = help.HelpWindow(parent=self, id=wx.ID_ANY,
  610. title=_("GRASS Quickstart"),
  611. size=(640, 480),
  612. file=file)
  613. helpFrame.Show(True)
  614. event.Skip()
  615. def OnCloseWindow(self, event):
  616. """!Close window event"""
  617. event.Skip()
  618. sys.exit(2)
  619. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  620. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  621. non-selectable items (e.g. mapsets with denied permission)"""
  622. def __init__(self, parent, id, size,
  623. choices, disabled=[]):
  624. wx.ListCtrl.__init__(self, parent, id, size=size,
  625. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  626. wx.BORDER_SUNKEN)
  627. listmix.ListCtrlAutoWidthMixin.__init__(self)
  628. self.InsertColumn(0, '')
  629. self.selected = wx.NOT_FOUND
  630. self.__LoadData(choices, disabled)
  631. def __LoadData(self, choices, disabled=[]):
  632. """
  633. Load data into list
  634. @param choices list of item
  635. @param disabled list of indeces of non-selectable items
  636. """
  637. idx = 0
  638. for item in choices:
  639. index = self.InsertStringItem(sys.maxint, item)
  640. self.SetStringItem(index, 0, item)
  641. if idx in disabled:
  642. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  643. idx += 1
  644. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  645. def Clear(self):
  646. self.DeleteAllItems()
  647. def InsertItems(self, choices, pos, disabled=[]):
  648. self.__LoadData(choices, disabled)
  649. def SetSelection(self, item, force = False):
  650. if item != wx.NOT_FOUND and \
  651. (platform.system() != 'Windows' or force):
  652. ### Windows -> FIXME
  653. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  654. self.selected = item
  655. def GetSelection(self):
  656. return self.selected
  657. class StartUp(wx.App):
  658. """!Start-up application"""
  659. def OnInit(self):
  660. wx.InitAllImageHandlers()
  661. StartUp = GRASSStartup()
  662. StartUp.CenterOnScreen()
  663. self.SetTopWindow(StartUp)
  664. StartUp.Show()
  665. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  666. wx.MessageBox(parent=StartUp,
  667. caption=_('Starting GRASS for the first time'),
  668. message=_('GRASS needs a directory in which to store its data. '
  669. 'Create one now if you have not already done so. '
  670. 'A popular choice is "grassdata", located in '
  671. 'your home directory.'),
  672. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  673. StartUp.OnBrowse(None)
  674. return 1
  675. if __name__ == "__main__":
  676. if os.getenv("GISBASE") is None:
  677. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  678. else:
  679. import gettext
  680. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  681. import gui_modules.gcmd as gcmd
  682. import gui_modules.utils as utils
  683. GRASSStartUp = StartUp(0)
  684. GRASSStartUp.MainLoop()