dialogs.py 12 KB

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