dialogs.py 54 KB

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