gis_set.py 31 KB

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