frame.py 5.9 KB

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