frame.py 11 KB

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