dialogs.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. """!
  2. @package animation.dialogs
  3. @brief Dialogs for animation management, changing speed of animation
  4. Classes:
  5. - dialogs::SpeedDialog
  6. - dialogs::InputDialog
  7. - dialogs::EditDialog
  8. - dialogs::AnimationData
  9. - dialogs::ExportDialog
  10. (C) 2012 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. @author Anna Kratochvilova <kratochanna gmail.com>
  14. """
  15. import os
  16. import sys
  17. import wx
  18. import copy
  19. import datetime
  20. import wx.lib.filebrowsebutton as filebrowse
  21. if __name__ == '__main__':
  22. sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "gui", "wxpython"))
  23. from core.gcmd import GMessage, GError, GException
  24. from core import globalvar
  25. from gui_core import gselect
  26. from gui_core.dialogs import MapLayersDialog, GetImageHandlers
  27. from gui_core.forms import GUI
  28. from core.settings import UserSettings
  29. from core.utils import _
  30. from utils import TemporalMode, getRegisteredMaps, validateTimeseriesName, validateMapNames
  31. from nviztask import NvizTask
  32. from grass.pydispatch.signal import Signal
  33. class SpeedDialog(wx.Dialog):
  34. def __init__(self, parent, title = _("Adjust speed of animation"),
  35. temporalMode = None, minimumDuration = 20, timeGranularity = None,
  36. initialSpeed = 200):#, framesCount = None
  37. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = title,
  38. style = wx.DEFAULT_DIALOG_STYLE)
  39. # signal emitted when speed has changed; has attribute 'ms'
  40. self.speedChanged = Signal('SpeedDialog.speedChanged')
  41. self.minimumDuration = minimumDuration
  42. # self.framesCount = framesCount
  43. self.defaultSpeed = initialSpeed
  44. self.lastAppliedValue = self.defaultSpeed
  45. self.lastAppliedValueTemp = self.defaultSpeed
  46. self._layout()
  47. self.temporalMode = temporalMode
  48. self.timeGranularity = timeGranularity
  49. self._fillUnitChoice(self.choiceUnits)
  50. self.InitTimeSpin(self.defaultSpeed)
  51. def SetTimeGranularity(self, gran):
  52. self._timeGranularity = gran
  53. def GetTimeGranularity(self):
  54. return self._timeGranularity
  55. timeGranularity = property(fset = SetTimeGranularity, fget = GetTimeGranularity)
  56. def SetTemporalMode(self, mode):
  57. self._temporalMode = mode
  58. self._setTemporalMode()
  59. def GetTemporalMode(self):
  60. return self._temporalMode
  61. temporalMode = property(fset = SetTemporalMode, fget = GetTemporalMode)
  62. def _layout(self):
  63. """!Layout window"""
  64. mainSizer = wx.BoxSizer(wx.VERTICAL)
  65. #
  66. # simple mode
  67. #
  68. self.nontemporalBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  69. label = ' %s ' % _("Simple mode"))
  70. box = wx.StaticBoxSizer(self.nontemporalBox, wx.VERTICAL)
  71. gridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  72. labelDuration = wx.StaticText(self, id = wx.ID_ANY, label = _("Frame duration:"))
  73. labelUnits = wx.StaticText(self, id = wx.ID_ANY, label = _("ms"))
  74. self.spinDuration = wx.SpinCtrl(self, id = wx.ID_ANY, min = self.minimumDuration,
  75. max = 10000, initial = self.defaultSpeed)
  76. # TODO total time
  77. gridSizer.Add(item = labelDuration, pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  78. gridSizer.Add(item = self.spinDuration, pos = (0, 1), flag = wx.ALIGN_CENTER)
  79. gridSizer.Add(item = labelUnits, pos = (0, 2), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  80. gridSizer.AddGrowableCol(0)
  81. box.Add(item = gridSizer, proportion = 1, border = 5, flag = wx.ALL | wx.EXPAND)
  82. self.nontemporalSizer = gridSizer
  83. mainSizer.Add(item = box, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
  84. #
  85. # temporal mode
  86. #
  87. self.temporalBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  88. label = ' %s ' % _("Temporal mode"))
  89. box = wx.StaticBoxSizer(self.temporalBox, wx.VERTICAL)
  90. gridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  91. labelTimeUnit = wx.StaticText(self, id = wx.ID_ANY, label = _("Time unit:"))
  92. labelDuration = wx.StaticText(self, id = wx.ID_ANY, label = _("Duration of time unit:"))
  93. labelUnits = wx.StaticText(self, id = wx.ID_ANY, label = _("ms"))
  94. self.spinDurationTemp = wx.SpinCtrl(self, id = wx.ID_ANY, min = self.minimumDuration,
  95. max = 10000, initial = self.defaultSpeed)
  96. self.choiceUnits = wx.Choice(self, id = wx.ID_ANY)
  97. # TODO total time
  98. gridSizer.Add(item = labelTimeUnit, pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  99. gridSizer.Add(item = self.choiceUnits, pos = (0, 1), flag = wx.ALIGN_CENTER | wx.EXPAND)
  100. gridSizer.Add(item = labelDuration, pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  101. gridSizer.Add(item = self.spinDurationTemp, pos = (1, 1), flag = wx.ALIGN_CENTER | wx.EXPAND)
  102. gridSizer.Add(item = labelUnits, pos = (1, 2), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  103. gridSizer.AddGrowableCol(1)
  104. self.temporalSizer = gridSizer
  105. box.Add(item = gridSizer, proportion = 1, border = 5, flag = wx.ALL | wx.EXPAND)
  106. mainSizer.Add(item = box, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
  107. self.btnOk = wx.Button(self, wx.ID_OK)
  108. self.btnApply = wx.Button(self, wx.ID_APPLY)
  109. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  110. self.btnOk.SetDefault()
  111. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  112. self.btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  113. self.btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  114. self.Bind(wx.EVT_CLOSE, self.OnCancel)
  115. # button sizer
  116. btnStdSizer = wx.StdDialogButtonSizer()
  117. btnStdSizer.AddButton(self.btnOk)
  118. btnStdSizer.AddButton(self.btnApply)
  119. btnStdSizer.AddButton(self.btnCancel)
  120. btnStdSizer.Realize()
  121. mainSizer.Add(item = btnStdSizer, proportion = 0,
  122. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  123. self.SetSizer(mainSizer)
  124. mainSizer.Fit(self)
  125. def _setTemporalMode(self):
  126. self.nontemporalBox.Enable(self.temporalMode == TemporalMode.NONTEMPORAL)
  127. self.temporalBox.Enable(self.temporalMode == TemporalMode.TEMPORAL)
  128. for child in self.temporalSizer.GetChildren():
  129. child.GetWindow().Enable(self.temporalMode == TemporalMode.TEMPORAL)
  130. for child in self.nontemporalSizer.GetChildren():
  131. child.GetWindow().Enable(self.temporalMode == TemporalMode.NONTEMPORAL)
  132. self.Layout()
  133. def _fillUnitChoice(self, choiceWidget):
  134. timeUnitsChoice = [_("year"), _("month"), _("day"), _("hour"), _("minute"), _("second")]
  135. timeUnits = ["years", "months", "days", "hours", "minutes", "seconds"]
  136. for item, cdata in zip(timeUnitsChoice, timeUnits):
  137. choiceWidget.Append(item, cdata)
  138. if self.temporalMode == TemporalMode.TEMPORAL:
  139. unit = self.timeGranularity[1]
  140. try:
  141. index = timeUnits.index(unit)
  142. except ValueError:
  143. index = 0
  144. choiceWidget.SetSelection(index)
  145. else:
  146. choiceWidget.SetSelection(0)
  147. def OnOk(self, event):
  148. self._apply()
  149. self.OnCancel(None)
  150. def OnApply(self, event):
  151. self._apply()
  152. def OnCancel(self, event):
  153. self.spinDuration.SetValue(self.lastAppliedValue)
  154. self.spinDurationTemp.SetValue(self.lastAppliedValueTemp)
  155. self.Hide()
  156. def InitTimeSpin(self, timeTick):
  157. if self.temporalMode == TemporalMode.TEMPORAL:
  158. index = self.choiceUnits.GetSelection()
  159. unit = self.choiceUnits.GetClientData(index)
  160. delta = self._timedelta(unit = unit, number = 1)
  161. seconds1 = self._total_seconds(delta)
  162. number, unit = self.timeGranularity
  163. number = float(number)
  164. delta = self._timedelta(unit = unit, number = number)
  165. seconds2 = self._total_seconds(delta)
  166. value = timeTick
  167. ms = value * seconds1 / float(seconds2)
  168. self.spinDurationTemp.SetValue(ms)
  169. else:
  170. self.spinDuration.SetValue(timeTick)
  171. def _apply(self):
  172. if self.temporalMode == TemporalMode.NONTEMPORAL:
  173. ms = self.spinDuration.GetValue()
  174. self.lastAppliedValue = self.spinDuration.GetValue()
  175. elif self.temporalMode == TemporalMode.TEMPORAL:
  176. index = self.choiceUnits.GetSelection()
  177. unit = self.choiceUnits.GetClientData(index)
  178. delta = self._timedelta(unit = unit, number = 1)
  179. seconds1 = self._total_seconds(delta)
  180. number, unit = self.timeGranularity
  181. number = float(number)
  182. delta = self._timedelta(unit = unit, number = number)
  183. seconds2 = self._total_seconds(delta)
  184. value = self.spinDurationTemp.GetValue()
  185. ms = value * seconds2 / float(seconds1)
  186. if ms < self.minimumDuration:
  187. GMessage(parent = self, message = _("Animation speed is too high."))
  188. return
  189. self.lastAppliedValueTemp = self.spinDurationTemp.GetValue()
  190. else:
  191. return
  192. self.speedChanged.emit(ms = ms)
  193. def _timedelta(self, unit, number):
  194. if unit in "years":
  195. delta = datetime.timedelta(days = 365.25 * number)
  196. elif unit in "months":
  197. delta = datetime.timedelta(days = 30.4375 * number) # 365.25/12
  198. elif unit in "days":
  199. delta = datetime.timedelta(days = 1 * number)
  200. elif unit in "hours":
  201. delta = datetime.timedelta(hours = 1 * number)
  202. elif unit in "minutes":
  203. delta = datetime.timedelta(minutes = 1 * number)
  204. elif unit in "seconds":
  205. delta = datetime.timedelta(seconds = 1 * number)
  206. return delta
  207. def _total_seconds(self, delta):
  208. """!timedelta.total_seconds is new in version 2.7.
  209. """
  210. return delta.seconds + delta.days * 24 * 3600
  211. class InputDialog(wx.Dialog):
  212. def __init__(self, parent, mode, animationData):
  213. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY,
  214. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
  215. if mode == 'add':
  216. self.SetTitle(_("Add new animation"))
  217. elif mode == 'edit':
  218. self.SetTitle(_("Edit animation"))
  219. self.animationData = animationData
  220. self._tmpLegendCmd = None
  221. self._layout()
  222. self.OnViewMode(event = None)
  223. def _layout(self):
  224. mainSizer = wx.BoxSizer(wx.VERTICAL)
  225. self.windowChoice = wx.Choice(self, id = wx.ID_ANY,
  226. choices = [_("top left"), _("top right"),
  227. _("bottom left"), _("bottom right")])
  228. self.windowChoice.SetSelection(self.animationData.windowIndex)
  229. self.nameCtrl = wx.TextCtrl(self, id = wx.ID_ANY, value = self.animationData.name)
  230. self.nDChoice = wx.Choice(self, id = wx.ID_ANY)
  231. mode = self.animationData.viewMode
  232. index = 0
  233. for i, (viewMode, viewModeName) in enumerate(self.animationData.viewModes):
  234. self.nDChoice.Append(viewModeName, clientData = viewMode)
  235. if mode == viewMode:
  236. index = i
  237. self.nDChoice.SetSelection(index)
  238. # TODO
  239. self.nDChoice.SetToolTipString(_(""))
  240. self.nDChoice.Bind(wx.EVT_CHOICE, self.OnViewMode)
  241. gridSizer = wx.FlexGridSizer(cols = 2, hgap = 5, vgap = 5)
  242. gridSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _("Name:")),
  243. flag = wx.ALIGN_CENTER_VERTICAL)
  244. gridSizer.Add(item = self.nameCtrl, proportion = 1, flag = wx.EXPAND)
  245. gridSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _("Window position:")),
  246. flag = wx.ALIGN_CENTER_VERTICAL)
  247. gridSizer.Add(item = self.windowChoice, proportion = 1, flag = wx.ALIGN_RIGHT)
  248. gridSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _("View mode:")),
  249. flag = wx.ALIGN_CENTER_VERTICAL)
  250. gridSizer.Add(item = self.nDChoice, proportion = 1, flag = wx.ALIGN_RIGHT)
  251. gridSizer.AddGrowableCol(0, 1)
  252. gridSizer.AddGrowableCol(1, 1)
  253. mainSizer.Add(item = gridSizer, proportion = 1, flag = wx.ALL | wx.EXPAND, border = 5)
  254. self.dataPanel = self._createDataPanel()
  255. self.threeDPanel = self._create3DPanel()
  256. mainSizer.Add(item = self.dataPanel, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  257. mainSizer.Add(item = self.threeDPanel, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  258. # buttons
  259. self.btnOk = wx.Button(self, wx.ID_OK)
  260. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  261. self.btnOk.SetDefault()
  262. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  263. # button sizer
  264. btnStdSizer = wx.StdDialogButtonSizer()
  265. btnStdSizer.AddButton(self.btnOk)
  266. btnStdSizer.AddButton(self.btnCancel)
  267. btnStdSizer.Realize()
  268. mainSizer.Add(item = btnStdSizer, proportion = 0,
  269. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  270. self.SetSizer(mainSizer)
  271. mainSizer.Fit(self)
  272. def _createDataPanel(self):
  273. panel = wx.Panel(self, id = wx.ID_ANY)
  274. dataStBox = wx.StaticBox(parent = panel, id = wx.ID_ANY,
  275. label = ' %s ' % _("Data"))
  276. dataBoxSizer = wx.StaticBoxSizer(dataStBox, wx.VERTICAL)
  277. self.dataChoice = wx.Choice(panel, id = wx.ID_ANY)
  278. self._setMapTypes()
  279. self.dataChoice.Bind(wx.EVT_CHOICE, self.OnDataType)
  280. self.dataSelect = gselect.Select(parent = panel, id = wx.ID_ANY,
  281. size = globalvar.DIALOG_GSELECT_SIZE)
  282. iconTheme = UserSettings.Get(group = 'appearance', key = 'iconTheme', subkey = 'type')
  283. bitmapPath = os.path.join(globalvar.ETCICONDIR, iconTheme, 'layer-open.png')
  284. if os.path.isfile(bitmapPath) and os.path.getsize(bitmapPath):
  285. bitmap = wx.Bitmap(name = bitmapPath)
  286. else:
  287. bitmap = wx.ArtProvider.GetBitmap(id = wx.ART_MISSING_IMAGE, client = wx.ART_TOOLBAR)
  288. self.addManyMapsButton = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bitmap)
  289. self.addManyMapsButton.Bind(wx.EVT_BUTTON, self.OnAddMaps)
  290. self.legend = wx.CheckBox(panel, label=_("Show raster legend"))
  291. self.legend.SetValue(bool(self.animationData.legendCmd))
  292. self.legendBtn = wx.Button(panel, label=_("Set options"))
  293. self.legendBtn.Bind(wx.EVT_BUTTON, self.OnLegend)
  294. tooltip = _("By default, legend is created for the first raster map in case of multiple maps "
  295. "and for the first raster map of space time raster dataset.")
  296. self.legend.SetToolTipString(tooltip)
  297. self.legendBtn.SetToolTipString(tooltip)
  298. self.OnDataType(None)
  299. if self.animationData.inputData is None:
  300. self.dataSelect.SetValue('')
  301. else:
  302. self.dataSelect.SetValue(self.animationData.inputData)
  303. hbox = wx.BoxSizer(wx.HORIZONTAL)
  304. hbox.Add(item = wx.StaticText(panel, wx.ID_ANY, label = _("Input data type:")),
  305. proportion = 1, flag = wx.ALIGN_CENTER_VERTICAL)
  306. hbox.Add(item = self.dataChoice, proportion = 1, flag = wx.EXPAND)
  307. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  308. hbox = wx.BoxSizer(wx.HORIZONTAL)
  309. # hbox.Add(item = wx.StaticText(panel, wx.ID_ANY, label = _("Input data:")),
  310. # proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL)
  311. hbox.Add(item = self.dataSelect, proportion = 1, flag = wx.ALIGN_CENTER)
  312. hbox.Add(item = self.addManyMapsButton, proportion = 0, flag = wx.LEFT, border = 5)
  313. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  314. hbox = wx.BoxSizer(wx.HORIZONTAL)
  315. hbox.Add(item=self.legend, proportion=1, flag=wx.ALIGN_CENTER_VERTICAL)
  316. hbox.Add(item=self.legendBtn, proportion=0, flag=wx.LEFT, border=5)
  317. dataBoxSizer.Add(item=hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=3)
  318. panel.SetSizerAndFit(dataBoxSizer)
  319. panel.SetAutoLayout(True)
  320. return panel
  321. def _create3DPanel(self):
  322. panel = wx.Panel(self, id = wx.ID_ANY)
  323. dataStBox = wx.StaticBox(parent = panel, id = wx.ID_ANY,
  324. label = ' %s ' % _("3D view parameters"))
  325. dataBoxSizer = wx.StaticBoxSizer(dataStBox, wx.VERTICAL)
  326. # workspace file
  327. self.fileSelector = filebrowse.FileBrowseButton(parent = panel, id = wx.ID_ANY,
  328. size = globalvar.DIALOG_GSELECT_SIZE,
  329. labelText = _("Workspace file:"),
  330. dialogTitle = _("Choose workspace file to import 3D view parameters"),
  331. buttonText = _('Browse'),
  332. startDirectory = os.getcwd(), fileMode = 0,
  333. fileMask = "GRASS Workspace File (*.gxw)|*.gxw")
  334. if self.animationData.workspaceFile:
  335. self.fileSelector.SetValue(self.animationData.workspaceFile)
  336. self.paramLabel = wx.StaticText(panel, wx.ID_ANY, label = _("Parameter for animation:"))
  337. self.paramChoice = wx.Choice(panel, id = wx.ID_ANY, choices = self.animationData.nvizParameters)
  338. self.paramChoice.SetStringSelection(self.animationData.nvizParameter)
  339. hbox = wx.BoxSizer(wx.HORIZONTAL)
  340. hbox.Add(item = self.fileSelector, proportion = 1, flag = wx.EXPAND | wx.ALIGN_CENTER)
  341. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  342. hbox = wx.BoxSizer(wx.HORIZONTAL)
  343. hbox.Add(item = self.paramLabel, proportion = 1, flag = wx.ALIGN_CENTER_VERTICAL)
  344. hbox.Add(item = self.paramChoice, proportion = 1, flag = wx.EXPAND)
  345. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  346. panel.SetSizerAndFit(dataBoxSizer)
  347. panel.SetAutoLayout(True)
  348. return panel
  349. def _setMapTypes(self, view2d = True):
  350. index = 0
  351. inputTypes = self.animationData.inputMapTypes
  352. self.dataChoice.Clear()
  353. for i, (itype, itypeName) in enumerate(inputTypes):
  354. self.dataChoice.Append(itypeName, clientData = itype)
  355. if itype == self.animationData.inputMapType:
  356. index = i
  357. self.dataChoice.SetSelection(index)
  358. def OnViewMode(self, event):
  359. mode = self.nDChoice.GetSelection()
  360. self.Freeze()
  361. sizer = self.threeDPanel.GetContainingSizer()
  362. sizer.Show(self.threeDPanel, mode != 0, True)
  363. sizer.Layout()
  364. self._setMapTypes(mode == 0)
  365. self.Layout()
  366. self.Fit()
  367. self.Thaw()
  368. def OnDataType(self, event):
  369. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  370. if etype in ('rast', 'vect'):
  371. self.dataSelect.SetType(etype = etype, multiple = True)
  372. self.addManyMapsButton.Enable(True)
  373. else:
  374. self.dataSelect.SetType(etype = etype, multiple = False)
  375. self.addManyMapsButton.Enable(False)
  376. self.legend.Enable(etype in ('rast', 'strds'))
  377. self.legendBtn.Enable(etype in ('rast', 'strds'))
  378. self.dataSelect.SetValue('')
  379. def OnAddMaps(self, event):
  380. # TODO: fix dialog
  381. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  382. index = 0
  383. if etype == 'vect':
  384. index = 2
  385. dlg = MapLayersDialog(self, title = _("Select raster maps for animation"))
  386. dlg.applyAddingMapLayers.connect(lambda mapLayers:
  387. self.dataSelect.SetValue(','.join(mapLayers)))
  388. dlg.layerType.SetSelection(index)
  389. dlg.LoadMapLayers(dlg.GetLayerType(cmd = True),
  390. dlg.mapset.GetStringSelection())
  391. if dlg.ShowModal() == wx.ID_OK:
  392. self.dataSelect.SetValue(','.join(dlg.GetMapLayers()))
  393. dlg.Destroy()
  394. def OnLegend(self, event):
  395. """!Set options for legend"""
  396. if self._tmpLegendCmd:
  397. cmd = self._tmpLegendCmd
  398. elif self.animationData.legendCmd:
  399. cmd = self.animationData.legendCmd
  400. else:
  401. cmd = ['d.legend', 'at=5,50,2,5']
  402. mapName = self._getLegendMapHint()
  403. if mapName:
  404. cmd.append("map=%s" % mapName)
  405. GUI(parent=self, modal=True).ParseCommand(cmd=cmd,
  406. completed=(self.GetOptData, '', ''))
  407. def _getLegendMapHint(self):
  408. """!Determine probable map"""
  409. inputData = self.dataSelect.GetValue()
  410. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  411. if etype == 'strds':
  412. timeseries = validateTimeseriesName(inputData, etype=etype)
  413. timeseriesMaps = getRegisteredMaps(timeseries, etype)
  414. if len(timeseriesMaps):
  415. return timeseriesMaps[0]
  416. else: # multiple raster
  417. maps = inputData.split(',')
  418. if len(maps):
  419. return maps[0]
  420. return None
  421. def GetOptData(self, dcmd, layer, params, propwin):
  422. """!Process decoration layer data"""
  423. self._tmpLegendCmd = dcmd
  424. if dcmd and not self.legend.IsChecked():
  425. self.legend.SetValue(True)
  426. def _update(self):
  427. self.animationData.name = self.nameCtrl.GetValue()
  428. self.animationData.windowIndex = self.windowChoice.GetSelection()
  429. sel = self.dataChoice.GetSelection()
  430. self.animationData.inputMapType = self.dataChoice.GetClientData(sel)
  431. self.animationData.inputData = self.dataSelect.GetValue()
  432. sel = self.nDChoice.GetSelection()
  433. self.animationData.viewMode = self.nDChoice.GetClientData(sel)
  434. self.animationData.legendCmd = None
  435. if self._tmpLegendCmd:
  436. if self.legend.IsChecked():
  437. self.animationData.legendCmd = self._tmpLegendCmd
  438. else:
  439. if self.legend.IsChecked():
  440. self.animationData.legendCmd = ['d.legend',
  441. 'at=5,50,2,5',
  442. 'map=%s' % self._getLegendMapHint()]
  443. if self.threeDPanel.IsShown():
  444. self.animationData.workspaceFile = self.fileSelector.GetValue()
  445. if self.threeDPanel.IsShown():
  446. self.animationData.nvizParameter = self.paramChoice.GetStringSelection()
  447. def OnOk(self, event):
  448. try:
  449. self._update()
  450. self.EndModal(wx.ID_OK)
  451. except (GException, ValueError, IOError) as e:
  452. GError(message = str(e), showTraceback = False, caption = _("Invalid input"))
  453. class EditDialog(wx.Dialog):
  454. def __init__(self, parent, evalFunction, animationData, maxAnimations):
  455. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY,
  456. style = wx.DEFAULT_DIALOG_STYLE)
  457. self.animationData = copy.deepcopy(animationData)
  458. self.eval = evalFunction
  459. self.SetTitle(_("Add, edit or remove animations"))
  460. self._layout()
  461. self.SetSize((300, -1))
  462. self.maxAnimations = maxAnimations
  463. self.result = None
  464. def _layout(self):
  465. mainSizer = wx.BoxSizer(wx.VERTICAL)
  466. box = wx.StaticBox (parent = self, id = wx.ID_ANY, label = " %s " % _("List of animations"))
  467. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  468. gridBagSizer = wx.GridBagSizer (hgap = 5, vgap = 5)
  469. gridBagSizer.AddGrowableCol(0)
  470. # gridBagSizer.AddGrowableCol(1,1)
  471. self.listbox = wx.ListBox(self, id = wx.ID_ANY, choices = [], style = wx.LB_SINGLE|wx.LB_NEEDED_SB)
  472. self.listbox.Bind(wx.EVT_LISTBOX_DCLICK, self.OnEdit)
  473. self.addButton = wx.Button(self, id = wx.ID_ANY, label = _("Add"))
  474. self.addButton.Bind(wx.EVT_BUTTON, self.OnAdd)
  475. self.editButton = wx.Button(self, id = wx.ID_ANY, label = _("Edit"))
  476. self.editButton.Bind(wx.EVT_BUTTON, self.OnEdit)
  477. self.removeButton = wx.Button(self, id = wx.ID_ANY, label = _("Remove"))
  478. self.removeButton.Bind(wx.EVT_BUTTON, self.OnRemove)
  479. self._updateListBox()
  480. gridBagSizer.Add(self.listbox, pos = (0,0), span = (3, 1), flag = wx.ALIGN_CENTER_VERTICAL| wx.EXPAND, border = 0)
  481. gridBagSizer.Add(self.addButton, pos = (0,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  482. gridBagSizer.Add(self.editButton, pos = (1,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  483. gridBagSizer.Add(self.removeButton, pos = (2,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  484. sizer.Add(gridBagSizer, proportion = 0, flag = wx.ALL | wx.EXPAND, border = 5)
  485. mainSizer.Add(item = sizer, proportion = 0,
  486. flag = wx.EXPAND | wx.ALL, border = 5)
  487. # buttons
  488. self.btnOk = wx.Button(self, wx.ID_OK)
  489. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  490. self.btnOk.SetDefault()
  491. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  492. # button sizer
  493. btnStdSizer = wx.StdDialogButtonSizer()
  494. btnStdSizer.AddButton(self.btnOk)
  495. btnStdSizer.AddButton(self.btnCancel)
  496. btnStdSizer.Realize()
  497. mainSizer.Add(item = btnStdSizer, proportion = 0,
  498. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  499. self.SetSizer(mainSizer)
  500. mainSizer.Fit(self)
  501. def _updateListBox(self):
  502. self.listbox.Clear()
  503. for anim in self.animationData:
  504. self.listbox.Append(anim.name, clientData = anim)
  505. self.listbox.SetSelection(0)
  506. def _getNextIndex(self):
  507. indices = [anim.windowIndex for anim in self.animationData]
  508. for i in range(self.maxAnimations):
  509. if i not in indices:
  510. return i
  511. return None
  512. def OnAdd(self, event):
  513. windowIndex = self._getNextIndex()
  514. if windowIndex is None:
  515. GMessage(self, message = _("Maximum number of animations is %d.") % self.maxAnimations)
  516. return
  517. animData = AnimationData()
  518. # number of active animations
  519. animationIndex = len(self.animationData)
  520. animData.SetDefaultValues(windowIndex, animationIndex)
  521. dlg = InputDialog(parent = self, mode = 'add', animationData = animData)
  522. if dlg.ShowModal() == wx.ID_CANCEL:
  523. dlg.Destroy()
  524. return
  525. dlg.Destroy()
  526. self.animationData.append(animData)
  527. self._updateListBox()
  528. def OnEdit(self, event):
  529. index = self.listbox.GetSelection()
  530. if index == wx.NOT_FOUND:
  531. return
  532. animData = self.listbox.GetClientData(index)
  533. dlg = InputDialog(parent = self, mode = 'edit', animationData = animData)
  534. if dlg.ShowModal() == wx.ID_CANCEL:
  535. dlg.Destroy()
  536. return
  537. dlg.Destroy()
  538. self._updateListBox()
  539. def OnRemove(self, event):
  540. index = self.listbox.GetSelection()
  541. if index == wx.NOT_FOUND:
  542. return
  543. animData = self.listbox.GetClientData(index)
  544. self.animationData.remove(animData)
  545. self._updateListBox()
  546. def GetResult(self):
  547. return self.result
  548. def OnOk(self, event):
  549. indices = set([anim.windowIndex for anim in self.animationData])
  550. if len(indices) != len(self.animationData):
  551. GError(parent = self, message = _("More animations are using one window."
  552. " Please select different window for each animation."))
  553. return
  554. try:
  555. temporalMode, tempManager = self.eval(self.animationData)
  556. except GException, e:
  557. GError(parent = self, message = e.value, showTraceback = False)
  558. return
  559. self.result = (self.animationData, temporalMode, tempManager)
  560. self.EndModal(wx.ID_OK)
  561. class AnimationData(object):
  562. def __init__(self):
  563. self._inputMapTypes = [('rast', _("Multiple raster maps")),
  564. ('vect', _("Multiple vector maps")),
  565. ('strds', _("Space time raster dataset")),
  566. ('stvds', _("Space time vector dataset"))]
  567. self._inputMapType = 'rast'
  568. self.inputData = None
  569. self.mapData = None
  570. self._viewModes = [('2d', _("2D view")),
  571. ('3d', _("3D view"))]
  572. self.viewMode = '2d'
  573. self.nvizTask = NvizTask()
  574. self._nvizParameters = self.nvizTask.ListMapParameters()
  575. self.nvizParameter = self._nvizParameters[0]
  576. self.workspaceFile = None
  577. self.legendCmd = None
  578. def GetName(self):
  579. return self._name
  580. def SetName(self, name):
  581. if name == '':
  582. raise ValueError(_("No animation name selected."))
  583. self._name = name
  584. name = property(fget = GetName, fset = SetName)
  585. def GetWindowIndex(self):
  586. return self._windowIndex
  587. def SetWindowIndex(self, windowIndex):
  588. self._windowIndex = windowIndex
  589. windowIndex = property(fget = GetWindowIndex, fset = SetWindowIndex)
  590. def GetInputMapTypes(self):
  591. return self._inputMapTypes
  592. inputMapTypes = property(fget = GetInputMapTypes)
  593. def GetInputMapType(self):
  594. return self._inputMapType
  595. def SetInputMapType(self, itype):
  596. if itype in [each[0] for each in self.inputMapTypes]:
  597. self._inputMapType = itype
  598. else:
  599. raise ValueError("Bad input type.")
  600. inputMapType = property(fget = GetInputMapType, fset = SetInputMapType)
  601. def GetInputData(self):
  602. return self._inputData
  603. def SetInputData(self, data):
  604. if data == '':
  605. raise ValueError(_("No data selected."))
  606. if data is None:
  607. self._inputData = data
  608. return
  609. if self.inputMapType in ('rast', 'vect'):
  610. maps = data.split(',')
  611. newNames = validateMapNames(maps, self.inputMapType)
  612. self._inputData = ','.join(newNames)
  613. self.mapData = newNames
  614. elif self.inputMapType in ('strds', 'stvds'):
  615. timeseries = validateTimeseriesName(data, etype=self.inputMapType)
  616. timeseriesMaps = getRegisteredMaps(timeseries, self.inputMapType)
  617. self._inputData = timeseries
  618. self.mapData = timeseriesMaps
  619. else:
  620. self._inputData = data
  621. inputData = property(fget = GetInputData, fset = SetInputData)
  622. def SetMapData(self, data):
  623. self._mapData = data
  624. def GetMapData(self):
  625. return self._mapData
  626. mapData = property(fget = GetMapData, fset = SetMapData)
  627. def GetWorkspaceFile(self):
  628. return self._workspaceFile
  629. def SetWorkspaceFile(self, fileName):
  630. if fileName is None:
  631. self._workspaceFile = None
  632. return
  633. if fileName == '':
  634. raise ValueError(_("No workspace file selected."))
  635. if not os.path.exists(fileName):
  636. raise IOError(_("File %s not found") % fileName)
  637. self._workspaceFile = fileName
  638. self.nvizTask.Load(self.workspaceFile)
  639. workspaceFile = property(fget = GetWorkspaceFile, fset = SetWorkspaceFile)
  640. def SetDefaultValues(self, windowIndex, animationIndex):
  641. self.windowIndex = windowIndex
  642. self.name = _("Animation %d") % (animationIndex + 1)
  643. def GetNvizParameters(self):
  644. return self._nvizParameters
  645. nvizParameters = property(fget = GetNvizParameters)
  646. def GetNvizParameter(self):
  647. return self._nvizParameter
  648. def SetNvizParameter(self, param):
  649. self._nvizParameter = param
  650. nvizParameter = property(fget = GetNvizParameter, fset = SetNvizParameter)
  651. def GetViewMode(self):
  652. return self._viewMode
  653. def SetViewMode(self, mode):
  654. self._viewMode = mode
  655. viewMode = property(fget = GetViewMode, fset = SetViewMode)
  656. def GetViewModes(self):
  657. return self._viewModes
  658. viewModes = property(fget = GetViewModes)
  659. def SetLegendCmd(self, cmd):
  660. self._legendCmd = cmd
  661. def GetLegendCmd(self):
  662. return self._legendCmd
  663. legendCmd = property(fget=GetLegendCmd, fset=SetLegendCmd)
  664. def GetNvizCommands(self):
  665. if not self.workspaceFile or not self.mapData:
  666. return []
  667. cmds = self.nvizTask.GetCommandSeries(series = self.mapData, paramName = self.nvizParameter)
  668. region = self.nvizTask.GetRegion()
  669. return {'commands': cmds, 'region': region}
  670. def __repr__(self):
  671. return "%s(%r)" % (self.__class__, self.__dict__)
  672. class ExportDialog(wx.Dialog):
  673. def __init__(self, parent, temporal, timeTick, visvis):
  674. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = _("Export animation"),
  675. style = wx.DEFAULT_DIALOG_STYLE)
  676. self.decorations = []
  677. self.temporal = temporal
  678. self.timeTick = timeTick
  679. self.visvis = visvis
  680. self._layout()
  681. # export animation
  682. self.doExport = Signal('ExportDialog::doExport')
  683. wx.CallAfter(self._hideAll)
  684. def _layout(self):
  685. notebook = wx.Notebook(self, id = wx.ID_ANY)
  686. mainSizer = wx.BoxSizer(wx.VERTICAL)
  687. notebook.AddPage(page = self._createExportFormatPanel(notebook), text = _("Format"))
  688. notebook.AddPage(page = self._createDecorationsPanel(notebook), text = _("Decorations"))
  689. mainSizer.Add(item = notebook, proportion = 0,
  690. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  691. self.btnExport = wx.Button(self, wx.ID_OK)
  692. self.btnExport.SetLabel(_("Export"))
  693. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  694. self.btnExport.SetDefault()
  695. self.btnExport.Bind(wx.EVT_BUTTON, self.OnExport)
  696. # button sizer
  697. btnStdSizer = wx.StdDialogButtonSizer()
  698. btnStdSizer.AddButton(self.btnExport)
  699. btnStdSizer.AddButton(self.btnCancel)
  700. btnStdSizer.Realize()
  701. mainSizer.Add(item = btnStdSizer, proportion = 0,
  702. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  703. self.SetSizer(mainSizer)
  704. # set the longest option to fit
  705. self.hidevbox.Show(self.fontBox, True)
  706. self.hidevbox.Show(self.imageBox, False)
  707. self.hidevbox.Show(self.textBox, True)
  708. self.hidevbox.Show(self.posBox, True)
  709. self.hidevbox.Show(self.informBox, False)
  710. mainSizer.Fit(self)
  711. def _createDecorationsPanel(self, notebook):
  712. panel = wx.Panel(notebook, id = wx.ID_ANY)
  713. sizer = wx.BoxSizer(wx.VERTICAL)
  714. sizer.Add(self._createDecorationsList(panel), proportion = 0, flag = wx.ALL | wx.EXPAND, border = 10)
  715. sizer.Add(self._createDecorationsProperties(panel), proportion = 0, flag = wx.ALL | wx.EXPAND, border = 10)
  716. panel.SetSizer(sizer)
  717. sizer.Fit(panel)
  718. return panel
  719. def _createDecorationsList(self, panel):
  720. gridBagSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  721. gridBagSizer.AddGrowableCol(0)
  722. self.listbox = wx.ListBox(panel, id = wx.ID_ANY, choices = [], style = wx.LB_SINGLE|wx.LB_NEEDED_SB)
  723. self.listbox.Bind(wx.EVT_LISTBOX, self.OnSelectionChanged)
  724. gridBagSizer.Add(self.listbox, pos = (0, 0), span = (4, 1),
  725. flag = wx.ALIGN_CENTER_VERTICAL| wx.EXPAND, border = 0)
  726. buttonNames = ['time', 'image', 'text']
  727. buttonLabels = [_("Add time stamp"), _("Add image"), _("Add text")]
  728. i = 0
  729. for buttonName, buttonLabel in zip(buttonNames, buttonLabels):
  730. if buttonName == 'time' and self.temporal == TemporalMode.NONTEMPORAL:
  731. continue
  732. btn = wx.Button(panel, id = wx.ID_ANY, name = buttonName, label = buttonLabel)
  733. btn.Bind(wx.EVT_BUTTON, lambda evt, temp = buttonName: self.OnAddDecoration(evt, temp))
  734. gridBagSizer.Add(btn, pos = (i ,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  735. i += 1
  736. removeButton = wx.Button(panel, id = wx.ID_ANY, label = _("Remove"))
  737. removeButton.Bind(wx.EVT_BUTTON, self.OnRemove)
  738. gridBagSizer.Add(removeButton, pos = (i, 1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  739. return gridBagSizer
  740. def _createDecorationsProperties(self, panel):
  741. self.hidevbox = wx.BoxSizer(wx.VERTICAL)
  742. # inform label
  743. self.informBox = wx.BoxSizer(wx.HORIZONTAL)
  744. if self.temporal == TemporalMode.TEMPORAL:
  745. label = _("Add time stamp, image or text decoration by one of the buttons above.")
  746. else:
  747. label = _("Add image or text decoration by one of the buttons above.")
  748. label = wx.StaticText(panel, id = wx.ID_ANY, label = label)
  749. label.Wrap(400)
  750. self.informBox.Add(label, proportion = 1, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
  751. self.hidevbox.Add(self.informBox, proportion = 0, flag = wx.EXPAND | wx.BOTTOM, border = 5)
  752. # font
  753. self.fontBox = wx.BoxSizer(wx.HORIZONTAL)
  754. self.fontBox.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Font settings:")),
  755. proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
  756. self.sampleLabel = wx.StaticText(panel, id = wx.ID_ANY, label = _("Sample text"))
  757. self.fontBox.Add(self.sampleLabel, proportion = 1,
  758. flag = wx.ALIGN_CENTER | wx.RIGHT | wx.LEFT, border = 5)
  759. fontButton = wx.Button(panel, id = wx.ID_ANY, label = _("Set font"))
  760. fontButton.Bind(wx.EVT_BUTTON, self.OnFont)
  761. self.fontBox.Add(fontButton, proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL)
  762. self.hidevbox.Add(self.fontBox, proportion = 0, flag = wx.EXPAND | wx.BOTTOM, border = 5)
  763. # image
  764. self.imageBox = wx.BoxSizer(wx.HORIZONTAL)
  765. filetype, ltype = GetImageHandlers(wx.EmptyImage(10, 10))
  766. self.browse = filebrowse.FileBrowseButton(parent = panel, id = wx.ID_ANY, fileMask = filetype,
  767. labelText = _("Image file:"),
  768. dialogTitle = _('Choose image file'),
  769. buttonText = _('Browse'),
  770. startDirectory = os.getcwd(), fileMode = wx.OPEN,
  771. changeCallback = self.OnSetImage)
  772. self.imageBox.Add(self.browse, proportion = 1, flag = wx.EXPAND)
  773. self.hidevbox.Add(self.imageBox, proportion = 0, flag = wx.EXPAND | wx.BOTTOM, border = 5)
  774. # text
  775. self.textBox = wx.BoxSizer(wx.HORIZONTAL)
  776. self.textBox.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Text:")),
  777. proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
  778. self.textCtrl = wx.TextCtrl(panel, id = wx.ID_ANY)
  779. self.textCtrl.Bind(wx.EVT_TEXT, self.OnText)
  780. self.textBox.Add(self.textCtrl, proportion = 1, flag = wx.EXPAND)
  781. self.hidevbox.Add(self.textBox, proportion = 0, flag = wx.EXPAND)
  782. self.posBox = self._positionWidget(panel)
  783. self.hidevbox.Add(self.posBox, proportion = 0, flag = wx.EXPAND | wx.TOP, border = 5)
  784. return self.hidevbox
  785. def _positionWidget(self, panel):
  786. grid = wx.GridBagSizer(vgap = 5, hgap = 5)
  787. label = wx.StaticText(panel, id = wx.ID_ANY, label = _("Placement as percentage of"
  788. " screen coordinates (X: 0, Y: 0 is top left):"))
  789. label.Wrap(400)
  790. self.spinX = wx.SpinCtrl(panel, id = wx.ID_ANY, min = 0, max = 100, initial = 10)
  791. self.spinY = wx.SpinCtrl(panel, id = wx.ID_ANY, min = 0, max = 100, initial = 10)
  792. self.spinX.Bind(wx.EVT_SPINCTRL, lambda evt, temp = 'X': self.OnPosition(evt, temp))
  793. self.spinY.Bind(wx.EVT_SPINCTRL, lambda evt, temp = 'Y': self.OnPosition(evt, temp))
  794. grid.Add(label, pos = (0, 0), span = (1, 4), flag = wx.EXPAND)
  795. grid.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("X:")), pos = (1, 0),
  796. flag = wx.ALIGN_CENTER_VERTICAL)
  797. grid.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Y:")), pos = (1, 2),
  798. flag = wx.ALIGN_CENTER_VERTICAL)
  799. grid.Add(self.spinX, pos = (1, 1))
  800. grid.Add(self.spinY, pos = (1, 3))
  801. return grid
  802. def _createExportFormatPanel(self, notebook):
  803. panel = wx.Panel(notebook, id = wx.ID_ANY)
  804. borderSizer = wx.BoxSizer(wx.VERTICAL)
  805. if not self.visvis:
  806. isVisvisText = wx.StaticText(panel, id = wx.ID_ANY,
  807. label = _("To enable export to GIF and SWF, please install visvis library."))
  808. isVisvisText.Wrap(400)
  809. borderSizer.Add(item = isVisvisText, proportion = 0,
  810. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border = 5)
  811. hSizer = wx.BoxSizer(wx.HORIZONTAL)
  812. if not self.visvis:
  813. choices = [_("image sequence")]
  814. else:
  815. choices = [_("image sequence"), _("animated GIF"), _("SWF"), _("AVI")]
  816. self.formatChoice = wx.Choice(parent = panel, id = wx.ID_ANY,
  817. choices = choices)
  818. self.formatChoice.Bind(wx.EVT_CHOICE, lambda event: self.ChangeFormat(event.GetSelection()))
  819. hSizer.Add(item = wx.StaticText(panel, id = wx.ID_ANY, label = _("Export to:")),
  820. proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 2)
  821. hSizer.Add(item = self.formatChoice, proportion = 1,
  822. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border = 2)
  823. borderSizer.Add(item = hSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  824. helpSizer = wx.BoxSizer(wx.HORIZONTAL)
  825. helpSizer.AddStretchSpacer(1)
  826. self.formatPanelSizer = wx.BoxSizer(wx.VERTICAL)
  827. helpSizer.Add(self.formatPanelSizer, proportion = 5)
  828. borderSizer.Add(helpSizer, proportion = 1, flag = wx.EXPAND)
  829. self.formatPanels = []
  830. # panel for image sequence
  831. imSeqPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  832. prefixLabel = wx.StaticText(imSeqPanel, id = wx.ID_ANY, label = _("File prefix:"))
  833. self.prefixCtrl = wx.TextCtrl(imSeqPanel, id = wx.ID_ANY, value = _("animation"))
  834. formatLabel = wx.StaticText(imSeqPanel, id = wx.ID_ANY, label = _("File format:"))
  835. self.imSeqFormatChoice = wx.Choice(imSeqPanel, id = wx.ID_ANY)
  836. wildcard, ltype = GetImageHandlers(wx.EmptyImage(10, 10))
  837. formats = [format for format in wildcard.split('|') if 'file' in format]
  838. for format, cdata in zip(formats, ltype):
  839. self.imSeqFormatChoice.Append(format, cdata)
  840. self.imSeqFormatChoice.SetSelection(0)
  841. self.dirBrowse = filebrowse.DirBrowseButton(parent = imSeqPanel, id = wx.ID_ANY,
  842. labelText = _("Directory:"),
  843. dialogTitle = _("Choose directory for export"),
  844. buttonText = _("Browse"),
  845. startDirectory = os.getcwd())
  846. dirGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  847. dirGridSizer.AddGrowableCol(1)
  848. dirGridSizer.Add(prefixLabel, pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL)
  849. dirGridSizer.Add(self.prefixCtrl, pos = (0, 1), flag = wx.EXPAND)
  850. dirGridSizer.Add(formatLabel, pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL)
  851. dirGridSizer.Add(self.imSeqFormatChoice, pos = (1, 1), flag = wx.EXPAND)
  852. dirGridSizer.Add(self.dirBrowse, pos = (2, 0), flag = wx.EXPAND, span = (1, 2))
  853. imSeqPanel.SetSizer(dirGridSizer)
  854. dirGridSizer.Fit(imSeqPanel)
  855. self.formatPanelSizer.Add(item = imSeqPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  856. self.formatPanels.append(imSeqPanel)
  857. # panel for gif
  858. gifPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  859. self.gifBrowse = filebrowse.FileBrowseButton(parent = gifPanel, id = wx.ID_ANY,
  860. fileMask = "GIF file (*.gif)|*.gif",
  861. labelText = _("GIF file:"),
  862. dialogTitle = _("Choose file to save animation"),
  863. buttonText = _("Browse"),
  864. startDirectory = os.getcwd(), fileMode = wx.SAVE)
  865. gifGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  866. gifGridSizer.AddGrowableCol(0)
  867. gifGridSizer.Add(self.gifBrowse, pos = (0, 0), flag = wx.EXPAND)
  868. gifPanel.SetSizer(gifGridSizer)
  869. gifGridSizer.Fit(gifPanel)
  870. self.formatPanelSizer.Add(item = gifPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  871. self.formatPanels.append(gifPanel)
  872. # panel for swf
  873. swfPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  874. self.swfBrowse = filebrowse.FileBrowseButton(parent = swfPanel, id = wx.ID_ANY,
  875. fileMask = "SWF file (*.swf)|*.swf",
  876. labelText = _("SWF file:"),
  877. dialogTitle = _("Choose file to save animation"),
  878. buttonText = _("Browse"),
  879. startDirectory = os.getcwd(), fileMode = wx.SAVE)
  880. swfGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  881. swfGridSizer.AddGrowableCol(0)
  882. swfGridSizer.Add(self.swfBrowse, pos = (0, 0), flag = wx.EXPAND)
  883. swfPanel.SetSizer(swfGridSizer)
  884. swfGridSizer.Fit(swfPanel)
  885. self.formatPanelSizer.Add(item = swfPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  886. self.formatPanels.append(swfPanel)
  887. # panel for avi
  888. aviPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  889. self.aviBrowse = filebrowse.FileBrowseButton(parent = aviPanel, id = wx.ID_ANY,
  890. fileMask = "AVI file (*.avi)|*.avi",
  891. labelText = _("AVI file:"),
  892. dialogTitle = _("Choose file to save animation"),
  893. buttonText = _("Browse"),
  894. startDirectory = os.getcwd(), fileMode = wx.SAVE)
  895. encodingLabel = wx.StaticText(parent = aviPanel, id = wx.ID_ANY, label = _("Video codec:"))
  896. self.encodingText = wx.TextCtrl(parent = aviPanel, id = wx.ID_ANY, value = 'mpeg4')
  897. aviGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  898. aviGridSizer.AddGrowableCol(1)
  899. aviGridSizer.Add(self.aviBrowse, pos = (0, 0), span = (1, 2), flag = wx.EXPAND)
  900. aviGridSizer.Add(encodingLabel, pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL)
  901. aviGridSizer.Add(self.encodingText, pos = (1, 1), flag = wx.EXPAND)
  902. aviPanel.SetSizer(aviGridSizer)
  903. aviGridSizer.Fit(aviPanel)
  904. self.formatPanelSizer.Add(item = aviPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  905. self.formatPanels.append(aviPanel)
  906. fpsSizer = wx.BoxSizer(wx.HORIZONTAL)
  907. fps = 1000 / self.timeTick
  908. fpsSizer.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Current frame rate: %.2f fps") % fps),
  909. proportion = 1, flag = wx.EXPAND)
  910. borderSizer.Add(fpsSizer, proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  911. panel.SetSizer(borderSizer)
  912. borderSizer.Fit(panel)
  913. self.ChangeFormat(index = 0)
  914. return panel
  915. def ChangeFormat(self, index):
  916. for i, panel in enumerate(self.formatPanels):
  917. self.formatPanelSizer.Show(item = panel, show = (i == index))
  918. self.formatPanelSizer.Layout()
  919. def OnFont(self, event):
  920. index = self.listbox.GetSelection()
  921. # should not happen
  922. if index == wx.NOT_FOUND:
  923. return
  924. cdata = self.listbox.GetClientData(index)
  925. font = cdata['font']
  926. fontdata = wx.FontData()
  927. fontdata.EnableEffects(True)
  928. fontdata.SetColour('black')
  929. fontdata.SetInitialFont(font)
  930. dlg = wx.FontDialog(self, fontdata)
  931. if dlg.ShowModal() == wx.ID_OK:
  932. newfontdata = dlg.GetFontData()
  933. font = newfontdata.GetChosenFont()
  934. self.sampleLabel.SetFont(font)
  935. cdata['font'] = font
  936. self.Layout()
  937. def OnPosition(self, event, coord):
  938. index = self.listbox.GetSelection()
  939. # should not happen
  940. if index == wx.NOT_FOUND:
  941. return
  942. cdata = self.listbox.GetClientData(index)
  943. cdata['pos'][coord == 'Y'] = event.GetInt()
  944. def OnSetImage(self, event):
  945. index = self.listbox.GetSelection()
  946. # should not happen
  947. if index == wx.NOT_FOUND:
  948. return
  949. cdata = self.listbox.GetClientData(index)
  950. cdata['file'] = event.GetString()
  951. def OnAddDecoration(self, event, name):
  952. if name == 'time':
  953. timeInfo = {'name': name, 'font': self.GetFont(), 'pos': [10, 10]}
  954. self.decorations.append(timeInfo)
  955. elif name == 'image':
  956. imageInfo = {'name': name, 'file': '', 'pos': [10, 10]}
  957. self.decorations.append(imageInfo)
  958. elif name == 'text':
  959. textInfo = {'name': name, 'font': self.GetFont(), 'text': '', 'pos': [10, 10]}
  960. self.decorations.append(textInfo)
  961. self._updateListBox()
  962. self.listbox.SetSelection(self.listbox.GetCount() - 1)
  963. self.OnSelectionChanged(event = None)
  964. def OnSelectionChanged(self, event):
  965. index = self.listbox.GetSelection()
  966. if index == wx.NOT_FOUND:
  967. self._hideAll()
  968. return
  969. cdata = self.listbox.GetClientData(index)
  970. self.hidevbox.Show(self.fontBox, (cdata['name'] in ('time', 'text')))
  971. self.hidevbox.Show(self.imageBox, (cdata['name'] == 'image'))
  972. self.hidevbox.Show(self.textBox, (cdata['name'] == 'text'))
  973. self.hidevbox.Show(self.posBox, True)
  974. self.hidevbox.Show(self.informBox, False)
  975. self.spinX.SetValue(cdata['pos'][0])
  976. self.spinY.SetValue(cdata['pos'][1])
  977. if cdata['name'] == 'image':
  978. self.browse.SetValue(cdata['file'])
  979. elif cdata['name'] in ('time', 'text'):
  980. self.sampleLabel.SetFont(cdata['font'])
  981. if cdata['name'] == 'text':
  982. self.textCtrl.SetValue(cdata['text'])
  983. self.hidevbox.Layout()
  984. # self.Layout()
  985. def OnText(self, event):
  986. index = self.listbox.GetSelection()
  987. # should not happen
  988. if index == wx.NOT_FOUND:
  989. return
  990. cdata = self.listbox.GetClientData(index)
  991. cdata['text'] = event.GetString()
  992. def OnRemove(self, event):
  993. index = self.listbox.GetSelection()
  994. if index == wx.NOT_FOUND:
  995. return
  996. decData = self.listbox.GetClientData(index)
  997. self.decorations.remove(decData)
  998. self._updateListBox()
  999. if self.listbox.GetCount():
  1000. self.listbox.SetSelection(0)
  1001. self.OnSelectionChanged(event = None)
  1002. def OnExport(self, event):
  1003. for decor in self.decorations:
  1004. if decor['name'] == 'image':
  1005. if not os.path.exists(decor['file']):
  1006. if decor['file']:
  1007. GError(parent = self, message = _("File %s not found.") % decor['file'])
  1008. else:
  1009. GError(parent = self, message = _("Decoration image file is missing."))
  1010. return
  1011. if self.formatChoice.GetSelection() == 0:
  1012. name = self.dirBrowse.GetValue()
  1013. if not os.path.exists(name):
  1014. if name:
  1015. GError(parent = self, message = _("Directory %s not found.") % name)
  1016. else:
  1017. GError(parent = self, message = _("Export directory is missing."))
  1018. return
  1019. elif self.formatChoice.GetSelection() == 1:
  1020. if not self.gifBrowse.GetValue():
  1021. GError(parent = self, message = _("Export file is missing."))
  1022. return
  1023. elif self.formatChoice.GetSelection() == 2:
  1024. if not self.swfBrowse.GetValue():
  1025. GError(parent = self, message = _("Export file is missing."))
  1026. return
  1027. # hide only to keep previous values
  1028. self.Hide()
  1029. self.doExport.emit(exportInfo=self.GetExportInformation(),
  1030. decorations=self.GetDecorations())
  1031. def GetDecorations(self):
  1032. return self.decorations
  1033. def GetExportInformation(self):
  1034. info = {}
  1035. if self.formatChoice.GetSelection() == 0:
  1036. info['method'] = 'sequence'
  1037. info['directory'] = self.dirBrowse.GetValue()
  1038. info['prefix'] = self.prefixCtrl.GetValue()
  1039. info['format'] = self.imSeqFormatChoice.GetClientData(self.imSeqFormatChoice.GetSelection())
  1040. elif self.formatChoice.GetSelection() == 1:
  1041. info['method'] = 'gif'
  1042. info['file'] = self.gifBrowse.GetValue()
  1043. elif self.formatChoice.GetSelection() == 2:
  1044. info['method'] = 'swf'
  1045. info['file'] = self.swfBrowse.GetValue()
  1046. elif self.formatChoice.GetSelection() == 3:
  1047. info['method'] = 'avi'
  1048. info['file'] = self.aviBrowse.GetValue()
  1049. info['encoding'] = self.encodingText.GetValue()
  1050. return info
  1051. def _updateListBox(self):
  1052. self.listbox.Clear()
  1053. names = {'time': _("Time stamp"), 'image': _("Image"), 'text': _("Text")}
  1054. for decor in self.decorations:
  1055. self.listbox.Append(names[decor['name']], clientData = decor)
  1056. def _hideAll(self):
  1057. self.hidevbox.Show(self.fontBox, False)
  1058. self.hidevbox.Show(self.imageBox, False)
  1059. self.hidevbox.Show(self.textBox, False)
  1060. self.hidevbox.Show(self.posBox, False)
  1061. self.hidevbox.Show(self.informBox, True)
  1062. self.hidevbox.Layout()
  1063. def test():
  1064. import wx.lib.inspection
  1065. import grass.script as grass
  1066. app = wx.PySimpleApp()
  1067. testExport()
  1068. # wx.lib.inspection.InspectionTool().Show()
  1069. app.MainLoop()
  1070. def testAnimInput():
  1071. anim = AnimationData()
  1072. anim.SetDefaultValues(animationIndex = 0, windowIndex = 0)
  1073. dlg = InputDialog(parent = None, mode = 'add', animationData = anim)
  1074. dlg.Show()
  1075. def testAnimEdit():
  1076. anim = AnimationData()
  1077. anim.SetDefaultValues(animationIndex = 0, windowIndex = 0)
  1078. dlg = EditDialog(parent = None, animationData = [anim])
  1079. dlg.Show()
  1080. def testExport():
  1081. dlg = ExportDialog(parent = None, temporal = TemporalMode.TEMPORAL,
  1082. timeTick = 200, visvis = True)
  1083. if dlg.ShowModal() == wx.ID_OK:
  1084. print dlg.GetDecorations()
  1085. print dlg.GetExportInformation()
  1086. dlg.Destroy()
  1087. else:
  1088. dlg.Destroy()
  1089. if __name__ == '__main__':
  1090. test()