ghelp.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. """!
  2. @package gui_core.ghelp
  3. @brief Help window
  4. Classes:
  5. - ghelp::SearchModuleWindow
  6. - ghelp::MenuTreeWindow
  7. - ghelp::MenuTree
  8. - ghelp::AboutWindow
  9. - ghelp::HelpFrame
  10. - ghelp::HelpWindow
  11. - ghelp::HelpPanel
  12. (C) 2008-2011 by the GRASS Development Team
  13. This program is free software under the GNU General Public License
  14. (>=v2). Read the file COPYING that comes with GRASS for details.
  15. @author Martin Landa <landa.martin gmail.com>
  16. """
  17. import os
  18. import codecs
  19. import wx
  20. from wx.html import HtmlWindow
  21. try:
  22. import wx.lib.agw.customtreectrl as CT
  23. from wx.lib.agw.hyperlink import HyperLinkCtrl
  24. except ImportError:
  25. import wx.lib.customtreectrl as CT
  26. from wx.lib.hyperlink import HyperLinkCtrl
  27. import wx.lib.flatnotebook as FN
  28. import wx.lib.scrolledpanel as scrolled
  29. import grass.script as grass
  30. from core import globalvar
  31. from core import utils
  32. from lmgr.menudata import ManagerData
  33. from core.gcmd import GError, DecodeString
  34. from gui_core.widgets import GNotebook, StaticWrapText, ItemTree
  35. class SearchModuleWindow(wx.Panel):
  36. """!Search module window (used in MenuTreeWindow)"""
  37. def __init__(self, parent, id = wx.ID_ANY, cmdPrompt = None,
  38. showChoice = True, showTip = False, **kwargs):
  39. self.showTip = showTip
  40. self.showChoice = showChoice
  41. self.cmdPrompt = cmdPrompt
  42. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  43. self._searchDict = { _('description') : 'description',
  44. _('command') : 'command',
  45. _('keywords') : 'keywords' }
  46. self.box = wx.StaticBox(parent = self, id = wx.ID_ANY,
  47. label = " %s " % _("Find module(s)"))
  48. self.searchBy = wx.Choice(parent = self, id = wx.ID_ANY,
  49. choices = [_('description'),
  50. _('keywords'),
  51. _('command')])
  52. self.searchBy.SetSelection(0)
  53. self.search = wx.TextCtrl(parent = self, id = wx.ID_ANY,
  54. value = "", size = (-1, 25),
  55. style = wx.TE_PROCESS_ENTER)
  56. self.search.Bind(wx.EVT_TEXT, self.OnSearchModule)
  57. if self.showTip:
  58. self.searchTip = StaticWrapText(parent = self, id = wx.ID_ANY,
  59. size = (-1, 35))
  60. if self.showChoice:
  61. self.searchChoice = wx.Choice(parent = self, id = wx.ID_ANY)
  62. if self.cmdPrompt:
  63. self.searchChoice.SetItems(self.cmdPrompt.GetCommandItems())
  64. self.searchChoice.Bind(wx.EVT_CHOICE, self.OnSelectModule)
  65. self._layout()
  66. def _layout(self):
  67. """!Do layout"""
  68. sizer = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  69. gridSizer = wx.GridBagSizer(hgap = 3, vgap = 3)
  70. gridSizer.AddGrowableCol(1)
  71. gridSizer.Add(item = self.searchBy,
  72. flag = wx.ALIGN_CENTER_VERTICAL, pos = (0, 0))
  73. gridSizer.Add(item = self.search,
  74. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos = (0, 1))
  75. row = 1
  76. if self.showTip:
  77. gridSizer.Add(item = self.searchTip,
  78. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos = (row, 0), span = (1, 2))
  79. row += 1
  80. if self.showChoice:
  81. gridSizer.Add(item = self.searchChoice,
  82. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos = (row, 0), span = (1, 2))
  83. sizer.Add(item = gridSizer, proportion = 1)
  84. self.SetSizer(sizer)
  85. sizer.Fit(self)
  86. def GetSelection(self):
  87. """!Get selected element"""
  88. selection = self.searchBy.GetStringSelection()
  89. return self._searchDict[selection]
  90. def SetSelection(self, i):
  91. """!Set selection element"""
  92. self.searchBy.SetSelection(i)
  93. def OnSearchModule(self, event):
  94. """!Search module by keywords or description"""
  95. if not self.cmdPrompt:
  96. event.Skip()
  97. return
  98. text = event.GetString()
  99. if not text:
  100. self.cmdPrompt.SetFilter(None)
  101. mList = self.cmdPrompt.GetCommandItems()
  102. self.searchChoice.SetItems(mList)
  103. if self.showTip:
  104. self.searchTip.SetLabel(_("%d modules found") % len(mList))
  105. event.Skip()
  106. return
  107. modules = dict()
  108. iFound = 0
  109. for module, data in self.cmdPrompt.moduleDesc.iteritems():
  110. found = False
  111. sel = self.searchBy.GetSelection()
  112. if sel == 0: # -> description
  113. if text in data['desc']:
  114. found = True
  115. elif sel == 1: # keywords
  116. if text in ','.join(data['keywords']):
  117. found = True
  118. else: # command
  119. if module[:len(text)] == text:
  120. found = True
  121. if found:
  122. iFound += 1
  123. try:
  124. group, name = module.split('.')
  125. except ValueError:
  126. continue # TODO
  127. if group not in modules:
  128. modules[group] = list()
  129. modules[group].append(name)
  130. self.cmdPrompt.SetFilter(modules)
  131. self.searchChoice.SetItems(self.cmdPrompt.GetCommandItems())
  132. if self.showTip:
  133. self.searchTip.SetLabel(_("%d modules found") % iFound)
  134. event.Skip()
  135. def OnSelectModule(self, event):
  136. """!Module selected from choice, update command prompt"""
  137. cmd = event.GetString().split(' ', 1)[0]
  138. text = cmd + ' '
  139. pos = len(text)
  140. if self.cmdPrompt:
  141. self.cmdPrompt.SetText(text)
  142. self.cmdPrompt.SetSelectionStart(pos)
  143. self.cmdPrompt.SetCurrentPos(pos)
  144. self.cmdPrompt.SetFocus()
  145. desc = self.cmdPrompt.GetCommandDesc(cmd)
  146. if self.showTip:
  147. self.searchTip.SetLabel(desc)
  148. def Reset(self):
  149. """!Reset widget"""
  150. self.searchBy.SetSelection(0)
  151. self.search.SetValue('')
  152. if self.showTip:
  153. self.searchTip.SetLabel('')
  154. class MenuTreeWindow(wx.Panel):
  155. """!Show menu tree"""
  156. def __init__(self, parent, id = wx.ID_ANY, **kwargs):
  157. self.parent = parent # LayerManager
  158. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  159. self.dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  160. label = " %s " % _("Menu tree (double-click to run command)"))
  161. # tree
  162. self.tree = MenuTree(parent = self, data = ManagerData())
  163. self.tree.Load()
  164. # search widget
  165. self.search = SearchModuleWindow(parent = self, showChoice = False)
  166. # buttons
  167. self.btnRun = wx.Button(self, id = wx.ID_OK, label = _("&Run"))
  168. self.btnRun.SetToolTipString(_("Run selected command"))
  169. self.btnRun.Enable(False)
  170. # bindings
  171. self.btnRun.Bind(wx.EVT_BUTTON, self.OnRun)
  172. self.tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnItemActivated)
  173. self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnItemSelected)
  174. self.search.Bind(wx.EVT_TEXT_ENTER, self.OnShowItem)
  175. self.search.Bind(wx.EVT_TEXT, self.OnUpdateStatusBar)
  176. self._layout()
  177. self.search.SetFocus()
  178. def _layout(self):
  179. """!Do dialog layout"""
  180. sizer = wx.BoxSizer(wx.VERTICAL)
  181. # body
  182. dataSizer = wx.StaticBoxSizer(self.dataBox, wx.HORIZONTAL)
  183. dataSizer.Add(item = self.tree, proportion =1,
  184. flag = wx.EXPAND)
  185. # buttons
  186. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  187. btnSizer.Add(item = self.btnRun, proportion = 0)
  188. sizer.Add(item = dataSizer, proportion = 1,
  189. flag = wx.EXPAND | wx.ALL, border = 5)
  190. sizer.Add(item = self.search, proportion = 0,
  191. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  192. sizer.Add(item = btnSizer, proportion = 0,
  193. flag = wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, border = 5)
  194. sizer.Fit(self)
  195. sizer.SetSizeHints(self)
  196. self.SetSizer(sizer)
  197. self.Fit()
  198. self.SetAutoLayout(True)
  199. self.Layout()
  200. def OnCloseWindow(self, event):
  201. """!Close window"""
  202. self.Destroy()
  203. def OnRun(self, event):
  204. """!Run selected command"""
  205. if not self.tree.GetSelected():
  206. return # should not happen
  207. data = self.tree.GetPyData(self.tree.GetSelected())
  208. if not data:
  209. return
  210. handler = 'self.parent.' + data['handler'].lstrip('self.')
  211. if data['handler'] == 'self.OnXTerm':
  212. wx.MessageBox(parent = self,
  213. message = _('You must run this command from the menu or command line',
  214. 'This command require an XTerm'),
  215. caption = _('Message'), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  216. elif data['command']:
  217. eval(handler)(event = None, cmd = data['command'].split())
  218. else:
  219. eval(handler)(None)
  220. def OnShowItem(self, event):
  221. """!Show selected item"""
  222. self.tree.OnShowItem(event)
  223. if self.tree.GetSelected():
  224. self.btnRun.Enable()
  225. else:
  226. self.btnRun.Enable(False)
  227. def OnItemActivated(self, event):
  228. """!Item activated (double-click)"""
  229. item = event.GetItem()
  230. if not item or not item.IsOk():
  231. return
  232. data = self.tree.GetPyData(item)
  233. if not data or 'command' not in data:
  234. return
  235. self.tree.itemSelected = item
  236. self.OnRun(None)
  237. def OnItemSelected(self, event):
  238. """!Item selected"""
  239. item = event.GetItem()
  240. if not item or not item.IsOk():
  241. return
  242. data = self.tree.GetPyData(item)
  243. if not data or 'command' not in data:
  244. return
  245. if data['command']:
  246. label = data['command'] + ' -- ' + data['description']
  247. else:
  248. label = data['description']
  249. self.parent.SetStatusText(label, 0)
  250. def OnUpdateStatusBar(self, event):
  251. """!Update statusbar text"""
  252. element = self.search.GetSelection()
  253. self.tree.SearchItems(element = element,
  254. value = event.GetString())
  255. nItems = len(self.tree.itemsMarked)
  256. if event.GetString():
  257. self.parent.SetStatusText(_("%d modules match") % nItems, 0)
  258. else:
  259. self.parent.SetStatusText("", 0)
  260. event.Skip()
  261. class MenuTree(ItemTree):
  262. """!Menu tree class"""
  263. def __init__(self, parent, data, **kwargs):
  264. self.parent = parent
  265. self.menudata = data
  266. super(MenuTree, self).__init__(parent, **kwargs)
  267. def Load(self, data = None):
  268. """!Load menu data tree
  269. @param data menu data (None to use self.menudata)
  270. """
  271. if not data:
  272. data = self.menudata
  273. self.itemsMarked = [] # list of marked items
  274. for eachMenuData in data.GetMenu():
  275. for label, items in eachMenuData:
  276. item = self.AppendItem(parentId = self.root,
  277. text = label.replace('&', ''))
  278. self.__AppendItems(item, items)
  279. def __AppendItems(self, item, data):
  280. """!Append items into tree (used by Load()
  281. @param item tree item (parent)
  282. @parent data menu data"""
  283. for eachItem in data:
  284. if len(eachItem) == 2:
  285. if eachItem[0]:
  286. itemSub = self.AppendItem(parentId = item,
  287. text = eachItem[0])
  288. self.__AppendItems(itemSub, eachItem[1])
  289. else:
  290. if eachItem[0]:
  291. itemNew = self.AppendItem(parentId = item,
  292. text = eachItem[0])
  293. data = { 'item' : eachItem[0],
  294. 'description' : eachItem[1],
  295. 'handler' : eachItem[2],
  296. 'command' : eachItem[3],
  297. 'keywords' : eachItem[4] }
  298. self.SetPyData(itemNew, data)
  299. class AboutWindow(wx.Frame):
  300. """!Create custom About Window
  301. @todo improve styling
  302. """
  303. def __init__(self, parent, size = (750, 400),
  304. title = _('About GRASS GIS'), **kwargs):
  305. wx.Frame.__init__(self, parent = parent, id = wx.ID_ANY, size = size, **kwargs)
  306. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  307. # icon
  308. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  309. # get version and web site
  310. vInfo = grass.version()
  311. infoTxt = wx.Panel(parent = panel, id = wx.ID_ANY)
  312. infoSizer = wx.BoxSizer(wx.VERTICAL)
  313. infoGridSizer = wx.GridBagSizer(vgap = 5, hgap = 5)
  314. infoGridSizer.AddGrowableCol(0)
  315. infoGridSizer.AddGrowableCol(1)
  316. logo = os.path.join(globalvar.ETCDIR, "gui", "icons", "grass.ico")
  317. logoBitmap = wx.StaticBitmap(parent = infoTxt, id = wx.ID_ANY,
  318. bitmap = wx.Bitmap(name = logo,
  319. type = wx.BITMAP_TYPE_ICO))
  320. infoSizer.Add(item = logoBitmap, proportion = 0,
  321. flag = wx.ALL | wx.ALIGN_CENTER, border = 25)
  322. info = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  323. label = 'GRASS GIS ' + vInfo['version'] + '\n\n')
  324. info.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  325. infoSizer.Add(item = info, proportion = 0,
  326. flag = wx.BOTTOM | wx.ALIGN_CENTER, border = 15)
  327. row = 0
  328. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  329. label = _('Official GRASS site:')),
  330. pos = (row, 0),
  331. flag = wx.ALIGN_RIGHT)
  332. infoGridSizer.Add(item = HyperLinkCtrl(parent = infoTxt, id = wx.ID_ANY,
  333. label = 'http://grass.osgeo.org'),
  334. pos = (row, 1),
  335. flag = wx.ALIGN_LEFT)
  336. row += 2
  337. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  338. label = _('SVN Revision:')),
  339. pos = (row, 0),
  340. flag = wx.ALIGN_RIGHT)
  341. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  342. label = vInfo['revision']),
  343. pos = (row, 1),
  344. flag = wx.ALIGN_LEFT)
  345. row += 1
  346. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  347. label = _('GIS Library Revision:')),
  348. pos = (row, 0),
  349. flag = wx.ALIGN_RIGHT)
  350. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  351. label = vInfo['libgis_revision'] + ' (' +
  352. vInfo['libgis_date'].split(' ')[0] + ')'),
  353. pos = (row, 1),
  354. flag = wx.ALIGN_LEFT)
  355. infoSizer.Add(item = infoGridSizer,
  356. proportion = 1,
  357. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL,
  358. border = 25)
  359. # create a flat notebook for displaying information about GRASS
  360. aboutNotebook = GNotebook(panel, style = globalvar.FNPageStyle | FN.FNB_NO_X_BUTTON)
  361. aboutNotebook.SetTabAreaColour(globalvar.FNPageColor)
  362. for title, win in ((_("Info"), infoTxt),
  363. (_("Copyright"), self._pageCopyright()),
  364. (_("License"), self._pageLicense()),
  365. (_("Authors"), self._pageCredit()),
  366. (_("Contributors"), self._pageContributors()),
  367. (_("Extra contributors"), self._pageContributors(extra = True)),
  368. (_("Translators"), self._pageTranslators())):
  369. aboutNotebook.AddPage(page = win, text = title)
  370. wx.CallAfter(aboutNotebook.SetSelection, 0)
  371. # buttons
  372. btnClose = wx.Button(parent = panel, id = wx.ID_CLOSE)
  373. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  374. btnSizer.Add(item = btnClose, proportion = 0,
  375. flag = wx.ALL | wx.ALIGN_RIGHT,
  376. border = 5)
  377. # bindings
  378. btnClose.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
  379. infoTxt.SetSizer(infoSizer)
  380. infoSizer.Fit(infoTxt)
  381. sizer = wx.BoxSizer(wx.VERTICAL)
  382. sizer.Add(item = aboutNotebook, proportion = 1,
  383. flag = wx.EXPAND | wx.ALL, border = 1)
  384. sizer.Add(item = btnSizer, proportion = 0,
  385. flag = wx.ALL | wx.ALIGN_RIGHT, border = 1)
  386. panel.SetSizer(sizer)
  387. self.Layout()
  388. def _pageCopyright(self):
  389. """Copyright information"""
  390. copyfile = os.path.join(os.getenv("GISBASE"), "COPYING")
  391. if os.path.exists(copyfile):
  392. copyrightFile = open(copyfile, 'r')
  393. copytext = copyrightFile.read()
  394. copyrightFile.close()
  395. else:
  396. copytext = _('%s file missing') % 'COPYING'
  397. # put text into a scrolling panel
  398. copyrightwin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  399. size = wx.DefaultSize,
  400. style = wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
  401. copyrighttxt = wx.StaticText(copyrightwin, id = wx.ID_ANY, label = copytext)
  402. copyrightwin.SetAutoLayout(True)
  403. copyrightwin.sizer = wx.BoxSizer(wx.VERTICAL)
  404. copyrightwin.sizer.Add(item = copyrighttxt, proportion = 1,
  405. flag = wx.EXPAND | wx.ALL, border = 3)
  406. copyrightwin.SetSizer(copyrightwin.sizer)
  407. copyrightwin.Layout()
  408. copyrightwin.SetupScrolling()
  409. return copyrightwin
  410. def _pageLicense(self):
  411. """Licence about"""
  412. licfile = os.path.join(os.getenv("GISBASE"), "GPL.TXT")
  413. if os.path.exists(licfile):
  414. licenceFile = open(licfile, 'r')
  415. license = ''.join(licenceFile.readlines())
  416. licenceFile.close()
  417. else:
  418. license = _('%s file missing') % 'GPL.TXT'
  419. # put text into a scrolling panel
  420. licensewin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  421. style = wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
  422. licensetxt = wx.StaticText(licensewin, id = wx.ID_ANY, label = license)
  423. licensewin.SetAutoLayout(True)
  424. licensewin.sizer = wx.BoxSizer(wx.VERTICAL)
  425. licensewin.sizer.Add(item = licensetxt, proportion = 1,
  426. flag = wx.EXPAND | wx.ALL, border = 3)
  427. licensewin.SetSizer(licensewin.sizer)
  428. licensewin.Layout()
  429. licensewin.SetupScrolling()
  430. return licensewin
  431. def _pageCredit(self):
  432. """Credit about"""
  433. # credits
  434. authfile = os.path.join(os.getenv("GISBASE"), "AUTHORS")
  435. if os.path.exists(authfile):
  436. authorsFile = open(authfile, 'r')
  437. authors = unicode(''.join(authorsFile.readlines()), "utf-8")
  438. authorsFile.close()
  439. else:
  440. authors = _('%s file missing') % 'AUTHORS'
  441. authorwin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  442. style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)
  443. authortxt = wx.StaticText(authorwin, id = wx.ID_ANY, label = authors)
  444. authorwin.SetAutoLayout(1)
  445. authorwin.SetupScrolling()
  446. authorwin.sizer = wx.BoxSizer(wx.VERTICAL)
  447. authorwin.sizer.Add(item = authortxt, proportion = 1,
  448. flag = wx.EXPAND | wx.ALL, border = 3)
  449. authorwin.SetSizer(authorwin.sizer)
  450. authorwin.Layout()
  451. return authorwin
  452. def _pageContributors(self, extra = False):
  453. """Contributors info"""
  454. if extra:
  455. contribfile = os.path.join(os.getenv("GISBASE"), "contributors_extra.csv")
  456. else:
  457. contribfile = os.path.join(os.getenv("GISBASE"), "contributors.csv")
  458. if os.path.exists(contribfile):
  459. contribFile = codecs.open(contribfile, encoding = 'utf-8', mode = 'r')
  460. contribs = list()
  461. errLines = list()
  462. for line in contribFile.readlines()[1:]:
  463. line = line.rstrip('\n')
  464. try:
  465. if extra:
  466. name, email, rfc2_agreed = line.split(',')
  467. else:
  468. cvs_id, name, email, country, osgeo_id, rfc2_agreed = line.split(',')
  469. except ValueError:
  470. errLines.append(line)
  471. continue
  472. if extra:
  473. contribs.append((name, email))
  474. else:
  475. contribs.append((name, email, country, osgeo_id))
  476. contribFile.close()
  477. if errLines:
  478. GError(parent = self,
  479. message = _("Error when reading file '%s'.") % contribfile + \
  480. "\n\n" + _("Lines:") + " %s" % \
  481. os.linesep.join(map(DecodeString, errLines)))
  482. else:
  483. contribs = None
  484. contribwin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  485. style = wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
  486. contribwin.SetAutoLayout(True)
  487. contribwin.SetupScrolling()
  488. contribwin.sizer = wx.BoxSizer(wx.VERTICAL)
  489. if not contribs:
  490. contribtxt = wx.StaticText(contribwin, id = wx.ID_ANY,
  491. label = _('%s file missing') % contribfile)
  492. contribwin.sizer.Add(item = contribtxt, proportion = 1,
  493. flag = wx.EXPAND | wx.ALL, border = 3)
  494. else:
  495. if extra:
  496. items = (_('Name'), _('E-mail'))
  497. else:
  498. items = (_('Name'), _('E-mail'), _('Country'), _('OSGeo_ID'))
  499. contribBox = wx.FlexGridSizer(cols = len(items), vgap = 5, hgap = 5)
  500. for item in items:
  501. contribBox.Add(item = wx.StaticText(parent = contribwin, id = wx.ID_ANY,
  502. label = item))
  503. for vals in sorted(contribs, key = lambda x: x[0]):
  504. for item in vals:
  505. contribBox.Add(item = wx.StaticText(parent = contribwin, id = wx.ID_ANY,
  506. label = item))
  507. contribwin.sizer.Add(item = contribBox, proportion = 1,
  508. flag = wx.EXPAND | wx.ALL, border = 3)
  509. contribwin.SetSizer(contribwin.sizer)
  510. contribwin.Layout()
  511. return contribwin
  512. def _pageTranslators(self):
  513. """Translators info"""
  514. translatorsfile = os.path.join(os.getenv("GISBASE"), "translators.csv")
  515. if os.path.exists(translatorsfile):
  516. translatorsFile = open(translatorsfile, 'r')
  517. translators = dict()
  518. errLines = list()
  519. for line in translatorsFile.readlines()[1:]:
  520. line = line.rstrip('\n')
  521. try:
  522. name, email, languages = line.split(',')
  523. except ValueError:
  524. errLines.append(line)
  525. continue
  526. for language in languages.split(' '):
  527. if language not in translators:
  528. translators[language] = list()
  529. translators[language].append((name, email))
  530. translatorsFile.close()
  531. if errLines:
  532. GError(parent = self,
  533. message = _("Error when reading file '%s'.") % translatorsfile + \
  534. "\n\n" + _("Lines:") + " %s" % \
  535. os.linesep.join(map(DecodeString, errLines)))
  536. else:
  537. translators = None
  538. translatorswin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  539. style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)
  540. translatorswin.SetAutoLayout(1)
  541. translatorswin.SetupScrolling()
  542. translatorswin.sizer = wx.BoxSizer(wx.VERTICAL)
  543. if not translators:
  544. translatorstxt = wx.StaticText(translatorswin, id = wx.ID_ANY,
  545. label = _('%s file missing') % 'translators.csv')
  546. translatorswin.sizer.Add(item = translatorstxt, proportion = 1,
  547. flag = wx.EXPAND | wx.ALL, border = 3)
  548. else:
  549. translatorsBox = wx.FlexGridSizer(cols = 3, vgap = 5, hgap = 5)
  550. languages = translators.keys()
  551. languages.sort()
  552. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  553. label = _('Name')))
  554. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  555. label = _('E-mail')))
  556. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  557. label = _('Language')))
  558. for lang in languages:
  559. for translator in translators[lang]:
  560. name, email = translator
  561. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  562. label = unicode(name, "utf-8")))
  563. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  564. label = email))
  565. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  566. label = lang))
  567. translatorswin.sizer.Add(item = translatorsBox, proportion = 1,
  568. flag = wx.EXPAND | wx.ALL, border = 3)
  569. translatorswin.SetSizer(translatorswin.sizer)
  570. translatorswin.Layout()
  571. return translatorswin
  572. def OnCloseWindow(self, event):
  573. """!Close window"""
  574. self.Close()
  575. class HelpFrame(wx.Frame):
  576. """!GRASS Quickstart help window"""
  577. def __init__(self, parent, id, title, size, file):
  578. wx.Frame.__init__(self, parent = parent, id = id, title = title, size = size)
  579. sizer = wx.BoxSizer(wx.VERTICAL)
  580. # text
  581. content = HelpPanel(parent = self)
  582. content.LoadPage(file)
  583. sizer.Add(item = content, proportion = 1, flag = wx.EXPAND)
  584. self.SetAutoLayout(True)
  585. self.SetSizer(sizer)
  586. self.Layout()
  587. class HelpWindow(HtmlWindow):
  588. """!This panel holds the text from GRASS docs.
  589. GISBASE must be set in the environment to find the html docs dir.
  590. The SYNOPSIS section is skipped, since this Panel is supposed to
  591. be integrated into the cmdPanel and options are obvious there.
  592. """
  593. def __init__(self, parent, grass_command, text, skip_description,
  594. **kwargs):
  595. """!If grass_command is given, the corresponding HTML help
  596. file will be presented, with all links pointing to absolute
  597. paths of local files.
  598. If 'skip_description' is True, the HTML corresponding to
  599. SYNOPSIS will be skipped, thus only presenting the help file
  600. from the DESCRIPTION section onwards.
  601. If 'text' is given, it must be the HTML text to be presented
  602. in the Panel.
  603. """
  604. self.parent = parent
  605. wx.InitAllImageHandlers()
  606. HtmlWindow.__init__(self, parent = parent, **kwargs)
  607. gisbase = os.getenv("GISBASE")
  608. self.loaded = False
  609. self.history = list()
  610. self.historyIdx = 0
  611. self.fspath = os.path.join(gisbase, "docs", "html")
  612. self.SetStandardFonts (size = 10)
  613. self.SetBorders(10)
  614. if text is None:
  615. if skip_description:
  616. url = os.path.join(self.fspath, grass_command + ".html")
  617. self.fillContentsFromFile(url,
  618. skip_description = skip_description)
  619. self.history.append(url)
  620. self.loaded = True
  621. else:
  622. ### FIXME: calling LoadPage() is strangely time-consuming (only first call)
  623. # self.LoadPage(self.fspath + grass_command + ".html")
  624. self.loaded = False
  625. else:
  626. self.SetPage(text)
  627. self.loaded = True
  628. def OnLinkClicked(self, linkinfo):
  629. url = linkinfo.GetHref()
  630. if url[:4] != 'http':
  631. url = os.path.join(self.fspath, url)
  632. self.history.append(url)
  633. self.historyIdx += 1
  634. self.parent.OnHistory()
  635. super(HelpWindow, self).OnLinkClicked(linkinfo)
  636. def fillContentsFromFile(self, htmlFile, skip_description = True):
  637. """!Load content from file"""
  638. aLink = re.compile(r'(<a href="?)(.+\.html?["\s]*>)', re.IGNORECASE)
  639. imgLink = re.compile(r'(<img src="?)(.+\.[png|gif])', re.IGNORECASE)
  640. try:
  641. contents = []
  642. skip = False
  643. for l in file(htmlFile, "rb").readlines():
  644. if "DESCRIPTION" in l:
  645. skip = False
  646. if not skip:
  647. # do skip the options description if requested
  648. if "SYNOPSIS" in l:
  649. skip = skip_description
  650. else:
  651. # FIXME: find only first item
  652. findALink = aLink.search(l)
  653. if findALink is not None:
  654. contents.append(aLink.sub(findALink.group(1)+
  655. self.fspath+findALink.group(2),l))
  656. findImgLink = imgLink.search(l)
  657. if findImgLink is not None:
  658. contents.append(imgLink.sub(findImgLink.group(1)+
  659. self.fspath+findImgLink.group(2),l))
  660. if findALink is None and findImgLink is None:
  661. contents.append(l)
  662. self.SetPage("".join(contents))
  663. self.loaded = True
  664. except: # The Manual file was not found
  665. self.loaded = False
  666. class HelpPanel(wx.Panel):
  667. def __init__(self, parent, grass_command = "index", text = None,
  668. skip_description = False, **kwargs):
  669. self.grass_command = grass_command
  670. wx.Panel.__init__(self, parent = parent, id = wx.ID_ANY)
  671. self.content = HelpWindow(self, grass_command, text,
  672. skip_description)
  673. self.btnNext = wx.Button(parent = self, id = wx.ID_ANY,
  674. label = _("&Next"))
  675. self.btnNext.Enable(False)
  676. self.btnPrev = wx.Button(parent = self, id = wx.ID_ANY,
  677. label = _("&Previous"))
  678. self.btnPrev.Enable(False)
  679. self.btnNext.Bind(wx.EVT_BUTTON, self.OnNext)
  680. self.btnPrev.Bind(wx.EVT_BUTTON, self.OnPrev)
  681. self._layout()
  682. def _layout(self):
  683. """!Do layout"""
  684. sizer = wx.BoxSizer(wx.VERTICAL)
  685. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  686. btnSizer.Add(item = self.btnPrev, proportion = 0,
  687. flag = wx.ALL, border = 5)
  688. btnSizer.Add(item = wx.Size(1, 1), proportion = 1)
  689. btnSizer.Add(item = self.btnNext, proportion = 0,
  690. flag = wx.ALIGN_RIGHT | wx.ALL, border = 5)
  691. sizer.Add(item = self.content, proportion = 1,
  692. flag = wx.EXPAND)
  693. sizer.Add(item = btnSizer, proportion = 0,
  694. flag = wx.EXPAND)
  695. self.SetSizer(sizer)
  696. sizer.Fit(self)
  697. def LoadPage(self, path = None):
  698. """!Load page"""
  699. if not path:
  700. path = os.path.join(self.content.fspath, self.grass_command + ".html")
  701. self.content.history.append(path)
  702. self.content.LoadPage(path)
  703. def IsFile(self):
  704. """!Check if file exists"""
  705. return os.path.isfile(os.path.join(self.content.fspath, self.grass_command + ".html"))
  706. def IsLoaded(self):
  707. return self.content.loaded
  708. def OnHistory(self):
  709. """!Update buttons"""
  710. nH = len(self.content.history)
  711. iH = self.content.historyIdx
  712. if iH == nH - 1:
  713. self.btnNext.Enable(False)
  714. elif iH > -1:
  715. self.btnNext.Enable(True)
  716. if iH < 1:
  717. self.btnPrev.Enable(False)
  718. else:
  719. self.btnPrev.Enable(True)
  720. def OnNext(self, event):
  721. """Load next page"""
  722. self.content.historyIdx += 1
  723. idx = self.content.historyIdx
  724. path = self.content.history[idx]
  725. self.content.LoadPage(path)
  726. self.OnHistory()
  727. event.Skip()
  728. def OnPrev(self, event):
  729. """Load previous page"""
  730. self.content.historyIdx -= 1
  731. idx = self.content.historyIdx
  732. path = self.content.history[idx]
  733. self.content.LoadPage(path)
  734. self.OnHistory()
  735. event.Skip()