dialogs.py 56 KB

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