frame.py 11 KB

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