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 utils import TemporalMode, validateTimeseriesName, validateMapNames
  31. from nviztask import NvizTask
  32. SpeedChangedEvent, EVT_SPEED_CHANGED = NewEvent()
  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. self.minimumDuration = minimumDuration
  40. # self.framesCount = framesCount
  41. self.defaultSpeed = initialSpeed
  42. self.lastAppliedValue = self.defaultSpeed
  43. self.lastAppliedValueTemp = self.defaultSpeed
  44. self._layout()
  45. self.temporalMode = temporalMode
  46. self.timeGranularity = timeGranularity
  47. self._fillUnitChoice(self.choiceUnits)
  48. self.InitTimeSpin(self.defaultSpeed)
  49. def SetTimeGranularity(self, gran):
  50. self._timeGranularity = gran
  51. def GetTimeGranularity(self):
  52. return self._timeGranularity
  53. timeGranularity = property(fset = SetTimeGranularity, fget = GetTimeGranularity)
  54. def SetTemporalMode(self, mode):
  55. self._temporalMode = mode
  56. self._setTemporalMode()
  57. def GetTemporalMode(self):
  58. return self._temporalMode
  59. temporalMode = property(fset = SetTemporalMode, fget = GetTemporalMode)
  60. def _layout(self):
  61. """!Layout window"""
  62. mainSizer = wx.BoxSizer(wx.VERTICAL)
  63. #
  64. # simple mode
  65. #
  66. self.nontemporalBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  67. label = ' %s ' % _("Simple mode"))
  68. box = wx.StaticBoxSizer(self.nontemporalBox, wx.VERTICAL)
  69. gridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  70. labelDuration = wx.StaticText(self, id = wx.ID_ANY, label = _("Frame duration:"))
  71. labelUnits = wx.StaticText(self, id = wx.ID_ANY, label = _("ms"))
  72. self.spinDuration = wx.SpinCtrl(self, id = wx.ID_ANY, min = self.minimumDuration,
  73. max = 10000, initial = self.defaultSpeed)
  74. # TODO total time
  75. gridSizer.Add(item = labelDuration, pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  76. gridSizer.Add(item = self.spinDuration, pos = (0, 1), flag = wx.ALIGN_CENTER)
  77. gridSizer.Add(item = labelUnits, pos = (0, 2), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  78. gridSizer.AddGrowableCol(0)
  79. box.Add(item = gridSizer, proportion = 1, border = 5, flag = wx.ALL | wx.EXPAND)
  80. self.nontemporalSizer = gridSizer
  81. mainSizer.Add(item = box, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
  82. #
  83. # temporal mode
  84. #
  85. self.temporalBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
  86. label = ' %s ' % _("Temporal mode"))
  87. box = wx.StaticBoxSizer(self.temporalBox, wx.VERTICAL)
  88. gridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  89. labelTimeUnit = wx.StaticText(self, id = wx.ID_ANY, label = _("Time unit:"))
  90. labelDuration = wx.StaticText(self, id = wx.ID_ANY, label = _("Duration of time unit:"))
  91. labelUnits = wx.StaticText(self, id = wx.ID_ANY, label = _("ms"))
  92. self.spinDurationTemp = wx.SpinCtrl(self, id = wx.ID_ANY, min = self.minimumDuration,
  93. max = 10000, initial = self.defaultSpeed)
  94. self.choiceUnits = wx.Choice(self, id = wx.ID_ANY)
  95. # TODO total time
  96. gridSizer.Add(item = labelTimeUnit, pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  97. gridSizer.Add(item = self.choiceUnits, pos = (0, 1), flag = wx.ALIGN_CENTER | wx.EXPAND)
  98. gridSizer.Add(item = labelDuration, pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  99. gridSizer.Add(item = self.spinDurationTemp, pos = (1, 1), flag = wx.ALIGN_CENTER | wx.EXPAND)
  100. gridSizer.Add(item = labelUnits, pos = (1, 2), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_LEFT)
  101. gridSizer.AddGrowableCol(1)
  102. self.temporalSizer = gridSizer
  103. box.Add(item = gridSizer, proportion = 1, border = 5, flag = wx.ALL | wx.EXPAND)
  104. mainSizer.Add(item = box, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
  105. self.btnOk = wx.Button(self, wx.ID_OK)
  106. self.btnApply = wx.Button(self, wx.ID_APPLY)
  107. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  108. self.btnOk.SetDefault()
  109. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  110. self.btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  111. self.btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
  112. self.Bind(wx.EVT_CLOSE, self.OnCancel)
  113. # button sizer
  114. btnStdSizer = wx.StdDialogButtonSizer()
  115. btnStdSizer.AddButton(self.btnOk)
  116. btnStdSizer.AddButton(self.btnApply)
  117. btnStdSizer.AddButton(self.btnCancel)
  118. btnStdSizer.Realize()
  119. mainSizer.Add(item = btnStdSizer, proportion = 0,
  120. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  121. self.SetSizer(mainSizer)
  122. mainSizer.Fit(self)
  123. def _setTemporalMode(self):
  124. self.nontemporalBox.Enable(self.temporalMode == TemporalMode.NONTEMPORAL)
  125. self.temporalBox.Enable(self.temporalMode == TemporalMode.TEMPORAL)
  126. for child in self.temporalSizer.GetChildren():
  127. child.GetWindow().Enable(self.temporalMode == TemporalMode.TEMPORAL)
  128. for child in self.nontemporalSizer.GetChildren():
  129. child.GetWindow().Enable(self.temporalMode == TemporalMode.NONTEMPORAL)
  130. self.Layout()
  131. def _fillUnitChoice(self, choiceWidget):
  132. timeUnitsChoice = [_("year"), _("month"), _("day"), _("hour"), _("minute"), _("second")]
  133. timeUnits = ["years", "months", "days", "hours", "minutes", "seconds"]
  134. for item, cdata in zip(timeUnitsChoice, timeUnits):
  135. choiceWidget.Append(item, cdata)
  136. if self.temporalMode == TemporalMode.TEMPORAL:
  137. unit = self.timeGranularity.split()[1]
  138. try:
  139. index = timeUnits.index(unit)
  140. except ValueError:
  141. index = 0
  142. choiceWidget.SetSelection(index)
  143. else:
  144. choiceWidget.SetSelection(0)
  145. def OnOk(self, event):
  146. self._apply()
  147. self.OnCancel(None)
  148. def OnApply(self, event):
  149. self._apply()
  150. def OnCancel(self, event):
  151. self.spinDuration.SetValue(self.lastAppliedValue)
  152. self.spinDurationTemp.SetValue(self.lastAppliedValueTemp)
  153. self.Hide()
  154. def InitTimeSpin(self, timeTick):
  155. if self.temporalMode == TemporalMode.TEMPORAL:
  156. index = self.choiceUnits.GetSelection()
  157. unit = self.choiceUnits.GetClientData(index)
  158. delta = self._timedelta(unit = unit, number = 1)
  159. seconds1 = self._total_seconds(delta)
  160. number, unit = self.timeGranularity.split()
  161. number = float(number)
  162. delta = self._timedelta(unit = unit, number = number)
  163. seconds2 = self._total_seconds(delta)
  164. value = timeTick
  165. ms = value * seconds1 / float(seconds2)
  166. self.spinDurationTemp.SetValue(ms)
  167. else:
  168. self.spinDuration.SetValue(timeTick)
  169. def _apply(self):
  170. if self.temporalMode == TemporalMode.NONTEMPORAL:
  171. ms = self.spinDuration.GetValue()
  172. self.lastAppliedValue = self.spinDuration.GetValue()
  173. elif self.temporalMode == TemporalMode.TEMPORAL:
  174. index = self.choiceUnits.GetSelection()
  175. unit = self.choiceUnits.GetClientData(index)
  176. delta = self._timedelta(unit = unit, number = 1)
  177. seconds1 = self._total_seconds(delta)
  178. number, unit = self.timeGranularity.split()
  179. number = float(number)
  180. delta = self._timedelta(unit = unit, number = number)
  181. seconds2 = self._total_seconds(delta)
  182. value = self.spinDurationTemp.GetValue()
  183. ms = value * seconds2 / float(seconds1)
  184. if ms < self.minimumDuration:
  185. GMessage(parent = self, message = _("Animation speed is too high."))
  186. return
  187. self.lastAppliedValueTemp = self.spinDurationTemp.GetValue()
  188. else:
  189. return
  190. event = SpeedChangedEvent(ms = ms)
  191. wx.PostEvent(self, event)
  192. def _timedelta(self, unit, number):
  193. if unit == "years":
  194. delta = datetime.timedelta(days = 365.25 * number)
  195. elif unit == "months":
  196. delta = datetime.timedelta(days = 30.4375 * number) # 365.25/12
  197. elif unit == "days":
  198. delta = datetime.timedelta(days = 1 * number)
  199. elif unit == "hours":
  200. delta = datetime.timedelta(hours = 1 * number)
  201. elif unit == "minutes":
  202. delta = datetime.timedelta(minutes = 1 * number)
  203. elif unit == "seconds":
  204. delta = datetime.timedelta(seconds = 1 * number)
  205. return delta
  206. def _total_seconds(self, delta):
  207. """!timedelta.total_seconds is new in version 2.7.
  208. """
  209. return delta.seconds + delta.days * 24 * 3600
  210. class InputDialog(wx.Dialog):
  211. def __init__(self, parent, mode, animationData):
  212. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY,
  213. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)
  214. if mode == 'add':
  215. self.SetTitle(_("Add new animation"))
  216. elif mode == 'edit':
  217. self.SetTitle(_("Edit animation"))
  218. self.animationData = animationData
  219. self._layout()
  220. self.OnViewMode(event = None)
  221. def _layout(self):
  222. mainSizer = wx.BoxSizer(wx.VERTICAL)
  223. self.windowChoice = wx.Choice(self, id = wx.ID_ANY,
  224. choices = [_("top left"), _("top right"),
  225. _("bottom left"), _("bottom right")])
  226. self.windowChoice.SetSelection(self.animationData.windowIndex)
  227. self.nameCtrl = wx.TextCtrl(self, id = wx.ID_ANY, value = self.animationData.name)
  228. self.nDChoice = wx.Choice(self, id = wx.ID_ANY)
  229. mode = self.animationData.viewMode
  230. index = 0
  231. for i, (viewMode, viewModeName) in enumerate(self.animationData.viewModes):
  232. self.nDChoice.Append(viewModeName, clientData = viewMode)
  233. if mode == viewMode:
  234. index = i
  235. self.nDChoice.SetSelection(index)
  236. # TODO
  237. self.nDChoice.SetToolTipString(_(""))
  238. self.nDChoice.Bind(wx.EVT_CHOICE, self.OnViewMode)
  239. gridSizer = wx.FlexGridSizer(cols = 2, hgap = 5, vgap = 5)
  240. gridSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _("Name:")),
  241. flag = wx.ALIGN_CENTER_VERTICAL)
  242. gridSizer.Add(item = self.nameCtrl, proportion = 1, flag = wx.EXPAND)
  243. gridSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _("Window position:")),
  244. flag = wx.ALIGN_CENTER_VERTICAL)
  245. gridSizer.Add(item = self.windowChoice, proportion = 1, flag = wx.ALIGN_RIGHT)
  246. gridSizer.Add(item = wx.StaticText(self, id = wx.ID_ANY, label = _("View mode:")),
  247. flag = wx.ALIGN_CENTER_VERTICAL)
  248. gridSizer.Add(item = self.nDChoice, proportion = 1, flag = wx.ALIGN_RIGHT)
  249. gridSizer.AddGrowableCol(0, 1)
  250. gridSizer.AddGrowableCol(1, 1)
  251. mainSizer.Add(item = gridSizer, proportion = 1, flag = wx.ALL | wx.EXPAND, border = 5)
  252. self.dataPanel = self._createDataPanel()
  253. self.threeDPanel = self._create3DPanel()
  254. mainSizer.Add(item = self.dataPanel, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  255. mainSizer.Add(item = self.threeDPanel, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  256. # buttons
  257. self.btnOk = wx.Button(self, wx.ID_OK)
  258. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  259. self.btnOk.SetDefault()
  260. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  261. # button sizer
  262. btnStdSizer = wx.StdDialogButtonSizer()
  263. btnStdSizer.AddButton(self.btnOk)
  264. btnStdSizer.AddButton(self.btnCancel)
  265. btnStdSizer.Realize()
  266. mainSizer.Add(item = btnStdSizer, proportion = 0,
  267. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  268. self.SetSizer(mainSizer)
  269. mainSizer.Fit(self)
  270. def _createDataPanel(self):
  271. panel = wx.Panel(self, id = wx.ID_ANY)
  272. dataStBox = wx.StaticBox(parent = panel, id = wx.ID_ANY,
  273. label = ' %s ' % _("Data"))
  274. dataBoxSizer = wx.StaticBoxSizer(dataStBox, wx.VERTICAL)
  275. self.dataChoice = wx.Choice(panel, id = wx.ID_ANY)
  276. self._setMapTypes()
  277. self.dataChoice.Bind(wx.EVT_CHOICE, self.OnDataType)
  278. self.dataSelect = gselect.Select(parent = panel, id = wx.ID_ANY,
  279. size = globalvar.DIALOG_GSELECT_SIZE)
  280. iconTheme = UserSettings.Get(group = 'appearance', key = 'iconTheme', subkey = 'type')
  281. bitmapPath = os.path.join(globalvar.ETCICONDIR, iconTheme, 'layer-open.png')
  282. if os.path.isfile(bitmapPath) and os.path.getsize(bitmapPath):
  283. bitmap = wx.Bitmap(name = bitmapPath)
  284. else:
  285. bitmap = wx.ArtProvider.GetBitmap(id = wx.ART_MISSING_IMAGE, client = wx.ART_TOOLBAR)
  286. self.addManyMapsButton = wx.BitmapButton(panel, id = wx.ID_ANY, bitmap = bitmap)
  287. self.addManyMapsButton.Bind(wx.EVT_BUTTON, self.OnAddMaps)
  288. self.OnDataType(None)
  289. if self.animationData.inputData is None:
  290. self.dataSelect.SetValue('')
  291. else:
  292. self.dataSelect.SetValue(self.animationData.inputData)
  293. hbox = wx.BoxSizer(wx.HORIZONTAL)
  294. hbox.Add(item = wx.StaticText(panel, wx.ID_ANY, label = _("Input data type:")),
  295. proportion = 1, flag = wx.ALIGN_CENTER_VERTICAL)
  296. hbox.Add(item = self.dataChoice, proportion = 1, flag = wx.EXPAND)
  297. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  298. hbox = wx.BoxSizer(wx.HORIZONTAL)
  299. # hbox.Add(item = wx.StaticText(panel, wx.ID_ANY, label = _("Input data:")),
  300. # proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL)
  301. hbox.Add(item = self.dataSelect, proportion = 1, flag = wx.ALIGN_CENTER)
  302. hbox.Add(item = self.addManyMapsButton, proportion = 0, flag = wx.LEFT, border = 5)
  303. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  304. panel.SetSizerAndFit(dataBoxSizer)
  305. panel.SetAutoLayout(True)
  306. return panel
  307. def _create3DPanel(self):
  308. panel = wx.Panel(self, id = wx.ID_ANY)
  309. dataStBox = wx.StaticBox(parent = panel, id = wx.ID_ANY,
  310. label = ' %s ' % _("3D view parameters"))
  311. dataBoxSizer = wx.StaticBoxSizer(dataStBox, wx.VERTICAL)
  312. # workspace file
  313. self.fileSelector = filebrowse.FileBrowseButton(parent = panel, id = wx.ID_ANY,
  314. size = globalvar.DIALOG_GSELECT_SIZE,
  315. labelText = _("Workspace file:"),
  316. dialogTitle = _("Choose workspace file to import 3D view parameters"),
  317. buttonText = _('Browse'),
  318. startDirectory = os.getcwd(), fileMode = 0,
  319. fileMask = "GRASS Workspace File (*.gxw)|*.gxw")
  320. if self.animationData.workspaceFile:
  321. self.fileSelector.SetValue(self.animationData.workspaceFile)
  322. self.paramLabel = wx.StaticText(panel, wx.ID_ANY, label = _("Parameter for animation:"))
  323. self.paramChoice = wx.Choice(panel, id = wx.ID_ANY, choices = self.animationData.nvizParameters)
  324. self.paramChoice.SetStringSelection(self.animationData.nvizParameter)
  325. hbox = wx.BoxSizer(wx.HORIZONTAL)
  326. hbox.Add(item = self.fileSelector, proportion = 1, flag = wx.EXPAND | wx.ALIGN_CENTER)
  327. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  328. hbox = wx.BoxSizer(wx.HORIZONTAL)
  329. hbox.Add(item = self.paramLabel, proportion = 1, flag = wx.ALIGN_CENTER_VERTICAL)
  330. hbox.Add(item = self.paramChoice, proportion = 1, flag = wx.EXPAND)
  331. dataBoxSizer.Add(item = hbox, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  332. panel.SetSizerAndFit(dataBoxSizer)
  333. panel.SetAutoLayout(True)
  334. return panel
  335. def _setMapTypes(self, view2d = True):
  336. index = 0
  337. if view2d:
  338. inputTypes = self.animationData.inputMapTypes[::2]
  339. else:
  340. inputTypes = self.animationData.inputMapTypes
  341. self.dataChoice.Clear()
  342. for i, (itype, itypeName) in enumerate(inputTypes):
  343. self.dataChoice.Append(itypeName, clientData = itype)
  344. if itype == self.animationData.inputMapType:
  345. index = i
  346. self.dataChoice.SetSelection(index)
  347. def OnViewMode(self, event):
  348. mode = self.nDChoice.GetSelection()
  349. self.Freeze()
  350. sizer = self.threeDPanel.GetContainingSizer()
  351. sizer.Show(self.threeDPanel, mode != 0, True)
  352. sizer.Layout()
  353. self._setMapTypes(mode == 0)
  354. self.Layout()
  355. self.Fit()
  356. self.Thaw()
  357. def OnDataType(self, event):
  358. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  359. if etype in ('rast', 'vect'):
  360. self.dataSelect.SetType(etype = etype, multiple = True)
  361. self.addManyMapsButton.Enable(True)
  362. else:
  363. self.dataSelect.SetType(etype = etype, multiple = False)
  364. self.addManyMapsButton.Enable(False)
  365. self.dataSelect.SetValue('')
  366. def OnAddMaps(self, event):
  367. # TODO: fix dialog
  368. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  369. index = 0
  370. if etype == 'vect':
  371. index = 2
  372. dlg = MapLayersDialog(self, title = _("Select raster maps for animation"))
  373. dlg.applyAddingMapLayers.connect(lambda mapLayers:
  374. self.dataSelect.SetValue(','.join(mapLayers)))
  375. dlg.layerType.SetSelection(index)
  376. dlg.LoadMapLayers(dlg.GetLayerType(cmd = True),
  377. dlg.mapset.GetStringSelection())
  378. if dlg.ShowModal() == wx.ID_OK:
  379. self.dataSelect.SetValue(','.join(dlg.GetMapLayers()))
  380. dlg.Destroy()
  381. def _update(self):
  382. self.animationData.name = self.nameCtrl.GetValue()
  383. self.animationData.windowIndex = self.windowChoice.GetSelection()
  384. sel = self.dataChoice.GetSelection()
  385. self.animationData.inputMapType = self.dataChoice.GetClientData(sel)
  386. self.animationData.inputData = self.dataSelect.GetValue()
  387. sel = self.nDChoice.GetSelection()
  388. self.animationData.viewMode = self.nDChoice.GetClientData(sel)
  389. if self.threeDPanel.IsShown():
  390. self.animationData.workspaceFile = self.fileSelector.GetValue()
  391. if self.threeDPanel.IsShown():
  392. self.animationData.nvizParameter = self.paramChoice.GetStringSelection()
  393. def OnOk(self, event):
  394. try:
  395. self._update()
  396. self.EndModal(wx.ID_OK)
  397. except (GException, ValueError, IOError) as e:
  398. GError(message = str(e), showTraceback = False, caption = _("Invalid input"))
  399. class EditDialog(wx.Dialog):
  400. def __init__(self, parent, evalFunction, animationData, maxAnimations):
  401. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY,
  402. style = wx.DEFAULT_DIALOG_STYLE)
  403. self.animationData = copy.deepcopy(animationData)
  404. self.eval = evalFunction
  405. self.SetTitle(_("Add, edit or remove animations"))
  406. self._layout()
  407. self.SetSize((300, -1))
  408. self.maxAnimations = maxAnimations
  409. self.result = None
  410. def _layout(self):
  411. mainSizer = wx.BoxSizer(wx.VERTICAL)
  412. box = wx.StaticBox (parent = self, id = wx.ID_ANY, label = " %s " % _("List of animations"))
  413. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  414. gridBagSizer = wx.GridBagSizer (hgap = 5, vgap = 5)
  415. gridBagSizer.AddGrowableCol(0)
  416. # gridBagSizer.AddGrowableCol(1,1)
  417. self.listbox = wx.ListBox(self, id = wx.ID_ANY, choices = [], style = wx.LB_SINGLE|wx.LB_NEEDED_SB)
  418. self.listbox.Bind(wx.EVT_LISTBOX_DCLICK, self.OnEdit)
  419. self.addButton = wx.Button(self, id = wx.ID_ANY, label = _("Add"))
  420. self.addButton.Bind(wx.EVT_BUTTON, self.OnAdd)
  421. self.editButton = wx.Button(self, id = wx.ID_ANY, label = _("Edit"))
  422. self.editButton.Bind(wx.EVT_BUTTON, self.OnEdit)
  423. self.removeButton = wx.Button(self, id = wx.ID_ANY, label = _("Remove"))
  424. self.removeButton.Bind(wx.EVT_BUTTON, self.OnRemove)
  425. self._updateListBox()
  426. gridBagSizer.Add(self.listbox, pos = (0,0), span = (3, 1), flag = wx.ALIGN_CENTER_VERTICAL| wx.EXPAND, border = 0)
  427. gridBagSizer.Add(self.addButton, pos = (0,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  428. gridBagSizer.Add(self.editButton, pos = (1,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  429. gridBagSizer.Add(self.removeButton, pos = (2,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  430. sizer.Add(gridBagSizer, proportion = 0, flag = wx.ALL | wx.EXPAND, border = 5)
  431. mainSizer.Add(item = sizer, proportion = 0,
  432. flag = wx.EXPAND | wx.ALL, border = 5)
  433. # buttons
  434. self.btnOk = wx.Button(self, wx.ID_OK)
  435. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  436. self.btnOk.SetDefault()
  437. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  438. # button sizer
  439. btnStdSizer = wx.StdDialogButtonSizer()
  440. btnStdSizer.AddButton(self.btnOk)
  441. btnStdSizer.AddButton(self.btnCancel)
  442. btnStdSizer.Realize()
  443. mainSizer.Add(item = btnStdSizer, proportion = 0,
  444. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  445. self.SetSizer(mainSizer)
  446. mainSizer.Fit(self)
  447. def _updateListBox(self):
  448. self.listbox.Clear()
  449. for anim in self.animationData:
  450. self.listbox.Append(anim.name, clientData = anim)
  451. self.listbox.SetSelection(0)
  452. def _getNextIndex(self):
  453. indices = [anim.windowIndex for anim in self.animationData]
  454. for i in range(self.maxAnimations):
  455. if i not in indices:
  456. return i
  457. return None
  458. def OnAdd(self, event):
  459. windowIndex = self._getNextIndex()
  460. if windowIndex is None:
  461. GMessage(self, message = _("Maximum number of animations is %d.") % self.maxAnimations)
  462. return
  463. animData = AnimationData()
  464. # number of active animations
  465. animationIndex = len(self.animationData)
  466. animData.SetDefaultValues(windowIndex, animationIndex)
  467. dlg = InputDialog(parent = self, mode = 'add', animationData = animData)
  468. if dlg.ShowModal() == wx.ID_CANCEL:
  469. dlg.Destroy()
  470. return
  471. dlg.Destroy()
  472. self.animationData.append(animData)
  473. self._updateListBox()
  474. def OnEdit(self, event):
  475. index = self.listbox.GetSelection()
  476. if index == wx.NOT_FOUND:
  477. return
  478. animData = self.listbox.GetClientData(index)
  479. dlg = InputDialog(parent = self, mode = 'edit', animationData = animData)
  480. if dlg.ShowModal() == wx.ID_CANCEL:
  481. dlg.Destroy()
  482. return
  483. dlg.Destroy()
  484. self._updateListBox()
  485. def OnRemove(self, event):
  486. index = self.listbox.GetSelection()
  487. if index == wx.NOT_FOUND:
  488. return
  489. animData = self.listbox.GetClientData(index)
  490. self.animationData.remove(animData)
  491. self._updateListBox()
  492. def GetResult(self):
  493. return self.result
  494. def OnOk(self, event):
  495. indices = set([anim.windowIndex for anim in self.animationData])
  496. if len(indices) != len(self.animationData):
  497. GError(parent = self, message = _("More animations are using one window."
  498. " Please select different window for each animation."))
  499. return
  500. try:
  501. temporalMode, tempManager = self.eval(self.animationData)
  502. except GException, e:
  503. GError(parent = self, message = e.value, showTraceback = False)
  504. return
  505. self.result = (self.animationData, temporalMode, tempManager)
  506. self.EndModal(wx.ID_OK)
  507. class AnimationData(object):
  508. def __init__(self):
  509. self._inputMapTypes = [('rast', _("Multiple raster maps")),
  510. ('vect', _("Multiple vector maps")),
  511. ('strds', _("Space time raster dataset")),
  512. ('stvds', _("Space time vector dataset"))]
  513. self._inputMapType = 'rast'
  514. self.inputData = None
  515. self.mapData = None
  516. self._viewModes = [('2d', _("2D view")),
  517. ('3d', _("3D view"))]
  518. self.viewMode = '2d'
  519. self.nvizTask = NvizTask()
  520. self._nvizParameters = self.nvizTask.ListMapParameters()
  521. self.nvizParameter = self._nvizParameters[0]
  522. self.workspaceFile = None
  523. def GetName(self):
  524. return self._name
  525. def SetName(self, name):
  526. if name == '':
  527. raise ValueError(_("No animation name selected."))
  528. self._name = name
  529. name = property(fget = GetName, fset = SetName)
  530. def GetWindowIndex(self):
  531. return self._windowIndex
  532. def SetWindowIndex(self, windowIndex):
  533. self._windowIndex = windowIndex
  534. windowIndex = property(fget = GetWindowIndex, fset = SetWindowIndex)
  535. def GetInputMapTypes(self):
  536. return self._inputMapTypes
  537. inputMapTypes = property(fget = GetInputMapTypes)
  538. def GetInputMapType(self):
  539. return self._inputMapType
  540. def SetInputMapType(self, itype):
  541. if itype in [each[0] for each in self.inputMapTypes]:
  542. self._inputMapType = itype
  543. else:
  544. raise ValueError("Bad input type.")
  545. inputMapType = property(fget = GetInputMapType, fset = SetInputMapType)
  546. def GetInputData(self):
  547. return self._inputData
  548. def SetInputData(self, data):
  549. if data == '':
  550. raise ValueError(_("No data selected."))
  551. if data is None:
  552. self._inputData = data
  553. return
  554. if self.inputMapType in ('rast', 'vect'):
  555. maps = data.split(',')
  556. newNames = validateMapNames(maps, self.inputMapType)
  557. self._inputData = ','.join(newNames)
  558. self.mapData = newNames
  559. elif self.inputMapType in ('strds', 'stvds'):
  560. timeseries = validateTimeseriesName(data, etype = self.inputMapType)
  561. if self.inputMapType == 'strds':
  562. sp = tgis.SpaceTimeRasterDataset(ident = timeseries)
  563. elif self.inputMapType == 'stvds':
  564. sp = tgis.SpaceTimeRasterDataset(ident = timeseries)
  565. # else ?
  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 gettext
  1011. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  1012. import grass.script as grass
  1013. app = wx.PySimpleApp()
  1014. testExport()
  1015. # wx.lib.inspection.InspectionTool().Show()
  1016. app.MainLoop()
  1017. def testAnimInput():
  1018. anim = AnimationData()
  1019. anim.SetDefaultValues(animationIndex = 0, windowIndex = 0)
  1020. dlg = InputDialog(parent = None, mode = 'add', animationData = anim)
  1021. dlg.Show()
  1022. def testAnimEdit():
  1023. anim = AnimationData()
  1024. anim.SetDefaultValues(animationIndex = 0, windowIndex = 0)
  1025. dlg = EditDialog(parent = None, animationData = [anim])
  1026. dlg.Show()
  1027. def testExport():
  1028. dlg = ExportDialog(parent = None, temporal = TemporalMode.TEMPORAL,
  1029. timeTick = 200, visvis = True)
  1030. if dlg.ShowModal() == wx.ID_OK:
  1031. print dlg.GetDecorations()
  1032. print dlg.GetExportInformation()
  1033. dlg.Destroy()
  1034. else:
  1035. dlg.Destroy()
  1036. if __name__ == '__main__':
  1037. test()