dialogs.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. @package rdigit.dialogs
  3. @brief rdigit dialog for craeting new map.
  4. Classes:
  5. - rdigit:NewRasterDialog
  6. (C) 2014 by the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. @author Anna Petrasova <kratochanna gmail.com>
  11. """
  12. import wx
  13. from core.utils import _
  14. from gui_core.gselect import Select
  15. from gui_core.wrap import Button, StaticText
  16. from core.gcmd import GWarning
  17. import grass.script.core as gcore
  18. import grass.script.raster as grast
  19. from grass.exceptions import CalledModuleError
  20. class NewRasterDialog(wx.Dialog):
  21. """Dialog for new raster map name and type selection
  22. and selection of optional background map."""
  23. def __init__(self, parent):
  24. wx.Dialog.__init__(self, parent)
  25. self.SetTitle(_("Create new raster map"))
  26. self._name = None
  27. self._type = None
  28. # create widgets
  29. self._mapSelect = Select(parent=self, type='raster')
  30. self._backgroundSelect = Select(parent=self, type='raster')
  31. self._typeChoice = wx.Choice(self, choices=['CELL', 'FCELL', 'DCELL'])
  32. self._typeChoice.SetSelection(0)
  33. self._mapSelect.SetFocus()
  34. btnCancel = Button(parent=self, id=wx.ID_CANCEL)
  35. btnOK = Button(parent=self, id=wx.ID_OK)
  36. btnOK.SetDefault()
  37. btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
  38. # do layout
  39. mainSizer = wx.BoxSizer(wx.VERTICAL)
  40. sizer = wx.GridBagSizer(hgap=10, vgap=10)
  41. sizer.Add(StaticText(self, label=_("Name for new raster map:")),
  42. pos=(0, 0), span=(1, 2), flag=wx.ALIGN_CENTER_VERTICAL)
  43. sizer.Add(self._mapSelect, pos=(1, 0), span=(1, 2))
  44. sizer.Add(
  45. StaticText(
  46. self, label=_("Optionally select background raster map:")), pos=(
  47. 2, 0), span=(
  48. 1, 2), flag=wx.ALIGN_CENTER_VERTICAL)
  49. sizer.Add(self._backgroundSelect, pos=(3, 0), span=(1, 2))
  50. sizer.Add(StaticText(self, label=_("New raster map type:")),
  51. pos=(4, 0), flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
  52. sizer.Add(self._typeChoice, pos=(4, 1), flag=wx.EXPAND)
  53. mainSizer.Add(sizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  54. btnSizer = wx.StdDialogButtonSizer()
  55. btnSizer.AddButton(btnCancel)
  56. btnSizer.AddButton(btnOK)
  57. btnSizer.Realize()
  58. mainSizer.Add(btnSizer, flag=wx.EXPAND | wx.ALL, border=10)
  59. self._backgroundSelect.Bind(wx.EVT_TEXT, self.OnBackgroundMap)
  60. self.SetSizer(mainSizer)
  61. mainSizer.Fit(self)
  62. def OnBackgroundMap(self, event):
  63. value = self._backgroundSelect.GetValue()
  64. try:
  65. ret = grast.raster_info(value)
  66. self._typeChoice.SetStringSelection(ret['datatype'])
  67. except CalledModuleError:
  68. return
  69. def OnOK(self, event):
  70. mapName = self.GetMapName()
  71. if not mapName:
  72. GWarning(parent=self.GetParent(), message=_(
  73. "Please specify name for a new raster map"))
  74. else:
  75. found = gcore.find_file(
  76. name=mapName, mapset=gcore.gisenv()['MAPSET'])
  77. if found and found['mapset'] == gcore.gisenv()['MAPSET']:
  78. dlgOverwrite = wx.MessageDialog(
  79. self.GetParent(),
  80. message=_(
  81. "Raster map <%s> already exists "
  82. "in the current mapset. "
  83. "Do you want to overwrite it?") %
  84. mapName,
  85. caption=_("Overwrite?"),
  86. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  87. if not dlgOverwrite.ShowModal() == wx.ID_YES:
  88. dlgOverwrite.Destroy()
  89. return
  90. else:
  91. dlgOverwrite.Destroy()
  92. self.EndModal(wx.ID_OK)
  93. else:
  94. self.EndModal(wx.ID_OK)
  95. def GetMapName(self):
  96. return self._mapSelect.GetValue()
  97. def GetBackgroundMapName(self):
  98. return self._backgroundSelect.GetValue()
  99. def GetMapType(self):
  100. return self._typeChoice.GetStringSelection()
  101. if __name__ == '__main__':
  102. app = wx.App()
  103. dlg = NewRasterDialog(None)
  104. dlg.Show()
  105. app.MainLoop()