frame.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. 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 = wx.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 = wx.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 = wx.Button(parent=self, id=wx.ID_EXIT)
  42. self.btn_ok = wx.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=_("GRASS"
  101. " GIS Setup for r.li modules"),
  102. style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER, **kwargs):
  103. # VARIABLES
  104. self.parent = parent
  105. # self.cmd = "r.li.setup"
  106. self.rlipath = retRLiPath()
  107. self.listfiles = self.ListFiles()
  108. # END VARIABLES
  109. # init of frame
  110. wx.Frame.__init__(self, parent=parent, id=id, title=title,
  111. **kwargs)
  112. self.SetIcon(wx.Icon(os.path.join(globalvar.ICONDIR, 'grass.ico'),
  113. wx.BITMAP_TYPE_ICO))
  114. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  115. # box for select configuration file
  116. self.confilesBox = wx.StaticBox(
  117. parent=self.panel, id=wx.ID_ANY,
  118. label=_('Available sampling area configuration files'))
  119. self.listfileBox = wx.ListBox(parent=self.panel, id=wx.ID_ANY,
  120. choices=self.listfiles)
  121. # BUTTONS #definition
  122. self.btn_close = wx.Button(parent=self, id=wx.ID_CLOSE)
  123. self.btn_help = wx.Button(parent=self, id=wx.ID_HELP)
  124. self.btn_remove = wx.Button(parent=self, id=wx.ID_ANY,
  125. label=_("Remove"))
  126. self.btn_remove.SetToolTipString(_('Remove a configuration file'))
  127. self.btn_new = wx.Button(parent=self, id=wx.ID_ANY,
  128. label=_("Create"))
  129. self.btn_new.SetToolTipString(_('Create a new configuration file'))
  130. self.btn_rename = wx.Button(parent=self, id=wx.ID_ANY,
  131. label=_("Rename"))
  132. self.btn_rename.SetToolTipString(_('Rename a configuration file'))
  133. self.btn_view = wx.Button(parent=self, id=wx.ID_ANY,
  134. label=_("View/Edit"))
  135. self.btn_view.SetToolTipString(_('View and edit a configuration file'))
  136. # set action for button
  137. self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
  138. self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp)
  139. self.btn_remove.Bind(wx.EVT_BUTTON, self.OnRemove)
  140. self.btn_new.Bind(wx.EVT_BUTTON, self.OnNew)
  141. self.btn_rename.Bind(wx.EVT_BUTTON, self.OnRename)
  142. self.btn_view.Bind(wx.EVT_BUTTON, self.OnView)
  143. self._layout()
  144. # END BUTTONS
  145. # SIZE FRAME
  146. self.SetMinSize(self.GetBestSize())
  147. # Please check this because without this the size it is not the min
  148. self.SetClientSize(self.GetBestSize())
  149. # END SIZE
  150. def _layout(self):
  151. """Set the layout"""
  152. panelsizer = wx.GridBagSizer(1, 1)
  153. mainsizer = wx.BoxSizer(wx.VERTICAL)
  154. # CONFILES
  155. confilesSizer = wx.StaticBoxSizer(self.confilesBox, wx.HORIZONTAL)
  156. confilesSizer.Add(self.listfileBox, proportion=1,
  157. flag=wx.EXPAND)
  158. # END CONFILES
  159. # BUTTONS
  160. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  161. buttonSizer.Add(self.btn_new, flag=wx.ALL, border=5)
  162. buttonSizer.Add(self.btn_rename, flag=wx.ALL, border=5)
  163. buttonSizer.Add(self.btn_view, flag=wx.ALL, border=5)
  164. buttonSizer.Add(self.btn_remove, flag=wx.ALL, border=5)
  165. buttonSizer.Add(self.btn_help, flag=wx.ALL, border=5)
  166. buttonSizer.Add(self.btn_close, flag=wx.ALL, border=5)
  167. # END BUTTONS
  168. # add listbox to staticbox
  169. panelsizer.Add(confilesSizer, pos=(0, 0), flag=wx.EXPAND,
  170. border=3)
  171. # add panel and buttons
  172. mainsizer.Add(self.panel, proportion=1, flag=wx.EXPAND, border=3)
  173. mainsizer.Add(buttonSizer, proportion=0, flag=wx.EXPAND, border=3)
  174. panelsizer.AddGrowableRow(0)
  175. panelsizer.AddGrowableCol(0)
  176. self.panel.SetAutoLayout(True)
  177. self.panel.SetSizerAndFit(panelsizer)
  178. self.SetSizer(mainsizer)
  179. self.Layout()
  180. def ListFiles(self):
  181. """Check the configuration files inside the path"""
  182. # list of configuration file
  183. listfiles = []
  184. # return all the configuration files in self.rlipath, check if there are
  185. # link or directory and doesn't add them
  186. for l in os.listdir(self.rlipath):
  187. if os.path.isfile(os.path.join(self.rlipath, l)):
  188. listfiles.append(l)
  189. return sorted(listfiles)
  190. def OnClose(self, event):
  191. """Close window"""
  192. self.Destroy()
  193. def OnHelp(self, event):
  194. """Launches help"""
  195. gcmd.RunCommand('g.manual', parent=self, entry='wxGUI.rlisetup')
  196. def OnRemove(self, event):
  197. """Remove configuration file from path and update the list"""
  198. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  199. dlg = wx.MessageDialog(
  200. parent=self.parent,
  201. message=_(
  202. "Do you want remove r.li "
  203. "configuration file <%s>?") %
  204. confile,
  205. caption=_("Remove new r.li configuration file?"),
  206. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  207. if dlg.ShowModal() == wx.ID_YES:
  208. self.listfileBox.Delete(self.listfileBox.GetSelections()[0])
  209. try_remove(os.path.join(self.rlipath, confile))
  210. self.listfiles = self.ListFiles()
  211. dlg.Destroy()
  212. return
  213. def OnNew(self, event):
  214. """Remove configuration file from path and update the list"""
  215. RLIWizard(self)
  216. self.listfiles = self.ListFiles()
  217. self.listfileBox.Clear()
  218. self.listfileBox.Set(self.listfiles)
  219. def OnRename(self, event):
  220. """Rename an existing configuration file"""
  221. try:
  222. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  223. except:
  224. gcmd.GMessage(parent=self,
  225. message=_("You have to select a configuration file"))
  226. return
  227. dlg = wx.TextEntryDialog(parent=self.parent,
  228. message=_('Set the new name for %s " \
  229. "configuration file') % confile,
  230. caption=_('Rename configuration file'))
  231. if dlg.ShowModal() == wx.ID_OK:
  232. res = dlg.GetValue()
  233. newname = "%s%s%s" % (self.rlipath, os.sep, res)
  234. os.rename(os.path.join(self.rlipath, confile), newname)
  235. self.listfiles = self.ListFiles()
  236. self.listfileBox.Clear()
  237. self.listfileBox.Set(self.listfiles)
  238. def OnView(self, event):
  239. """Show and edit a configuration file"""
  240. try:
  241. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  242. except:
  243. gcmd.GMessage(parent=self,
  244. message=_("You have to select a configuration file"))
  245. return
  246. frame = ViewFrame(self, conf=confile)
  247. frame.Show()