gis_set.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. self.tgisdbase.SetValue(self.gisdbase)
  168. self.OnSetDatabase(None)
  169. location = self.GetRCValue("LOCATION_NAME")
  170. if location == "<UNKNOWN>" or \
  171. not os.path.isdir(os.path.join(self.gisdbase, location)):
  172. location = None
  173. if location:
  174. # list of locations
  175. self.UpdateLocations(self.gisdbase)
  176. try:
  177. self.lblocations.SetSelection(self.listOfLocations.index(location),
  178. force=True)
  179. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  180. except ValueError:
  181. print >> sys.stderr, _("ERROR: Location <%s> not found") % \
  182. (location)
  183. # list of mapsets
  184. self.UpdateMapsets(os.path.join(self.gisdbase,location))
  185. mapset = self.GetRCValue("MAPSET")
  186. if mapset:
  187. try:
  188. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  189. force=True)
  190. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  191. except ValueError:
  192. self.lbmapsets.Clear()
  193. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % \
  194. (mapset)
  195. # self.bstart.Enable(True)
  196. def _do_layout(self):
  197. label_style = wx.ADJUST_MINSIZE | wx.ALIGN_CENTER_HORIZONTAL
  198. sizer = wx.BoxSizer(wx.VERTICAL)
  199. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  200. location_sizer = wx.FlexGridSizer(rows=1, cols=2, vgap=4, hgap=4)
  201. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  202. select_sizer = wx.FlexGridSizer(rows=2, cols=2, vgap=4, hgap=4)
  203. manage_boxsizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  204. manage_sizer = wx.BoxSizer(wx.VERTICAL)
  205. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  206. # gis data directory
  207. dbase_sizer.Add(item=self.ldbase, proportion=0,
  208. flag=wx.ALIGN_CENTER_VERTICAL |
  209. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  210. border=3)
  211. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  212. flag=wx.ALIGN_CENTER_VERTICAL |
  213. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  214. border=3)
  215. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  216. flag=wx.ALIGN_CENTER_VERTICAL |
  217. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  218. border=3)
  219. # select sizer
  220. select_sizer.Add(item=self.llocation, proportion=0,
  221. flag=label_style | wx.ALL,
  222. border=3)
  223. select_sizer.Add(item=self.lmapset, proportion=0,
  224. flag=label_style | wx.ALL,
  225. border=3)
  226. select_sizer.Add(item=self.lpanel, proportion=0,
  227. flag=wx.ADJUST_MINSIZE |
  228. wx.ALIGN_CENTER_VERTICAL |
  229. wx.ALIGN_CENTER_HORIZONTAL)
  230. select_sizer.Add(item=self.mpanel, proportion=0,
  231. flag=wx.ADJUST_MINSIZE |
  232. wx.ALIGN_CENTER_VERTICAL |
  233. wx.ALIGN_CENTER_HORIZONTAL)
  234. select_boxsizer.Add(item=select_sizer, proportion=0)
  235. # define new location and mapset
  236. manage_sizer.Add(item=self.ldefine, proportion=0,
  237. flag=label_style | wx.ALL,
  238. border=3)
  239. manage_sizer.Add(item=self.bwizard, proportion=0,
  240. flag=label_style | wx.BOTTOM,
  241. border=5)
  242. manage_sizer.Add(item=self.lcreate, proportion=0,
  243. flag=label_style | wx.ALL,
  244. border=3)
  245. manage_sizer.Add(item=self.bmapset, proportion=0,
  246. flag=label_style | wx.BOTTOM,
  247. border=5)
  248. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  249. flag=label_style | wx.ALL,
  250. border=3)
  251. manage_sizer.Add(item=self.manageloc, proportion=0,
  252. flag=label_style | wx.BOTTOM,
  253. border=5)
  254. manage_boxsizer.Add(item=manage_sizer, proportion=0)
  255. # location sizer
  256. location_sizer.Add(item=select_boxsizer, proportion=0,
  257. flag=wx.ADJUST_MINSIZE |
  258. wx.ALIGN_CENTER_VERTICAL |
  259. wx.ALIGN_CENTER_HORIZONTAL |
  260. wx.RIGHT | wx.LEFT | wx.EXPAND,
  261. border=3) # GISDBASE setting
  262. location_sizer.Add(item=manage_boxsizer, proportion=0,
  263. flag=wx.ADJUST_MINSIZE |
  264. wx.ALIGN_TOP |
  265. wx.ALIGN_CENTER_HORIZONTAL |
  266. wx.RIGHT | wx.EXPAND,
  267. border=3)
  268. # buttons
  269. btns_sizer.Add(item=self.bstart, 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.bexit, proportion=0,
  275. flag=wx.ALIGN_CENTER_HORIZONTAL |
  276. wx.ALIGN_CENTER_VERTICAL |
  277. wx.ALL,
  278. border=5)
  279. btns_sizer.Add(item=self.bhelp, proportion=0,
  280. flag=wx.ALIGN_CENTER_HORIZONTAL |
  281. wx.ALIGN_CENTER_VERTICAL |
  282. wx.ALL,
  283. border=5)
  284. # main sizer
  285. sizer.Add(item=self.hbitmap,
  286. proportion=0,
  287. flag=wx.ALIGN_CENTER_VERTICAL |
  288. wx.ALIGN_CENTER_HORIZONTAL |
  289. wx.ALL,
  290. border=3) # image
  291. sizer.Add(item=self.lwelcome, # welcome message
  292. proportion=0,
  293. flag=wx.ALIGN_CENTER_VERTICAL |
  294. wx.ALIGN_CENTER_HORIZONTAL |
  295. wx.BOTTOM,
  296. border=5)
  297. sizer.Add(item=self.ltitle, # title
  298. proportion=0,
  299. flag=wx.ALIGN_CENTER_VERTICAL |
  300. wx.ALIGN_CENTER_HORIZONTAL)
  301. sizer.Add(item=dbase_sizer, proportion=0,
  302. flag=wx.ALIGN_CENTER_HORIZONTAL |
  303. wx.RIGHT | wx.LEFT,
  304. border=1) # GISDBASE setting
  305. sizer.Add(item=location_sizer, proportion=1,
  306. flag=wx.ALIGN_CENTER_VERTICAL |
  307. wx.ALIGN_CENTER_HORIZONTAL |
  308. wx.RIGHT | wx.LEFT,
  309. border=1)
  310. sizer.Add(item=btns_sizer, proportion=0,
  311. flag=wx.ALIGN_CENTER_VERTICAL |
  312. wx.ALIGN_CENTER_HORIZONTAL |
  313. wx.RIGHT | wx.LEFT,
  314. border=1)
  315. self.panel.SetAutoLayout(True)
  316. self.panel.SetSizer(sizer)
  317. sizer.Fit(self.panel)
  318. sizer.SetSizeHints(self)
  319. self.Layout()
  320. def _read_grassrc(self):
  321. """!Read variables from $HOME/.grass7/rc file
  322. """
  323. grassrc = {}
  324. gisrc = os.getenv("GISRC")
  325. if gisrc and os.path.isfile(gisrc):
  326. try:
  327. rc = open(gisrc, "r")
  328. for line in rc.readlines():
  329. key, val = line.split(":", 1)
  330. grassrc[key.strip()] = val.strip()
  331. finally:
  332. rc.close()
  333. return grassrc
  334. def GetRCValue(self, value):
  335. "Return GRASS variable (read from GISRC)"""
  336. if self.grassrc.has_key(value):
  337. return self.grassrc[value]
  338. else:
  339. return None
  340. def OnWizard(self, event):
  341. """!Location wizard started"""
  342. from gui_modules import location_wizard
  343. gWizard = location_wizard.LocationWizard(self, self.tgisdbase.GetValue())
  344. if gWizard.location != None:
  345. self.OnSetDatabase(event)
  346. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  347. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  348. self.lbmapsets.SetSelection(0)
  349. def OnManageLoc(self, event):
  350. """
  351. Location management choice control handler
  352. """
  353. if event.GetString() == 'Rename mapset':
  354. self.RenameMapset()
  355. elif event.GetString() == 'Rename location':
  356. self.RenameLocation()
  357. elif event.GetString() == 'Delete mapset':
  358. self.DeleteMapset()
  359. elif event.GetString() == 'Delete location':
  360. self.DeleteLocation()
  361. def RenameMapset(self):
  362. """
  363. Rename selected mapset
  364. """
  365. location = self.listOfLocations[self.lblocations.GetSelection()]
  366. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  367. dlg = wx.TextEntryDialog(parent=self,
  368. message=_('Current name: %s\nEnter new name:') % mapset,
  369. caption=_('Rename selected mapset'))
  370. if dlg.ShowModal() == wx.ID_OK:
  371. newmapset = dlg.GetValue()
  372. if newmapset != mapset:
  373. try:
  374. os.rename(os.path.join(self.gisdbase, location, mapset),
  375. os.path.join(self.gisdbase, location, newmapset))
  376. self.OnSelectLocation(None)
  377. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  378. except:
  379. wx.MessageBox(message=_('Unable to rename mapset'))
  380. dlg.Destroy()
  381. def RenameLocation(self):
  382. """
  383. Rename selected location
  384. """
  385. location = self.listOfLocations[self.lblocations.GetSelection()]
  386. dlg = wx.TextEntryDialog(parent=self,
  387. message=_('Current name: %s\nEnter new name:') % location,
  388. caption=_('Rename selected location'))
  389. if dlg.ShowModal() == wx.ID_OK:
  390. newlocation = dlg.GetValue()
  391. if newlocation != location:
  392. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  393. try:
  394. os.rename(os.path.join(self.gisdbase, location),
  395. os.path.join(self.gisdbase, newlocation))
  396. self.UpdateLocations(self.gisdbase)
  397. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  398. self.UpdateMapsets(newlocation)
  399. except:
  400. wx.MessageBox(message=_('Unable to rename location'))
  401. dlg.Destroy()
  402. def DeleteMapset(self):
  403. """
  404. Delete selected mapset
  405. """
  406. location = self.listOfLocations[self.lblocations.GetSelection()]
  407. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  408. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting mapset <%(mapset)s> "
  409. "from location <%(location)s>?\n\n"
  410. "ALL MAPS included in this mapset will be "
  411. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  412. 'location' : location},
  413. caption=_("Delete selected mapset"),
  414. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  415. if dlg.ShowModal() == wx.ID_YES:
  416. try:
  417. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  418. self.OnSelectLocation(None)
  419. self.lbmapsets.SetSelection(0)
  420. except:
  421. wx.MessageBox(message=_('Unable to delete mapset'))
  422. dlg.Destroy()
  423. def DeleteLocation(self):
  424. """
  425. Delete selected location
  426. """
  427. location = self.listOfLocations[self.lblocations.GetSelection()]
  428. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting "
  429. "location <%s>?\n\n"
  430. "ALL MAPS included in this location will be "
  431. "PERMANENTLY DELETED!") % (location),
  432. caption=_("Delete selected location"),
  433. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  434. if dlg.ShowModal() == wx.ID_YES:
  435. try:
  436. shutil.rmtree(os.path.join(self.gisdbase, location))
  437. self.UpdateLocations(self.gisdbase)
  438. self.lblocations.SetSelection(0)
  439. self.OnSelectLocation(None)
  440. self.lbmapsets.SetSelection(0)
  441. except:
  442. wx.MessageBox(message=_('Unable to delete location'))
  443. dlg.Destroy()
  444. def UpdateLocations(self, dbase):
  445. """!Update list of locations"""
  446. self.listOfLocations = utils.GetListOfLocations(dbase)
  447. self.lblocations.Clear()
  448. self.lblocations.InsertItems(self.listOfLocations, 0)
  449. if len(self.listOfLocations) > 0:
  450. self.lblocations.SetSelection(0)
  451. else:
  452. self.lblocations.SetSelection(wx.NOT_FOUND)
  453. return self.listOfLocations
  454. def UpdateMapsets(self, location):
  455. """!Update list of mapsets"""
  456. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  457. self.listOfMapsetsSelectable = list()
  458. self.listOfMapsets = utils.GetListOfMapsets(self.gisdbase, location)
  459. self.lbmapsets.Clear()
  460. # disable mapset with denied permission
  461. locationName = os.path.basename(location)
  462. try:
  463. ret = gcmd.RunCommand('g.mapset',
  464. read = True,
  465. flags = 'l',
  466. location = locationName,
  467. gisdbase = self.gisdbase)
  468. if not ret:
  469. raise gcmd.CmdError("")
  470. for line in ret.splitlines():
  471. self.listOfMapsetsSelectable += line.split(' ')
  472. except:
  473. gcmd.RunCommand("g.gisenv",
  474. set= "GISDBASE=%s" % self.gisdbase)
  475. gcmd.RunCommand("g.gisenv",
  476. set = "LOCATION_NAME=%s" % locationName)
  477. gcmd.RunCommand("g.gisenv",
  478. set = "MAPSET=PERMANENT")
  479. # first run only
  480. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  481. disabled = []
  482. idx = 0
  483. for mapset in self.listOfMapsets:
  484. if mapset not in self.listOfMapsetsSelectable or \
  485. os.path.isfile(os.path.join(self.gisdbase,
  486. locationName,
  487. mapset, ".gislock")):
  488. disabled.append(idx)
  489. idx += 1
  490. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  491. return self.listOfMapsets
  492. def OnSelectLocation(self, event):
  493. """!Location selected"""
  494. if event:
  495. self.lblocations.SetSelection(event.GetIndex())
  496. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  497. self.UpdateMapsets(os.path.join(self.gisdbase,
  498. self.listOfLocations[self.lblocations.GetSelection()]))
  499. else:
  500. self.listOfMapsets = []
  501. disabled = []
  502. idx = 0
  503. try:
  504. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  505. except IndexError:
  506. locationName = ''
  507. for mapset in self.listOfMapsets:
  508. if mapset not in self.listOfMapsetsSelectable or \
  509. os.path.isfile(os.path.join(self.gisdbase,
  510. locationName,
  511. mapset, ".gislock")):
  512. disabled.append(idx)
  513. idx += 1
  514. self.lbmapsets.Clear()
  515. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  516. if len(self.listOfMapsets) > 0:
  517. self.lbmapsets.SetSelection(0)
  518. if locationName:
  519. # enable start button when location and mapset is selected
  520. self.bstart.Enable()
  521. self.bmapset.Enable()
  522. self.manageloc.Enable()
  523. else:
  524. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  525. self.bstart.Enable(False)
  526. self.bmapset.Enable(False)
  527. self.manageloc.Enable(False)
  528. def OnSelectMapset(self, event):
  529. """!Mapset selected"""
  530. self.lbmapsets.SetSelection(event.GetIndex())
  531. if event.GetText() not in self.listOfMapsetsSelectable:
  532. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  533. else:
  534. self.FormerMapsetSelection = event.GetIndex()
  535. event.Skip()
  536. def OnSetDatabase(self, event):
  537. """!Database set"""
  538. self.gisdbase = self.tgisdbase.GetValue()
  539. self.UpdateLocations(self.gisdbase)
  540. self.OnSelectLocation(None)
  541. def OnBrowse(self, event):
  542. """'Browse' button clicked"""
  543. grassdata = None
  544. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  545. style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  546. if dlg.ShowModal() == wx.ID_OK:
  547. self.gisdbase = dlg.GetPath()
  548. self.tgisdbase.SetValue(self.gisdbase)
  549. self.OnSetDatabase(event)
  550. dlg.Destroy()
  551. def OnCreateMapset(self,event):
  552. """!Create new mapset"""
  553. self.gisdbase = self.tgisdbase.GetValue()
  554. location = self.listOfLocations[self.lblocations.GetSelection()]
  555. dlg = wx.TextEntryDialog(parent=self,
  556. message=_('Enter name for new mapset:'),
  557. caption=_('Create new mapset'))
  558. if dlg.ShowModal() == wx.ID_OK:
  559. mapset = dlg.GetValue()
  560. try:
  561. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  562. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  563. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  564. os.path.join(self.gisdbase, location, mapset))
  565. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  566. self.OnSelectLocation(None)
  567. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  568. except StandardError, e:
  569. dlg = wx.MessageDialog(parent=self, message=_("Unable to create new mapset: %s") % e,
  570. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  571. dlg.ShowModal()
  572. dlg.Destroy()
  573. return False
  574. return True
  575. def OnStart(self, event):
  576. """'Start GRASS' button clicked"""
  577. gcmd.RunCommand("g.gisenv",
  578. set = "GISDBASE=%s" % \
  579. self.tgisdbase.GetValue())
  580. gcmd.RunCommand("g.gisenv",
  581. set = "LOCATION_NAME=%s" % \
  582. self.listOfLocations[self.lblocations.GetSelection()])
  583. gcmd.RunCommand("g.gisenv",
  584. set = "MAPSET=%s" % \
  585. self.listOfMapsets[self.lbmapsets.GetSelection()])
  586. self.Destroy()
  587. sys.exit(0)
  588. def OnExit(self, event):
  589. """'Exit' button clicked"""
  590. self.Destroy()
  591. sys.exit (2)
  592. def OnHelp(self, event):
  593. """'Help' button clicked"""
  594. # help text in lib/init/helptext.html
  595. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  596. helpFrame = help.HelpWindow(parent=self, id=wx.ID_ANY,
  597. title=_("GRASS Quickstart"),
  598. size=(640, 480),
  599. file=file)
  600. helpFrame.Show(True)
  601. event.Skip()
  602. def OnCloseWindow(self, event):
  603. """!Close window event"""
  604. event.Skip()
  605. sys.exit(2)
  606. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  607. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  608. non-selectable items (e.g. mapsets with denied permission)"""
  609. def __init__(self, parent, id, size,
  610. choices, disabled=[]):
  611. wx.ListCtrl.__init__(self, parent, id, size=size,
  612. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  613. wx.BORDER_SUNKEN)
  614. listmix.ListCtrlAutoWidthMixin.__init__(self)
  615. self.InsertColumn(0, '')
  616. self.selected = wx.NOT_FOUND
  617. self.__LoadData(choices, disabled)
  618. def __LoadData(self, choices, disabled=[]):
  619. """
  620. Load data into list
  621. @param choices list of item
  622. @param disabled list of indeces of non-selectable items
  623. """
  624. idx = 0
  625. for item in choices:
  626. index = self.InsertStringItem(sys.maxint, item)
  627. self.SetStringItem(index, 0, item)
  628. if idx in disabled:
  629. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  630. idx += 1
  631. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  632. def Clear(self):
  633. self.DeleteAllItems()
  634. def InsertItems(self, choices, pos, disabled=[]):
  635. self.__LoadData(choices, disabled)
  636. def SetSelection(self, item, force = False):
  637. if item != wx.NOT_FOUND and \
  638. (platform.system() != 'Windows' or force):
  639. ### Windows -> FIXME
  640. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  641. self.selected = item
  642. def GetSelection(self):
  643. return self.selected
  644. class StartUp(wx.App):
  645. """!Start-up application"""
  646. def OnInit(self):
  647. wx.InitAllImageHandlers()
  648. StartUp = GRASSStartup()
  649. StartUp.CenterOnScreen()
  650. self.SetTopWindow(StartUp)
  651. StartUp.Show()
  652. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  653. wx.MessageBox(parent=StartUp,
  654. caption=_('Starting GRASS for the first time'),
  655. message=_('GRASS needs a directory in which to store its data. '
  656. 'Create one now if you have not already done so. '
  657. 'A popular choice is "grassdata", located in '
  658. 'your home directory.'),
  659. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  660. StartUp.OnBrowse(None)
  661. return 1
  662. if __name__ == "__main__":
  663. if os.getenv("GISBASE") is None:
  664. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  665. else:
  666. import gettext
  667. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  668. import gui_modules.gcmd as gcmd
  669. import gui_modules.utils as utils
  670. GRASSStartUp = StartUp(0)
  671. GRASSStartUp.MainLoop()