dialogs.py 12 KB

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