dialogs.py 53 KB

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