dialogs.py 55 KB

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