dialogs.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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._switchMode(simple=True)
  83. self.SetSizer(mainSizer)
  84. mainSizer.Fit(self)
  85. def _createSimplePanel(self):
  86. panel = wx.Panel(self)
  87. sizer = wx.BoxSizer(wx.VERTICAL)
  88. self._firstRaster = gselect.Select(
  89. parent=panel,
  90. type='raster',
  91. size=globalvar.DIALOG_GSELECT_SIZE,
  92. validator=SimpleValidator(
  93. callback=self.ValidatorCallback))
  94. self._secondRaster = gselect.Select(
  95. parent=panel,
  96. type='raster',
  97. size=globalvar.DIALOG_GSELECT_SIZE,
  98. validator=SimpleValidator(
  99. callback=self.ValidatorCallback))
  100. sizer.Add(
  101. wx.StaticText(
  102. panel,
  103. label=_("Name of top/left raster map:")),
  104. proportion=0,
  105. flag=wx.EXPAND | wx.ALL,
  106. border=5)
  107. sizer.Add(self._firstRaster, proportion=0,
  108. flag=wx.EXPAND | wx.ALL, border=1)
  109. sizer.Add(
  110. wx.StaticText(
  111. panel,
  112. label=_("Name of bottom/right raster map:")),
  113. proportion=0,
  114. flag=wx.EXPAND | wx.ALL,
  115. border=1)
  116. sizer.Add(self._secondRaster, proportion=0,
  117. flag=wx.EXPAND | wx.ALL, border=1)
  118. self._firstRaster.SetFocus()
  119. panel.SetSizer(sizer)
  120. sizer.Fit(panel)
  121. return panel
  122. def _createAdvancedPanel(self):
  123. panel = wx.Panel(self)
  124. sizer = wx.BoxSizer(wx.HORIZONTAL)
  125. self._firstLmgr = SimpleLayerManager(
  126. parent=panel, layerList=self._firstLayerList,
  127. lmgrStyle=SIMPLE_LMGR_RASTER | SIMPLE_LMGR_RGB | SIMPLE_LMGR_VECTOR |
  128. SIMPLE_LMGR_TB_LEFT)
  129. self._secondLmgr = SimpleLayerManager(
  130. parent=panel, layerList=self._secondLayerList,
  131. lmgrStyle=SIMPLE_LMGR_RASTER | SIMPLE_LMGR_RGB | SIMPLE_LMGR_VECTOR |
  132. SIMPLE_LMGR_TB_RIGHT)
  133. sizer.Add(
  134. self._firstLmgr,
  135. proportion=1,
  136. flag=wx.EXPAND | wx.ALL,
  137. border=5)
  138. sizer.Add(
  139. self._secondLmgr,
  140. proportion=1,
  141. flag=wx.EXPAND | wx.ALL,
  142. border=5)
  143. panel.SetSizer(sizer)
  144. sizer.Fit(panel)
  145. return panel
  146. def _switchMode(self, simple):
  147. if simple:
  148. self._switchSizer.Show(self._firstPanel, show=True, recursive=True)
  149. self._switchSizer.Show(
  150. self._secondPanel, show=False, recursive=True)
  151. self.btnSwitch.SetLabel(_("Switch to advanced mode"))
  152. self.btnCancel.SetLabel(_("Cancel"))
  153. else:
  154. self._switchSizer.Show(
  155. self._firstPanel, show=False, recursive=True)
  156. self._switchSizer.Show(
  157. self._secondPanel, show=True, recursive=True)
  158. self.btnSwitch.SetLabel(_("Switch to simple mode"))
  159. self.btnCancel.SetLabel(_("Close"))
  160. self.Freeze() # doesn't do anything (at least on Ubuntu)
  161. self.btnSizer.Show(self.btnApply, simple)
  162. self.btnSizer.Show(self.btnOK, simple)
  163. self.btnSizer.Layout()
  164. self._switchSizer.Layout()
  165. self.Fit()
  166. self.Thaw()
  167. self.applyChanges.emit()
  168. def OnSwitchMode(self, event):
  169. if self._switchSizer.IsShown(self._secondPanel):
  170. self._switchMode(simple=True)
  171. else:
  172. self._switchMode(simple=False)
  173. def ValidatorCallback(self, win):
  174. if self._switchSizer.IsShown(self._secondPanel):
  175. return
  176. if win == self._firstRaster.GetTextCtrl():
  177. GMessage(parent=self, message=_(
  178. "Name of the first map is missing."))
  179. else:
  180. GMessage(parent=self, message=_(
  181. "Name of the second map is missing."))
  182. def _ok(self):
  183. self._apply()
  184. self.Close()
  185. def _apply(self):
  186. # TODO check if not empty
  187. self.applyChanges.emit()
  188. def GetValues(self):
  189. """Get raster maps"""
  190. if self.IsSimpleMode():
  191. return (self._firstRaster.GetValue(),
  192. self._secondRaster.GetValue())
  193. else:
  194. return (self._firstLayerList, self._secondLayerList)
  195. def IsSimpleMode(self):
  196. if self._switchSizer.IsShown(self._firstPanel):
  197. return True
  198. return False
  199. def GetFirstSimpleLmgr(self):
  200. return self._firstLmgr
  201. def GetSecondSimpleLmgr(self):
  202. return self._secondLmgr
  203. class PreferencesDialog(PreferencesBaseDialog):
  204. """Mapswipe preferences dialog"""
  205. def __init__(self, parent, giface, title=_("Map Swipe settings"),
  206. settings=UserSettings):
  207. PreferencesBaseDialog.__init__(
  208. self, parent=parent, giface=giface, title=title, settings=settings,
  209. size=(-1, 300))
  210. # create notebook pages
  211. self._createMirrorModePage(self.notebook)
  212. self.SetMinSize(self.GetBestSize())
  213. self.SetSize(self.size)
  214. def _createMirrorModePage(self, notebook):
  215. """Create notebook page for general settings"""
  216. panel = SP.ScrolledPanel(parent=notebook)
  217. panel.SetupScrolling(scroll_x=False, scroll_y=True)
  218. notebook.AddPage(page=panel, text=_("Mirror mode"))
  219. border = wx.BoxSizer(wx.VERTICAL)
  220. box = wx.StaticBox(parent=panel, label=" %s " % _("Mirrored cursor"))
  221. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  222. gridSizer = wx.GridBagSizer(hgap=3, vgap=3)
  223. row = 0
  224. gridSizer.Add(
  225. item=wx.StaticText(
  226. parent=panel,
  227. label=_("Color:")),
  228. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL,
  229. pos=(
  230. row,
  231. 0))
  232. color = csel.ColourSelect(
  233. parent=panel,
  234. colour=UserSettings.Get(
  235. group='mapswipe',
  236. key='cursor',
  237. subkey='color'),
  238. size=globalvar.DIALOG_COLOR_SIZE)
  239. color.SetName('GetColour')
  240. self.winId['mapswipe:cursor:color'] = color.GetId()
  241. gridSizer.Add(item=color, pos=(row, 1), flag=wx.ALIGN_RIGHT)
  242. row += 1
  243. gridSizer.Add(
  244. item=wx.StaticText(
  245. parent=panel,
  246. label=_("Shape:")),
  247. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL,
  248. pos=(
  249. row,
  250. 0))
  251. cursors = wx.Choice(
  252. parent=panel,
  253. choices=self.settings.Get(
  254. group='mapswipe',
  255. key='cursor',
  256. subkey=[
  257. 'type',
  258. 'choices'],
  259. settings_type='internal'),
  260. name="GetSelection")
  261. cursors.SetSelection(self.settings.Get(group='mapswipe', key='cursor',
  262. subkey=['type', 'selection']))
  263. self.winId['mapswipe:cursor:type:selection'] = cursors.GetId()
  264. gridSizer.Add(item=cursors, flag=wx.ALIGN_RIGHT |
  265. wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos=(row, 1))
  266. row += 1
  267. gridSizer.Add(
  268. item=wx.StaticText(
  269. parent=panel,
  270. label=_("Line width:")),
  271. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL,
  272. pos=(
  273. row,
  274. 0))
  275. width = wx.SpinCtrl(
  276. parent=panel,
  277. min=1,
  278. max=10,
  279. initial=self.settings.Get(
  280. group='mapswipe',
  281. key='cursor',
  282. subkey='width'),
  283. name="GetValue")
  284. self.winId['mapswipe:cursor:width'] = width.GetId()
  285. gridSizer.Add(item=width, flag=wx.ALIGN_RIGHT |
  286. wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos=(row, 1))
  287. row += 1
  288. gridSizer.Add(
  289. item=wx.StaticText(
  290. parent=panel,
  291. label=_("Size:")),
  292. flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL,
  293. pos=(
  294. row,
  295. 0))
  296. size = wx.SpinCtrl(
  297. parent=panel,
  298. min=4,
  299. max=50,
  300. initial=self.settings.Get(
  301. group='mapswipe',
  302. key='cursor',
  303. subkey='size'),
  304. name="GetValue")
  305. self.winId['mapswipe:cursor:size'] = size.GetId()
  306. gridSizer.Add(item=size, flag=wx.ALIGN_RIGHT |
  307. wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, pos=(row, 1))
  308. gridSizer.AddGrowableCol(1)
  309. sizer.Add(
  310. item=gridSizer,
  311. proportion=1,
  312. flag=wx.ALL | wx.EXPAND,
  313. border=3)
  314. border.Add(item=sizer, proportion=0, flag=wx.ALL | wx.EXPAND, border=3)
  315. panel.SetSizer(border)
  316. return panel