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 HelpFrame(wx.Frame):
  36. """!GRASS Quickstart help window"""
  37. def __init__(self, parent, id, title, size, file):
  38. wx.Frame.__init__(self, parent = parent, id = id, title = title, size = size)
  39. sizer = wx.BoxSizer(wx.VERTICAL)
  40. # text
  41. content = HelpPanel(parent = self)
  42. content.LoadPage(file)
  43. sizer.Add(item = content, proportion = 1, flag = wx.EXPAND)
  44. self.SetAutoLayout(True)
  45. self.SetSizer(sizer)
  46. self.Layout()
  47. class SearchModuleWindow(wx.Panel):
  48. """!Search module window (used in MenuTreeWindow)"""
  49. def __init__(self, parent, id = wx.ID_ANY, cmdPrompt = None,
  50. showChoice = True, showTip = False, **kwargs):
  51. self.showTip = showTip
  52. self.showChoice = showChoice
  53. self.cmdPrompt = cmdPrompt
  54. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  55. self._searchDict = { _('description') : 'description',
  56. _('command') : 'command',
  57. _('keywords') : 'keywords' }
  58. self.box = wx.StaticBox(parent = self, id = wx.ID_ANY,
  59. label = " %s " % _("Find module(s)"))
  60. self.searchBy = wx.Choice(parent = self, id = wx.ID_ANY,
  61. choices = [_('description'),
  62. _('keywords'),
  63. _('command')])
  64. self.searchBy.SetSelection(0)
  65. self.search = wx.TextCtrl(parent = self, id = wx.ID_ANY,
  66. value = "", size = (-1, 25),
  67. style = wx.TE_PROCESS_ENTER)
  68. self.search.Bind(wx.EVT_TEXT, self.OnSearchModule)
  69. if self.showTip:
  70. self.searchTip = StaticWrapText(parent = self, id = wx.ID_ANY,
  71. size = (-1, 35))
  72. if self.showChoice:
  73. self.searchChoice = wx.Choice(parent = self, id = wx.ID_ANY)
  74. if self.cmdPrompt:
  75. self.searchChoice.SetItems(self.cmdPrompt.GetCommandItems())
  76. self.searchChoice.Bind(wx.EVT_CHOICE, self.OnSelectModule)
  77. self._layout()
  78. def _layout(self):
  79. """!Do layout"""
  80. sizer = wx.StaticBoxSizer(self.box, wx.HORIZONTAL)
  81. gridSizer = wx.GridBagSizer(hgap = 3, vgap = 3)
  82. gridSizer.AddGrowableCol(1)
  83. gridSizer.Add(item = self.searchBy,
  84. flag = wx.ALIGN_CENTER_VERTICAL, pos = (0, 0))
  85. gridSizer.Add(item = self.search,
  86. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos = (0, 1))
  87. row = 1
  88. if self.showTip:
  89. gridSizer.Add(item = self.searchTip,
  90. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos = (row, 0), span = (1, 2))
  91. row += 1
  92. if self.showChoice:
  93. gridSizer.Add(item = self.searchChoice,
  94. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos = (row, 0), span = (1, 2))
  95. sizer.Add(item = gridSizer, proportion = 1)
  96. self.SetSizer(sizer)
  97. sizer.Fit(self)
  98. def GetSelection(self):
  99. """!Get selected element"""
  100. selection = self.searchBy.GetStringSelection()
  101. return self._searchDict[selection]
  102. def SetSelection(self, i):
  103. """!Set selection element"""
  104. self.searchBy.SetSelection(i)
  105. def OnSearchModule(self, event):
  106. """!Search module by keywords or description"""
  107. if not self.cmdPrompt:
  108. event.Skip()
  109. return
  110. text = event.GetString()
  111. if not text:
  112. self.cmdPrompt.SetFilter(None)
  113. mList = self.cmdPrompt.GetCommandItems()
  114. self.searchChoice.SetItems(mList)
  115. if self.showTip:
  116. self.searchTip.SetLabel(_("%d modules found") % len(mList))
  117. event.Skip()
  118. return
  119. modules = dict()
  120. iFound = 0
  121. for module, data in self.cmdPrompt.moduleDesc.iteritems():
  122. found = False
  123. sel = self.searchBy.GetSelection()
  124. if sel == 0: # -> description
  125. if text in data['desc']:
  126. found = True
  127. elif sel == 1: # keywords
  128. if text in ','.join(data['keywords']):
  129. found = True
  130. else: # command
  131. if module[:len(text)] == text:
  132. found = True
  133. if found:
  134. iFound += 1
  135. try:
  136. group, name = module.split('.')
  137. except ValueError:
  138. continue # TODO
  139. if group not in modules:
  140. modules[group] = list()
  141. modules[group].append(name)
  142. self.cmdPrompt.SetFilter(modules)
  143. self.searchChoice.SetItems(self.cmdPrompt.GetCommandItems())
  144. if self.showTip:
  145. self.searchTip.SetLabel(_("%d modules found") % iFound)
  146. event.Skip()
  147. def OnSelectModule(self, event):
  148. """!Module selected from choice, update command prompt"""
  149. cmd = event.GetString().split(' ', 1)[0]
  150. text = cmd + ' '
  151. pos = len(text)
  152. if self.cmdPrompt:
  153. self.cmdPrompt.SetText(text)
  154. self.cmdPrompt.SetSelectionStart(pos)
  155. self.cmdPrompt.SetCurrentPos(pos)
  156. self.cmdPrompt.SetFocus()
  157. desc = self.cmdPrompt.GetCommandDesc(cmd)
  158. if self.showTip:
  159. self.searchTip.SetLabel(desc)
  160. def Reset(self):
  161. """!Reset widget"""
  162. self.searchBy.SetSelection(0)
  163. self.search.SetValue('')
  164. if self.showTip:
  165. self.searchTip.SetLabel('')
  166. class MenuTreeWindow(wx.Panel):
  167. """!Show menu tree"""
  168. def __init__(self, parent, id = wx.ID_ANY, **kwargs):
  169. self.parent = parent # LayerManager
  170. wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
  171. self.dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  172. label = " %s " % _("Menu tree (double-click to run command)"))
  173. # tree
  174. self.tree = MenuTree(parent = self, data = ManagerData())
  175. self.tree.Load()
  176. # search widget
  177. self.search = SearchModuleWindow(parent = self, showChoice = False)
  178. # buttons
  179. self.btnRun = wx.Button(self, id = wx.ID_OK, label = _("&Run"))
  180. self.btnRun.SetToolTipString(_("Run selected command"))
  181. self.btnRun.Enable(False)
  182. # bindings
  183. self.btnRun.Bind(wx.EVT_BUTTON, self.OnRun)
  184. self.tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnItemActivated)
  185. self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnItemSelected)
  186. self.search.Bind(wx.EVT_TEXT_ENTER, self.OnShowItem)
  187. self.search.Bind(wx.EVT_TEXT, self.OnUpdateStatusBar)
  188. self._layout()
  189. self.search.SetFocus()
  190. def _layout(self):
  191. """!Do dialog layout"""
  192. sizer = wx.BoxSizer(wx.VERTICAL)
  193. # body
  194. dataSizer = wx.StaticBoxSizer(self.dataBox, wx.HORIZONTAL)
  195. dataSizer.Add(item = self.tree, proportion =1,
  196. flag = wx.EXPAND)
  197. # buttons
  198. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  199. btnSizer.Add(item = self.btnRun, proportion = 0)
  200. sizer.Add(item = dataSizer, proportion = 1,
  201. flag = wx.EXPAND | wx.ALL, border = 5)
  202. sizer.Add(item = self.search, proportion = 0,
  203. flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
  204. sizer.Add(item = btnSizer, proportion = 0,
  205. flag = wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, border = 5)
  206. sizer.Fit(self)
  207. sizer.SetSizeHints(self)
  208. self.SetSizer(sizer)
  209. self.Fit()
  210. self.SetAutoLayout(True)
  211. self.Layout()
  212. def OnCloseWindow(self, event):
  213. """!Close window"""
  214. self.Destroy()
  215. def OnRun(self, event):
  216. """!Run selected command"""
  217. if not self.tree.GetSelected():
  218. return # should not happen
  219. data = self.tree.GetPyData(self.tree.GetSelected())
  220. if not data:
  221. return
  222. handler = 'self.parent.' + data['handler'].lstrip('self.')
  223. if data['handler'] == 'self.OnXTerm':
  224. wx.MessageBox(parent = self,
  225. message = _('You must run this command from the menu or command line',
  226. 'This command require an XTerm'),
  227. caption = _('Message'), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  228. elif data['command']:
  229. eval(handler)(event = None, cmd = data['command'].split())
  230. else:
  231. eval(handler)(None)
  232. def OnShowItem(self, event):
  233. """!Show selected item"""
  234. self.tree.OnShowItem(event)
  235. if self.tree.GetSelected():
  236. self.btnRun.Enable()
  237. else:
  238. self.btnRun.Enable(False)
  239. def OnItemActivated(self, event):
  240. """!Item activated (double-click)"""
  241. item = event.GetItem()
  242. if not item or not item.IsOk():
  243. return
  244. data = self.tree.GetPyData(item)
  245. if not data or 'command' not in data:
  246. return
  247. self.tree.itemSelected = item
  248. self.OnRun(None)
  249. def OnItemSelected(self, event):
  250. """!Item selected"""
  251. item = event.GetItem()
  252. if not item or not item.IsOk():
  253. return
  254. data = self.tree.GetPyData(item)
  255. if not data or 'command' not in data:
  256. return
  257. if data['command']:
  258. label = data['command'] + ' -- ' + data['description']
  259. else:
  260. label = data['description']
  261. self.parent.SetStatusText(label, 0)
  262. def OnUpdateStatusBar(self, event):
  263. """!Update statusbar text"""
  264. element = self.search.GetSelection()
  265. self.tree.SearchItems(element = element,
  266. value = event.GetString())
  267. nItems = len(self.tree.itemsMarked)
  268. if event.GetString():
  269. self.parent.SetStatusText(_("%d modules match") % nItems, 0)
  270. else:
  271. self.parent.SetStatusText("", 0)
  272. event.Skip()
  273. class MenuTree(ItemTree):
  274. """!Menu tree class"""
  275. def __init__(self, parent, data, **kwargs):
  276. self.parent = parent
  277. self.menudata = data
  278. super(MenuTree, self).__init__(parent, **kwargs)
  279. def Load(self, data = None):
  280. """!Load menu data tree
  281. @param data menu data (None to use self.menudata)
  282. """
  283. if not data:
  284. data = self.menudata
  285. self.itemsMarked = [] # list of marked items
  286. for eachMenuData in data.GetMenu():
  287. for label, items in eachMenuData:
  288. item = self.AppendItem(parentId = self.root,
  289. text = label.replace('&', ''))
  290. self.__AppendItems(item, items)
  291. def __AppendItems(self, item, data):
  292. """!Append items into tree (used by Load()
  293. @param item tree item (parent)
  294. @parent data menu data"""
  295. for eachItem in data:
  296. if len(eachItem) == 2:
  297. if eachItem[0]:
  298. itemSub = self.AppendItem(parentId = item,
  299. text = eachItem[0])
  300. self.__AppendItems(itemSub, eachItem[1])
  301. else:
  302. if eachItem[0]:
  303. itemNew = self.AppendItem(parentId = item,
  304. text = eachItem[0])
  305. data = { 'item' : eachItem[0],
  306. 'description' : eachItem[1],
  307. 'handler' : eachItem[2],
  308. 'command' : eachItem[3],
  309. 'keywords' : eachItem[4] }
  310. self.SetPyData(itemNew, data)
  311. class AboutWindow(wx.Frame):
  312. """!Create custom About Window
  313. @todo improve styling
  314. """
  315. def __init__(self, parent, size = (750, 400),
  316. title = _('About GRASS GIS'), **kwargs):
  317. wx.Frame.__init__(self, parent = parent, id = wx.ID_ANY, size = size, **kwargs)
  318. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  319. # icon
  320. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'), wx.BITMAP_TYPE_ICO))
  321. # get version and web site
  322. vInfo = grass.version()
  323. infoTxt = wx.Panel(parent = panel, id = wx.ID_ANY)
  324. infoSizer = wx.BoxSizer(wx.VERTICAL)
  325. infoGridSizer = wx.GridBagSizer(vgap = 5, hgap = 5)
  326. infoGridSizer.AddGrowableCol(0)
  327. infoGridSizer.AddGrowableCol(1)
  328. logo = os.path.join(globalvar.ETCDIR, "gui", "icons", "grass.ico")
  329. logoBitmap = wx.StaticBitmap(parent = infoTxt, id = wx.ID_ANY,
  330. bitmap = wx.Bitmap(name = logo,
  331. type = wx.BITMAP_TYPE_ICO))
  332. infoSizer.Add(item = logoBitmap, proportion = 0,
  333. flag = wx.ALL | wx.ALIGN_CENTER, border = 25)
  334. info = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  335. label = 'GRASS GIS ' + vInfo['version'] + '\n\n')
  336. info.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  337. infoSizer.Add(item = info, proportion = 0,
  338. flag = wx.BOTTOM | wx.ALIGN_CENTER, border = 15)
  339. row = 0
  340. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  341. label = _('Official GRASS site:')),
  342. pos = (row, 0),
  343. flag = wx.ALIGN_RIGHT)
  344. infoGridSizer.Add(item = HyperLinkCtrl(parent = infoTxt, id = wx.ID_ANY,
  345. label = 'http://grass.osgeo.org'),
  346. pos = (row, 1),
  347. flag = wx.ALIGN_LEFT)
  348. row += 2
  349. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  350. label = _('SVN Revision:')),
  351. pos = (row, 0),
  352. flag = wx.ALIGN_RIGHT)
  353. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  354. label = vInfo['revision']),
  355. pos = (row, 1),
  356. flag = wx.ALIGN_LEFT)
  357. row += 1
  358. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  359. label = _('GIS Library Revision:')),
  360. pos = (row, 0),
  361. flag = wx.ALIGN_RIGHT)
  362. infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  363. label = vInfo['libgis_revision'] + ' (' +
  364. vInfo['libgis_date'].split(' ')[0] + ')'),
  365. pos = (row, 1),
  366. flag = wx.ALIGN_LEFT)
  367. infoSizer.Add(item = infoGridSizer,
  368. proportion = 1,
  369. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL,
  370. border = 25)
  371. # create a flat notebook for displaying information about GRASS
  372. aboutNotebook = GNotebook(panel, style = globalvar.FNPageStyle | FN.FNB_NO_X_BUTTON)
  373. aboutNotebook.SetTabAreaColour(globalvar.FNPageColor)
  374. for title, win in ((_("Info"), infoTxt),
  375. (_("Copyright"), self._pageCopyright()),
  376. (_("License"), self._pageLicense()),
  377. (_("Authors"), self._pageCredit()),
  378. (_("Contributors"), self._pageContributors()),
  379. (_("Extra contributors"), self._pageContributors(extra = True)),
  380. (_("Translators"), self._pageTranslators())):
  381. aboutNotebook.AddPage(page = win, text = title)
  382. wx.CallAfter(aboutNotebook.SetSelection, 0)
  383. # buttons
  384. btnClose = wx.Button(parent = panel, id = wx.ID_CLOSE)
  385. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  386. btnSizer.Add(item = btnClose, proportion = 0,
  387. flag = wx.ALL | wx.ALIGN_RIGHT,
  388. border = 5)
  389. # bindings
  390. btnClose.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
  391. infoTxt.SetSizer(infoSizer)
  392. infoSizer.Fit(infoTxt)
  393. sizer = wx.BoxSizer(wx.VERTICAL)
  394. sizer.Add(item = aboutNotebook, proportion = 1,
  395. flag = wx.EXPAND | wx.ALL, border = 1)
  396. sizer.Add(item = btnSizer, proportion = 0,
  397. flag = wx.ALL | wx.ALIGN_RIGHT, border = 1)
  398. panel.SetSizer(sizer)
  399. self.Layout()
  400. def _pageCopyright(self):
  401. """Copyright information"""
  402. copyfile = os.path.join(os.getenv("GISBASE"), "COPYING")
  403. if os.path.exists(copyfile):
  404. copyrightFile = open(copyfile, 'r')
  405. copytext = copyrightFile.read()
  406. copyrightFile.close()
  407. else:
  408. copytext = _('%s file missing') % 'COPYING'
  409. # put text into a scrolling panel
  410. copyrightwin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  411. size = wx.DefaultSize,
  412. style = wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
  413. copyrighttxt = wx.StaticText(copyrightwin, id = wx.ID_ANY, label = copytext)
  414. copyrightwin.SetAutoLayout(True)
  415. copyrightwin.sizer = wx.BoxSizer(wx.VERTICAL)
  416. copyrightwin.sizer.Add(item = copyrighttxt, proportion = 1,
  417. flag = wx.EXPAND | wx.ALL, border = 3)
  418. copyrightwin.SetSizer(copyrightwin.sizer)
  419. copyrightwin.Layout()
  420. copyrightwin.SetupScrolling()
  421. return copyrightwin
  422. def _pageLicense(self):
  423. """Licence about"""
  424. licfile = os.path.join(os.getenv("GISBASE"), "GPL.TXT")
  425. if os.path.exists(licfile):
  426. licenceFile = open(licfile, 'r')
  427. license = ''.join(licenceFile.readlines())
  428. licenceFile.close()
  429. else:
  430. license = _('%s file missing') % 'GPL.TXT'
  431. # put text into a scrolling panel
  432. licensewin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  433. style = wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
  434. licensetxt = wx.StaticText(licensewin, id = wx.ID_ANY, label = license)
  435. licensewin.SetAutoLayout(True)
  436. licensewin.sizer = wx.BoxSizer(wx.VERTICAL)
  437. licensewin.sizer.Add(item = licensetxt, proportion = 1,
  438. flag = wx.EXPAND | wx.ALL, border = 3)
  439. licensewin.SetSizer(licensewin.sizer)
  440. licensewin.Layout()
  441. licensewin.SetupScrolling()
  442. return licensewin
  443. def _pageCredit(self):
  444. """Credit about"""
  445. # credits
  446. authfile = os.path.join(os.getenv("GISBASE"), "AUTHORS")
  447. if os.path.exists(authfile):
  448. authorsFile = open(authfile, 'r')
  449. authors = unicode(''.join(authorsFile.readlines()), "utf-8")
  450. authorsFile.close()
  451. else:
  452. authors = _('%s file missing') % 'AUTHORS'
  453. authorwin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  454. style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)
  455. authortxt = wx.StaticText(authorwin, id = wx.ID_ANY, label = authors)
  456. authorwin.SetAutoLayout(1)
  457. authorwin.SetupScrolling()
  458. authorwin.sizer = wx.BoxSizer(wx.VERTICAL)
  459. authorwin.sizer.Add(item = authortxt, proportion = 1,
  460. flag = wx.EXPAND | wx.ALL, border = 3)
  461. authorwin.SetSizer(authorwin.sizer)
  462. authorwin.Layout()
  463. return authorwin
  464. def _pageContributors(self, extra = False):
  465. """Contributors info"""
  466. if extra:
  467. contribfile = os.path.join(os.getenv("GISBASE"), "contributors_extra.csv")
  468. else:
  469. contribfile = os.path.join(os.getenv("GISBASE"), "contributors.csv")
  470. if os.path.exists(contribfile):
  471. contribFile = codecs.open(contribfile, encoding = 'utf-8', mode = 'r')
  472. contribs = list()
  473. errLines = list()
  474. for line in contribFile.readlines()[1:]:
  475. line = line.rstrip('\n')
  476. try:
  477. if extra:
  478. name, email, rfc2_agreed = line.split(',')
  479. else:
  480. cvs_id, name, email, country, osgeo_id, rfc2_agreed = line.split(',')
  481. except ValueError:
  482. errLines.append(line)
  483. continue
  484. if extra:
  485. contribs.append((name, email))
  486. else:
  487. contribs.append((name, email, country, osgeo_id))
  488. contribFile.close()
  489. if errLines:
  490. GError(parent = self,
  491. message = _("Error when reading file '%s'.") % contribfile + \
  492. "\n\n" + _("Lines:") + " %s" % \
  493. os.linesep.join(map(DecodeString, errLines)))
  494. else:
  495. contribs = None
  496. contribwin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  497. style = wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER)
  498. contribwin.SetAutoLayout(True)
  499. contribwin.SetupScrolling()
  500. contribwin.sizer = wx.BoxSizer(wx.VERTICAL)
  501. if not contribs:
  502. contribtxt = wx.StaticText(contribwin, id = wx.ID_ANY,
  503. label = _('%s file missing') % contribfile)
  504. contribwin.sizer.Add(item = contribtxt, proportion = 1,
  505. flag = wx.EXPAND | wx.ALL, border = 3)
  506. else:
  507. if extra:
  508. items = (_('Name'), _('E-mail'))
  509. else:
  510. items = (_('Name'), _('E-mail'), _('Country'), _('OSGeo_ID'))
  511. contribBox = wx.FlexGridSizer(cols = len(items), vgap = 5, hgap = 5)
  512. for item in items:
  513. contribBox.Add(item = wx.StaticText(parent = contribwin, id = wx.ID_ANY,
  514. label = item))
  515. for vals in sorted(contribs, key = lambda x: x[0]):
  516. for item in vals:
  517. contribBox.Add(item = wx.StaticText(parent = contribwin, id = wx.ID_ANY,
  518. label = item))
  519. contribwin.sizer.Add(item = contribBox, proportion = 1,
  520. flag = wx.EXPAND | wx.ALL, border = 3)
  521. contribwin.SetSizer(contribwin.sizer)
  522. contribwin.Layout()
  523. return contribwin
  524. def _pageTranslators(self):
  525. """Translators info"""
  526. translatorsfile = os.path.join(os.getenv("GISBASE"), "translators.csv")
  527. if os.path.exists(translatorsfile):
  528. translatorsFile = open(translatorsfile, 'r')
  529. translators = dict()
  530. errLines = list()
  531. for line in translatorsFile.readlines()[1:]:
  532. line = line.rstrip('\n')
  533. try:
  534. name, email, languages = line.split(',')
  535. except ValueError:
  536. errLines.append(line)
  537. continue
  538. for language in languages.split(' '):
  539. if language not in translators:
  540. translators[language] = list()
  541. translators[language].append((name, email))
  542. translatorsFile.close()
  543. if errLines:
  544. GError(parent = self,
  545. message = _("Error when reading file '%s'.") % translatorsfile + \
  546. "\n\n" + _("Lines:") + " %s" % \
  547. os.linesep.join(map(DecodeString, errLines)))
  548. else:
  549. translators = None
  550. translatorswin = scrolled.ScrolledPanel(self, id = wx.ID_ANY,
  551. style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER)
  552. translatorswin.SetAutoLayout(1)
  553. translatorswin.SetupScrolling()
  554. translatorswin.sizer = wx.BoxSizer(wx.VERTICAL)
  555. if not translators:
  556. translatorstxt = wx.StaticText(translatorswin, id = wx.ID_ANY,
  557. label = _('%s file missing') % 'translators.csv')
  558. translatorswin.sizer.Add(item = translatorstxt, proportion = 1,
  559. flag = wx.EXPAND | wx.ALL, border = 3)
  560. else:
  561. translatorsBox = wx.FlexGridSizer(cols = 3, vgap = 5, hgap = 5)
  562. languages = translators.keys()
  563. languages.sort()
  564. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  565. label = _('Name')))
  566. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  567. label = _('E-mail')))
  568. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  569. label = _('Language')))
  570. for lang in languages:
  571. for translator in translators[lang]:
  572. name, email = translator
  573. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  574. label = unicode(name, "utf-8")))
  575. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  576. label = email))
  577. translatorsBox.Add(item = wx.StaticText(parent = translatorswin, id = wx.ID_ANY,
  578. label = lang))
  579. translatorswin.sizer.Add(item = translatorsBox, proportion = 1,
  580. flag = wx.EXPAND | wx.ALL, border = 3)
  581. translatorswin.SetSizer(translatorswin.sizer)
  582. translatorswin.Layout()
  583. return translatorswin
  584. def OnCloseWindow(self, event):
  585. """!Close window"""
  586. self.Close()
  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()