frame.py 11 KB

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