gis_set.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. if not os.getenv("GRASS_WXBUNDLED"):
  27. globalvar.CheckForWx()
  28. import wx
  29. import wx.html
  30. import wx.lib.rcsizer as rcs
  31. import wx.lib.filebrowsebutton as filebrowse
  32. import wx.lib.mixins.listctrl as listmix
  33. class GRASSStartup(wx.Frame):
  34. """GRASS start-up screen"""
  35. def __init__(self, parent=None, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE):
  36. #
  37. # GRASS variables
  38. #
  39. self.gisbase = os.getenv("GISBASE")
  40. self.grassrc = self._read_grassrc()
  41. self.gisdbase = self.GetRCValue("GISDBASE")
  42. #
  43. # list of locations/mapsets
  44. #
  45. self.listOfLocations = []
  46. self.listOfMapsets = []
  47. self.listOfMapsetsSelectable = []
  48. wx.Frame.__init__(self, parent=parent, id=id, style=style)
  49. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  50. #
  51. # graphical elements
  52. #
  53. # image
  54. try:
  55. name = os.path.join(globalvar.ETCDIR, "gui", "images", "startup_banner.png")
  56. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
  57. wx.Bitmap(name=name,
  58. type=wx.BITMAP_TYPE_PNG))
  59. except:
  60. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
  61. # labels
  62. ### crashes when LOCATION doesn't exist
  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.ltitle = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  75. label=_("Select an existing project location and mapset\n"
  76. "or define a new location"),
  77. style=wx.ALIGN_CENTRE)
  78. self.ldbase = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  79. label=_("GIS Data Directory:"))
  80. self.llocation = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  81. label=_("Project location\n(projection/coordinate system)"),
  82. style=wx.ALIGN_CENTRE)
  83. self.lmapset = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  84. label=_("Accessible mapsets\n(directories of GIS files)"),
  85. style=wx.ALIGN_CENTRE)
  86. self.lcreate = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  87. label=_("Create new mapset\nin selected location"),
  88. style=wx.ALIGN_CENTRE)
  89. self.ldefine = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  90. label=_("Define new location"),
  91. style=wx.ALIGN_CENTRE)
  92. self.lmanageloc = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  93. label=_("Rename/delete selected\nmapset or location"),
  94. style=wx.ALIGN_CENTRE)
  95. # buttons
  96. self.bstart = wx.Button(parent=self.panel, id=wx.ID_ANY,
  97. label=_("Start GRASS"), size=(180, -1))
  98. self.bstart.SetDefault()
  99. self.bexit = wx.Button(parent=self.panel, id=wx.ID_EXIT)
  100. self.bhelp = wx.Button(parent=self.panel, id=wx.ID_HELP)
  101. self.bbrowse = wx.Button(parent=self.panel, id=wx.ID_ANY,
  102. label=_("Browse"))
  103. self.bmapset = wx.Button(parent=self.panel, id=wx.ID_ANY,
  104. label=_("Create mapset"))
  105. self.bwizard = wx.Button(parent=self.panel, id=wx.ID_ANY,
  106. label=_("Location wizard"))
  107. self.manageloc = wx.Choice(parent=self.panel, id=wx.ID_ANY,
  108. choices=[_('Rename mapset'), _('Rename location'),
  109. _('Delete mapset'), _('Delete location')])
  110. self.manageloc.SetSelection(0)
  111. # textinputs
  112. self.tgisdbase = wx.TextCtrl(parent=self.panel, id=wx.ID_ANY, value="", size=(300, -1),
  113. style=wx.TE_PROCESS_ENTER)
  114. # Locations
  115. self.lpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  116. self.lblocations = GListBox(parent=self.lpanel,
  117. id=wx.ID_ANY, size=(180, 200),
  118. choices=self.listOfLocations)
  119. self.lblocations.SetColumnWidth(0, 180)
  120. # TODO: sort; but keep PERMANENT on top of list
  121. # Mapsets
  122. self.mpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  123. self.lbmapsets = GListBox(parent=self.mpanel,
  124. id=wx.ID_ANY, size=(180, 200),
  125. choices=self.listOfMapsets)
  126. self.lbmapsets.SetColumnWidth(0, 180)
  127. # layout & properties
  128. self._set_properties()
  129. self._do_layout()
  130. # events
  131. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  132. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  133. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  134. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  135. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  136. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  137. self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
  138. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  139. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  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. # set database
  154. if not self.gisdbase:
  155. # sets an initial path for gisdbase if nothing in GISRC
  156. if os.path.isdir(os.getenv("HOME")):
  157. self.gisdbase = os.getenv("HOME")
  158. else:
  159. self.gisdbase = os.getcwd()
  160. self.tgisdbase.SetValue(self.gisdbase)
  161. self.OnSetDatabase(None)
  162. location = self.GetRCValue("LOCATION_NAME")
  163. if location == "<UNKNOWN>" or \
  164. not os.path.isdir(os.path.join(self.gisdbase, location)):
  165. location = None
  166. if location:
  167. # list of locations
  168. self.UpdateLocations(self.gisdbase)
  169. try:
  170. self.lblocations.SetSelection(self.listOfLocations.index(location),
  171. force=True)
  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. force=True)
  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=3)
  204. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  205. flag=wx.ALIGN_CENTER_VERTICAL |
  206. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  207. border=3)
  208. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  209. flag=wx.ALIGN_CENTER_VERTICAL |
  210. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  211. border=3)
  212. # select sizer
  213. select_sizer.Add(item=self.llocation, proportion=0,
  214. flag=label_style | wx.ALL,
  215. border=3)
  216. select_sizer.Add(item=self.lmapset, proportion=0,
  217. flag=label_style | wx.ALL,
  218. border=3)
  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=3)
  232. manage_sizer.Add(item=self.bwizard, proportion=0,
  233. flag=label_style | wx.BOTTOM,
  234. border=5)
  235. manage_sizer.Add(item=self.lcreate, proportion=0,
  236. flag=label_style | wx.ALL,
  237. border=3)
  238. manage_sizer.Add(item=self.bmapset, proportion=0,
  239. flag=label_style | wx.BOTTOM,
  240. border=5)
  241. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  242. flag=label_style | wx.ALL,
  243. border=3)
  244. manage_sizer.Add(item=self.manageloc, proportion=0,
  245. flag=label_style | wx.BOTTOM,
  246. border=5)
  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=3) # 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=3)
  261. # buttons
  262. btns_sizer.Add(item=self.bstart, proportion=0,
  263. flag=wx.ALIGN_CENTER_HORIZONTAL |
  264. wx.ALL,
  265. border=5)
  266. btns_sizer.Add(item=self.bexit, proportion=0,
  267. flag=wx.ALIGN_CENTER_HORIZONTAL |
  268. wx.ALL,
  269. border=5)
  270. btns_sizer.Add(item=self.bhelp, proportion=0,
  271. flag=wx.ALIGN_CENTER_HORIZONTAL |
  272. wx.ALL,
  273. border=5)
  274. # main sizer
  275. sizer.Add(item=self.hbitmap,
  276. proportion=0,
  277. flag=wx.ALIGN_CENTER_VERTICAL |
  278. wx.ALIGN_CENTER_HORIZONTAL |
  279. wx.ALL,
  280. border=3) # image
  281. sizer.Add(item=self.lwelcome, # welcome message
  282. proportion=0,
  283. flag=wx.ALIGN_CENTER_VERTICAL |
  284. wx.ALIGN_CENTER_HORIZONTAL |
  285. wx.BOTTOM,
  286. border=1)
  287. sizer.Add(item=self.ltitle, # title
  288. proportion=0,
  289. flag=wx.ALIGN_CENTER_VERTICAL |
  290. wx.ALIGN_CENTER_HORIZONTAL)
  291. sizer.Add(item=dbase_sizer, proportion=0,
  292. flag=wx.ALIGN_CENTER_HORIZONTAL |
  293. wx.RIGHT | wx.LEFT,
  294. border=1) # GISDBASE setting
  295. sizer.Add(item=location_sizer, proportion=1,
  296. flag=wx.ALIGN_CENTER_VERTICAL |
  297. wx.ALIGN_CENTER_HORIZONTAL |
  298. wx.RIGHT | wx.LEFT,
  299. border=1)
  300. sizer.Add(item=btns_sizer, proportion=0,
  301. flag=wx.ALIGN_CENTER_VERTICAL |
  302. wx.ALIGN_CENTER_HORIZONTAL |
  303. wx.RIGHT | wx.LEFT,
  304. border=1)
  305. self.panel.SetAutoLayout(True)
  306. self.panel.SetSizer(sizer)
  307. sizer.Fit(self.panel)
  308. sizer.SetSizeHints(self)
  309. self.Layout()
  310. def _read_grassrc(self):
  311. """
  312. Read variables from $HOME/.grassrc7 file
  313. """
  314. grassrc = {}
  315. gisrc = os.getenv("GISRC")
  316. if gisrc and os.path.isfile(gisrc):
  317. try:
  318. rc = open(gisrc, "r")
  319. for line in rc.readlines():
  320. key, val = line.split(":", 1)
  321. grassrc[key.strip()] = val.strip()
  322. finally:
  323. rc.close()
  324. return grassrc
  325. def GetRCValue(self, value):
  326. "Return GRASS variable (read from GISRC)"""
  327. if self.grassrc.has_key(value):
  328. return self.grassrc[value]
  329. else:
  330. return None
  331. def OnWizard(self, event):
  332. """Location wizard started"""
  333. from gui_modules import location_wizard
  334. gWizard = location_wizard.LocationWizard(self, self.tgisdbase.GetValue())
  335. if gWizard.location != None:
  336. self.OnSetDatabase(event)
  337. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  338. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  339. self.lbmapsets.SetSelection(0)
  340. def OnManageLoc(self, event):
  341. """
  342. Location management choice control handler
  343. """
  344. if event.GetString() == 'Rename mapset':
  345. self.RenameMapset()
  346. elif event.GetString() == 'Rename location':
  347. self.RenameLocation()
  348. elif event.GetString() == 'Delete mapset':
  349. self.DeleteMapset()
  350. elif event.GetString() == 'Delete location':
  351. self.DeleteLocation()
  352. def RenameMapset(self):
  353. """
  354. Rename selected mapset
  355. """
  356. location = self.listOfLocations[self.lblocations.GetSelection()]
  357. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  358. dlg = wx.TextEntryDialog(parent=self,
  359. message=_('Current name: %s\nEnter new name:') % mapset,
  360. caption=_('Rename selected mapset'))
  361. if dlg.ShowModal() == wx.ID_OK:
  362. newmapset = dlg.GetValue()
  363. if newmapset != mapset:
  364. try:
  365. os.rename(os.path.join(self.gisdbase, location, mapset),
  366. os.path.join(self.gisdbase, location, newmapset))
  367. self.OnSelectLocation(None)
  368. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  369. except:
  370. wx.MessageBox(message=_('Unable to rename mapset'))
  371. dlg.Destroy()
  372. def RenameLocation(self):
  373. """
  374. Rename selected location
  375. """
  376. location = self.listOfLocations[self.lblocations.GetSelection()]
  377. dlg = wx.TextEntryDialog(parent=self,
  378. message=_('Current name: %s\nEnter new name:') % location,
  379. caption=_('Rename selected location'))
  380. if dlg.ShowModal() == wx.ID_OK:
  381. newlocation = dlg.GetValue()
  382. if newlocation != location:
  383. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  384. try:
  385. os.rename(os.path.join(self.gisdbase, location),
  386. os.path.join(self.gisdbase, newlocation))
  387. self.UpdateLocations(self.gisdbase)
  388. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  389. self.UpdateMapsets(newlocation)
  390. except:
  391. wx.MessageBox(message=_('Unable to rename location'))
  392. dlg.Destroy()
  393. def DeleteMapset(self):
  394. """
  395. Delete selected mapset
  396. """
  397. location = self.listOfLocations[self.lblocations.GetSelection()]
  398. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  399. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting mapset <%(mapset)s> "
  400. "from location <%(location)s>?\n\n"
  401. "ALL MAPS included in this mapset will be "
  402. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  403. 'location' : location},
  404. caption=_("Delete selected mapset"),
  405. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  406. if dlg.ShowModal() == wx.ID_YES:
  407. try:
  408. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  409. self.OnSelectLocation(None)
  410. self.lbmapsets.SetSelection(0)
  411. except:
  412. wx.MessageBox(message=_('Unable to delete mapset'))
  413. dlg.Destroy()
  414. def DeleteLocation(self):
  415. """
  416. Delete selected location
  417. """
  418. location = self.listOfLocations[self.lblocations.GetSelection()]
  419. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting "
  420. "location <%s>?\n\n"
  421. "ALL MAPS included in this location will be "
  422. "PERMANENTLY DELETED!") % (location),
  423. caption=_("Delete selected location"),
  424. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  425. if dlg.ShowModal() == wx.ID_YES:
  426. try:
  427. shutil.rmtree(os.path.join(self.gisdbase, location))
  428. self.UpdateLocations(self.gisdbase)
  429. self.lblocations.SetSelection(0)
  430. self.OnSelectLocation(None)
  431. self.lbmapsets.SetSelection(0)
  432. except:
  433. wx.MessageBox(message=_('Unable to delete location'))
  434. dlg.Destroy()
  435. def UpdateLocations(self, dbase):
  436. """Update list of locations"""
  437. self.listOfLocations = []
  438. for location in glob.glob(os.path.join(dbase, "*")):
  439. try:
  440. if os.path.join(location, "PERMANENT") in glob.glob(os.path.join(location, "*")):
  441. self.listOfLocations.append(os.path.basename(location))
  442. except:
  443. pass
  444. utils.ListSortLower(self.listOfLocations)
  445. self.lblocations.Clear()
  446. self.lblocations.InsertItems(self.listOfLocations, 0)
  447. if len(self.listOfLocations) > 0:
  448. self.lblocations.SetSelection(0)
  449. else:
  450. self.lblocations.SetSelection(wx.NOT_FOUND)
  451. return self.listOfLocations
  452. def UpdateMapsets(self, location):
  453. """Update list of mapsets"""
  454. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  455. self.listOfMapsets = []
  456. self.listOfMapsetsSelectable = []
  457. for mapset in glob.glob(os.path.join(self.gisdbase, location, "*")):
  458. if os.path.isdir(mapset) and \
  459. os.path.isfile(os.path.join(self.gisdbase, location, mapset, "WIND")) and \
  460. os.path.basename(mapset) != 'PERMANENT':
  461. self.listOfMapsets.append(os.path.basename(mapset))
  462. utils.ListSortLower(self.listOfMapsets)
  463. self.listOfMapsets.insert(0, 'PERMANENT')
  464. self.lbmapsets.Clear()
  465. # disable mapset with denied permission
  466. locationName = os.path.basename(location)
  467. try:
  468. ret = gcmd.RunCommand('g.mapset',
  469. read = True,
  470. flags = 'l',
  471. location = locationName,
  472. gisdbase = self.gisdbase)
  473. if not ret:
  474. raise gcmd.CmdError("")
  475. for line in ret.splitlines():
  476. self.listOfMapsetsSelectable += line.split(' ')
  477. except:
  478. gcmd.RunCommand("g.gisenv",
  479. set= "GISDBASE=%s" % self.gisdbase)
  480. gcmd.RunCommand("g.gisenv",
  481. set = "LOCATION_NAME=%s" % locationName)
  482. gcmd.RunCommand("g.gisenv",
  483. set = "MAPSET=PERMANENT")
  484. # first run only
  485. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  486. disabled = []
  487. idx = 0
  488. for mapset in self.listOfMapsets:
  489. if mapset not in self.listOfMapsetsSelectable or \
  490. os.path.isfile(os.path.join(self.gisdbase,
  491. locationName,
  492. mapset, ".gislock")):
  493. disabled.append(idx)
  494. idx += 1
  495. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  496. return self.listOfMapsets
  497. def OnSelectLocation(self, event):
  498. """Location selected"""
  499. if event:
  500. self.lblocations.SetSelection(event.GetIndex())
  501. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  502. self.UpdateMapsets(os.path.join(self.gisdbase,
  503. self.listOfLocations[self.lblocations.GetSelection()]))
  504. else:
  505. self.listOfMapsets = []
  506. disabled = []
  507. idx = 0
  508. try:
  509. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  510. except IndexError:
  511. locationName = ''
  512. for mapset in self.listOfMapsets:
  513. if mapset not in self.listOfMapsetsSelectable or \
  514. os.path.isfile(os.path.join(self.gisdbase,
  515. locationName,
  516. mapset, ".gislock")):
  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.RunCommand("g.gisenv",
  575. set = "GISDBASE=%s" % \
  576. self.tgisdbase.GetValue())
  577. gcmd.RunCommand("g.gisenv",
  578. set = "LOCATION_NAME=%s" % \
  579. self.listOfLocations[self.lblocations.GetSelection()])
  580. gcmd.RunCommand("g.gisenv",
  581. set = "MAPSET=%s" % \
  582. self.listOfMapsets[self.lbmapsets.GetSelection()])
  583. self.Destroy()
  584. sys.exit(0)
  585. def OnExit(self, event):
  586. """'Exit' button clicked"""
  587. self.Destroy()
  588. sys.exit (2)
  589. def OnHelp(self, event):
  590. """'Help' button clicked"""
  591. # help text in lib/init/helptext.html
  592. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  593. helpFrame = HelpWindow(parent=self, id=wx.ID_ANY,
  594. title=_("GRASS Quickstart"),
  595. size=(640, 480),
  596. file=file)
  597. helpFrame.Show(True)
  598. event.Skip()
  599. def OnCloseWindow(self, event):
  600. """Close window event"""
  601. event.Skip()
  602. sys.exit(2)
  603. class HelpWindow(wx.Frame):
  604. """GRASS Quickstart help window"""
  605. def __init__(self, parent, id, title, size, file):
  606. wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size)
  607. sizer = wx.BoxSizer(wx.VERTICAL)
  608. # text
  609. helpFrame = wx.html.HtmlWindow(parent=self, id=wx.ID_ANY)
  610. helpFrame.SetStandardFonts (size = 10)
  611. helpFrame.SetBorders(10)
  612. wx.InitAllImageHandlers()
  613. helpFrame.LoadFile(file)
  614. self.Ok = True
  615. sizer.Add(item=helpFrame, proportion=1, flag=wx.EXPAND)
  616. self.SetAutoLayout(True)
  617. self.SetSizer(sizer)
  618. # sizer.Fit(self)
  619. # sizer.SetSizeHints(self)
  620. self.Layout()
  621. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  622. """Use wx.ListCtrl instead of wx.ListBox, different style for
  623. non-selectable items (e.g. mapsets with denied permission)"""
  624. def __init__(self, parent, id, size,
  625. choices, disabled=[]):
  626. wx.ListCtrl.__init__(self, parent, id, size=size,
  627. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  628. wx.BORDER_SUNKEN)
  629. listmix.ListCtrlAutoWidthMixin.__init__(self)
  630. self.InsertColumn(0, '')
  631. self.selected = wx.NOT_FOUND
  632. self.__LoadData(choices, disabled)
  633. def __LoadData(self, choices, disabled=[]):
  634. """
  635. Load data into list
  636. @param choices list of item
  637. @param disabled list of indeces of non-selectable items
  638. """
  639. idx = 0
  640. for item in choices:
  641. index = self.InsertStringItem(sys.maxint, item)
  642. self.SetStringItem(index, 0, item)
  643. if idx in disabled:
  644. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  645. idx += 1
  646. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  647. def Clear(self):
  648. self.DeleteAllItems()
  649. def InsertItems(self, choices, pos, disabled=[]):
  650. self.__LoadData(choices, disabled)
  651. def SetSelection(self, item, force = False):
  652. if item != wx.NOT_FOUND and \
  653. (platform.system() != 'Windows' or force):
  654. ### Windows -> FIXME
  655. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  656. self.selected = item
  657. def GetSelection(self):
  658. return self.selected
  659. class StartUp(wx.App):
  660. """Start-up application"""
  661. def OnInit(self):
  662. wx.InitAllImageHandlers()
  663. StartUp = GRASSStartup()
  664. StartUp.CenterOnScreen()
  665. self.SetTopWindow(StartUp)
  666. StartUp.Show()
  667. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  668. wx.MessageBox(parent=StartUp,
  669. caption=_('Starting GRASS for the first time'),
  670. message=_('GRASS needs a directory in which to store its data. '
  671. 'Create one now if you have not already done so. '
  672. 'A popular choice is "grassdata", located in '
  673. 'your home directory.'),
  674. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  675. StartUp.OnBrowse(None)
  676. return 1
  677. if __name__ == "__main__":
  678. if os.getenv("GISBASE") is None:
  679. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  680. else:
  681. import gettext
  682. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  683. import gui_modules.gcmd as gcmd
  684. import gui_modules.utils as utils
  685. GRASSStartUp = StartUp(0)
  686. GRASSStartUp.MainLoop()