gis_set.py 31 KB

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