dialogs.py 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. inputTypes = self.animationData.inputMapTypes
  339. self.dataChoice.Clear()
  340. for i, (itype, itypeName) in enumerate(inputTypes):
  341. self.dataChoice.Append(itypeName, clientData = itype)
  342. if itype == self.animationData.inputMapType:
  343. index = i
  344. self.dataChoice.SetSelection(index)
  345. def OnViewMode(self, event):
  346. mode = self.nDChoice.GetSelection()
  347. self.Freeze()
  348. sizer = self.threeDPanel.GetContainingSizer()
  349. sizer.Show(self.threeDPanel, mode != 0, True)
  350. sizer.Layout()
  351. self._setMapTypes(mode == 0)
  352. self.Layout()
  353. self.Fit()
  354. self.Thaw()
  355. def OnDataType(self, event):
  356. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  357. if etype in ('rast', 'vect'):
  358. self.dataSelect.SetType(etype = etype, multiple = True)
  359. self.addManyMapsButton.Enable(True)
  360. else:
  361. self.dataSelect.SetType(etype = etype, multiple = False)
  362. self.addManyMapsButton.Enable(False)
  363. self.dataSelect.SetValue('')
  364. def OnAddMaps(self, event):
  365. # TODO: fix dialog
  366. etype = self.dataChoice.GetClientData(self.dataChoice.GetSelection())
  367. index = 0
  368. if etype == 'vect':
  369. index = 2
  370. dlg = MapLayersDialog(self, title = _("Select raster maps for animation"))
  371. dlg.applyAddingMapLayers.connect(lambda mapLayers:
  372. self.dataSelect.SetValue(','.join(mapLayers)))
  373. dlg.layerType.SetSelection(index)
  374. dlg.LoadMapLayers(dlg.GetLayerType(cmd = True),
  375. dlg.mapset.GetStringSelection())
  376. if dlg.ShowModal() == wx.ID_OK:
  377. self.dataSelect.SetValue(','.join(dlg.GetMapLayers()))
  378. dlg.Destroy()
  379. def _update(self):
  380. self.animationData.name = self.nameCtrl.GetValue()
  381. self.animationData.windowIndex = self.windowChoice.GetSelection()
  382. sel = self.dataChoice.GetSelection()
  383. self.animationData.inputMapType = self.dataChoice.GetClientData(sel)
  384. self.animationData.inputData = self.dataSelect.GetValue()
  385. sel = self.nDChoice.GetSelection()
  386. self.animationData.viewMode = self.nDChoice.GetClientData(sel)
  387. if self.threeDPanel.IsShown():
  388. self.animationData.workspaceFile = self.fileSelector.GetValue()
  389. if self.threeDPanel.IsShown():
  390. self.animationData.nvizParameter = self.paramChoice.GetStringSelection()
  391. def OnOk(self, event):
  392. try:
  393. self._update()
  394. self.EndModal(wx.ID_OK)
  395. except (GException, ValueError, IOError) as e:
  396. GError(message = str(e), showTraceback = False, caption = _("Invalid input"))
  397. class EditDialog(wx.Dialog):
  398. def __init__(self, parent, evalFunction, animationData, maxAnimations):
  399. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY,
  400. style = wx.DEFAULT_DIALOG_STYLE)
  401. self.animationData = copy.deepcopy(animationData)
  402. self.eval = evalFunction
  403. self.SetTitle(_("Add, edit or remove animations"))
  404. self._layout()
  405. self.SetSize((300, -1))
  406. self.maxAnimations = maxAnimations
  407. self.result = None
  408. def _layout(self):
  409. mainSizer = wx.BoxSizer(wx.VERTICAL)
  410. box = wx.StaticBox (parent = self, id = wx.ID_ANY, label = " %s " % _("List of animations"))
  411. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  412. gridBagSizer = wx.GridBagSizer (hgap = 5, vgap = 5)
  413. gridBagSizer.AddGrowableCol(0)
  414. # gridBagSizer.AddGrowableCol(1,1)
  415. self.listbox = wx.ListBox(self, id = wx.ID_ANY, choices = [], style = wx.LB_SINGLE|wx.LB_NEEDED_SB)
  416. self.listbox.Bind(wx.EVT_LISTBOX_DCLICK, self.OnEdit)
  417. self.addButton = wx.Button(self, id = wx.ID_ANY, label = _("Add"))
  418. self.addButton.Bind(wx.EVT_BUTTON, self.OnAdd)
  419. self.editButton = wx.Button(self, id = wx.ID_ANY, label = _("Edit"))
  420. self.editButton.Bind(wx.EVT_BUTTON, self.OnEdit)
  421. self.removeButton = wx.Button(self, id = wx.ID_ANY, label = _("Remove"))
  422. self.removeButton.Bind(wx.EVT_BUTTON, self.OnRemove)
  423. self._updateListBox()
  424. gridBagSizer.Add(self.listbox, pos = (0,0), span = (3, 1), flag = wx.ALIGN_CENTER_VERTICAL| wx.EXPAND, border = 0)
  425. gridBagSizer.Add(self.addButton, pos = (0,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  426. gridBagSizer.Add(self.editButton, pos = (1,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  427. gridBagSizer.Add(self.removeButton, pos = (2,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  428. sizer.Add(gridBagSizer, proportion = 0, flag = wx.ALL | wx.EXPAND, border = 5)
  429. mainSizer.Add(item = sizer, proportion = 0,
  430. flag = wx.EXPAND | wx.ALL, border = 5)
  431. # buttons
  432. self.btnOk = wx.Button(self, wx.ID_OK)
  433. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  434. self.btnOk.SetDefault()
  435. self.btnOk.Bind(wx.EVT_BUTTON, self.OnOk)
  436. # button sizer
  437. btnStdSizer = wx.StdDialogButtonSizer()
  438. btnStdSizer.AddButton(self.btnOk)
  439. btnStdSizer.AddButton(self.btnCancel)
  440. btnStdSizer.Realize()
  441. mainSizer.Add(item = btnStdSizer, proportion = 0,
  442. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  443. self.SetSizer(mainSizer)
  444. mainSizer.Fit(self)
  445. def _updateListBox(self):
  446. self.listbox.Clear()
  447. for anim in self.animationData:
  448. self.listbox.Append(anim.name, clientData = anim)
  449. self.listbox.SetSelection(0)
  450. def _getNextIndex(self):
  451. indices = [anim.windowIndex for anim in self.animationData]
  452. for i in range(self.maxAnimations):
  453. if i not in indices:
  454. return i
  455. return None
  456. def OnAdd(self, event):
  457. windowIndex = self._getNextIndex()
  458. if windowIndex is None:
  459. GMessage(self, message = _("Maximum number of animations is %d.") % self.maxAnimations)
  460. return
  461. animData = AnimationData()
  462. # number of active animations
  463. animationIndex = len(self.animationData)
  464. animData.SetDefaultValues(windowIndex, animationIndex)
  465. dlg = InputDialog(parent = self, mode = 'add', animationData = animData)
  466. if dlg.ShowModal() == wx.ID_CANCEL:
  467. dlg.Destroy()
  468. return
  469. dlg.Destroy()
  470. self.animationData.append(animData)
  471. self._updateListBox()
  472. def OnEdit(self, event):
  473. index = self.listbox.GetSelection()
  474. if index == wx.NOT_FOUND:
  475. return
  476. animData = self.listbox.GetClientData(index)
  477. dlg = InputDialog(parent = self, mode = 'edit', animationData = animData)
  478. if dlg.ShowModal() == wx.ID_CANCEL:
  479. dlg.Destroy()
  480. return
  481. dlg.Destroy()
  482. self._updateListBox()
  483. def OnRemove(self, event):
  484. index = self.listbox.GetSelection()
  485. if index == wx.NOT_FOUND:
  486. return
  487. animData = self.listbox.GetClientData(index)
  488. self.animationData.remove(animData)
  489. self._updateListBox()
  490. def GetResult(self):
  491. return self.result
  492. def OnOk(self, event):
  493. indices = set([anim.windowIndex for anim in self.animationData])
  494. if len(indices) != len(self.animationData):
  495. GError(parent = self, message = _("More animations are using one window."
  496. " Please select different window for each animation."))
  497. return
  498. try:
  499. temporalMode, tempManager = self.eval(self.animationData)
  500. except GException, e:
  501. GError(parent = self, message = e.value, showTraceback = False)
  502. return
  503. self.result = (self.animationData, temporalMode, tempManager)
  504. self.EndModal(wx.ID_OK)
  505. class AnimationData(object):
  506. def __init__(self):
  507. self._inputMapTypes = [('rast', _("Multiple raster maps")),
  508. ('vect', _("Multiple vector maps")),
  509. ('strds', _("Space time raster dataset")),
  510. ('stvds', _("Space time vector dataset"))]
  511. self._inputMapType = 'rast'
  512. self.inputData = None
  513. self.mapData = None
  514. self._viewModes = [('2d', _("2D view")),
  515. ('3d', _("3D view"))]
  516. self.viewMode = '2d'
  517. self.nvizTask = NvizTask()
  518. self._nvizParameters = self.nvizTask.ListMapParameters()
  519. self.nvizParameter = self._nvizParameters[0]
  520. self.workspaceFile = None
  521. def GetName(self):
  522. return self._name
  523. def SetName(self, name):
  524. if name == '':
  525. raise ValueError(_("No animation name selected."))
  526. self._name = name
  527. name = property(fget = GetName, fset = SetName)
  528. def GetWindowIndex(self):
  529. return self._windowIndex
  530. def SetWindowIndex(self, windowIndex):
  531. self._windowIndex = windowIndex
  532. windowIndex = property(fget = GetWindowIndex, fset = SetWindowIndex)
  533. def GetInputMapTypes(self):
  534. return self._inputMapTypes
  535. inputMapTypes = property(fget = GetInputMapTypes)
  536. def GetInputMapType(self):
  537. return self._inputMapType
  538. def SetInputMapType(self, itype):
  539. if itype in [each[0] for each in self.inputMapTypes]:
  540. self._inputMapType = itype
  541. else:
  542. raise ValueError("Bad input type.")
  543. inputMapType = property(fget = GetInputMapType, fset = SetInputMapType)
  544. def GetInputData(self):
  545. return self._inputData
  546. def SetInputData(self, data):
  547. if data == '':
  548. raise ValueError(_("No data selected."))
  549. if data is None:
  550. self._inputData = data
  551. return
  552. if self.inputMapType in ('rast', 'vect'):
  553. maps = data.split(',')
  554. newNames = validateMapNames(maps, self.inputMapType)
  555. self._inputData = ','.join(newNames)
  556. self.mapData = newNames
  557. elif self.inputMapType in ('strds', 'stvds'):
  558. timeseries = validateTimeseriesName(data, etype = self.inputMapType)
  559. if self.inputMapType == 'strds':
  560. sp = tgis.SpaceTimeRasterDataset(ident = timeseries)
  561. elif self.inputMapType == 'stvds':
  562. sp = tgis.SpaceTimeVectorDataset(ident = timeseries)
  563. if sp.is_in_db() == False:
  564. raise GException(_("Space time dataset <%s> not found.") % timeseries)
  565. sp.select()
  566. rows = sp.get_registered_maps(columns = "id", where = None, order = "start_time", dbif = None)
  567. timeseriesMaps = []
  568. if rows:
  569. for row in rows:
  570. timeseriesMaps.append(row["id"])
  571. self._inputData = timeseries
  572. self.mapData = timeseriesMaps
  573. else:
  574. self._inputData = data
  575. inputData = property(fget = GetInputData, fset = SetInputData)
  576. def SetMapData(self, data):
  577. self._mapData = data
  578. def GetMapData(self):
  579. return self._mapData
  580. mapData = property(fget = GetMapData, fset = SetMapData)
  581. def GetWorkspaceFile(self):
  582. return self._workspaceFile
  583. def SetWorkspaceFile(self, fileName):
  584. if fileName is None:
  585. self._workspaceFile = None
  586. return
  587. if fileName == '':
  588. raise ValueError(_("No workspace file selected."))
  589. if not os.path.exists(fileName):
  590. raise IOError(_("File %s not found") % fileName)
  591. self._workspaceFile = fileName
  592. self.nvizTask.Load(self.workspaceFile)
  593. workspaceFile = property(fget = GetWorkspaceFile, fset = SetWorkspaceFile)
  594. def SetDefaultValues(self, windowIndex, animationIndex):
  595. self.windowIndex = windowIndex
  596. self.name = _("Animation %d") % (animationIndex + 1)
  597. def GetNvizParameters(self):
  598. return self._nvizParameters
  599. nvizParameters = property(fget = GetNvizParameters)
  600. def GetNvizParameter(self):
  601. return self._nvizParameter
  602. def SetNvizParameter(self, param):
  603. self._nvizParameter = param
  604. nvizParameter = property(fget = GetNvizParameter, fset = SetNvizParameter)
  605. def GetViewMode(self):
  606. return self._viewMode
  607. def SetViewMode(self, mode):
  608. self._viewMode = mode
  609. viewMode = property(fget = GetViewMode, fset = SetViewMode)
  610. def GetViewModes(self):
  611. return self._viewModes
  612. viewModes = property(fget = GetViewModes)
  613. def GetNvizCommands(self):
  614. if not self.workspaceFile or not self.mapData:
  615. return []
  616. cmds = self.nvizTask.GetCommandSeries(series = self.mapData, paramName = self.nvizParameter)
  617. region = self.nvizTask.GetRegion()
  618. return {'commands': cmds, 'region': region}
  619. def __repr__(self):
  620. return "%s(%r)" % (self.__class__, self.__dict__)
  621. class ExportDialog(wx.Dialog):
  622. def __init__(self, parent, temporal, timeTick, visvis):
  623. wx.Dialog.__init__(self, parent = parent, id = wx.ID_ANY, title = _("Export animation"),
  624. style = wx.DEFAULT_DIALOG_STYLE)
  625. self.decorations = []
  626. self.temporal = temporal
  627. self.timeTick = timeTick
  628. self.visvis = visvis
  629. self._layout()
  630. wx.CallAfter(self._hideAll)
  631. def _layout(self):
  632. notebook = wx.Notebook(self, id = wx.ID_ANY)
  633. mainSizer = wx.BoxSizer(wx.VERTICAL)
  634. notebook.AddPage(page = self._createExportFormatPanel(notebook), text = _("Format"))
  635. notebook.AddPage(page = self._createDecorationsPanel(notebook), text = _("Decorations"))
  636. mainSizer.Add(item = notebook, proportion = 0,
  637. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  638. self.btnExport = wx.Button(self, wx.ID_OK)
  639. self.btnExport.SetLabel(_("Export"))
  640. self.btnCancel = wx.Button(self, wx.ID_CANCEL)
  641. self.btnExport.SetDefault()
  642. self.btnExport.Bind(wx.EVT_BUTTON, self.OnExport)
  643. # button sizer
  644. btnStdSizer = wx.StdDialogButtonSizer()
  645. btnStdSizer.AddButton(self.btnExport)
  646. btnStdSizer.AddButton(self.btnCancel)
  647. btnStdSizer.Realize()
  648. mainSizer.Add(item = btnStdSizer, proportion = 0,
  649. flag = wx.EXPAND | wx.ALL | wx.ALIGN_RIGHT, border = 5)
  650. self.SetSizer(mainSizer)
  651. # set the longest option to fit
  652. self.hidevbox.Show(self.fontBox, True)
  653. self.hidevbox.Show(self.imageBox, False)
  654. self.hidevbox.Show(self.textBox, True)
  655. self.hidevbox.Show(self.posBox, True)
  656. self.hidevbox.Show(self.informBox, False)
  657. mainSizer.Fit(self)
  658. def _createDecorationsPanel(self, notebook):
  659. panel = wx.Panel(notebook, id = wx.ID_ANY)
  660. sizer = wx.BoxSizer(wx.VERTICAL)
  661. sizer.Add(self._createDecorationsList(panel), proportion = 0, flag = wx.ALL | wx.EXPAND, border = 10)
  662. sizer.Add(self._createDecorationsProperties(panel), proportion = 0, flag = wx.ALL | wx.EXPAND, border = 10)
  663. panel.SetSizer(sizer)
  664. sizer.Fit(panel)
  665. return panel
  666. def _createDecorationsList(self, panel):
  667. gridBagSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  668. gridBagSizer.AddGrowableCol(0)
  669. self.listbox = wx.ListBox(panel, id = wx.ID_ANY, choices = [], style = wx.LB_SINGLE|wx.LB_NEEDED_SB)
  670. self.listbox.Bind(wx.EVT_LISTBOX, self.OnSelectionChanged)
  671. gridBagSizer.Add(self.listbox, pos = (0, 0), span = (4, 1),
  672. flag = wx.ALIGN_CENTER_VERTICAL| wx.EXPAND, border = 0)
  673. buttonNames = ['time', 'image', 'text']
  674. buttonLabels = [_("Add time stamp"), _("Add image"), _("Add text")]
  675. i = 0
  676. for buttonName, buttonLabel in zip(buttonNames, buttonLabels):
  677. if buttonName == 'time' and self.temporal == TemporalMode.NONTEMPORAL:
  678. continue
  679. btn = wx.Button(panel, id = wx.ID_ANY, name = buttonName, label = buttonLabel)
  680. btn.Bind(wx.EVT_BUTTON, lambda evt, temp = buttonName: self.OnAddDecoration(evt, temp))
  681. gridBagSizer.Add(btn, pos = (i ,1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  682. i += 1
  683. removeButton = wx.Button(panel, id = wx.ID_ANY, label = _("Remove"))
  684. removeButton.Bind(wx.EVT_BUTTON, self.OnRemove)
  685. gridBagSizer.Add(removeButton, pos = (i, 1), flag = wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border = 0)
  686. return gridBagSizer
  687. def _createDecorationsProperties(self, panel):
  688. self.hidevbox = wx.BoxSizer(wx.VERTICAL)
  689. # inform label
  690. self.informBox = wx.BoxSizer(wx.HORIZONTAL)
  691. if self.temporal == TemporalMode.TEMPORAL:
  692. label = _("Add time stamp, image or text decoration by one of the buttons above.")
  693. else:
  694. label = _("Add image or text decoration by one of the buttons above.")
  695. label = wx.StaticText(panel, id = wx.ID_ANY, label = label)
  696. label.Wrap(400)
  697. self.informBox.Add(label, proportion = 1, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
  698. self.hidevbox.Add(self.informBox, proportion = 0, flag = wx.EXPAND | wx.BOTTOM, border = 5)
  699. # font
  700. self.fontBox = wx.BoxSizer(wx.HORIZONTAL)
  701. self.fontBox.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Font settings:")),
  702. proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
  703. self.sampleLabel = wx.StaticText(panel, id = wx.ID_ANY, label = _("Sample text"))
  704. self.fontBox.Add(self.sampleLabel, proportion = 1,
  705. flag = wx.ALIGN_CENTER | wx.RIGHT | wx.LEFT, border = 5)
  706. fontButton = wx.Button(panel, id = wx.ID_ANY, label = _("Set font"))
  707. fontButton.Bind(wx.EVT_BUTTON, self.OnFont)
  708. self.fontBox.Add(fontButton, proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL)
  709. self.hidevbox.Add(self.fontBox, proportion = 0, flag = wx.EXPAND | wx.BOTTOM, border = 5)
  710. # image
  711. self.imageBox = wx.BoxSizer(wx.HORIZONTAL)
  712. filetype, ltype = GetImageHandlers(wx.EmptyImage(10, 10))
  713. self.browse = filebrowse.FileBrowseButton(parent = panel, id = wx.ID_ANY, fileMask = filetype,
  714. labelText = _("Image file:"),
  715. dialogTitle = _('Choose image file'),
  716. buttonText = _('Browse'),
  717. startDirectory = os.getcwd(), fileMode = wx.OPEN,
  718. changeCallback = self.OnSetImage)
  719. self.imageBox.Add(self.browse, proportion = 1, flag = wx.EXPAND)
  720. self.hidevbox.Add(self.imageBox, proportion = 0, flag = wx.EXPAND | wx.BOTTOM, border = 5)
  721. # text
  722. self.textBox = wx.BoxSizer(wx.HORIZONTAL)
  723. self.textBox.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Text:")),
  724. proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 5)
  725. self.textCtrl = wx.TextCtrl(panel, id = wx.ID_ANY)
  726. self.textCtrl.Bind(wx.EVT_TEXT, self.OnText)
  727. self.textBox.Add(self.textCtrl, proportion = 1, flag = wx.EXPAND)
  728. self.hidevbox.Add(self.textBox, proportion = 0, flag = wx.EXPAND)
  729. self.posBox = self._positionWidget(panel)
  730. self.hidevbox.Add(self.posBox, proportion = 0, flag = wx.EXPAND | wx.TOP, border = 5)
  731. return self.hidevbox
  732. def _positionWidget(self, panel):
  733. grid = wx.GridBagSizer(vgap = 5, hgap = 5)
  734. label = wx.StaticText(panel, id = wx.ID_ANY, label = _("Placement as percentage of"
  735. " screen coordinates (X: 0, Y: 0 is top left):"))
  736. label.Wrap(400)
  737. self.spinX = wx.SpinCtrl(panel, id = wx.ID_ANY, min = 0, max = 100, initial = 10)
  738. self.spinY = wx.SpinCtrl(panel, id = wx.ID_ANY, min = 0, max = 100, initial = 10)
  739. self.spinX.Bind(wx.EVT_SPINCTRL, lambda evt, temp = 'X': self.OnPosition(evt, temp))
  740. self.spinY.Bind(wx.EVT_SPINCTRL, lambda evt, temp = 'Y': self.OnPosition(evt, temp))
  741. grid.Add(label, pos = (0, 0), span = (1, 4), flag = wx.EXPAND)
  742. grid.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("X:")), pos = (1, 0),
  743. flag = wx.ALIGN_CENTER_VERTICAL)
  744. grid.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Y:")), pos = (1, 2),
  745. flag = wx.ALIGN_CENTER_VERTICAL)
  746. grid.Add(self.spinX, pos = (1, 1))
  747. grid.Add(self.spinY, pos = (1, 3))
  748. return grid
  749. def _createExportFormatPanel(self, notebook):
  750. panel = wx.Panel(notebook, id = wx.ID_ANY)
  751. borderSizer = wx.BoxSizer(wx.VERTICAL)
  752. if not self.visvis:
  753. isVisvisText = wx.StaticText(panel, id = wx.ID_ANY,
  754. label = _("To enable export to GIF and SWF, please install visvis library."))
  755. isVisvisText.Wrap(400)
  756. borderSizer.Add(item = isVisvisText, proportion = 0,
  757. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border = 5)
  758. hSizer = wx.BoxSizer(wx.HORIZONTAL)
  759. if not self.visvis:
  760. choices = [_("image sequence")]
  761. else:
  762. choices = [_("image sequence"), _("animated GIF"), _("SWF"), _("AVI")]
  763. self.formatChoice = wx.Choice(parent = panel, id = wx.ID_ANY,
  764. choices = choices)
  765. self.formatChoice.Bind(wx.EVT_CHOICE, lambda event: self.ChangeFormat(event.GetSelection()))
  766. hSizer.Add(item = wx.StaticText(panel, id = wx.ID_ANY, label = _("Export to:")),
  767. proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 2)
  768. hSizer.Add(item = self.formatChoice, proportion = 1,
  769. flag = wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border = 2)
  770. borderSizer.Add(item = hSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 3)
  771. helpSizer = wx.BoxSizer(wx.HORIZONTAL)
  772. helpSizer.AddStretchSpacer(1)
  773. self.formatPanelSizer = wx.BoxSizer(wx.VERTICAL)
  774. helpSizer.Add(self.formatPanelSizer, proportion = 5)
  775. borderSizer.Add(helpSizer, proportion = 1, flag = wx.EXPAND)
  776. self.formatPanels = []
  777. # panel for image sequence
  778. imSeqPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  779. prefixLabel = wx.StaticText(imSeqPanel, id = wx.ID_ANY, label = _("File prefix:"))
  780. self.prefixCtrl = wx.TextCtrl(imSeqPanel, id = wx.ID_ANY, value = _("animation"))
  781. formatLabel = wx.StaticText(imSeqPanel, id = wx.ID_ANY, label = _("File format:"))
  782. self.imSeqFormatChoice = wx.Choice(imSeqPanel, id = wx.ID_ANY)
  783. wildcard, ltype = GetImageHandlers(wx.EmptyImage(10, 10))
  784. formats = [format for format in wildcard.split('|') if 'file' in format]
  785. for format, cdata in zip(formats, ltype):
  786. self.imSeqFormatChoice.Append(format, cdata)
  787. self.imSeqFormatChoice.SetSelection(0)
  788. self.dirBrowse = filebrowse.DirBrowseButton(parent = imSeqPanel, id = wx.ID_ANY,
  789. labelText = _("Directory:"),
  790. dialogTitle = _("Choose directory for export"),
  791. buttonText = _("Browse"),
  792. startDirectory = os.getcwd())
  793. dirGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  794. dirGridSizer.AddGrowableCol(1)
  795. dirGridSizer.Add(prefixLabel, pos = (0, 0), flag = wx.ALIGN_CENTER_VERTICAL)
  796. dirGridSizer.Add(self.prefixCtrl, pos = (0, 1), flag = wx.EXPAND)
  797. dirGridSizer.Add(formatLabel, pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL)
  798. dirGridSizer.Add(self.imSeqFormatChoice, pos = (1, 1), flag = wx.EXPAND)
  799. dirGridSizer.Add(self.dirBrowse, pos = (2, 0), flag = wx.EXPAND, span = (1, 2))
  800. imSeqPanel.SetSizer(dirGridSizer)
  801. dirGridSizer.Fit(imSeqPanel)
  802. self.formatPanelSizer.Add(item = imSeqPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  803. self.formatPanels.append(imSeqPanel)
  804. # panel for gif
  805. gifPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  806. self.gifBrowse = filebrowse.FileBrowseButton(parent = gifPanel, id = wx.ID_ANY,
  807. fileMask = "GIF file (*.gif)|*.gif",
  808. labelText = _("GIF file:"),
  809. dialogTitle = _("Choose file to save animation"),
  810. buttonText = _("Browse"),
  811. startDirectory = os.getcwd(), fileMode = wx.SAVE)
  812. gifGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  813. gifGridSizer.AddGrowableCol(0)
  814. gifGridSizer.Add(self.gifBrowse, pos = (0, 0), flag = wx.EXPAND)
  815. gifPanel.SetSizer(gifGridSizer)
  816. gifGridSizer.Fit(gifPanel)
  817. self.formatPanelSizer.Add(item = gifPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  818. self.formatPanels.append(gifPanel)
  819. # panel for swf
  820. swfPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  821. self.swfBrowse = filebrowse.FileBrowseButton(parent = swfPanel, id = wx.ID_ANY,
  822. fileMask = "SWF file (*.swf)|*.swf",
  823. labelText = _("SWF file:"),
  824. dialogTitle = _("Choose file to save animation"),
  825. buttonText = _("Browse"),
  826. startDirectory = os.getcwd(), fileMode = wx.SAVE)
  827. swfGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  828. swfGridSizer.AddGrowableCol(0)
  829. swfGridSizer.Add(self.swfBrowse, pos = (0, 0), flag = wx.EXPAND)
  830. swfPanel.SetSizer(swfGridSizer)
  831. swfGridSizer.Fit(swfPanel)
  832. self.formatPanelSizer.Add(item = swfPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  833. self.formatPanels.append(swfPanel)
  834. # panel for avi
  835. aviPanel = wx.Panel(parent = panel, id = wx.ID_ANY)
  836. self.aviBrowse = filebrowse.FileBrowseButton(parent = aviPanel, id = wx.ID_ANY,
  837. fileMask = "AVI file (*.avi)|*.avi",
  838. labelText = _("AVI file:"),
  839. dialogTitle = _("Choose file to save animation"),
  840. buttonText = _("Browse"),
  841. startDirectory = os.getcwd(), fileMode = wx.SAVE)
  842. encodingLabel = wx.StaticText(parent = aviPanel, id = wx.ID_ANY, label = _("Video codec:"))
  843. self.encodingText = wx.TextCtrl(parent = aviPanel, id = wx.ID_ANY, value = 'mpeg4')
  844. aviGridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  845. aviGridSizer.AddGrowableCol(1)
  846. aviGridSizer.Add(self.aviBrowse, pos = (0, 0), span = (1, 2), flag = wx.EXPAND)
  847. aviGridSizer.Add(encodingLabel, pos = (1, 0), flag = wx.ALIGN_CENTER_VERTICAL)
  848. aviGridSizer.Add(self.encodingText, pos = (1, 1), flag = wx.EXPAND)
  849. aviPanel.SetSizer(aviGridSizer)
  850. aviGridSizer.Fit(aviPanel)
  851. self.formatPanelSizer.Add(item = aviPanel, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  852. self.formatPanels.append(aviPanel)
  853. fpsSizer = wx.BoxSizer(wx.HORIZONTAL)
  854. fps = 1000 / self.timeTick
  855. fpsSizer.Add(wx.StaticText(panel, id = wx.ID_ANY, label = _("Current frame rate: %.2f fps") % fps),
  856. proportion = 1, flag = wx.EXPAND)
  857. borderSizer.Add(fpsSizer, proportion = 0, flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL, border = 5)
  858. panel.SetSizer(borderSizer)
  859. borderSizer.Fit(panel)
  860. self.ChangeFormat(index = 0)
  861. return panel
  862. def ChangeFormat(self, index):
  863. for i, panel in enumerate(self.formatPanels):
  864. self.formatPanelSizer.Show(item = panel, show = (i == index))
  865. self.formatPanelSizer.Layout()
  866. def OnFont(self, event):
  867. index = self.listbox.GetSelection()
  868. # should not happen
  869. if index == wx.NOT_FOUND:
  870. return
  871. cdata = self.listbox.GetClientData(index)
  872. font = cdata['font']
  873. fontdata = wx.FontData()
  874. fontdata.EnableEffects(True)
  875. fontdata.SetColour('black')
  876. fontdata.SetInitialFont(font)
  877. dlg = wx.FontDialog(self, fontdata)
  878. if dlg.ShowModal() == wx.ID_OK:
  879. newfontdata = dlg.GetFontData()
  880. font = newfontdata.GetChosenFont()
  881. self.sampleLabel.SetFont(font)
  882. cdata['font'] = font
  883. self.Layout()
  884. def OnPosition(self, event, coord):
  885. index = self.listbox.GetSelection()
  886. # should not happen
  887. if index == wx.NOT_FOUND:
  888. return
  889. cdata = self.listbox.GetClientData(index)
  890. cdata['pos'][coord == 'Y'] = event.GetInt()
  891. def OnSetImage(self, event):
  892. index = self.listbox.GetSelection()
  893. # should not happen
  894. if index == wx.NOT_FOUND:
  895. return
  896. cdata = self.listbox.GetClientData(index)
  897. cdata['file'] = event.GetString()
  898. def OnAddDecoration(self, event, name):
  899. if name == 'time':
  900. timeInfo = {'name': name, 'font': self.GetFont(), 'pos': [10, 10]}
  901. self.decorations.append(timeInfo)
  902. elif name == 'image':
  903. imageInfo = {'name': name, 'file': '', 'pos': [10, 10]}
  904. self.decorations.append(imageInfo)
  905. elif name == 'text':
  906. textInfo = {'name': name, 'font': self.GetFont(), 'text': '', 'pos': [10, 10]}
  907. self.decorations.append(textInfo)
  908. self._updateListBox()
  909. self.listbox.SetSelection(self.listbox.GetCount() - 1)
  910. self.OnSelectionChanged(event = None)
  911. def OnSelectionChanged(self, event):
  912. index = self.listbox.GetSelection()
  913. if index == wx.NOT_FOUND:
  914. self._hideAll()
  915. return
  916. cdata = self.listbox.GetClientData(index)
  917. self.hidevbox.Show(self.fontBox, (cdata['name'] in ('time', 'text')))
  918. self.hidevbox.Show(self.imageBox, (cdata['name'] == 'image'))
  919. self.hidevbox.Show(self.textBox, (cdata['name'] == 'text'))
  920. self.hidevbox.Show(self.posBox, True)
  921. self.hidevbox.Show(self.informBox, False)
  922. self.spinX.SetValue(cdata['pos'][0])
  923. self.spinY.SetValue(cdata['pos'][1])
  924. if cdata['name'] == 'image':
  925. self.browse.SetValue(cdata['file'])
  926. elif cdata['name'] in ('time', 'text'):
  927. self.sampleLabel.SetFont(cdata['font'])
  928. if cdata['name'] == 'text':
  929. self.textCtrl.SetValue(cdata['text'])
  930. self.hidevbox.Layout()
  931. # self.Layout()
  932. def OnText(self, event):
  933. index = self.listbox.GetSelection()
  934. # should not happen
  935. if index == wx.NOT_FOUND:
  936. return
  937. cdata = self.listbox.GetClientData(index)
  938. cdata['text'] = event.GetString()
  939. def OnRemove(self, event):
  940. index = self.listbox.GetSelection()
  941. if index == wx.NOT_FOUND:
  942. return
  943. decData = self.listbox.GetClientData(index)
  944. self.decorations.remove(decData)
  945. self._updateListBox()
  946. if self.listbox.GetCount():
  947. self.listbox.SetSelection(0)
  948. self.OnSelectionChanged(event = None)
  949. def OnExport(self, event):
  950. for decor in self.decorations:
  951. if decor['name'] == 'image':
  952. if not os.path.exists(decor['file']):
  953. if decor['file']:
  954. GError(parent = self, message = _("File %s not found.") % decor['file'])
  955. else:
  956. GError(parent = self, message = _("Decoration image file is missing."))
  957. return
  958. if self.formatChoice.GetSelection() == 0:
  959. name = self.dirBrowse.GetValue()
  960. if not os.path.exists(name):
  961. if name:
  962. GError(parent = self, message = _("Directory %s not found.") % name)
  963. else:
  964. GError(parent = self, message = _("Export directory is missing."))
  965. return
  966. elif self.formatChoice.GetSelection() == 1:
  967. if not self.gifBrowse.GetValue():
  968. GError(parent = self, message = _("Export file is missing."))
  969. return
  970. elif self.formatChoice.GetSelection() == 2:
  971. if not self.swfBrowse.GetValue():
  972. GError(parent = self, message = _("Export file is missing."))
  973. return
  974. self.EndModal(wx.ID_OK)
  975. def GetDecorations(self):
  976. return self.decorations
  977. def GetExportInformation(self):
  978. info = {}
  979. if self.formatChoice.GetSelection() == 0:
  980. info['method'] = 'sequence'
  981. info['directory'] = self.dirBrowse.GetValue()
  982. info['prefix'] = self.prefixCtrl.GetValue()
  983. info['format'] = self.imSeqFormatChoice.GetClientData(self.imSeqFormatChoice.GetSelection())
  984. elif self.formatChoice.GetSelection() == 1:
  985. info['method'] = 'gif'
  986. info['file'] = self.gifBrowse.GetValue()
  987. elif self.formatChoice.GetSelection() == 2:
  988. info['method'] = 'swf'
  989. info['file'] = self.swfBrowse.GetValue()
  990. elif self.formatChoice.GetSelection() == 3:
  991. info['method'] = 'avi'
  992. info['file'] = self.aviBrowse.GetValue()
  993. info['encoding'] = self.encodingText.GetValue()
  994. return info
  995. def _updateListBox(self):
  996. self.listbox.Clear()
  997. names = {'time': _("Time stamp"), 'image': _("Image"), 'text': _("Text")}
  998. for decor in self.decorations:
  999. self.listbox.Append(names[decor['name']], clientData = decor)
  1000. def _hideAll(self):
  1001. self.hidevbox.Show(self.fontBox, False)
  1002. self.hidevbox.Show(self.imageBox, False)
  1003. self.hidevbox.Show(self.textBox, False)
  1004. self.hidevbox.Show(self.posBox, False)
  1005. self.hidevbox.Show(self.informBox, True)
  1006. self.hidevbox.Layout()
  1007. def test():
  1008. import wx.lib.inspection
  1009. import gettext
  1010. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  1011. import grass.script as grass
  1012. app = wx.PySimpleApp()
  1013. testExport()
  1014. # wx.lib.inspection.InspectionTool().Show()
  1015. app.MainLoop()
  1016. def testAnimInput():
  1017. anim = AnimationData()
  1018. anim.SetDefaultValues(animationIndex = 0, windowIndex = 0)
  1019. dlg = InputDialog(parent = None, mode = 'add', animationData = anim)
  1020. dlg.Show()
  1021. def testAnimEdit():
  1022. anim = AnimationData()
  1023. anim.SetDefaultValues(animationIndex = 0, windowIndex = 0)
  1024. dlg = EditDialog(parent = None, animationData = [anim])
  1025. dlg.Show()
  1026. def testExport():
  1027. dlg = ExportDialog(parent = None, temporal = TemporalMode.TEMPORAL,
  1028. timeTick = 200, visvis = True)
  1029. if dlg.ShowModal() == wx.ID_OK:
  1030. print dlg.GetDecorations()
  1031. print dlg.GetExportInformation()
  1032. dlg.Destroy()
  1033. else:
  1034. dlg.Destroy()
  1035. if __name__ == '__main__':
  1036. test()