ghelp.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. """
  2. @package gui_core.ghelp
  3. @brief Help/about window, menu tree, search module tree
  4. Classes:
  5. - ghelp::AboutWindow
  6. - ghelp::HelpFrame
  7. - ghelp::HelpWindow
  8. - ghelp::HelpPanel
  9. (C) 2008-2019 by the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. @author Martin Landa <landa.martin gmail.com>
  13. """
  14. import os
  15. import codecs
  16. import platform
  17. import re
  18. import textwrap
  19. import sys
  20. import six
  21. if sys.version_info.major == 2:
  22. _unichr = unichr
  23. else:
  24. _unichr = chr
  25. import wx
  26. from wx.html import HtmlWindow
  27. try:
  28. from wx.lib.agw.hyperlink import HyperLinkCtrl
  29. except ImportError:
  30. from wx.lib.hyperlink import HyperLinkCtrl
  31. try:
  32. from wx.adv import AboutDialogInfo
  33. from wx.adv import AboutBox
  34. except ImportError:
  35. from wx import AboutDialogInfo
  36. from wx import AboutBox
  37. import grass.script as grass
  38. # needed just for testing
  39. if __name__ == '__main__':
  40. from grass.script.setup import set_gui_path
  41. set_gui_path()
  42. from core import globalvar
  43. from core.gcmd import GError, DecodeString
  44. from gui_core.widgets import FormNotebook, ScrolledPanel
  45. from gui_core.wrap import Button, StaticText, TextCtrl
  46. from core.debug import Debug
  47. class AboutWindow(wx.Frame):
  48. """Create custom About Window
  49. """
  50. def __init__(self, parent, size=(770, 460),
  51. title=_('About GRASS GIS'), **kwargs):
  52. wx.Frame.__init__(
  53. self,
  54. parent=parent,
  55. id=wx.ID_ANY,
  56. title=title,
  57. size=size,
  58. **kwargs)
  59. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  60. # icon
  61. self.SetIcon(
  62. wx.Icon(
  63. os.path.join(
  64. globalvar.ICONDIR,
  65. 'grass.ico'),
  66. wx.BITMAP_TYPE_ICO))
  67. # notebook
  68. self.aboutNotebook = FormNotebook(self.panel, style=wx.BK_LEFT)
  69. for title, win in ((_("Info"), self._pageInfo()),
  70. (_("Copyright"), self._pageCopyright()),
  71. (_("License"), self._pageLicense()),
  72. (_("Citation"), self._pageCitation()),
  73. (_("Authors"), self._pageCredit()),
  74. (_("Contributors"), self._pageContributors()),
  75. (_("Extra contributors"), self._pageContributors(extra=True)),
  76. (_("Translators"), self._pageTranslators()),
  77. (_("Translation status"), self._pageStats())):
  78. self.aboutNotebook.AddPage(page=win, text=title)
  79. wx.CallAfter(self.aboutNotebook.SetSelection, 0)
  80. wx.CallAfter(self.aboutNotebook.Refresh)
  81. # buttons
  82. self.btnClose = Button(parent=self.panel, id=wx.ID_CLOSE)
  83. self.btnClose.Bind(wx.EVT_BUTTON, self.OnCloseWindow)
  84. self._doLayout()
  85. def _doLayout(self):
  86. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  87. btnSizer.Add(self.btnClose, proportion=0, flag=wx.ALL, border=5)
  88. sizer = wx.BoxSizer(wx.VERTICAL)
  89. sizer.Add(self.aboutNotebook, proportion=1,
  90. flag=wx.EXPAND | wx.ALL, border=1)
  91. sizer.Add(btnSizer, proportion=0,
  92. flag=wx.ALL | wx.ALIGN_RIGHT, border=1)
  93. self.SetMinSize((400, 400))
  94. self.panel.SetSizer(sizer)
  95. sizer.Fit(self.panel)
  96. self.Layout()
  97. def _pageInfo(self):
  98. """Info page"""
  99. # get version and web site
  100. vInfo = grass.version()
  101. if not vInfo:
  102. sys.stderr.write(_("Unable to get GRASS version\n"))
  103. infoTxt = ScrolledPanel(self.aboutNotebook)
  104. infoTxt.SetBackgroundColour('WHITE')
  105. infoTxt.SetupScrolling()
  106. infoSizer = wx.BoxSizer(wx.VERTICAL)
  107. infoGridSizer = wx.GridBagSizer(vgap=5, hgap=5)
  108. logo = os.path.join(globalvar.ICONDIR, "grass-64x64.png")
  109. logoBitmap = wx.StaticBitmap(infoTxt, wx.ID_ANY,
  110. wx.Bitmap(name=logo, type=wx.BITMAP_TYPE_PNG))
  111. infoSizer.Add(logoBitmap, proportion=0,
  112. flag=wx.ALL | wx.ALIGN_CENTER, border=20)
  113. infoLabel = 'GRASS GIS %s' % vInfo.get('version', _('unknown version'))
  114. if 'x86_64' in vInfo.get('build_platform', ''):
  115. infoLabel += ' (64bit)'
  116. info = StaticText(parent=infoTxt, id=wx.ID_ANY,
  117. label=infoLabel + os.linesep)
  118. info.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  119. info.SetForegroundColour(wx.Colour(35, 142, 35))
  120. infoSizer.Add(info, proportion=0,
  121. flag=wx.BOTTOM | wx.ALIGN_CENTER, border=1)
  122. team = StaticText(parent=infoTxt, label=_grassDevTeam(1999) + '\n')
  123. infoSizer.Add(team, proportion=0,
  124. flag=wx.BOTTOM | wx.ALIGN_CENTER, border=1)
  125. row = 0
  126. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  127. label=_('Official GRASS site:')),
  128. pos=(row, 0),
  129. flag=wx.ALIGN_RIGHT)
  130. infoGridSizer.Add(HyperLinkCtrl(parent=infoTxt, id=wx.ID_ANY,
  131. label='https://grass.osgeo.org'),
  132. pos=(row, 1),
  133. flag=wx.ALIGN_LEFT)
  134. row += 2
  135. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  136. label='%s:' % _('Code Revision')),
  137. pos=(row, 0),
  138. flag=wx.ALIGN_RIGHT)
  139. infoGridSizer.Add(HyperLinkCtrl(parent=infoTxt, id=wx.ID_ANY,
  140. label=vInfo.get('revision', '?'),
  141. URL='https://github.com/OSGeo/grass.git'),
  142. pos=(row, 1),
  143. flag=wx.ALIGN_LEFT)
  144. row += 1
  145. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  146. label='%s:' % _('Build Date')),
  147. pos=(row, 0),
  148. flag=wx.ALIGN_RIGHT)
  149. infoGridSizer.Add(
  150. StaticText(
  151. parent=infoTxt, id=wx.ID_ANY, label=vInfo.get(
  152. 'build_date', '?')), pos=(
  153. row, 1), flag=wx.ALIGN_LEFT)
  154. # show only basic info
  155. # row += 1
  156. # infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  157. # label = '%s:' % _('GIS Library Revision')),
  158. # pos = (row, 0),
  159. # flag = wx.ALIGN_RIGHT)
  160. # infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY,
  161. # label = vInfo['libgis_revision'] + ' (' +
  162. # vInfo['libgis_date'].split(' ')[0] + ')'),
  163. # pos = (row, 1),
  164. # flag = wx.ALIGN_LEFT)
  165. row += 2
  166. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  167. label='Python:'),
  168. pos=(row, 0),
  169. flag=wx.ALIGN_RIGHT)
  170. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  171. label=platform.python_version()),
  172. pos=(row, 1),
  173. flag=wx.ALIGN_LEFT)
  174. row += 1
  175. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  176. label='wxPython:'),
  177. pos=(row, 0),
  178. flag=wx.ALIGN_RIGHT)
  179. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  180. label=wx.__version__),
  181. pos=(row, 1),
  182. flag=wx.ALIGN_LEFT)
  183. infoGridSizer.AddGrowableCol(0)
  184. infoGridSizer.AddGrowableCol(1)
  185. infoSizer.Add(infoGridSizer, proportion=1, flag=wx.EXPAND)
  186. row += 2
  187. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  188. label="%s:" % _('Language')),
  189. pos=(row, 0),
  190. flag=wx.ALIGN_RIGHT)
  191. self.langUsed = grass.gisenv().get('LANG', None)
  192. if not self.langUsed:
  193. import locale
  194. loc = locale.getdefaultlocale()
  195. if loc == (None, None):
  196. self.langUsed = _('unknown')
  197. else:
  198. self.langUsed = u'%s.%s' % (loc[0], loc[1])
  199. infoGridSizer.Add(StaticText(parent=infoTxt, id=wx.ID_ANY,
  200. label=self.langUsed),
  201. pos=(row, 1),
  202. flag=wx.ALIGN_LEFT)
  203. infoTxt.SetSizer(infoSizer)
  204. infoSizer.Fit(infoTxt)
  205. return infoTxt
  206. def _pageCopyright(self):
  207. """Copyright information"""
  208. copyfile = os.path.join(os.getenv("GISBASE"), "COPYING")
  209. if os.path.exists(copyfile):
  210. copyrightFile = open(copyfile, 'r')
  211. copytext = copyrightFile.read()
  212. copyrightFile.close()
  213. else:
  214. copytext = _('%s file missing') % 'COPYING'
  215. # put text into a scrolling panel
  216. copyrightwin = ScrolledPanel(self.aboutNotebook)
  217. copyrighttxt = TextCtrl(
  218. copyrightwin, id=wx.ID_ANY, value=copytext,
  219. style=wx.TE_MULTILINE | wx.TE_READONLY)
  220. copyrightwin.SetAutoLayout(True)
  221. copyrightwin.sizer = wx.BoxSizer(wx.VERTICAL)
  222. copyrightwin.sizer.Add(copyrighttxt, proportion=1,
  223. flag=wx.EXPAND | wx.ALL, border=3)
  224. copyrightwin.SetSizer(copyrightwin.sizer)
  225. copyrightwin.Layout()
  226. copyrightwin.SetupScrolling()
  227. return copyrightwin
  228. def _pageLicense(self):
  229. """Licence about"""
  230. licfile = os.path.join(os.getenv("GISBASE"), "GPL.TXT")
  231. if os.path.exists(licfile):
  232. licenceFile = open(licfile, 'r')
  233. license = ''.join(licenceFile.readlines())
  234. licenceFile.close()
  235. else:
  236. license = _('%s file missing') % 'GPL.TXT'
  237. # put text into a scrolling panel
  238. licensewin = ScrolledPanel(self.aboutNotebook)
  239. licensetxt = TextCtrl(
  240. licensewin, id=wx.ID_ANY, value=license,
  241. style=wx.TE_MULTILINE | wx.TE_READONLY)
  242. licensewin.SetAutoLayout(True)
  243. licensewin.sizer = wx.BoxSizer(wx.VERTICAL)
  244. licensewin.sizer.Add(licensetxt, proportion=1,
  245. flag=wx.EXPAND | wx.ALL, border=3)
  246. licensewin.SetSizer(licensewin.sizer)
  247. licensewin.Layout()
  248. licensewin.SetupScrolling()
  249. return licensewin
  250. def _pageCitation(self):
  251. """Citation information"""
  252. try:
  253. # import only when needed
  254. import grass.script as gscript
  255. text = gscript.read_command('g.version', flags='x')
  256. except CalledModuleError as error:
  257. text = _("Unable to provide citation suggestion,"
  258. " see GRASS GIS website instead."
  259. " The error was: {0}").format(error)
  260. # put text into a scrolling panel
  261. window = ScrolledPanel(self.aboutNotebook)
  262. stat_text = TextCtrl(
  263. window, id=wx.ID_ANY, value=text,
  264. style=wx.TE_MULTILINE | wx.TE_READONLY)
  265. window.SetAutoLayout(True)
  266. window.sizer = wx.BoxSizer(wx.VERTICAL)
  267. window.sizer.Add(stat_text, proportion=1,
  268. flag=wx.EXPAND | wx.ALL, border=3)
  269. window.SetSizer(window.sizer)
  270. window.Layout()
  271. window.SetupScrolling()
  272. return window
  273. def _pageCredit(self):
  274. """Credit about"""
  275. # credits
  276. authfile = os.path.join(os.getenv("GISBASE"), "AUTHORS")
  277. if os.path.exists(authfile):
  278. with codecs.open(authfile, encoding='utf-8', mode='r') as authorsFile:
  279. authors = ''.join(authorsFile.readlines())
  280. else:
  281. authors = _('%s file missing') % 'AUTHORS'
  282. authorwin = ScrolledPanel(self.aboutNotebook)
  283. authortxt = TextCtrl(
  284. authorwin, id=wx.ID_ANY, value=authors,
  285. style=wx.TE_MULTILINE | wx.TE_READONLY)
  286. authorwin.SetAutoLayout(True)
  287. authorwin.SetupScrolling()
  288. authorwin.sizer = wx.BoxSizer(wx.VERTICAL)
  289. authorwin.sizer.Add(authortxt, proportion=1,
  290. flag=wx.EXPAND | wx.ALL, border=3)
  291. authorwin.SetSizer(authorwin.sizer)
  292. authorwin.Layout()
  293. return authorwin
  294. def _pageContributors(self, extra=False):
  295. """Contributors info"""
  296. if extra:
  297. contribfile = os.path.join(
  298. os.getenv("GISBASE"),
  299. "contributors_extra.csv")
  300. else:
  301. contribfile = os.path.join(
  302. os.getenv("GISBASE"),
  303. "contributors.csv")
  304. if os.path.exists(contribfile):
  305. contribFile = codecs.open(contribfile, encoding='utf-8', mode='r')
  306. contribs = list()
  307. errLines = list()
  308. for line in contribFile.readlines()[1:]:
  309. line = line.rstrip('\n')
  310. try:
  311. if extra:
  312. name, email, country, rfc2_agreed = line.split(',')
  313. else:
  314. cvs_id, name, email, country, osgeo_id, rfc2_agreed = line.split(
  315. ',')
  316. except ValueError:
  317. errLines.append(line)
  318. continue
  319. if extra:
  320. contribs.append((name, email, country))
  321. else:
  322. contribs.append((name, email, country, osgeo_id))
  323. contribFile.close()
  324. if errLines:
  325. GError(parent=self, message=_("Error when reading file '%s'.") %
  326. contribfile + "\n\n" + _("Lines:") + " %s" %
  327. os.linesep.join(map(DecodeString, errLines)))
  328. else:
  329. contribs = None
  330. contribwin = ScrolledPanel(self.aboutNotebook)
  331. contribwin.SetAutoLayout(True)
  332. contribwin.SetupScrolling()
  333. contribwin.sizer = wx.BoxSizer(wx.VERTICAL)
  334. if not contribs:
  335. contribtxt = StaticText(
  336. contribwin,
  337. id=wx.ID_ANY,
  338. label=_('%s file missing') %
  339. contribfile)
  340. contribwin.sizer.Add(contribtxt, proportion=1,
  341. flag=wx.EXPAND | wx.ALL, border=3)
  342. else:
  343. if extra:
  344. items = (_('Name'), _('E-mail'), _('Country'))
  345. else:
  346. items = (_('Name'), _('E-mail'), _('Country'), _('OSGeo_ID'))
  347. contribBox = wx.FlexGridSizer(cols=len(items), vgap=5, hgap=5)
  348. for item in items:
  349. text = StaticText(parent=contribwin, id=wx.ID_ANY,
  350. label=item)
  351. text.SetFont(
  352. wx.Font(
  353. 10,
  354. wx.DEFAULT,
  355. wx.NORMAL,
  356. wx.BOLD,
  357. 0,
  358. ""))
  359. contribBox.Add(text)
  360. for vals in sorted(contribs, key=lambda x: x[0]):
  361. for item in vals:
  362. contribBox.Add(
  363. StaticText(
  364. parent=contribwin,
  365. id=wx.ID_ANY,
  366. label=item))
  367. contribwin.sizer.Add(contribBox, proportion=1,
  368. flag=wx.EXPAND | wx.ALL, border=3)
  369. contribwin.SetSizer(contribwin.sizer)
  370. contribwin.Layout()
  371. return contribwin
  372. def _pageTranslators(self):
  373. """Translators info"""
  374. translatorsfile = os.path.join(os.getenv("GISBASE"), "translators.csv")
  375. if os.path.exists(translatorsfile):
  376. translatorsFile = codecs.open(translatorsfile, encoding='utf-8', mode='r')
  377. translators = dict()
  378. errLines = list()
  379. for line in translatorsFile.readlines()[1:]:
  380. line = line.rstrip('\n')
  381. try:
  382. name, email, languages = line.split(',')
  383. except ValueError:
  384. errLines.append(line)
  385. continue
  386. for language in languages.split(' '):
  387. if language not in translators:
  388. translators[language] = list()
  389. translators[language].append((name, email))
  390. translatorsFile.close()
  391. if errLines:
  392. GError(parent=self, message=_("Error when reading file '%s'.") %
  393. translatorsfile + "\n\n" + _("Lines:") + " %s" %
  394. os.linesep.join(map(DecodeString, errLines)))
  395. else:
  396. translators = None
  397. translatorswin = ScrolledPanel(self.aboutNotebook)
  398. translatorswin.SetBackgroundColour('WHITE')
  399. translatorswin.SetAutoLayout(True)
  400. translatorswin.SetupScrolling()
  401. translatorswin.sizer = wx.BoxSizer(wx.VERTICAL)
  402. if not translators:
  403. translatorstxt = StaticText(
  404. translatorswin,
  405. id=wx.ID_ANY,
  406. label=_('%s file missing') %
  407. 'translators.csv')
  408. translatorswin.sizer.Add(translatorstxt, proportion=1,
  409. flag=wx.EXPAND | wx.ALL, border=3)
  410. else:
  411. translatorsBox = wx.FlexGridSizer(cols=4, vgap=5, hgap=5)
  412. languages = sorted(translators.keys())
  413. tname = StaticText(parent=translatorswin, id=wx.ID_ANY,
  414. label=_('Name'))
  415. tname.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  416. translatorsBox.Add(tname)
  417. temail = StaticText(parent=translatorswin, id=wx.ID_ANY,
  418. label=_('E-mail'))
  419. temail.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  420. translatorsBox.Add(temail)
  421. tlang = StaticText(parent=translatorswin, id=wx.ID_ANY,
  422. label=_('Language'))
  423. tlang.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  424. translatorsBox.Add(tlang)
  425. tnat = StaticText(parent=translatorswin, id=wx.ID_ANY,
  426. label=_('Nation'))
  427. tnat.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  428. translatorsBox.Add(tnat)
  429. for lang in languages:
  430. for translator in translators[lang]:
  431. name, email = translator
  432. translatorsBox.Add(
  433. StaticText(
  434. parent=translatorswin,
  435. id=wx.ID_ANY,
  436. label=name))
  437. translatorsBox.Add(
  438. StaticText(
  439. parent=translatorswin,
  440. id=wx.ID_ANY,
  441. label=email))
  442. translatorsBox.Add(
  443. StaticText(
  444. parent=translatorswin,
  445. id=wx.ID_ANY,
  446. label=lang))
  447. flag = os.path.join(
  448. globalvar.ICONDIR, "flags", "%s.png" %
  449. lang.lower())
  450. if os.path.exists(flag):
  451. flagBitmap = wx.StaticBitmap(translatorswin, wx.ID_ANY, wx.Bitmap(
  452. name=flag, type=wx.BITMAP_TYPE_PNG))
  453. translatorsBox.Add(flagBitmap)
  454. else:
  455. translatorsBox.Add(
  456. StaticText(
  457. parent=translatorswin,
  458. id=wx.ID_ANY,
  459. label=lang))
  460. translatorswin.sizer.Add(translatorsBox, proportion=1,
  461. flag=wx.EXPAND | wx.ALL, border=3)
  462. translatorswin.SetSizer(translatorswin.sizer)
  463. translatorswin.Layout()
  464. return translatorswin
  465. def _langString(self, k, v):
  466. """Return string for the status of translation"""
  467. allStr = "%s :" % k.upper()
  468. try:
  469. allStr += _(" %d translated" % v['good'])
  470. except:
  471. pass
  472. try:
  473. allStr += _(" %d fuzzy" % v['fuzzy'])
  474. except:
  475. pass
  476. try:
  477. allStr += _(" %d untranslated" % v['bad'])
  478. except:
  479. pass
  480. return allStr
  481. def _langBox(self, par, k, v):
  482. """Return box"""
  483. langBox = wx.FlexGridSizer(cols=4, vgap=5, hgap=5)
  484. tkey = StaticText(parent=par, id=wx.ID_ANY,
  485. label=k.upper())
  486. langBox.Add(tkey)
  487. try:
  488. tgood = StaticText(parent=par, id=wx.ID_ANY,
  489. label=_("%d translated" % v['good']))
  490. tgood.SetForegroundColour(wx.Colour(35, 142, 35))
  491. langBox.Add(tgood)
  492. except:
  493. tgood = StaticText(parent=par, id=wx.ID_ANY,
  494. label="")
  495. langBox.Add(tgood)
  496. try:
  497. tfuzzy = StaticText(parent=par, id=wx.ID_ANY,
  498. label=_(" %d fuzzy" % v['fuzzy']))
  499. tfuzzy.SetForegroundColour(wx.Colour(255, 142, 0))
  500. langBox.Add(tfuzzy)
  501. except:
  502. tfuzzy = StaticText(parent=par, id=wx.ID_ANY,
  503. label="")
  504. langBox.Add(tfuzzy)
  505. try:
  506. tbad = StaticText(parent=par, id=wx.ID_ANY,
  507. label=_(" %d untranslated" % v['bad']))
  508. tbad.SetForegroundColour(wx.Colour(255, 0, 0))
  509. langBox.Add(tbad)
  510. except:
  511. tbad = StaticText(parent=par, id=wx.ID_ANY,
  512. label="")
  513. langBox.Add(tbad)
  514. return langBox
  515. def _langPanel(self, lang, js):
  516. """Create panel for each languages"""
  517. text = self._langString(lang, js['total'])
  518. panel = wx.CollapsiblePane(
  519. self.statswin, -1, label=text, style=wx.CP_DEFAULT_STYLE | wx.CP_NO_TLW_RESIZE)
  520. panel.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged)
  521. win = panel.GetPane()
  522. # TODO IT DOESN'T WORK
  523. # TO ADD ONLY WHEN TAB IS OPENED
  524. # if lang == self.langUsed.split('_')[0]:
  525. # panel.Collapse(False)
  526. # else:
  527. # panel.Collapse(True)
  528. pageSizer = wx.BoxSizer(wx.VERTICAL)
  529. for k, v in six.iteritems(js):
  530. if k != 'total' and k != 'name':
  531. box = self._langBox(win, k, v)
  532. pageSizer.Add(box, proportion=1,
  533. flag=wx.EXPAND | wx.ALL, border=3)
  534. win.SetSizer(pageSizer)
  535. pageSizer.SetSizeHints(win)
  536. return panel
  537. def OnPaneChanged(self, evt):
  538. """Redo the layout"""
  539. # TODO better to test on Windows
  540. self.statswin.SetupScrolling(scrollToTop=False)
  541. def _pageStats(self):
  542. """Translation statistics info"""
  543. fname = "translation_status.json"
  544. statsfile = os.path.join(os.getenv("GISBASE"), fname)
  545. if os.path.exists(statsfile):
  546. statsFile = open(statsfile)
  547. import json
  548. jsStats = json.load(statsFile)
  549. else:
  550. jsStats = None
  551. self.statswin = ScrolledPanel(self.aboutNotebook)
  552. self.statswin.SetBackgroundColour('WHITE')
  553. self.statswin.SetAutoLayout(True)
  554. if not jsStats:
  555. Debug.msg(5, _("File <%s> not found") % fname)
  556. statsSizer = wx.BoxSizer(wx.VERTICAL)
  557. statstext = StaticText(self.statswin, id=wx.ID_ANY,
  558. label=_('%s file missing') % fname)
  559. statsSizer.Add(statstext, proportion=1,
  560. flag=wx.EXPAND | wx.ALL, border=3)
  561. else:
  562. languages = sorted(jsStats['langs'].keys())
  563. statsSizer = wx.BoxSizer(wx.VERTICAL)
  564. for lang in languages:
  565. v = jsStats['langs'][lang]
  566. panel = self._langPanel(lang, v)
  567. statsSizer.Add(panel)
  568. self.statswin.SetSizer(statsSizer)
  569. self.statswin.SetupScrolling(scroll_x=False, scroll_y=True)
  570. self.statswin.Layout()
  571. self.statswin.Fit()
  572. return self.statswin
  573. def OnCloseWindow(self, event):
  574. """Close window"""
  575. self.Close()
  576. class HelpFrame(wx.Dialog):
  577. """GRASS Quickstart help window
  578. As a base class wx.Dialog is used, because of not working
  579. close button with wx.Frame when dialog is called from wizard.
  580. If parent is None, application TopLevelWindow is used (wxPython
  581. standard behaviour).
  582. Currently not used (was in location wizard before)
  583. due to unsolved problems - window sometimes does not respond.
  584. """
  585. def __init__(self, parent, id, title, size, file):
  586. wx.Dialog.__init__(
  587. self, parent=parent, id=id, title=title, size=size,
  588. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.MINIMIZE_BOX)
  589. sizer = wx.BoxSizer(wx.VERTICAL)
  590. # text
  591. content = HelpPanel(parent=self)
  592. content.LoadPage(file)
  593. sizer.Add(content, proportion=1, flag=wx.EXPAND)
  594. self.SetAutoLayout(True)
  595. self.SetSizer(sizer)
  596. self.Layout()
  597. class HelpWindow(HtmlWindow):
  598. """This panel holds the text from GRASS docs.
  599. GISBASE must be set in the environment to find the html docs dir.
  600. The SYNOPSIS section is skipped, since this Panel is supposed to
  601. be integrated into the cmdPanel and options are obvious there.
  602. """
  603. def __init__(self, parent, command, text, skipDescription,
  604. **kwargs):
  605. """If command is given, the corresponding HTML help
  606. file will be presented, with all links pointing to absolute
  607. paths of local files.
  608. If 'skipDescription' is True, the HTML corresponding to
  609. SYNOPSIS will be skipped, thus only presenting the help file
  610. from the DESCRIPTION section onwards.
  611. If 'text' is given, it must be the HTML text to be presented
  612. in the Panel.
  613. """
  614. self.parent = parent
  615. HtmlWindow.__init__(self, parent=parent, **kwargs)
  616. self.loaded = False
  617. self.history = list()
  618. self.historyIdx = 0
  619. self.fspath = os.path.join(os.getenv("GISBASE"), "docs", "html")
  620. self.SetStandardFonts(size=10)
  621. self.SetBorders(10)
  622. if text is None:
  623. if skipDescription:
  624. url = os.path.join(self.fspath, command + ".html")
  625. self.fillContentsFromFile(url,
  626. skipDescription=skipDescription)
  627. self.history.append(url)
  628. self.loaded = True
  629. else:
  630. # FIXME: calling LoadPage() is strangely time-consuming (only first call)
  631. # self.LoadPage(self.fspath + command + ".html")
  632. self.loaded = False
  633. else:
  634. self.SetPage(text)
  635. self.loaded = True
  636. def OnLinkClicked(self, linkinfo):
  637. url = linkinfo.GetHref()
  638. if url[:4] != 'http':
  639. url = os.path.join(self.fspath, url)
  640. self.history.append(url)
  641. self.historyIdx += 1
  642. self.parent.OnHistory()
  643. super(HelpWindow, self).OnLinkClicked(linkinfo)
  644. def LoadPage(self, path):
  645. super(HelpWindow, self).LoadPage(path)
  646. self.loaded = True
  647. def fillContentsFromFile(self, htmlFile, skipDescription=True):
  648. """Load content from file.
  649. Currently not used.
  650. """
  651. aLink = re.compile(r'(<a href="?)(.+\.html?["\s]*>)', re.IGNORECASE)
  652. imgLink = re.compile(r'(<img src="?)(.+\.[png|gif])', re.IGNORECASE)
  653. try:
  654. contents = []
  655. skip = False
  656. for l in open(htmlFile, "rb").readlines():
  657. if "DESCRIPTION" in l:
  658. skip = False
  659. if not skip:
  660. # do skip the options description if requested
  661. if "SYNOPSIS" in l:
  662. skip = skipDescription
  663. else:
  664. # FIXME: find only first item
  665. findALink = aLink.search(l)
  666. if findALink is not None:
  667. contents.append(
  668. aLink.sub(
  669. findALink.group(1) +
  670. self.fspath +
  671. findALink.group(2),
  672. l))
  673. findImgLink = imgLink.search(l)
  674. if findImgLink is not None:
  675. contents.append(
  676. imgLink.sub(
  677. findImgLink.group(1) +
  678. self.fspath +
  679. findImgLink.group(2),
  680. l))
  681. if findALink is None and findImgLink is None:
  682. contents.append(l)
  683. self.SetPage("".join(contents))
  684. self.loaded = True
  685. except: # The Manual file was not found
  686. self.loaded = False
  687. class HelpPanel(wx.Panel):
  688. def __init__(self, parent, command="index", text=None,
  689. skipDescription=False, **kwargs):
  690. self.command = command
  691. wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
  692. self.content = HelpWindow(self, command, text, skipDescription)
  693. self.btnNext = Button(parent=self, id=wx.ID_ANY,
  694. label=_("&Next"))
  695. self.btnNext.Enable(False)
  696. self.btnPrev = Button(parent=self, id=wx.ID_ANY,
  697. label=_("&Previous"))
  698. self.btnPrev.Enable(False)
  699. self.btnNext.Bind(wx.EVT_BUTTON, self.OnNext)
  700. self.btnPrev.Bind(wx.EVT_BUTTON, self.OnPrev)
  701. self._layout()
  702. def _layout(self):
  703. """Do layout"""
  704. sizer = wx.BoxSizer(wx.VERTICAL)
  705. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  706. btnSizer.Add(self.btnPrev, proportion=0,
  707. flag=wx.ALL, border=5)
  708. btnSizer.Add(wx.Size(1, 1), proportion=1)
  709. btnSizer.Add(self.btnNext, proportion=0,
  710. flag=wx.ALL, border=5)
  711. sizer.Add(self.content, proportion=1,
  712. flag=wx.EXPAND)
  713. sizer.Add(btnSizer, proportion=0,
  714. flag=wx.EXPAND)
  715. self.SetSizer(sizer)
  716. sizer.Fit(self)
  717. def LoadPage(self, path=None):
  718. """Load page"""
  719. if not path:
  720. path = self.GetFile()
  721. self.content.history.append(path)
  722. self.content.LoadPage(path)
  723. def GetFile(self):
  724. """Get HTML file"""
  725. fMan = os.path.join(self.content.fspath, self.command + ".html")
  726. if os.path.isfile(fMan):
  727. return fMan
  728. # check also addons
  729. faMan = os.path.join(os.getenv('GRASS_ADDON_BASE'), "docs", "html",
  730. self.command + ".html")
  731. if os.getenv('GRASS_ADDON_BASE') and \
  732. os.path.isfile(faMan):
  733. return faMan
  734. return None
  735. def IsLoaded(self):
  736. return self.content.loaded
  737. def OnHistory(self):
  738. """Update buttons"""
  739. nH = len(self.content.history)
  740. iH = self.content.historyIdx
  741. if iH == nH - 1:
  742. self.btnNext.Enable(False)
  743. elif iH > -1:
  744. self.btnNext.Enable(True)
  745. if iH < 1:
  746. self.btnPrev.Enable(False)
  747. else:
  748. self.btnPrev.Enable(True)
  749. def OnNext(self, event):
  750. """Load next page"""
  751. self.content.historyIdx += 1
  752. idx = self.content.historyIdx
  753. path = self.content.history[idx]
  754. self.content.LoadPage(path)
  755. self.OnHistory()
  756. event.Skip()
  757. def OnPrev(self, event):
  758. """Load previous page"""
  759. self.content.historyIdx -= 1
  760. idx = self.content.historyIdx
  761. path = self.content.history[idx]
  762. self.content.LoadPage(path)
  763. self.OnHistory()
  764. event.Skip()
  765. def ShowAboutDialog(prgName, startYear):
  766. """Displays About window.
  767. :param prgName: name of the program
  768. :param startYear: the first year of existence of the program
  769. """
  770. info = AboutDialogInfo()
  771. info.SetIcon(
  772. wx.Icon(
  773. os.path.join(
  774. globalvar.ICONDIR,
  775. 'grass.ico'),
  776. wx.BITMAP_TYPE_ICO))
  777. info.SetName(prgName)
  778. info.SetWebSite('https://grass.osgeo.org')
  779. info.SetDescription(
  780. _grassDevTeam(startYear) +
  781. '\n\n' +
  782. '\n'.join(
  783. textwrap.wrap(
  784. 'This program is free software under the GNU General Public License'
  785. '(>=v2). Read the file COPYING that comes with GRASS for details.',
  786. 75)))
  787. AboutBox(info)
  788. def _grassDevTeam(start):
  789. try:
  790. end = grass.version()['date']
  791. except KeyError:
  792. sys.stderr.write(_("Unable to get GRASS version\n"))
  793. from datetime import date
  794. end = date.today().year
  795. return '%(c)s %(start)s-%(end)s by the GRASS Development Team' % {
  796. 'c': _unichr(169), 'start': start, 'end': end}
  797. def main():
  798. """Test application (potentially useful as g.gui.gmanual)"""
  799. app = wx.App()
  800. frame = HelpFrame(parent=None, id=wx.ID_ANY,
  801. title="Test help application",
  802. size=(600, 800), file=sys.argv[1])
  803. frame.Show()
  804. app.MainLoop()
  805. if __name__ == '__main__':
  806. main()