dialogs.py 48 KB

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