dialogs.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 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 = wx.Button(parent=self, id=wx.ID_CANCEL)
  34. btnOK = wx.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(wx.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(wx.StaticText(self, label=_("Optionally select background raster map:")),
  44. pos=(2, 0), span=(1, 2), flag=wx.ALIGN_CENTER_VERTICAL)
  45. sizer.Add(self._backgroundSelect, pos=(3, 0), span=(1, 2))
  46. sizer.Add(wx.StaticText(self, label=_("New raster map type:")),
  47. pos=(4, 0), flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
  48. sizer.Add(self._typeChoice, pos=(4, 1), flag=wx.EXPAND)
  49. mainSizer.Add(sizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)
  50. btnSizer = wx.StdDialogButtonSizer()
  51. btnSizer.AddButton(btnCancel)
  52. btnSizer.AddButton(btnOK)
  53. btnSizer.Realize()
  54. mainSizer.Add(btnSizer, flag=wx.EXPAND | wx.ALL, border=10)
  55. self._backgroundSelect.Bind(wx.EVT_TEXT, self.OnBackgroundMap)
  56. self.SetSizer(mainSizer)
  57. mainSizer.Fit(self)
  58. def OnBackgroundMap(self, event):
  59. value = self._backgroundSelect.GetValue()
  60. try:
  61. ret = grast.raster_info(value)
  62. self._typeChoice.SetStringSelection(ret['datatype'])
  63. except CalledModuleError:
  64. return
  65. def OnOK(self, event):
  66. mapName = self.GetMapName()
  67. if not mapName:
  68. GWarning(parent=self.GetParent(), message=_("Please specify name for a new raster map"))
  69. else:
  70. found = gcore.find_file(name=mapName, mapset=gcore.gisenv()['MAPSET'])
  71. if found and found['mapset'] == gcore.gisenv()['MAPSET']:
  72. dlgOverwrite = wx.MessageDialog(
  73. self.GetParent(), message=_("Raster map <%s> already exists "
  74. "in the current mapset. "
  75. "Do you want to overwrite it?") % mapName,
  76. caption=_("Overwrite?"), style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  77. if not dlgOverwrite.ShowModal() == wx.ID_YES:
  78. dlgOverwrite.Destroy()
  79. return
  80. else:
  81. dlgOverwrite.Destroy()
  82. self.EndModal(wx.ID_OK)
  83. else:
  84. self.EndModal(wx.ID_OK)
  85. def GetMapName(self):
  86. return self._mapSelect.GetValue()
  87. def GetBackgroundMapName(self):
  88. return self._backgroundSelect.GetValue()
  89. def GetMapType(self):
  90. return self._typeChoice.GetStringSelection()
  91. if __name__ == '__main__':
  92. app = wx.App()
  93. dlg = NewRasterDialog(None)
  94. dlg.Show()
  95. app.MainLoop()