ghelp.py 32 KB

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