gis_set.py 32 KB

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