dialogs.py 55 KB

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