dialogs.py 12 KB

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