ghelp.py 34 KB

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