dialogs.py 4.2 KB

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