gis_set.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. 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.ETCICONDIR, "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. 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=5)
  205. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  206. flag=wx.ALIGN_CENTER_VERTICAL |
  207. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  208. border=5)
  209. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  210. flag=wx.ALIGN_CENTER_VERTICAL |
  211. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  212. border=5)
  213. # select sizer
  214. select_sizer.Add(item=self.llocation, proportion=0,
  215. flag=label_style | wx.ALL,
  216. border=5)
  217. select_sizer.Add(item=self.lmapset, proportion=0,
  218. flag=label_style | wx.ALL,
  219. border=5)
  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=5)
  233. manage_sizer.Add(item=self.bwizard, proportion=0,
  234. flag=label_style | wx.BOTTOM,
  235. border=8)
  236. manage_sizer.Add(item=self.lcreate, proportion=0,
  237. flag=label_style | wx.ALL,
  238. border=5)
  239. manage_sizer.Add(item=self.bmapset, proportion=0,
  240. flag=label_style | wx.BOTTOM,
  241. border=8)
  242. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  243. flag=label_style | wx.ALL,
  244. border=5)
  245. manage_sizer.Add(item=self.manageloc, proportion=0,
  246. flag=label_style | wx.BOTTOM,
  247. border=8)
  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=5) # 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=5)
  262. # buttons
  263. btns_sizer.Add(item=self.bstart, proportion=0,
  264. flag=wx.ALIGN_CENTER_HORIZONTAL |
  265. wx.ALL,
  266. border=10)
  267. btns_sizer.Add(item=self.bexit, proportion=0,
  268. flag=wx.ALIGN_CENTER_HORIZONTAL |
  269. wx.ALL,
  270. border=10)
  271. btns_sizer.Add(item=self.bhelp, proportion=0,
  272. flag=wx.ALIGN_CENTER_HORIZONTAL |
  273. wx.ALL,
  274. border=10)
  275. # main sizer
  276. sizer.Add(item=self.hbitmap, proportion=0,
  277. flag=wx.ADJUST_MINSIZE |
  278. wx.ALIGN_CENTER_VERTICAL |
  279. wx.ALIGN_CENTER_HORIZONTAL |
  280. wx.ALL,
  281. border=5) # image
  282. sizer.Add(item=self.lwelcome, # welcome message
  283. proportion=0,
  284. flag= wx.ADJUST_MINSIZE |
  285. wx.ALIGN_CENTER_VERTICAL |
  286. wx.ALIGN_CENTER_HORIZONTAL |
  287. wx.EXPAND |
  288. wx.BOTTOM,
  289. border=10)
  290. sizer.Add(item=self.ltitle, # title
  291. proportion=0,
  292. flag=wx.ADJUST_MINSIZE |
  293. wx.ALIGN_CENTER_VERTICAL |
  294. wx.ALIGN_CENTER_HORIZONTAL |
  295. wx.EXPAND |
  296. wx.BOTTOM,
  297. border=5)
  298. sizer.Add(item=dbase_sizer, proportion=0,
  299. flag=wx.ADJUST_MINSIZE |
  300. wx.ALIGN_CENTER_VERTICAL |
  301. wx.ALIGN_CENTER_HORIZONTAL |
  302. wx.RIGHT | wx.LEFT,
  303. border=5) # GISDBASE setting
  304. sizer.Add(item=location_sizer, proportion=1,
  305. flag=wx.ADJUST_MINSIZE |
  306. wx.ALIGN_CENTER_VERTICAL |
  307. wx.ALIGN_CENTER_HORIZONTAL |
  308. wx.RIGHT | wx.LEFT,
  309. border=5)
  310. sizer.Add(item=btns_sizer, proportion=0,
  311. flag=wx.ALIGN_CENTER_VERTICAL |
  312. wx.ALIGN_CENTER_HORIZONTAL |
  313. wx.RIGHT | wx.LEFT,
  314. border=5)
  315. self.panel.SetAutoLayout(True)
  316. self.panel.SetSizer(sizer)
  317. sizer.Fit(self.panel)
  318. sizer.SetSizeHints(self)
  319. self.Layout()
  320. def _read_grassrc(self):
  321. """
  322. Read variables from $HOME/.grassrc7 file
  323. """
  324. grassrc = {}
  325. gisrc = os.getenv("GISRC")
  326. if gisrc and os.path.isfile(gisrc):
  327. try:
  328. rc = open(gisrc, "r")
  329. for line in rc.readlines():
  330. key, val = line.split(":", 1)
  331. grassrc[key.strip()] = val.strip()
  332. finally:
  333. rc.close()
  334. return grassrc
  335. def _getRCValue(self, value):
  336. "Return GRASS variable (read from GISRC)"""
  337. if self.grassrc.has_key(value):
  338. return self.grassrc[value]
  339. else:
  340. return None
  341. def OnWizard(self, event):
  342. """Location wizard started"""
  343. from gui_modules import location_wizard
  344. gWizard = location_wizard.LocationWizard(self, self.tgisdbase.GetValue())
  345. if gWizard.location != None:
  346. self.OnSetDatabase(event)
  347. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  348. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  349. self.lbmapsets.SetSelection(0)
  350. def OnManageLoc(self, event):
  351. """
  352. Location management choice control handler
  353. """
  354. if event.GetString() == 'Rename mapset':
  355. self.RenameMapset()
  356. elif event.GetString() == 'Rename location':
  357. self.RenameLocation()
  358. elif event.GetString() == 'Delete mapset':
  359. self.DeleteMapset()
  360. elif event.GetString() == 'Delete location':
  361. self.DeleteLocation()
  362. def RenameMapset(self):
  363. """
  364. Rename selected mapset
  365. """
  366. location = self.listOfLocations[self.lblocations.GetSelection()]
  367. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  368. dlg = wx.TextEntryDialog(parent=self,
  369. message=_('Current name: %s\nEnter new name:') % mapset,
  370. caption=_('Rename selected mapset'))
  371. if dlg.ShowModal() == wx.ID_OK:
  372. newmapset = dlg.GetValue()
  373. if newmapset != mapset:
  374. try:
  375. os.rename(os.path.join(self.gisdbase, location, mapset),
  376. os.path.join(self.gisdbase, location, newmapset))
  377. self.OnSelectLocation(None)
  378. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  379. except:
  380. wx.MessageBox(message=_('Unable to rename mapset'))
  381. dlg.Destroy()
  382. def RenameLocation(self):
  383. """
  384. Rename selected location
  385. """
  386. location = self.listOfLocations[self.lblocations.GetSelection()]
  387. dlg = wx.TextEntryDialog(parent=self,
  388. message=_('Current name: %s\nEnter new name:') % location,
  389. caption=_('Rename selected location'))
  390. if dlg.ShowModal() == wx.ID_OK:
  391. newlocation = dlg.GetValue()
  392. if newlocation != location:
  393. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  394. try:
  395. os.rename(os.path.join(self.gisdbase, location),
  396. os.path.join(self.gisdbase, newlocation))
  397. self.UpdateLocations(self.gisdbase)
  398. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  399. self.UpdateMapsets(newlocation)
  400. except:
  401. wx.MessageBox(message=_('Unable to rename location'))
  402. dlg.Destroy()
  403. def DeleteMapset(self):
  404. """
  405. Delete selected mapset
  406. """
  407. location = self.listOfLocations[self.lblocations.GetSelection()]
  408. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  409. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting mapset <%(mapset)s> "
  410. "from location <%(location)s>?\n\n"
  411. "ALL MAPS included in this mapset will be "
  412. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  413. 'location' : location},
  414. caption=_("Delete selected mapset"),
  415. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  416. if dlg.ShowModal() == wx.ID_YES:
  417. try:
  418. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  419. self.OnSelectLocation(None)
  420. self.lbmapsets.SetSelection(0)
  421. except:
  422. wx.MessageBox(message=_('Unable to delete mapset'))
  423. dlg.Destroy()
  424. def DeleteLocation(self):
  425. """
  426. Delete selected location
  427. """
  428. location = self.listOfLocations[self.lblocations.GetSelection()]
  429. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting "
  430. "location <%s>?\n\n"
  431. "ALL MAPS included in this location will be "
  432. "PERMANENTLY DELETED!") % (location),
  433. caption=_("Delete selected location"),
  434. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  435. if dlg.ShowModal() == wx.ID_YES:
  436. try:
  437. shutil.rmtree(os.path.join(self.gisdbase, location))
  438. self.UpdateLocations(self.gisdbase)
  439. self.lblocations.SetSelection(0)
  440. self.OnSelectLocation(None)
  441. self.lbmapsets.SetSelection(0)
  442. except:
  443. wx.MessageBox(message=_('Unable to delete location'))
  444. dlg.Destroy()
  445. def UpdateLocations(self, dbase):
  446. """Update list of locations"""
  447. self.listOfLocations = []
  448. for location in glob.glob(os.path.join(dbase, "*")):
  449. try:
  450. if os.path.join(location, "PERMANENT") in glob.glob(os.path.join(location, "*")):
  451. self.listOfLocations.append(os.path.basename(location))
  452. except:
  453. pass
  454. utils.ListSortLower(self.listOfLocations)
  455. self.lblocations.Clear()
  456. self.lblocations.InsertItems(self.listOfLocations, 0)
  457. if len(self.listOfLocations) > 0:
  458. self.lblocations.SetSelection(0)
  459. else:
  460. self.lblocations.SetSelection(wx.NOT_FOUND)
  461. return self.listOfLocations
  462. def UpdateMapsets(self, location):
  463. """Update list of mapsets"""
  464. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  465. self.listOfMapsets = []
  466. self.listOfMapsetsSelectable = []
  467. for mapset in glob.glob(os.path.join(self.gisdbase, location, "*")):
  468. if os.path.isdir(mapset) and \
  469. os.path.isfile(os.path.join(self.gisdbase, location, mapset, "WIND")) and \
  470. os.path.basename(mapset) != 'PERMANENT':
  471. self.listOfMapsets.append(os.path.basename(mapset))
  472. utils.ListSortLower(self.listOfMapsets)
  473. self.listOfMapsets.insert(0, 'PERMANENT')
  474. self.lbmapsets.Clear()
  475. # disable mapset with denied permission
  476. locationName = os.path.basename(location)
  477. try:
  478. ret = gcmd.RunCommand('g.mapset',
  479. read = True
  480. flags = '-l',
  481. location = locationName,
  482. gisdbase = self.gisdbase)
  483. if not ret:
  484. raise gcmd.CmdError("")
  485. for line in ret.splitlines():
  486. self.listOfMapsetsSelectable += line.split(' ')
  487. except:
  488. gcmd.RunCommand("g.gisenv",
  489. set= "GISDBASE=%s" % self.gisdbase)
  490. gcmd.RunCommand("g.gisenv",
  491. set = "LOCATION_NAME=%s" % locationName)
  492. gcmd.RunCommand("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.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. return 1
  668. if __name__ == "__main__":
  669. if os.getenv("GISBASE") is None:
  670. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  671. else:
  672. import gettext
  673. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  674. import gui_modules.gcmd as gcmd
  675. import gui_modules.utils as utils
  676. GRASSStartUp = StartUp(0)
  677. GRASSStartUp.MainLoop()