frame.py 11 KB

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