frame.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 import core as grass
  11. from rlisetup.functions import retRLiPath
  12. from rlisetup.wizard import RLIWizard
  13. class RLiSetupFrame(wx.Frame):
  14. def __init__(self, parent, giface = None, id=wx.ID_ANY, title=_("GRASS" \
  15. " GIS Setup for r.li modules"),
  16. style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER, **kwargs):
  17. ###VARIABLES
  18. self.parent = parent
  19. self.cmd = "r.li.setup"
  20. self.rlipath = retRLiPath()
  21. self.listfiles = self.ListFiles()
  22. ###END VARIABLES
  23. #init of frame
  24. wx.Frame.__init__(self, parent=parent, id=id, title=title,
  25. **kwargs)
  26. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 'grass.ico'),
  27. wx.BITMAP_TYPE_ICO))
  28. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  29. #box for select configuration file
  30. self.confilesBox = wx.StaticBox(parent=self.panel, id=wx.ID_ANY,
  31. label=_('Available sampling area configuration files'))
  32. self.listfileBox = wx.ListBox(parent=self.panel, id=wx.ID_ANY,
  33. choices=self.listfiles)
  34. ###BUTTONS
  35. #definition
  36. self.btn_close = wx.Button(parent=self.panel, id=wx.ID_CLOSE)
  37. self.btn_help = wx.Button(parent=self.panel, id=wx.ID_HELP)
  38. self.btn_remove = wx.Button(parent=self.panel, id=wx.ID_ANY,
  39. label=_("Remove"))
  40. self.btn_remove.SetToolTipString(_('Remove a configuration file'))
  41. self.btn_new = wx.Button(parent=self.panel, id=wx.ID_ANY,
  42. label=_("Create"))
  43. self.btn_new.SetToolTipString(_('Create a new configuration file'))
  44. self.btn_rename = wx.Button(parent=self.panel, id=wx.ID_ANY,
  45. label=_("Rename"))
  46. self.btn_rename.SetToolTipString(_('Rename a configuration file'))
  47. #set action for button
  48. self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
  49. self.btn_help.Bind(wx.EVT_BUTTON, self.OnHelp)
  50. self.btn_remove.Bind(wx.EVT_BUTTON, self.OnRemove)
  51. self.btn_new.Bind(wx.EVT_BUTTON, self.OnNew)
  52. self.btn_rename.Bind(wx.EVT_BUTTON, self.OnRename)
  53. self._layout()
  54. ###END BUTTONS
  55. ###SIZE FRAME
  56. self.SetMinSize(self.GetBestSize())
  57. ##Please check this because without this the size it is not the min
  58. self.SetClientSize(self.GetBestSize())
  59. ###END SIZE
  60. def _layout(self):
  61. """Set the layout"""
  62. sizer = wx.BoxSizer(wx.VERTICAL)
  63. ###CONFILES
  64. confilesSizer = wx.StaticBoxSizer(self.confilesBox, wx.HORIZONTAL)
  65. confilesSizer.Add(item=self.listfileBox, proportion=1,
  66. flag=wx.EXPAND)
  67. ###END CONFILES
  68. ###BUTTONS
  69. buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
  70. buttonSizer.Add(item=self.btn_new, flag=wx.ALL, border=5)
  71. buttonSizer.Add(item=self.btn_rename, flag=wx.ALL, border=5)
  72. buttonSizer.Add(item=self.btn_remove, flag=wx.ALL, border=5)
  73. buttonSizer.Add(item=self.btn_help, flag=wx.ALL, border=5)
  74. buttonSizer.Add(item=self.btn_close, flag=wx.ALL, border=5)
  75. ###END BUTTONS
  76. #add to sizer
  77. sizer.Add(item=confilesSizer, proportion=0,
  78. flag=wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, border=3)
  79. sizer.Add(item=buttonSizer, proportion=0,
  80. flag=wx.ALIGN_RIGHT | wx.ALL, border=3)
  81. #set dimension
  82. self.panel.SetAutoLayout(True)
  83. self.panel.SetSizer(sizer)
  84. sizer.Fit(self.panel)
  85. self.Layout()
  86. def ListFiles(self):
  87. """!Check the configuration files inside the path"""
  88. # list of configuration file
  89. listfiles = []
  90. #return all the configuration files in self.rlipath, check if there are
  91. #link or directory and doesn't add them
  92. for l in os.listdir(self.rlipath):
  93. if os.path.isfile(os.path.join(self.rlipath, l)):
  94. listfiles.append(l)
  95. return listfiles
  96. def OnClose(self, event):
  97. """!Close window"""
  98. self.Destroy()
  99. def OnHelp(self, event):
  100. """!Launches help"""
  101. gcmd.RunCommand('g.manual', parent = self, entry = 'wxGUI.rlisetup')
  102. def OnRemove(self, event):
  103. """!Remove configuration file from path and update the list"""
  104. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  105. self.listfileBox.Delete(self.listfileBox.GetSelections()[0])
  106. grass.try_remove(os.path.join(self.rlipath, confile))
  107. self.listfiles = self.ListFiles()
  108. return
  109. def OnNew(self, event):
  110. """!Remove configuration file from path and update the list"""
  111. RLIWizard(self)
  112. self.listfiles = self.ListFiles()
  113. self.listfileBox.Clear()
  114. self.listfileBox.Set(self.listfiles)
  115. def OnRename(self, event):
  116. """!Rename an existing configuration file"""
  117. try:
  118. confile = self.listfiles[self.listfileBox.GetSelections()[0]]
  119. except:
  120. gcmd.GMessage(parent=self,
  121. message=_("You have to select a configuration file"))
  122. return
  123. dlg = wx.TextEntryDialog(parent=self.parent,
  124. message=_('Set te new name for %s " \
  125. "configuration file') % confile,
  126. caption=_('Rename configuration file'))
  127. if dlg.ShowModal() == wx.ID_OK:
  128. res = dlg.GetValue()
  129. newname = "%s%s%s" % (self.rlipath, os.sep, res)
  130. os.rename(os.path.join(self.rlipath, confile), newname)
  131. self.listfiles = self.ListFiles()
  132. self.listfileBox.Clear()
  133. self.listfileBox.Set(self.listfiles)