dialogs.py 12 KB

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