gis_set.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. - HelpWindow
  9. - StartUp
  10. COPYRIGHT: (C) 2006-2008 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Michael Barton and Jachym Cepicky (original author)
  15. Martin Landa <landa.martin gmail.com> (various updates)
  16. """
  17. import os
  18. import sys
  19. import glob
  20. import shutil
  21. import copy
  22. ### i18N
  23. import gettext
  24. from gui_modules import globalvar
  25. globalvar.CheckForWx()
  26. import wx
  27. import wx.html
  28. import wx.lib.rcsizer as rcs
  29. import wx.lib.filebrowsebutton as filebrowse
  30. import wx.lib.mixins.listctrl as listmix
  31. class GRASSStartup(wx.Frame):
  32. """GRASS start-up screen"""
  33. def __init__(self, parent=None, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE):
  34. #
  35. # GRASS variables
  36. #
  37. self.gisbase = os.getenv("GISBASE")
  38. self.grassrc = self._read_grassrc()
  39. self.gisdbase = self._getRCValue("GISDBASE")
  40. #
  41. # list of locations/mapsets
  42. #
  43. self.listOfLocations = []
  44. self.listOfMapsets = []
  45. self.listOfMapsetsSelectable = []
  46. wx.Frame.__init__(self, parent=parent, id=id, style=style)
  47. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  48. #
  49. # graphical elements
  50. #
  51. # image
  52. try:
  53. name = os.path.join(globalvar.ETCDIR, "gui", "images", "startup_banner.png")
  54. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
  55. wx.Bitmap(name=name,
  56. type=wx.BITMAP_TYPE_PNG))
  57. except:
  58. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
  59. # labels
  60. ### crashes when LOCATION doesn't exist
  61. # versionCmd = gcmd.Command(['g.version'], log=None)
  62. # grassVersion = versionCmd.ReadStdOutput()[0].replace('GRASS', '').strip()
  63. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  64. grassVersion = versionFile.readline().replace('%s' % os.linesep, '').strip()
  65. versionFile.close()
  66. self.select_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  67. label=" %s " % _("Choose project location and mapset"))
  68. self.manage_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  69. label=" %s " % _("Manage"))
  70. self.lwelcome = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  71. label=_("Welcome to GRASS GIS %s\n"
  72. "The world's leading open source GIS") % grassVersion,
  73. style=wx.ALIGN_CENTRE)
  74. #self.SetFont(wx.Font(pointSize=9, family=wx.FONTFAMILY_DEFAULT,
  75. # style=wx.NORMAL, weight=wx.NORMAL))
  76. self.ltitle = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  77. label=_("Select an existing project location and mapset\n"
  78. "or define a new location"),
  79. style=wx.ALIGN_CENTRE)
  80. self.ldbase = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  81. label=_("GIS Data Directory:"))
  82. self.llocation = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  83. label=_("Project location\n(projection/coordinate system)"),
  84. style=wx.ALIGN_CENTRE)
  85. self.lmapset = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  86. label=_("Accessible mapsets\n(directories of GIS files)"),
  87. style=wx.ALIGN_CENTRE)
  88. self.lcreate = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  89. label=_("Create new mapset\nin selected location"),
  90. style=wx.ALIGN_CENTRE)
  91. self.ldefine = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  92. label=_("Define new location"),
  93. style=wx.ALIGN_CENTRE)
  94. self.lmanageloc = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  95. label=_("Rename/delete selected\nmapset or location"),
  96. style=wx.ALIGN_CENTRE)
  97. # buttons
  98. self.bstart = wx.Button(parent=self.panel, id=wx.ID_ANY,
  99. label=_("Start GRASS"), size=(180, -1))
  100. self.bstart.SetDefault()
  101. self.bexit = wx.Button(parent=self.panel, id=wx.ID_EXIT)
  102. self.bhelp = wx.Button(parent=self.panel, id=wx.ID_HELP)
  103. self.bbrowse = wx.Button(parent=self.panel, id=wx.ID_ANY,
  104. label=_("Browse"))
  105. self.bmapset = wx.Button(parent=self.panel, id=wx.ID_ANY,
  106. label=_("Create mapset"))
  107. self.bwizard = wx.Button(parent=self.panel, id=wx.ID_ANY,
  108. label=_("Location wizard"))
  109. self.manageloc = wx.Choice(parent=self.panel, id=wx.ID_ANY,
  110. choices=[_('Rename mapset'), _('Rename location'),
  111. _('Delete mapset'), _('Delete location')])
  112. self.manageloc.SetSelection(0)
  113. # textinputs
  114. self.tgisdbase = wx.TextCtrl(parent=self.panel, id=wx.ID_ANY, value="", size=(300, -1),
  115. style=wx.TE_PROCESS_ENTER)
  116. # Locations
  117. self.lpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  118. self.lblocations = GListBox(parent=self.lpanel,
  119. id=wx.ID_ANY, size=(180, 200),
  120. choices=self.listOfLocations)
  121. self.lblocations.SetColumnWidth(0, 180)
  122. # TODO: sort; but keep PERMANENT on top of list
  123. # Mapsets
  124. self.mpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  125. self.lbmapsets = GListBox(parent=self.mpanel,
  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.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  143. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  144. def _set_properties(self):
  145. """Set frame properties"""
  146. self.SetTitle(_("Welcome to GRASS GIS"))
  147. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
  148. wx.BITMAP_TYPE_ICO))
  149. self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
  150. self.lwelcome.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  151. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  152. self.bstart.SetToolTipString(_("Enter GRASS session"))
  153. # self.bstart.Enable(False)
  154. # self.bmapset.Enable(False)
  155. # set database
  156. if not self.gisdbase:
  157. # sets an initial path for gisdbase if nothing in GISRC
  158. if os.path.isdir(os.getenv("HOME")):
  159. self.gisdbase = os.getenv("HOME")
  160. else:
  161. self.gisdbase = os.getcwd()
  162. self.tgisdbase.SetValue(self.gisdbase)
  163. self.OnSetDatabase(None)
  164. location = self._getRCValue("LOCATION_NAME")
  165. if location == "<UNKNOWN>" or \
  166. not os.path.isdir(os.path.join(self.gisdbase, location)):
  167. location = None
  168. if location:
  169. # list of locations
  170. self.UpdateLocations(self.gisdbase)
  171. try:
  172. self.lblocations.SetSelection(self.listOfLocations.index(location))
  173. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  174. except ValueError:
  175. print >> sys.stderr, _("ERROR: Location <%s> not found") % \
  176. (location)
  177. # list of mapsets
  178. self.UpdateMapsets(os.path.join(self.gisdbase,location))
  179. mapset = self._getRCValue("MAPSET")
  180. if mapset:
  181. try:
  182. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  183. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  184. except ValueError:
  185. self.lbmapsets.Clear()
  186. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % \
  187. (mapset)
  188. # self.bstart.Enable(True)
  189. def _do_layout(self):
  190. label_style = wx.ADJUST_MINSIZE | wx.ALIGN_CENTER_HORIZONTAL
  191. sizer = wx.BoxSizer(wx.VERTICAL)
  192. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  193. location_sizer = wx.FlexGridSizer(rows=1, cols=2, vgap=4, hgap=4)
  194. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  195. select_sizer = wx.FlexGridSizer(rows=2, cols=2, vgap=4, hgap=4)
  196. manage_boxsizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  197. manage_sizer = wx.BoxSizer(wx.VERTICAL)
  198. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  199. # gis data directory
  200. dbase_sizer.Add(item=self.ldbase, proportion=0,
  201. flag=wx.ALIGN_CENTER_VERTICAL |
  202. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  203. border=5)
  204. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  205. flag=wx.ALIGN_CENTER_VERTICAL |
  206. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  207. border=5)
  208. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  209. flag=wx.ALIGN_CENTER_VERTICAL |
  210. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  211. border=5)
  212. # select sizer
  213. select_sizer.Add(item=self.llocation, proportion=0,
  214. flag=label_style | wx.ALL,
  215. border=5)
  216. select_sizer.Add(item=self.lmapset, proportion=0,
  217. flag=label_style | wx.ALL,
  218. border=5)
  219. select_sizer.Add(item=self.lpanel, proportion=0,
  220. flag=wx.ADJUST_MINSIZE |
  221. wx.ALIGN_CENTER_VERTICAL |
  222. wx.ALIGN_CENTER_HORIZONTAL)
  223. select_sizer.Add(item=self.mpanel, proportion=0,
  224. flag=wx.ADJUST_MINSIZE |
  225. wx.ALIGN_CENTER_VERTICAL |
  226. wx.ALIGN_CENTER_HORIZONTAL)
  227. select_boxsizer.Add(item=select_sizer, proportion=0)
  228. # define new location and mapset
  229. manage_sizer.Add(item=self.ldefine, proportion=0,
  230. flag=label_style | wx.ALL,
  231. border=5)
  232. manage_sizer.Add(item=self.bwizard, proportion=0,
  233. flag=label_style | wx.BOTTOM,
  234. border=8)
  235. manage_sizer.Add(item=self.lcreate, proportion=0,
  236. flag=label_style | wx.ALL,
  237. border=5)
  238. manage_sizer.Add(item=self.bmapset, proportion=0,
  239. flag=label_style | wx.BOTTOM,
  240. border=8)
  241. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  242. flag=label_style | wx.ALL,
  243. border=5)
  244. manage_sizer.Add(item=self.manageloc, proportion=0,
  245. flag=label_style | wx.BOTTOM,
  246. border=8)
  247. manage_boxsizer.Add(item=manage_sizer, proportion=0)
  248. # location sizer
  249. location_sizer.Add(item=select_boxsizer, proportion=0,
  250. flag=wx.ADJUST_MINSIZE |
  251. wx.ALIGN_CENTER_VERTICAL |
  252. wx.ALIGN_CENTER_HORIZONTAL |
  253. wx.RIGHT | wx.LEFT | wx.EXPAND,
  254. border=5) # GISDBASE setting
  255. location_sizer.Add(item=manage_boxsizer, proportion=0,
  256. flag=wx.ADJUST_MINSIZE |
  257. wx.ALIGN_TOP |
  258. wx.ALIGN_CENTER_HORIZONTAL |
  259. wx.RIGHT | wx.EXPAND,
  260. border=5)
  261. # buttons
  262. btns_sizer.Add(item=self.bstart, proportion=0,
  263. flag=wx.ALIGN_CENTER_HORIZONTAL |
  264. wx.ALL,
  265. border=10)
  266. btns_sizer.Add(item=self.bexit, proportion=0,
  267. flag=wx.ALIGN_CENTER_HORIZONTAL |
  268. wx.ALL,
  269. border=10)
  270. btns_sizer.Add(item=self.bhelp, proportion=0,
  271. flag=wx.ALIGN_CENTER_HORIZONTAL |
  272. wx.ALL,
  273. border=10)
  274. # main sizer
  275. sizer.Add(item=self.hbitmap, proportion=0,
  276. flag=wx.ADJUST_MINSIZE |
  277. wx.ALIGN_CENTER_VERTICAL |
  278. wx.ALIGN_CENTER_HORIZONTAL |
  279. wx.ALL,
  280. border=5) # image
  281. sizer.Add(item=self.lwelcome, # welcome message
  282. proportion=0,
  283. flag= wx.ADJUST_MINSIZE |
  284. wx.ALIGN_CENTER_VERTICAL |
  285. wx.ALIGN_CENTER_HORIZONTAL |
  286. wx.EXPAND |
  287. wx.BOTTOM,
  288. border=10)
  289. sizer.Add(item=self.ltitle, # title
  290. proportion=0,
  291. flag=wx.ADJUST_MINSIZE |
  292. wx.ALIGN_CENTER_VERTICAL |
  293. wx.ALIGN_CENTER_HORIZONTAL |
  294. wx.EXPAND |
  295. wx.BOTTOM,
  296. border=5)
  297. sizer.Add(item=dbase_sizer, proportion=0,
  298. flag=wx.ADJUST_MINSIZE |
  299. wx.ALIGN_CENTER_VERTICAL |
  300. wx.ALIGN_CENTER_HORIZONTAL |
  301. wx.RIGHT | wx.LEFT,
  302. border=5) # GISDBASE setting
  303. sizer.Add(item=location_sizer, proportion=1,
  304. flag=wx.ADJUST_MINSIZE |
  305. wx.ALIGN_CENTER_VERTICAL |
  306. wx.ALIGN_CENTER_HORIZONTAL |
  307. wx.RIGHT | wx.LEFT,
  308. border=5)
  309. sizer.Add(item=btns_sizer, proportion=0,
  310. flag=wx.ALIGN_CENTER_VERTICAL |
  311. wx.ALIGN_CENTER_HORIZONTAL |
  312. wx.RIGHT | wx.LEFT,
  313. border=5)
  314. self.panel.SetAutoLayout(True)
  315. self.panel.SetSizer(sizer)
  316. sizer.Fit(self.panel)
  317. sizer.SetSizeHints(self)
  318. self.Layout()
  319. def _read_grassrc(self):
  320. """
  321. Read variables from $HOME/.grassrc7 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 = []
  447. for location in glob.glob(os.path.join(dbase, "*")):
  448. try:
  449. if os.path.join(location, "PERMANENT") in glob.glob(os.path.join(location, "*")):
  450. self.listOfLocations.append(os.path.basename(location))
  451. except:
  452. pass
  453. utils.ListSortLower(self.listOfLocations)
  454. self.lblocations.Clear()
  455. self.lblocations.InsertItems(self.listOfLocations, 0)
  456. if len(self.listOfLocations) > 0:
  457. self.lblocations.SetSelection(0)
  458. else:
  459. self.lblocations.SetSelection(wx.NOT_FOUND)
  460. return self.listOfLocations
  461. def UpdateMapsets(self, location):
  462. """Update list of mapsets"""
  463. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  464. self.listOfMapsets = []
  465. self.listOfMapsetsSelectable = []
  466. for mapset in glob.glob(os.path.join(self.gisdbase, location, "*")):
  467. if os.path.isdir(mapset) and \
  468. os.path.isfile(os.path.join(self.gisdbase, location, mapset, "WIND")) and \
  469. os.path.basename(mapset) != 'PERMANENT':
  470. self.listOfMapsets.append(os.path.basename(mapset))
  471. utils.ListSortLower(self.listOfMapsets)
  472. self.listOfMapsets.insert(0, 'PERMANENT')
  473. self.lbmapsets.Clear()
  474. # disable mapset with denied permission
  475. locationName = os.path.basename(location)
  476. try:
  477. mapsets = gcmd.Command(['g.mapset',
  478. '-l',
  479. 'location=%s' % locationName,
  480. 'gisdbase=%s' % self.gisdbase],
  481. stderr=None)
  482. for line in mapsets.ReadStdOutput():
  483. self.listOfMapsetsSelectable += line.split(' ')
  484. except gcmd.CmdError:
  485. gcmd.Command(["g.gisenv",
  486. "set=GISDBASE=%s" % self.gisdbase])
  487. gcmd.Command(["g.gisenv",
  488. "set=LOCATION_NAME=%s" % locationName])
  489. gcmd.Command(["g.gisenv",
  490. "set=MAPSET=PERMANENT"])
  491. # first run only
  492. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  493. disabled = []
  494. idx = 0
  495. for mapset in self.listOfMapsets:
  496. if mapset not in self.listOfMapsetsSelectable:
  497. disabled.append(idx)
  498. idx += 1
  499. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  500. return self.listOfMapsets
  501. def OnSelectLocation(self, event):
  502. """Location selected"""
  503. if event:
  504. self.lblocations.SetSelection(event.GetIndex())
  505. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  506. self.UpdateMapsets(os.path.join(self.gisdbase,
  507. self.listOfLocations[self.lblocations.GetSelection()]))
  508. else:
  509. self.listOfMapsets = []
  510. disabled = []
  511. idx = 0
  512. for mapset in self.listOfMapsets:
  513. if mapset not in self.listOfMapsetsSelectable:
  514. disabled.append(idx)
  515. idx += 1
  516. self.lbmapsets.Clear()
  517. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  518. if len(self.listOfMapsets) > 0:
  519. self.lbmapsets.SetSelection(0)
  520. else:
  521. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  522. def OnSelectMapset(self, event):
  523. """Mapset selected"""
  524. self.lbmapsets.SetSelection(event.GetIndex())
  525. if event.GetText() not in self.listOfMapsetsSelectable:
  526. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  527. else:
  528. self.FormerMapsetSelection = event.GetIndex()
  529. event.Skip()
  530. def OnSetDatabase(self, event):
  531. """Database set"""
  532. self.gisdbase = self.tgisdbase.GetValue()
  533. self.UpdateLocations(self.gisdbase)
  534. self.OnSelectLocation(None)
  535. def OnBrowse(self, event):
  536. """'Browse' button clicked"""
  537. grassdata = None
  538. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  539. style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  540. if dlg.ShowModal() == wx.ID_OK:
  541. self.gisdbase = dlg.GetPath()
  542. self.tgisdbase.SetValue(self.gisdbase)
  543. self.OnSetDatabase(event)
  544. dlg.Destroy()
  545. def OnCreateMapset(self,event):
  546. """Create new mapset"""
  547. self.gisdbase = self.tgisdbase.GetValue()
  548. location = self.listOfLocations[self.lblocations.GetSelection()]
  549. dlg = wx.TextEntryDialog(parent=self,
  550. message=_('Enter name for new mapset:'),
  551. caption=_('Create new mapset'))
  552. if dlg.ShowModal() == wx.ID_OK:
  553. mapset = dlg.GetValue()
  554. try:
  555. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  556. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  557. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  558. os.path.join(self.gisdbase, location, mapset))
  559. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  560. self.OnSelectLocation(None)
  561. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  562. except StandardError, e:
  563. dlg = wx.MessageDialog(parent=self, message=_("Unable to create new mapset: %s") % e,
  564. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  565. dlg.ShowModal()
  566. dlg.Destroy()
  567. return False
  568. return True
  569. def OnStart(self, event):
  570. """'Start GRASS' button clicked"""
  571. gcmd.Command(["g.gisenv",
  572. "set=GISDBASE=%s" % self.tgisdbase.GetValue()])
  573. gcmd.Command(["g.gisenv",
  574. "set=LOCATION_NAME=%s" % self.listOfLocations[self.lblocations.GetSelection()]])
  575. gcmd.Command(["g.gisenv",
  576. "set=MAPSET=%s" % self.listOfMapsets[self.lbmapsets.GetSelection()]])
  577. self.Destroy()
  578. sys.exit(0)
  579. def OnExit(self, event):
  580. """'Exit' button clicked"""
  581. self.Destroy()
  582. sys.exit (2)
  583. def OnHelp(self, event):
  584. """'Help' button clicked"""
  585. # help text in lib/init/helptext.html
  586. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  587. helpFrame = HelpWindow(parent=self, id=wx.ID_ANY,
  588. title=_("GRASS Quickstart"),
  589. size=(640, 480),
  590. file=file)
  591. helpFrame.Show(True)
  592. event.Skip()
  593. def OnCloseWindow(self, event):
  594. """Close window event"""
  595. event.Skip()
  596. sys.exit(2)
  597. class HelpWindow(wx.Frame):
  598. """GRASS Quickstart help window"""
  599. def __init__(self, parent, id, title, size, file):
  600. wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size)
  601. sizer = wx.BoxSizer(wx.VERTICAL)
  602. # text
  603. helpFrame = wx.html.HtmlWindow(parent=self, id=wx.ID_ANY)
  604. helpFrame.SetStandardFonts (size = 10)
  605. helpFrame.SetBorders(10)
  606. wx.InitAllImageHandlers()
  607. helpFrame.LoadFile(file)
  608. self.Ok = True
  609. sizer.Add(item=helpFrame, proportion=1, flag=wx.EXPAND)
  610. self.SetAutoLayout(True)
  611. self.SetSizer(sizer)
  612. # sizer.Fit(self)
  613. # sizer.SetSizeHints(self)
  614. self.Layout()
  615. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  616. """Use wx.ListCtrl instead of wx.ListBox, different style for
  617. non-selectable items (e.g. mapsets with denied permission)"""
  618. def __init__(self, parent, id, size,
  619. choices, disabled=[]):
  620. wx.ListCtrl.__init__(self, parent, id, size=size,
  621. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  622. wx.BORDER_SUNKEN)
  623. listmix.ListCtrlAutoWidthMixin.__init__(self)
  624. self.InsertColumn(0, '')
  625. self.selected = wx.NOT_FOUND
  626. self.__LoadData(choices, disabled)
  627. def __LoadData(self, choices, disabled=[]):
  628. """
  629. Load data into list
  630. @param choices list of item
  631. @param disabled list of indeces of non-selectable items
  632. """
  633. idx = 0
  634. for item in choices:
  635. index = self.InsertStringItem(sys.maxint, item)
  636. self.SetStringItem(index, 0, item)
  637. if idx in disabled:
  638. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  639. idx += 1
  640. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  641. def Clear(self):
  642. self.DeleteAllItems()
  643. def InsertItems(self, choices, pos, disabled=[]):
  644. self.__LoadData(choices, disabled)
  645. def SetSelection(self, item):
  646. if item != wx.NOT_FOUND:
  647. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  648. self.selected = item
  649. def GetSelection(self):
  650. return self.selected
  651. class StartUp(wx.App):
  652. """Start-up application"""
  653. def OnInit(self):
  654. wx.InitAllImageHandlers()
  655. StartUp = GRASSStartup()
  656. StartUp.CenterOnScreen()
  657. self.SetTopWindow(StartUp)
  658. StartUp.Show()
  659. return 1
  660. if __name__ == "__main__":
  661. if os.getenv("GISBASE") is None:
  662. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  663. else:
  664. import gettext
  665. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  666. import gui_modules.gcmd as gcmd
  667. import gui_modules.utils as utils
  668. GRASSStartUp = StartUp(0)
  669. GRASSStartUp.MainLoop()