dialogs.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. import wx.lib.scrolledpanel as SP
  14. import wx.lib.colourselect as csel
  15. from core import globalvar
  16. from core.utils import _
  17. from gui_core import gselect
  18. from gui_core.widgets import SimpleValidator
  19. from gui_core.preferences import PreferencesBaseDialog
  20. from core.gcmd import GMessage
  21. from core.layerlist import LayerList
  22. from core.settings import UserSettings
  23. from gui_core.simplelmgr import SimpleLayerManager, SIMPLE_LMGR_RASTER, \
  24. SIMPLE_LMGR_VECTOR, SIMPLE_LMGR_RGB, SIMPLE_LMGR_TB_LEFT, SIMPLE_LMGR_TB_RIGHT
  25. from grass.pydispatch.signal import Signal
  26. class SwipeMapDialog(wx.Dialog):
  27. """!Dialog used to select maps.
  28. There are two modes - simple (only two raster maps),
  29. or two layer lists.
  30. """
  31. def __init__(self, parent, title=_("Select raster maps"),
  32. first=None, second=None,
  33. firstLayerList=None, secondLayerList=None):
  34. wx.Dialog.__init__(self, parent=parent, title=title,
  35. style=wx.RESIZE_BORDER | wx.DEFAULT_DIALOG_STYLE)
  36. if firstLayerList is None:
  37. self._firstLayerList = LayerList()
  38. else:
  39. self._firstLayerList = copy.deepcopy(firstLayerList)
  40. if secondLayerList is None:
  41. self._secondLayerList = LayerList()
  42. else:
  43. self._secondLayerList = copy.deepcopy(secondLayerList)
  44. self._firstPanel = self._createSimplePanel()
  45. self._secondPanel = self._createAdvancedPanel()
  46. self.btnSwitch = wx.Button(self)
  47. self.btnCancel = wx.Button(self, id=wx.ID_CANCEL)
  48. self.btnApply = wx.Button(self, id=wx.ID_APPLY)
  49. self.btnOK = wx.Button(self, id=wx.ID_OK)
  50. self.btnOK.SetDefault()
  51. self.btnSwitch.Bind(wx.EVT_BUTTON, self.OnSwitchMode)
  52. self.btnApply.Bind(wx.EVT_BUTTON, lambda evt: self._apply())
  53. self.btnOK.Bind(wx.EVT_BUTTON, lambda evt: self._ok())
  54. self.btnCancel.Bind(wx.EVT_BUTTON, lambda evt: self.Close())
  55. self.Bind(wx.EVT_CLOSE, lambda evt: self.Hide())
  56. self.applyChanges = Signal('SwipeMapDialog.applyChanges')
  57. if first:
  58. self._firstRaster.SetValue(first)
  59. if second:
  60. self._secondRaster.SetValue(second)
  61. self._layout()
  62. def _layout(self):
  63. """!Do layout"""
  64. mainSizer = wx.BoxSizer(wx.VERTICAL)
  65. self._switchSizer = wx.BoxSizer()
  66. self._switchSizer.Add(self._firstPanel, proportion=1,
  67. flag=wx.EXPAND | wx.ALL, border=5)
  68. self._switchSizer.Add(self._secondPanel, proportion=1,
  69. flag=wx.EXPAND | wx.ALL, border=5)
  70. mainSizer.Add(self._switchSizer, proportion=1,
  71. flag=wx.EXPAND | wx.ALL)
  72. self.btnSizer = wx.StdDialogButtonSizer()
  73. self.btnSizer.AddButton(self.btnCancel)
  74. self.btnSizer.AddButton(self.btnOK)
  75. self.btnSizer.AddButton(self.btnApply)
  76. self.btnSizer.Realize()
  77. mainSizer.Add(item=self.btnSwitch, proportion=0,
  78. flag=wx.ALL | wx.ALIGN_LEFT, border=5)
  79. mainSizer.Add(item=self.btnSizer, proportion=0,
  80. flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5)
  81. self.mainSizer = mainSizer
  82. self.SetSizer(mainSizer)
  83. mainSizer.Fit(self)
  84. self._switchMode(simple=True)
  85. def _createSimplePanel(self):
  86. panel = wx.Panel(self)
  87. sizer = wx.BoxSizer(wx.VERTICAL)
  88. self._firstRaster = gselect.Select(parent=panel, type='raster',
  89. size=globalvar.DIALOG_GSELECT_SIZE,
  90. validator=SimpleValidator(callback=self.ValidatorCallback))
  91. self._secondRaster = gselect.Select(parent=panel, type='raster',
  92. size=globalvar.DIALOG_GSELECT_SIZE,
  93. validator=SimpleValidator(callback=self.ValidatorCallback))
  94. sizer.Add(wx.StaticText(panel, label=_("Name of top/left raster map:")),
  95. proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
  96. sizer.Add(self._firstRaster, proportion=0,
  97. flag=wx.EXPAND | wx.ALL, border=1)
  98. sizer.Add(wx.StaticText(panel, label=_("Name of bottom/right raster map:")),
  99. proportion=0, flag=wx.EXPAND | wx.ALL, border=1)
  100. sizer.Add(self._secondRaster, proportion=0,
  101. flag=wx.EXPAND | wx.ALL, border=1)
  102. self._firstRaster.SetFocus()
  103. panel.SetSizer(sizer)
  104. sizer.Fit(panel)
  105. return panel
  106. def _createAdvancedPanel(self):
  107. panel = wx.Panel(self)
  108. sizer = wx.BoxSizer(wx.HORIZONTAL)
  109. self._firstLmgr = SimpleLayerManager(parent=panel, layerList=self._firstLayerList,
  110. lmgrStyle=SIMPLE_LMGR_RASTER | SIMPLE_LMGR_RGB |
  111. SIMPLE_LMGR_VECTOR | SIMPLE_LMGR_TB_LEFT)
  112. self._secondLmgr = SimpleLayerManager(parent=panel, layerList=self._secondLayerList,
  113. lmgrStyle=SIMPLE_LMGR_RASTER | SIMPLE_LMGR_RGB |
  114. SIMPLE_LMGR_VECTOR | SIMPLE_LMGR_TB_RIGHT)
  115. sizer.Add(self._firstLmgr, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  116. sizer.Add(self._secondLmgr, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  117. panel.SetSizer(sizer)
  118. sizer.Fit(panel)
  119. return panel
  120. def _switchMode(self, simple):
  121. if simple:
  122. self._switchSizer.Show(self._firstPanel, show=True, recursive=True)
  123. self._switchSizer.Show(self._secondPanel, show=False, recursive=True)
  124. self.btnSwitch.SetLabel(_("Switch to advanced mode"))
  125. self.btnCancel.SetLabel(_("Cancel"))
  126. else:
  127. self._switchSizer.Show(self._firstPanel, show=False, recursive=True)
  128. self._switchSizer.Show(self._secondPanel, show=True, recursive=True)
  129. self.btnSwitch.SetLabel(_("Switch to simple mode"))
  130. self.btnCancel.SetLabel(_("Close"))
  131. self.Freeze() # doesn't do anything (at least on Ubuntu)
  132. self.btnSizer.Show(self.btnApply, simple)
  133. self.btnSizer.Show(self.btnOK, simple)
  134. self.btnSizer.Layout()
  135. self._switchSizer.Layout()
  136. self.Fit()
  137. self.Thaw()
  138. self.applyChanges.emit()
  139. def OnSwitchMode(self, event):
  140. if self._switchSizer.IsShown(self._secondPanel):
  141. self._switchMode(simple=True)
  142. else:
  143. self._switchMode(simple=False)
  144. def ValidatorCallback(self, win):
  145. if self._switchSizer.IsShown(self._secondPanel):
  146. return
  147. if win == self._firstRaster.GetTextCtrl():
  148. GMessage(parent=self, message=_("Name of the first map is missing."))
  149. else:
  150. GMessage(parent=self, message=_("Name of the second map is missing."))
  151. def _ok(self):
  152. self._apply()
  153. self.Close()
  154. def _apply(self):
  155. # TODO check if not empty
  156. self.applyChanges.emit()
  157. def GetValues(self):
  158. """!Get raster maps"""
  159. if self.IsSimpleMode():
  160. return (self._firstRaster.GetValue(), self._secondRaster.GetValue())
  161. else:
  162. return (self._firstLayerList, self._secondLayerList)
  163. def IsSimpleMode(self):
  164. if self._switchSizer.IsShown(self._firstPanel):
  165. return True
  166. return False
  167. def GetFirstSimpleLmgr(self):
  168. return self._firstLmgr
  169. def GetSecondSimpleLmgr(self):
  170. return self._secondLmgr
  171. class PreferencesDialog(PreferencesBaseDialog):
  172. """!Mapswipe preferences dialog"""
  173. def __init__(self, parent, giface, title=_("Map Swipe settings"),
  174. settings=UserSettings):
  175. PreferencesBaseDialog.__init__(self, parent=parent, giface=giface, title=title,
  176. settings=settings, size=(-1, 300))
  177. # create notebook pages
  178. self._createMirrorModePage(self.notebook)
  179. self.SetMinSize(self.GetBestSize())
  180. self.SetSize(self.size)
  181. def _createMirrorModePage(self, notebook):
  182. """!Create notebook page for general settings"""
  183. panel = SP.ScrolledPanel(parent=notebook)
  184. panel.SetupScrolling(scroll_x=False, scroll_y=True)
  185. notebook.AddPage(page=panel, text=_("Mirror mode"))
  186. border = wx.BoxSizer(wx.VERTICAL)
  187. box = wx.StaticBox(parent=panel, label=" %s " % _("Mirrored cursor"))
  188. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  189. gridSizer = wx.GridBagSizer(hgap=3, vgap=3)
  190. row = 0
  191. gridSizer.Add(item=wx.StaticText(parent=panel,
  192. label=_("Color:")),
  193. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  194. color = csel.ColourSelect(parent=panel,
  195. colour=UserSettings.Get(group='mapswipe',
  196. key='cursor', subkey='color'),
  197. size=globalvar.DIALOG_COLOR_SIZE)
  198. color.SetName('GetColour')
  199. self.winId['mapswipe:cursor:color'] = color.GetId()
  200. gridSizer.Add(item=color, pos=(row, 1), flag=wx.ALIGN_RIGHT)
  201. row += 1
  202. gridSizer.Add(item=wx.StaticText(parent=panel,
  203. label=_("Shape:")),
  204. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  205. cursors = wx.Choice(parent=panel,
  206. choices=self.settings.Get(group='mapswipe', key='cursor',
  207. subkey=['type', 'choices'], internal=True),
  208. name="GetSelection")
  209. cursors.SetSelection(self.settings.Get(group='mapswipe', key='cursor',
  210. subkey=['type', 'selection']))
  211. self.winId['mapswipe:cursor:type:selection'] = cursors.GetId()
  212. gridSizer.Add(item=cursors, flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
  213. pos=(row, 1))
  214. row += 1
  215. gridSizer.Add(item=wx.StaticText(parent=panel,
  216. label=_("Line width:")),
  217. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  218. width = wx.SpinCtrl(parent=panel, min=1, max=10,
  219. initial=self.settings.Get(group='mapswipe', key='cursor',
  220. subkey='width'),
  221. name="GetValue")
  222. self.winId['mapswipe:cursor:width'] = width.GetId()
  223. gridSizer.Add(item=width, flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
  224. pos=(row, 1))
  225. row += 1
  226. gridSizer.Add(item=wx.StaticText(parent=panel,
  227. label=_("Size:")),
  228. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL, pos=(row, 0))
  229. size = wx.SpinCtrl(parent=panel, min=4, max=50,
  230. initial=self.settings.Get(group='mapswipe', key='cursor',
  231. subkey='size'),
  232. name="GetValue")
  233. self.winId['mapswipe:cursor:size'] = size.GetId()
  234. gridSizer.Add(item=size, flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
  235. pos=(row, 1))
  236. gridSizer.AddGrowableCol(1)
  237. sizer.Add(item=gridSizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=3)
  238. border.Add(item=sizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=3)
  239. panel.SetSizer(border)
  240. return panel