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