dialogs.py 53 KB

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