frame.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. """
  2. Created on Mon Nov 26 11:57:54 2012
  3. @author: lucadelu
  4. """
  5. import wx
  6. import os
  7. from core import globalvar, gcmd
  8. from grass.script.utils import try_remove
  9. from rlisetup.functions import retRLiPath
  10. from rlisetup.wizard import RLIWizard
  11. import locale
  12. import codecs
  13. from gui_core.wrap import Button, StaticBox, TextCtrl
  14. class ViewFrame(wx.Frame):
  15. def __init__(
  16. self,
  17. parent,
  18. conf,
  19. giface=None,
  20. id=wx.ID_ANY,
  21. title=_("Modify the configuration file"),
  22. style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER,
  23. **kwargs,
  24. ):
  25. # VARIABLES
  26. self.parent = parent
  27. self.rlipath = retRLiPath()
  28. self.confile = conf
  29. self.pathfile = os.path.join(self.rlipath, conf)
  30. wx.Frame.__init__(self, parent=parent, id=id, title=title, **kwargs)
  31. self.SetIcon(
  32. wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"), wx.BITMAP_TYPE_ICO)
  33. )
  34. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  35. self.confilesBox = StaticBox(
  36. parent=self.panel,
  37. id=wx.ID_ANY,
  38. label=_(
  39. "View and modify the "
  40. "configuration file '{name}'".format(name=self.confile)
  41. ),
  42. )
  43. self.textCtrl = TextCtrl(
  44. parent=self.panel, id=wx.ID_ANY, style=wx.TE_MULTILINE, size=(-1, 75)
  45. )
  46. self.textCtrl.Bind(wx.EVT_TEXT, self.OnFileText)
  47. f = open(self.pathfile)
  48. self.textCtrl.SetValue("".join(f.readlines()))
  49. f.close()
  50. # BUTTONS #definition
  51. self.btn_close = Button(parent=self, id=wx.ID_EXIT)
  52. self.btn_ok = Button(parent=self, id=wx.ID_SAVE)
  53. self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
  54. self.btn_ok.Bind(wx.EVT_BUTTON, self.OnOk)
  55. self._layout()
  56. self.enc = locale.getdefaultlocale()[1]
  57. def _layout(self):
  58. """Set the layout"""
  59. panelsizer = wx.GridBagSizer(1, 1)
  60. mainsizer = wx.BoxSizer(wx.VERTICAL)
  61. # CONFILES
  62. confilesSizer = wx.StaticBoxSizer(self.confilesBox, wx.HORIZONTAL)
  63. confilesSizer.Add(self.textCtrl, proportion=1, flag=wx.EXPAND)
  64. # END CONFILES
  65. # BUTTONS
  66. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  67. buttonSizer.Add(self.btn_ok, flag=wx.ALL, border=5)
  68. buttonSizer.Add(self.btn_close, flag=wx.ALL, border=5)
  69. # END BUTTONS
  70. # add listbox to staticbox
  71. panelsizer.Add(confilesSizer, pos=(0, 0), flag=wx.EXPAND, border=3)
  72. # add panel and buttons
  73. mainsizer.Add(self.panel, proportion=1, flag=wx.EXPAND, border=3)
  74. mainsizer.Add(buttonSizer, proportion=0, flag=wx.EXPAND, border=3)
  75. panelsizer.AddGrowableRow(0)
  76. panelsizer.AddGrowableCol(0)
  77. self.panel.SetAutoLayout(True)
  78. self.panel.SetSizerAndFit(panelsizer)
  79. self.SetSizer(mainsizer)
  80. self.Layout()
  81. def OnClose(self, event):
  82. """Close window"""
  83. self.Destroy()
  84. def OnOk(self, event):
  85. """Launches help"""
  86. dlg = wx.MessageDialog(
  87. parent=self.parent,
  88. message=_(
  89. "Are you sure that you want modify"
  90. " r.li configuration file {name}?"
  91. "\nYou could broke the configuration"
  92. " file..."
  93. ).format(name=self.confile),
  94. caption=_("WARNING"),
  95. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_WARNING,
  96. )
  97. if dlg.ShowModal() == wx.ID_YES:
  98. f = codecs.open(
  99. self.pathfile, encoding=self.enc, mode="w", errors="replace"
  100. )
  101. f.write(self.text + os.linesep)
  102. f.close()
  103. dlg.Destroy()
  104. self.Destroy()
  105. def OnFileText(self, event):
  106. """File input interactively entered"""
  107. self.text = event.GetString()
  108. class RLiSetupFrame(wx.Frame):
  109. def __init__(
  110. self,
  111. parent,
  112. giface=None,
  113. id=wx.ID_ANY,
  114. title=_("Setup for r.li modules"),
  115. style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER,
  116. **kwargs,
  117. ):
  118. # VARIABLES
  119. self.parent = parent
  120. # self.cmd = "r.li.setup"
  121. self.rlipath = retRLiPath()
  122. self.listfiles = self.ListFiles()
  123. # END VARIABLES
  124. # init of frame
  125. wx.Frame.__init__(self, parent=parent, id=id, title=title, **kwargs)
  126. self.SetIcon(
  127. wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"), wx.BITMAP_TYPE_ICO)
  128. )
  129. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  130. # box for select configuration file
  131. self.confilesBox = StaticBox(
  132. parent=self.panel,
  133. id=wx.ID_ANY,
  134. label=_("Available sampling area configuration files"),
  135. )
  136. self.listfileBox = wx.ListBox(
  137. parent=self.panel, id=wx.ID_ANY, choices=self.listfiles
  138. )
  139. # BUTTONS #definition
  140. self.btn_close = Button(parent=self, id=wx.ID_CLOSE)
  141. self.btn_help = Button(parent=self, id=wx.ID_HELP)
  142. self.btn_remove = Button(parent=self, id=wx.ID_ANY, label=_("Remove"))
  143. self.btn_remove.SetToolTip(_("Remove a configuration file"))
  144. self.btn_new = Button(parent=self, id=wx.ID_ANY, label=_("Create"))
  145. self.btn_new.SetToolTip(_("Create a new configuration file"))
  146. self.btn_rename = Button(parent=self, id=wx.ID_ANY, label=_("Rename"))
  147. self.btn_rename.SetToolTip(_("Rename a configuration file"))
  148. self.btn_view = Button(parent=self, id=wx.ID_ANY, label=_("View/Edit"))
  149. self.btn_view.SetToolTip(_("View and edit a configuration file"))
  150. # set action for button
  151. self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
  152. self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp)
  153. self.btn_remove.Bind(wx.EVT_BUTTON, self.OnRemove)
  154. self.btn_new.Bind(wx.EVT_BUTTON, self.OnNew)
  155. self.btn_rename.Bind(wx.EVT_BUTTON, self.OnRename)
  156. self.btn_view.Bind(wx.EVT_BUTTON, self.OnView)
  157. self._layout()
  158. # END BUTTONS
  159. # SIZE FRAME
  160. self.SetMinSize(self.GetBestSize())
  161. # Please check this because without this the size it is not the min
  162. self.SetClientSize(self.GetBestSize())
  163. # END SIZE
  164. def _layout(self):
  165. """Set the layout"""
  166. panelsizer = wx.GridBagSizer(1, 1)
  167. mainsizer = wx.BoxSizer(wx.VERTICAL)
  168. # CONFILES
  169. confilesSizer = wx.StaticBoxSizer(self.confilesBox, wx.HORIZONTAL)
  170. confilesSizer.Add(self.listfileBox, proportion=1, flag=wx.EXPAND)
  171. # END CONFILES
  172. # BUTTONS
  173. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  174. buttonSizer.Add(self.btn_new, flag=wx.ALL, border=5)
  175. buttonSizer.Add(self.btn_rename, flag=wx.ALL, border=5)
  176. buttonSizer.Add(self.btn_view, flag=wx.ALL, border=5)
  177. buttonSizer.Add(self.btn_remove, flag=wx.ALL, border=5)
  178. buttonSizer.Add(self.btn_help, flag=wx.ALL, border=5)
  179. buttonSizer.Add(self.btn_close, flag=wx.ALL, border=5)
  180. # END BUTTONS
  181. # add listbox to staticbox
  182. panelsizer.Add(confilesSizer, pos=(0, 0), flag=wx.EXPAND, border=3)
  183. # add panel and buttons
  184. mainsizer.Add(self.panel, proportion=1, flag=wx.EXPAND, border=3)
  185. mainsizer.Add(buttonSizer, proportion=0, flag=wx.EXPAND, border=3)
  186. panelsizer.AddGrowableRow(0)
  187. panelsizer.AddGrowableCol(0)
  188. self.panel.SetAutoLayout(True)
  189. self.panel.SetSizerAndFit(panelsizer)
  190. self.SetSizer(mainsizer)
  191. self.Layout()
  192. def ListFiles(self):
  193. """Check the configuration files inside the path"""
  194. # list of configuration file
  195. listfiles = []
  196. # return all the configuration files in self.rlipath, check if there are
  197. # link or directory and doesn't add them
  198. for l in os.listdir(self.rlipath):
  199. if os.path.isfile(os.path.join(self.rlipath, l)):
  200. listfiles.append(l)
  201. return sorted(listfiles)
  202. def OnClose(self, event):
  203. """Close window"""
  204. self.Destroy()
  205. def OnHelp(self, event):
  206. """Launches help"""
  207. gcmd.RunCommand("g.manual", parent=self, entry="wxGUI.rlisetup")
  208. def OnRemove(self, event):
  209. """Remove configuration file from path and update the list"""
  210. try:
  211. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  212. except IndexError:
  213. gcmd.GMessage(
  214. parent=self, message=_("You have to select a configuration file")
  215. )
  216. return
  217. dlg = wx.MessageDialog(
  218. parent=self.parent,
  219. message=_("Do you want remove r.li " "configuration file <%s>?") % confile,
  220. caption=_("Remove new r.li configuration file?"),
  221. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
  222. )
  223. if dlg.ShowModal() == wx.ID_YES:
  224. self.listfileBox.Delete(self.listfileBox.GetSelections()[0])
  225. try_remove(os.path.join(self.rlipath, confile))
  226. self.listfiles = self.ListFiles()
  227. dlg.Destroy()
  228. return
  229. def OnNew(self, event):
  230. """Remove configuration file from path and update the list"""
  231. RLIWizard(self)
  232. self.listfiles = self.ListFiles()
  233. self.listfileBox.Clear()
  234. self.listfileBox.Set(self.listfiles)
  235. def OnRename(self, event):
  236. """Rename an existing configuration file"""
  237. try:
  238. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  239. except IndexError:
  240. gcmd.GMessage(
  241. parent=self, message=_("You have to select a configuration file")
  242. )
  243. return
  244. dlg = wx.TextEntryDialog(
  245. parent=self.parent,
  246. message=_(
  247. 'Set the new name for %s " \
  248. "configuration file'
  249. )
  250. % confile,
  251. caption=_("Rename configuration file"),
  252. )
  253. if dlg.ShowModal() == wx.ID_OK:
  254. res = dlg.GetValue()
  255. newname = "%s%s%s" % (self.rlipath, os.sep, res)
  256. os.rename(os.path.join(self.rlipath, confile), newname)
  257. self.listfiles = self.ListFiles()
  258. self.listfileBox.Clear()
  259. self.listfileBox.Set(self.listfiles)
  260. def OnView(self, event):
  261. """Show and edit a configuration file"""
  262. try:
  263. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  264. except IndexError:
  265. gcmd.GMessage(
  266. parent=self, message=_("You have to select a configuration file")
  267. )
  268. return
  269. frame = ViewFrame(self, conf=confile)
  270. frame.Show()