dialogs.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """!
  2. @package mapswipe.dialogs
  3. @brief Dialogs used in Map Swipe
  4. Classes:
  5. - dialogs::SwipeMapDialog
  6. (C) 2013 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Anna Petrasova <kratochanna gmail.com>
  10. """
  11. import copy
  12. import wx
  13. from core import globalvar
  14. from core.utils import _
  15. from gui_core import gselect
  16. from gui_core.widgets import SimpleValidator
  17. from core.gcmd import GMessage
  18. from core.layerlist import LayerList
  19. from gui_core.simplelmgr import SimpleLayerManager, SIMPLE_LMGR_RASTER, \
  20. SIMPLE_LMGR_VECTOR, SIMPLE_LMGR_TB_LEFT, SIMPLE_LMGR_TB_RIGHT
  21. from grass.pydispatch.signal import Signal
  22. class SwipeMapDialog(wx.Dialog):
  23. """!Dialog used to select maps.
  24. There are two modes - simple (only two raster maps),
  25. or two layer lists.
  26. """
  27. def __init__(self, parent, title=_("Select raster maps"),
  28. first=None, second=None,
  29. firstLayerList=None, secondLayerList=None):
  30. wx.Dialog.__init__(self, parent=parent, title=title,
  31. style=wx.RESIZE_BORDER | wx.DEFAULT_DIALOG_STYLE)
  32. if firstLayerList is None:
  33. self._firstLayerList = LayerList()
  34. else:
  35. self._firstLayerList = copy.deepcopy(firstLayerList)
  36. if secondLayerList is None:
  37. self._secondLayerList = LayerList()
  38. else:
  39. self._secondLayerList = copy.deepcopy(secondLayerList)
  40. self._firstPanel = self._createSimplePanel()
  41. self._secondPanel = self._createAdvancedPanel()
  42. self.btnSwitch = wx.Button(self)
  43. self.btnCancel = wx.Button(self, id=wx.ID_CANCEL)
  44. self.btnApply = wx.Button(self, id=wx.ID_APPLY)
  45. self.btnOK = wx.Button(self, id=wx.ID_OK)
  46. self.btnOK.SetDefault()
  47. self.btnSwitch.Bind(wx.EVT_BUTTON, self.OnSwitchMode)
  48. self.btnApply.Bind(wx.EVT_BUTTON, lambda evt: self._apply())
  49. self.btnOK.Bind(wx.EVT_BUTTON, lambda evt: self._ok())
  50. self.btnCancel.Bind(wx.EVT_BUTTON, lambda evt: self.Close())
  51. self.Bind(wx.EVT_CLOSE, lambda evt: self.Hide())
  52. self.applyChanges = Signal('SwipeMapDialog.applyChanges')
  53. if first:
  54. self._firstRaster.SetValue(first)
  55. if second:
  56. self._secondRaster.SetValue(second)
  57. self._layout()
  58. def _layout(self):
  59. """!Do layout"""
  60. mainSizer = wx.BoxSizer(wx.VERTICAL)
  61. self._switchSizer = wx.BoxSizer()
  62. self._switchSizer.Add(self._firstPanel, proportion=1,
  63. flag=wx.EXPAND | wx.ALL, border=5)
  64. self._switchSizer.Add(self._secondPanel, proportion=1,
  65. flag=wx.EXPAND | wx.ALL, border=5)
  66. mainSizer.Add(self._switchSizer, proportion=1,
  67. flag=wx.EXPAND | wx.ALL)
  68. self.btnSizer = wx.StdDialogButtonSizer()
  69. self.btnSizer.AddButton(self.btnCancel)
  70. self.btnSizer.AddButton(self.btnOK)
  71. self.btnSizer.AddButton(self.btnApply)
  72. self.btnSizer.Realize()
  73. mainSizer.Add(item=self.btnSwitch, proportion=0,
  74. flag=wx.ALL | wx.ALIGN_LEFT, border=5)
  75. mainSizer.Add(item=self.btnSizer, proportion=0,
  76. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  77. self.mainSizer = mainSizer
  78. self.SetSizer(mainSizer)
  79. mainSizer.Fit(self)
  80. self._switchMode(simple=True)
  81. def _createSimplePanel(self):
  82. panel = wx.Panel(self)
  83. sizer = wx.BoxSizer(wx.VERTICAL)
  84. self._firstRaster = gselect.Select(parent=panel, type='raster',
  85. size=globalvar.DIALOG_GSELECT_SIZE,
  86. validator=SimpleValidator(callback=self.ValidatorCallback))
  87. self._secondRaster = gselect.Select(parent=panel, type='raster',
  88. size=globalvar.DIALOG_GSELECT_SIZE,
  89. validator=SimpleValidator(callback=self.ValidatorCallback))
  90. sizer.Add(wx.StaticText(panel, label=_("Name of top/left raster map:")),
  91. proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
  92. sizer.Add(self._firstRaster, proportion=0,
  93. flag=wx.EXPAND | wx.ALL, border=1)
  94. sizer.Add(wx.StaticText(panel, label=_("Name of bottom/right raster map:")),
  95. proportion=0, flag=wx.EXPAND | wx.ALL, border=1)
  96. sizer.Add(self._secondRaster, proportion=0,
  97. flag=wx.EXPAND | wx.ALL, border=1)
  98. self._firstRaster.SetFocus()
  99. panel.SetSizer(sizer)
  100. sizer.Fit(panel)
  101. return panel
  102. def _createAdvancedPanel(self):
  103. panel = wx.Panel(self)
  104. sizer = wx.BoxSizer(wx.HORIZONTAL)
  105. self._firstLmgr = SimpleLayerManager(parent=panel, layerList=self._firstLayerList,
  106. lmgrStyle=SIMPLE_LMGR_RASTER |
  107. SIMPLE_LMGR_VECTOR | SIMPLE_LMGR_TB_LEFT)
  108. self._secondLmgr = SimpleLayerManager(parent=panel, layerList=self._secondLayerList,
  109. lmgrStyle=SIMPLE_LMGR_RASTER |
  110. SIMPLE_LMGR_VECTOR | SIMPLE_LMGR_TB_RIGHT)
  111. sizer.Add(self._firstLmgr, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  112. sizer.Add(self._secondLmgr, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  113. panel.SetSizer(sizer)
  114. sizer.Fit(panel)
  115. return panel
  116. def _switchMode(self, simple):
  117. if simple:
  118. self._switchSizer.Show(self._firstPanel, show=True, recursive=True)
  119. self._switchSizer.Show(self._secondPanel, show=False, recursive=True)
  120. self.btnSwitch.SetLabel(_("Switch to advanced mode"))
  121. self.btnCancel.SetLabel(_("Cancel"))
  122. else:
  123. self._switchSizer.Show(self._firstPanel, show=False, recursive=True)
  124. self._switchSizer.Show(self._secondPanel, show=True, recursive=True)
  125. self.btnSwitch.SetLabel(_("Switch to simple mode"))
  126. self.btnCancel.SetLabel(_("Close"))
  127. self.Freeze() # doesn't do anything (at least on Ubuntu)
  128. self.btnSizer.Show(self.btnApply, simple)
  129. self.btnSizer.Show(self.btnOK, simple)
  130. self.btnSizer.Layout()
  131. self._switchSizer.Layout()
  132. self.Fit()
  133. self.Thaw()
  134. self.applyChanges.emit()
  135. def OnSwitchMode(self, event):
  136. if self._switchSizer.IsShown(self._secondPanel):
  137. self._switchMode(simple=True)
  138. else:
  139. self._switchMode(simple=False)
  140. def ValidatorCallback(self, win):
  141. if self._switchSizer.IsShown(self._secondPanel):
  142. return
  143. if win == self._firstRaster.GetTextCtrl():
  144. GMessage(parent=self, message=_("Name of the first map is missing."))
  145. else:
  146. GMessage(parent=self, message=_("Name of the second map is missing."))
  147. def _ok(self):
  148. self._apply()
  149. self.Close()
  150. def _apply(self):
  151. # TODO check if not empty
  152. self.applyChanges.emit()
  153. def GetValues(self):
  154. """!Get raster maps"""
  155. if self.IsSimpleMode():
  156. return (self._firstRaster.GetValue(), self._secondRaster.GetValue())
  157. else:
  158. return (self._firstLayerList, self._secondLayerList)
  159. def IsSimpleMode(self):
  160. if self._switchSizer.IsShown(self._firstPanel):
  161. return True
  162. return False
  163. def GetFirstSimpleLmgr(self):
  164. return self._firstLmgr
  165. def GetSecondSimpleLmgr(self):
  166. return self._secondLmgr