dialogs.py 4.4 KB

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