gis_set.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. """!
  2. @package gis_set
  3. GRASS start-up screen.
  4. Initialization module for wxPython GRASS GUI.
  5. Location/mapset management (selection, creation, etc.).
  6. Classes:
  7. - gis_set::GRASSStartup
  8. - gis_set::GListBox
  9. - gis_set::StartUp
  10. (C) 2006-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. @author Michael Barton and Jachym Cepicky (original author)
  14. @author Martin Landa <landa.martin gmail.com> (various updates)
  15. """
  16. import os
  17. import sys
  18. import shutil
  19. import copy
  20. import platform
  21. import codecs
  22. ### i18N
  23. import gettext
  24. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  25. from core import globalvar
  26. import wx
  27. import wx.lib.mixins.listctrl as listmix
  28. import wx.lib.scrolledpanel as scrolled
  29. from gui_core.ghelp import HelpFrame
  30. from core.gcmd import GMessage, GError
  31. sys.stderr = codecs.getwriter('utf8')(sys.stderr)
  32. class GRASSStartup(wx.Frame):
  33. """!GRASS start-up screen"""
  34. def __init__(self, parent = None, id = wx.ID_ANY, style = wx.DEFAULT_FRAME_STYLE):
  35. #
  36. # GRASS variables
  37. #
  38. self.gisbase = os.getenv("GISBASE")
  39. self.grassrc = self._readGisRC()
  40. self.gisdbase = self.GetRCValue("GISDBASE")
  41. #
  42. # list of locations/mapsets
  43. #
  44. self.listOfLocations = []
  45. self.listOfMapsets = []
  46. self.listOfMapsetsSelectable = []
  47. wx.Frame.__init__(self, parent = parent, id = id, style = style)
  48. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  49. self.panel = scrolled.ScrolledPanel(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.lblocations = GListBox(parent = self.panel,
  120. id = wx.ID_ANY, size = (180, 200),
  121. choices = self.listOfLocations)
  122. self.lblocations.SetColumnWidth(0, 180)
  123. # TODO: sort; but keep PERMANENT on top of list
  124. # Mapsets
  125. self.lbmapsets = GListBox(parent = self.panel,
  126. id = wx.ID_ANY, size = (180, 200),
  127. choices = self.listOfMapsets)
  128. self.lbmapsets.SetColumnWidth(0, 180)
  129. # layout & properties
  130. self._set_properties()
  131. self._do_layout()
  132. # events
  133. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  134. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  135. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  136. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  137. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  138. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  139. self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
  140. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  141. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  142. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
  143. self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  144. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  145. def _set_properties(self):
  146. """!Set frame properties"""
  147. self.SetTitle(_("Welcome to GRASS GIS"))
  148. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
  149. wx.BITMAP_TYPE_ICO))
  150. self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
  151. self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  152. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  153. self.bstart.SetToolTipString(_("Enter GRASS session"))
  154. self.bstart.Enable(False)
  155. self.bmapset.Enable(False)
  156. self.manageloc.Enable(False)
  157. # set database
  158. if not self.gisdbase:
  159. # sets an initial path for gisdbase if nothing in GISRC
  160. if os.path.isdir(os.getenv("HOME")):
  161. self.gisdbase = os.getenv("HOME")
  162. else:
  163. self.gisdbase = os.getcwd()
  164. try:
  165. self.tgisdbase.SetValue(self.gisdbase)
  166. except UnicodeDecodeError:
  167. wx.MessageBox(parent = self, caption = _("Error"),
  168. message = _("Unable to set GRASS database. "
  169. "Check your locale settings."),
  170. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  171. self.OnSetDatabase(None)
  172. location = self.GetRCValue("LOCATION_NAME")
  173. if location == "<UNKNOWN>" or \
  174. not os.path.isdir(os.path.join(self.gisdbase, location)):
  175. location = None
  176. if location:
  177. # list of locations
  178. self.UpdateLocations(self.gisdbase)
  179. try:
  180. self.lblocations.SetSelection(self.listOfLocations.index(location),
  181. force = True)
  182. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  183. except ValueError:
  184. print >> sys.stderr, _("ERROR: Location <%s> not found") % location
  185. # list of mapsets
  186. self.UpdateMapsets(os.path.join(self.gisdbase, location))
  187. mapset = self.GetRCValue("MAPSET")
  188. if mapset:
  189. try:
  190. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  191. force = True)
  192. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  193. except ValueError:
  194. self.lbmapsets.Clear()
  195. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % mapset
  196. def _do_layout(self):
  197. sizer = wx.BoxSizer(wx.VERTICAL)
  198. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  199. location_sizer = wx.BoxSizer(wx.HORIZONTAL)
  200. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  201. select_sizer = wx.FlexGridSizer(rows = 2, cols = 2, vgap = 4, hgap = 4)
  202. select_sizer.AddGrowableRow(1)
  203. select_sizer.AddGrowableCol(0)
  204. select_sizer.AddGrowableCol(1)
  205. manage_sizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  206. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  207. # gis data directory
  208. dbase_sizer.Add(item = self.ldbase, proportion = 0,
  209. flag = wx.ALIGN_CENTER_VERTICAL |
  210. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  211. border = 3)
  212. dbase_sizer.Add(item = self.tgisdbase, proportion = 1,
  213. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  214. border = 3)
  215. dbase_sizer.Add(item = self.bbrowse, proportion = 0,
  216. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  217. border = 3)
  218. # select sizer
  219. select_sizer.Add(item = self.llocation, proportion = 0,
  220. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  221. border = 3)
  222. select_sizer.Add(item = self.lmapset, proportion = 0,
  223. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  224. border = 3)
  225. select_sizer.Add(item = self.lblocations, proportion = 1,
  226. flag = wx.EXPAND)
  227. select_sizer.Add(item = self.lbmapsets, proportion = 1,
  228. flag = wx.EXPAND)
  229. select_boxsizer.Add(item = select_sizer, proportion = 1,
  230. flag = wx.EXPAND)
  231. # define new location and mapset
  232. manage_sizer.Add(item = self.ldefine, proportion = 0,
  233. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  234. border = 3)
  235. manage_sizer.Add(item = self.bwizard, proportion = 0,
  236. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
  237. border = 5)
  238. manage_sizer.Add(item = self.lcreate, proportion = 0,
  239. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  240. border = 3)
  241. manage_sizer.Add(item = self.bmapset, proportion = 0,
  242. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
  243. border = 5)
  244. manage_sizer.Add(item = self.lmanageloc, proportion = 0,
  245. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  246. border = 3)
  247. manage_sizer.Add(item = self.manageloc, proportion = 0,
  248. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
  249. border = 5)
  250. # location sizer
  251. location_sizer.Add(item = select_boxsizer, proportion = 1,
  252. flag = wx.LEFT | wx.RIGHT | wx.EXPAND,
  253. border = 3)
  254. location_sizer.Add(item = manage_sizer, proportion = 0,
  255. flag = wx.RIGHT | wx.EXPAND,
  256. border = 3)
  257. # buttons
  258. btns_sizer.Add(item = self.bstart, proportion = 0,
  259. flag = wx.ALIGN_CENTER_HORIZONTAL |
  260. wx.ALIGN_CENTER_VERTICAL |
  261. wx.ALL,
  262. border = 5)
  263. btns_sizer.Add(item = self.bexit, proportion = 0,
  264. flag = wx.ALIGN_CENTER_HORIZONTAL |
  265. wx.ALIGN_CENTER_VERTICAL |
  266. wx.ALL,
  267. border = 5)
  268. btns_sizer.Add(item = self.bhelp, proportion = 0,
  269. flag = wx.ALIGN_CENTER_HORIZONTAL |
  270. wx.ALIGN_CENTER_VERTICAL |
  271. wx.ALL,
  272. border = 5)
  273. # main sizer
  274. sizer.Add(item = self.hbitmap,
  275. proportion = 0,
  276. flag = wx.ALIGN_CENTER_VERTICAL |
  277. wx.ALIGN_CENTER_HORIZONTAL |
  278. wx.ALL,
  279. border = 3) # image
  280. sizer.Add(item = self.lwelcome, # welcome message
  281. proportion = 0,
  282. flag = wx.ALIGN_CENTER_VERTICAL |
  283. wx.ALIGN_CENTER_HORIZONTAL |
  284. wx.BOTTOM,
  285. border = 5)
  286. sizer.Add(item = self.ltitle, # title
  287. proportion = 0,
  288. flag = wx.ALIGN_CENTER_VERTICAL |
  289. wx.ALIGN_CENTER_HORIZONTAL)
  290. sizer.Add(item = dbase_sizer, proportion = 0,
  291. flag = wx.ALIGN_CENTER_HORIZONTAL |
  292. wx.RIGHT | wx.LEFT | wx.EXPAND,
  293. border = 20) # GISDBASE setting
  294. sizer.Add(item = location_sizer, proportion = 1,
  295. flag = wx.RIGHT | wx.LEFT | wx.EXPAND,
  296. border = 1)
  297. sizer.Add(item = btns_sizer, proportion = 0,
  298. flag = wx.ALIGN_CENTER_VERTICAL |
  299. wx.ALIGN_CENTER_HORIZONTAL |
  300. wx.RIGHT | wx.LEFT,
  301. border = 1)
  302. self.panel.SetAutoLayout(True)
  303. self.panel.SetSizer(sizer)
  304. sizer.Fit(self.panel)
  305. sizer.SetSizeHints(self)
  306. self.Layout()
  307. def _readGisRC(self):
  308. """!Read variables from $HOME/.grass7/rc file
  309. """
  310. grassrc = {}
  311. gisrc = os.getenv("GISRC")
  312. if gisrc and os.path.isfile(gisrc):
  313. try:
  314. rc = open(gisrc, "r")
  315. for line in rc.readlines():
  316. key, val = line.split(":", 1)
  317. grassrc[key.strip()] = DecodeString(val.strip())
  318. finally:
  319. rc.close()
  320. return grassrc
  321. def GetRCValue(self, value):
  322. """!Return GRASS variable (read from GISRC)
  323. """
  324. if self.grassrc.has_key(value):
  325. return self.grassrc[value]
  326. else:
  327. return None
  328. def OnWizard(self, event):
  329. """!Location wizard started"""
  330. from location_wizard.wizard import LocationWizard
  331. gWizard = LocationWizard(parent = self,
  332. grassdatabase = self.tgisdbase.GetValue())
  333. if gWizard.location != None:
  334. self.OnSetDatabase(event)
  335. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  336. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  337. self.lbmapsets.SetSelection(0)
  338. def OnManageLoc(self, event):
  339. """!Location management choice control handler
  340. """
  341. sel = event.GetSelection()
  342. if sel == 0:
  343. self.RenameMapset()
  344. elif sel == 1:
  345. self.RenameLocation()
  346. elif sel == 2:
  347. self.DeleteMapset()
  348. elif sel == 3:
  349. self.DeleteLocation()
  350. event.Skip()
  351. def RenameMapset(self):
  352. """!Rename selected mapset
  353. """
  354. location = self.listOfLocations[self.lblocations.GetSelection()]
  355. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  356. if mapset == 'PERMANENT':
  357. GMessage(parent = self,
  358. message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  359. 'This mapset cannot be renamed.'))
  360. return
  361. dlg = wx.TextEntryDialog(parent = self,
  362. message = _('Current name: %s\n\nEnter new name:') % mapset,
  363. caption = _('Rename selected mapset'))
  364. if dlg.ShowModal() == wx.ID_OK:
  365. newmapset = dlg.GetValue()
  366. if newmapset == mapset:
  367. dlg.Destroy()
  368. return
  369. if newmapset in self.listOfMapsets:
  370. wx.MessageBox(parent = self,
  371. caption = _('Message'),
  372. message = _('Unable to rename mapset.\n\n'
  373. 'Mapset <%s> already exists in location.') % newmapset,
  374. style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  375. else:
  376. try:
  377. os.rename(os.path.join(self.gisdbase, location, mapset),
  378. os.path.join(self.gisdbase, location, newmapset))
  379. self.OnSelectLocation(None)
  380. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  381. except StandardError, e:
  382. wx.MessageBox(parent = self,
  383. caption = _('Error'),
  384. message = _('Unable to rename mapset.\n\n%s') % e,
  385. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  386. dlg.Destroy()
  387. def RenameLocation(self):
  388. """!Rename selected location
  389. """
  390. location = self.listOfLocations[self.lblocations.GetSelection()]
  391. dlg = wx.TextEntryDialog(parent = self,
  392. message = _('Current name: %s\n\nEnter new name:') % location,
  393. caption = _('Rename selected location'))
  394. if dlg.ShowModal() == wx.ID_OK:
  395. newlocation = dlg.GetValue()
  396. if newlocation == location:
  397. dlg.Destroy()
  398. return
  399. if newlocation in self.listOfLocations:
  400. wx.MessageBox(parent = self,
  401. caption = _('Message'),
  402. message = _('Unable to rename location.\n\n'
  403. 'Location <%s> already exists in GRASS database.') % newlocation,
  404. style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  405. else:
  406. try:
  407. os.rename(os.path.join(self.gisdbase, location),
  408. os.path.join(self.gisdbase, newlocation))
  409. self.UpdateLocations(self.gisdbase)
  410. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  411. self.UpdateMapsets(newlocation)
  412. except StandardError, e:
  413. wx.MessageBox(parent = self,
  414. caption = _('Error'),
  415. message = _('Unable to rename location.\n\n%s') % e,
  416. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  417. dlg.Destroy()
  418. def DeleteMapset(self):
  419. """!Delete selected mapset
  420. """
  421. location = self.listOfLocations[self.lblocations.GetSelection()]
  422. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  423. if mapset == 'PERMANENT':
  424. GMessage(parent = self,
  425. message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  426. 'This mapset cannot be deleted.'))
  427. return
  428. dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting mapset <%(mapset)s> "
  429. "from location <%(location)s>?\n\n"
  430. "ALL MAPS included in this mapset will be "
  431. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  432. 'location' : location},
  433. caption = _("Delete selected mapset"),
  434. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  435. if dlg.ShowModal() == wx.ID_YES:
  436. try:
  437. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  438. self.OnSelectLocation(None)
  439. self.lbmapsets.SetSelection(0)
  440. except:
  441. wx.MessageBox(message = _('Unable to delete mapset'))
  442. dlg.Destroy()
  443. def DeleteLocation(self):
  444. """
  445. Delete selected location
  446. """
  447. location = self.listOfLocations[self.lblocations.GetSelection()]
  448. dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting "
  449. "location <%s>?\n\n"
  450. "ALL MAPS included in this location will be "
  451. "PERMANENTLY DELETED!") % (location),
  452. caption = _("Delete selected location"),
  453. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  454. if dlg.ShowModal() == wx.ID_YES:
  455. try:
  456. shutil.rmtree(os.path.join(self.gisdbase, location))
  457. self.UpdateLocations(self.gisdbase)
  458. self.lblocations.SetSelection(0)
  459. self.OnSelectLocation(None)
  460. self.lbmapsets.SetSelection(0)
  461. except:
  462. wx.MessageBox(message = _('Unable to delete location'))
  463. dlg.Destroy()
  464. def UpdateLocations(self, dbase):
  465. """!Update list of locations"""
  466. try:
  467. self.listOfLocations = GetListOfLocations(dbase)
  468. except UnicodeEncodeError:
  469. wx.MessageBox(parent = self, caption = _("Error"),
  470. message = _("Unable to set GRASS database. "
  471. "Check your locale settings."),
  472. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  473. self.lblocations.Clear()
  474. self.lblocations.InsertItems(self.listOfLocations, 0)
  475. if len(self.listOfLocations) > 0:
  476. self.lblocations.SetSelection(0)
  477. else:
  478. self.lblocations.SetSelection(wx.NOT_FOUND)
  479. return self.listOfLocations
  480. def UpdateMapsets(self, location):
  481. """!Update list of mapsets"""
  482. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  483. self.listOfMapsetsSelectable = list()
  484. self.listOfMapsets = GetListOfMapsets(self.gisdbase, location)
  485. self.lbmapsets.Clear()
  486. # disable mapset with denied permission
  487. locationName = os.path.basename(location)
  488. ret = RunCommand('g.mapset',
  489. read = True,
  490. flags = 'l',
  491. location = locationName,
  492. gisdbase = self.gisdbase)
  493. if ret:
  494. for line in ret.splitlines():
  495. self.listOfMapsetsSelectable += line.split(' ')
  496. else:
  497. RunCommand("g.gisenv",
  498. set = "GISDBASE=%s" % self.gisdbase)
  499. RunCommand("g.gisenv",
  500. set = "LOCATION_NAME=%s" % locationName)
  501. RunCommand("g.gisenv",
  502. set = "MAPSET=PERMANENT")
  503. # first run only
  504. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  505. disabled = []
  506. idx = 0
  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.InsertItems(self.listOfMapsets, 0, disabled = disabled)
  515. return self.listOfMapsets
  516. def OnSelectLocation(self, event):
  517. """!Location selected"""
  518. if event:
  519. self.lblocations.SetSelection(event.GetIndex())
  520. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  521. self.UpdateMapsets(os.path.join(self.gisdbase,
  522. self.listOfLocations[self.lblocations.GetSelection()]))
  523. else:
  524. self.listOfMapsets = []
  525. disabled = []
  526. idx = 0
  527. try:
  528. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  529. except IndexError:
  530. locationName = ''
  531. for mapset in self.listOfMapsets:
  532. if mapset not in self.listOfMapsetsSelectable or \
  533. os.path.isfile(os.path.join(self.gisdbase,
  534. locationName,
  535. mapset, ".gislock")):
  536. disabled.append(idx)
  537. idx += 1
  538. self.lbmapsets.Clear()
  539. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
  540. if len(self.listOfMapsets) > 0:
  541. self.lbmapsets.SetSelection(0)
  542. if locationName:
  543. # enable start button when location and mapset is selected
  544. self.bstart.Enable()
  545. self.bmapset.Enable()
  546. self.manageloc.Enable()
  547. else:
  548. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  549. self.bstart.Enable(False)
  550. self.bmapset.Enable(False)
  551. self.manageloc.Enable(False)
  552. def OnSelectMapset(self, event):
  553. """!Mapset selected"""
  554. self.lbmapsets.SetSelection(event.GetIndex())
  555. if event.GetText() not in self.listOfMapsetsSelectable:
  556. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  557. else:
  558. self.FormerMapsetSelection = event.GetIndex()
  559. event.Skip()
  560. def OnSetDatabase(self, event):
  561. """!Database set"""
  562. self.gisdbase = self.tgisdbase.GetValue()
  563. self.UpdateLocations(self.gisdbase)
  564. self.OnSelectLocation(None)
  565. def OnBrowse(self, event):
  566. """'Browse' button clicked"""
  567. grassdata = None
  568. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  569. style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  570. if dlg.ShowModal() == wx.ID_OK:
  571. self.gisdbase = dlg.GetPath()
  572. self.tgisdbase.SetValue(self.gisdbase)
  573. self.OnSetDatabase(event)
  574. dlg.Destroy()
  575. def OnCreateMapset(self,event):
  576. """!Create new mapset"""
  577. self.gisdbase = self.tgisdbase.GetValue()
  578. location = self.listOfLocations[self.lblocations.GetSelection()]
  579. dlg = wx.TextEntryDialog(parent = self,
  580. message = _('Enter name for new mapset:'),
  581. caption = _('Create new mapset'))
  582. if dlg.ShowModal() == wx.ID_OK:
  583. mapset = dlg.GetValue()
  584. if mapset in self.listOfMapsets:
  585. GMessage(parent = self,
  586. message = _("Mapset <%s> already exists.") % mapset)
  587. return
  588. if mapset.lower() == 'ogr':
  589. dlg1 = wx.MessageDialog(parent = self,
  590. message = _("Mapset <%s> is reserved for direct "
  591. "read access to OGR layers. Please consider to use "
  592. "another name for your mapset.\n\n"
  593. "Are you really sure that you want to create this mapset?") % mapset,
  594. caption = _("Reserved mapset name"),
  595. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  596. ret = dlg1.ShowModal()
  597. dlg1.Destroy()
  598. if ret == wx.ID_NO:
  599. dlg.Destroy()
  600. return
  601. try:
  602. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  603. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  604. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  605. os.path.join(self.gisdbase, location, mapset))
  606. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  607. self.OnSelectLocation(None)
  608. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  609. except StandardError, e:
  610. GError(parent = self,
  611. message = _("Unable to create new mapset: %s") % e)
  612. return False
  613. dlg.Destroy()
  614. self.bstart.SetFocus()
  615. return True
  616. def OnStart(self, event):
  617. """'Start GRASS' button clicked"""
  618. dbase = self.tgisdbase.GetValue()
  619. location = self.listOfLocations[self.lblocations.GetSelection()]
  620. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  621. lockfile = os.path.join(dbase, location, mapset, '.gislock')
  622. if os.path.isfile(lockfile):
  623. dlg = wx.MessageDialog(parent = self,
  624. message = _("GRASS is already running in selected mapset <%(mapset)s>\n"
  625. "(file %(lock)s found).\n\n"
  626. "Concurrent use not allowed.\n\n"
  627. "Do you want to try to remove .gislock (note that you "
  628. "need permission for this operation) and continue?") %
  629. { 'mapset' : mapset, 'lock' : lockfile },
  630. caption = _("Lock file found"),
  631. style = wx.YES_NO | wx.NO_DEFAULT |
  632. wx.ICON_QUESTION | wx.CENTRE)
  633. ret = dlg.ShowModal()
  634. dlg.Destroy()
  635. if ret == wx.ID_YES:
  636. dlg1 = wx.MessageDialog(parent = self,
  637. message = _("ARE YOU REALLY SURE?\n\n"
  638. "If you really are running another GRASS session doing this "
  639. "could corrupt your data. Have another look in the processor "
  640. "manager just to be sure..."),
  641. caption = _("Lock file found"),
  642. style = wx.YES_NO | wx.NO_DEFAULT |
  643. wx.ICON_QUESTION | wx.CENTRE)
  644. ret = dlg1.ShowModal()
  645. dlg1.Destroy()
  646. if ret == wx.ID_YES:
  647. try:
  648. os.remove(lockfile)
  649. except IOError, e:
  650. GError(_("Unable to remove '%(lock)s'.\n\n"
  651. "Details: %(reason)s") % { 'lock' : lockfile, 'reason' : e})
  652. else:
  653. return
  654. else:
  655. return
  656. RunCommand("g.gisenv",
  657. set = "GISDBASE=%s" % dbase)
  658. RunCommand("g.gisenv",
  659. set = "LOCATION_NAME=%s" % location)
  660. RunCommand("g.gisenv",
  661. set = "MAPSET=%s" % mapset)
  662. self.Destroy()
  663. sys.exit(0)
  664. def OnExit(self, event):
  665. """'Exit' button clicked"""
  666. self.Destroy()
  667. sys.exit(2)
  668. def OnHelp(self, event):
  669. """'Help' button clicked"""
  670. # help text in lib/init/helptext.html
  671. file = os.path.join(self.gisbase, "docs", "html", "helptext.html")
  672. helpFrame = HelpFrame(parent = self, id = wx.ID_ANY,
  673. title = _("GRASS Quickstart"),
  674. size = (640, 480),
  675. file = file)
  676. helpFrame.Show(True)
  677. event.Skip()
  678. def OnCloseWindow(self, event):
  679. """!Close window event"""
  680. event.Skip()
  681. sys.exit(2)
  682. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  683. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  684. non-selectable items (e.g. mapsets with denied permission)"""
  685. def __init__(self, parent, id, size,
  686. choices, disabled = []):
  687. wx.ListCtrl.__init__(self, parent, id, size = size,
  688. style = wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  689. wx.BORDER_SUNKEN)
  690. listmix.ListCtrlAutoWidthMixin.__init__(self)
  691. self.InsertColumn(0, '')
  692. self.selected = wx.NOT_FOUND
  693. self._LoadData(choices, disabled)
  694. def _LoadData(self, choices, disabled = []):
  695. """!Load data into list
  696. @param choices list of item
  697. @param disabled list of indeces of non-selectable items
  698. """
  699. idx = 0
  700. for item in choices:
  701. index = self.InsertStringItem(sys.maxint, item)
  702. self.SetStringItem(index, 0, item)
  703. if idx in disabled:
  704. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  705. idx += 1
  706. def Clear(self):
  707. self.DeleteAllItems()
  708. def InsertItems(self, choices, pos, disabled = []):
  709. self._LoadData(choices, disabled)
  710. def SetSelection(self, item, force = False):
  711. if item != wx.NOT_FOUND and \
  712. (platform.system() != 'Windows' or force):
  713. ### Windows -> FIXME
  714. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  715. self.selected = item
  716. def GetSelection(self):
  717. return self.selected
  718. class StartUp(wx.App):
  719. """!Start-up application"""
  720. def OnInit(self):
  721. wx.InitAllImageHandlers()
  722. StartUp = GRASSStartup()
  723. StartUp.CenterOnScreen()
  724. self.SetTopWindow(StartUp)
  725. StartUp.Show()
  726. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  727. wx.MessageBox(parent = StartUp,
  728. caption = _('Starting GRASS for the first time'),
  729. message = _('GRASS needs a directory in which to store its data. '
  730. 'Create one now if you have not already done so. '
  731. 'A popular choice is "grassdata", located in '
  732. 'your home directory.'),
  733. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  734. StartUp.OnBrowse(None)
  735. return 1
  736. if __name__ == "__main__":
  737. if os.getenv("GISBASE") is None:
  738. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  739. else:
  740. import gettext
  741. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  742. from core.gcmd import RunCommand, DecodeString
  743. from core.utils import GetListOfMapsets, GetListOfLocations
  744. GRASSStartUp = StartUp(0)
  745. GRASSStartUp.MainLoop()