dialogs.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. """
  2. @package gmodeler.dialogs
  3. @brief wxGUI Graphical Modeler - dialogs
  4. Classes:
  5. - dialogs::ModelDataDialog
  6. - dialogs::ModelSearchDialog
  7. - dialogs::ModelRelationDialog
  8. - dialogs::ModelItemDialog
  9. - dialogs::ModelLoopDialog
  10. - dialogs::ModelConditionDialog
  11. - dialogs::ModelListCtrl
  12. - dialogs::ValiableListCtrl
  13. - dialogs::ItemListCtrl
  14. - dialogs::ItemCheckListCtrl
  15. (C) 2010-2016 by the GRASS Development Team
  16. This program is free software under the GNU General Public License
  17. (>=v2). Read the file COPYING that comes with GRASS for details.
  18. @author Martin Landa <landa.martin gmail.com>
  19. """
  20. import os
  21. import six
  22. import wx
  23. import wx.lib.mixins.listctrl as listmix
  24. from core import globalvar
  25. from core import utils
  26. from gui_core.widgets import SearchModuleWidget, SimpleValidator
  27. from core.gcmd import GError
  28. from gui_core.dialogs import SimpleDialog, MapLayersDialogForModeler
  29. from gui_core.prompt import GPromptSTC
  30. from gui_core.gselect import Select, ElementSelect
  31. from gmodeler.model import *
  32. from lmgr.menudata import LayerManagerMenuData
  33. from gui_core.wrap import (
  34. Button,
  35. StaticText,
  36. StaticBox,
  37. TextCtrl,
  38. Menu,
  39. ListCtrl,
  40. NewId,
  41. CheckListCtrlMixin,
  42. )
  43. class ModelDataDialog(SimpleDialog):
  44. """Data item properties dialog"""
  45. def __init__(self, parent, shape, title=_("Data properties")):
  46. self.parent = parent
  47. self.shape = shape
  48. label, etype = self._getLabel()
  49. self.etype = etype
  50. SimpleDialog.__init__(self, parent, title)
  51. self.element = Select(
  52. parent=self.panel,
  53. type=self.shape.GetPrompt(),
  54. validator=SimpleValidator(callback=self.ValidatorCallback),
  55. )
  56. if shape.GetValue():
  57. self.element.SetValue(shape.GetValue())
  58. self.Bind(wx.EVT_BUTTON, self.OnOK, self.btnOK)
  59. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel)
  60. if self.etype:
  61. self.typeSelect = ElementSelect(
  62. parent=self.panel,
  63. elements=["raster", "raster_3d", "vector"],
  64. size=globalvar.DIALOG_GSELECT_SIZE,
  65. )
  66. self.typeSelect.Bind(wx.EVT_CHOICE, self.OnType)
  67. self.typeSelect.SetSelection(0)
  68. self.element.SetType("raster")
  69. if shape.GetValue():
  70. self.btnOK.Enable()
  71. self._layout()
  72. self.SetMinSize(self.GetSize())
  73. def _getLabel(self):
  74. etype = False
  75. prompt = self.shape.GetPrompt()
  76. if prompt == "raster":
  77. label = _("Name of raster map:")
  78. elif prompt == "vector":
  79. label = _("Name of vector map:")
  80. else:
  81. etype = True
  82. label = _("Name of element:")
  83. return label, etype
  84. def _layout(self):
  85. """Do layout"""
  86. if self.etype:
  87. self.dataSizer.Add(
  88. StaticText(
  89. parent=self.panel, id=wx.ID_ANY, label=_("Type of element:")
  90. ),
  91. proportion=0,
  92. flag=wx.ALL,
  93. border=1,
  94. )
  95. self.dataSizer.Add(self.typeSelect, proportion=0, flag=wx.ALL, border=1)
  96. self.dataSizer.Add(
  97. StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Name of element:")),
  98. proportion=0,
  99. flag=wx.ALL,
  100. border=1,
  101. )
  102. self.dataSizer.Add(
  103. self.element, proportion=0, flag=wx.EXPAND | wx.ALL, border=1
  104. )
  105. self.panel.SetSizer(self.sizer)
  106. self.sizer.Fit(self)
  107. def GetType(self):
  108. """Get element type"""
  109. if not self.etype:
  110. return
  111. return self.element.tcp.GetType()
  112. def OnType(self, event):
  113. """Select element type"""
  114. evalue = self.typeSelect.GetValue(event.GetString())
  115. self.element.SetType(evalue)
  116. def OnOK(self, event):
  117. """Ok pressed"""
  118. self.shape.SetValue(self.element.GetValue())
  119. if self.etype:
  120. elem = self.GetType()
  121. if elem == "raster":
  122. self.shape.SetPrompt("raster")
  123. elif elem == "vector":
  124. self.shape.SetPrompt("vector")
  125. elif elem == "raster_3d":
  126. self.shape.SetPrompt("raster_3d")
  127. self.parent.canvas.Refresh()
  128. self.parent.SetStatusText("", 0)
  129. self.shape.SetPropDialog(None)
  130. if self.IsModal():
  131. event.Skip()
  132. else:
  133. self.Destroy()
  134. def OnCancel(self, event):
  135. """Cancel pressed"""
  136. self.shape.SetPropDialog(None)
  137. if self.IsModal():
  138. event.Skip()
  139. else:
  140. self.Destroy()
  141. class ModelSearchDialog(wx.Dialog):
  142. def __init__(
  143. self,
  144. parent,
  145. giface,
  146. title=_("Add GRASS command to the model"),
  147. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  148. **kwargs,
  149. ):
  150. """Graphical modeler tool search window
  151. :param parent: parent window
  152. :param id: window id
  153. :param title: window title
  154. :param kwargs: wx.Dialogs' arguments
  155. """
  156. self.parent = parent
  157. wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, title=title, **kwargs)
  158. self.SetName("ModelerDialog")
  159. self.SetIcon(
  160. wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"), wx.BITMAP_TYPE_ICO)
  161. )
  162. self._command = None
  163. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  164. self.cmdBox = StaticBox(
  165. parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Command")
  166. )
  167. self.labelBox = StaticBox(
  168. parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Label and comment")
  169. )
  170. # menu data for search widget and prompt
  171. menuModel = LayerManagerMenuData()
  172. self.cmd_prompt = GPromptSTC(
  173. parent=self, giface=giface, menuModel=menuModel.GetModel()
  174. )
  175. self.cmd_prompt.promptRunCmd.connect(self.OnCommand)
  176. self.cmd_prompt.commandSelected.connect(
  177. lambda command: self.label.SetValue(command)
  178. )
  179. self.search = SearchModuleWidget(
  180. parent=self.panel, model=menuModel.GetModel(), showTip=True
  181. )
  182. self.search.moduleSelected.connect(
  183. lambda name: (
  184. self.cmd_prompt.SetText(name + " "),
  185. self.label.SetValue(name),
  186. )
  187. )
  188. self.label = TextCtrl(parent=self.panel, id=wx.ID_ANY)
  189. self.comment = TextCtrl(parent=self.panel, id=wx.ID_ANY, style=wx.TE_MULTILINE)
  190. self.btnCancel = Button(self.panel, wx.ID_CANCEL)
  191. self.btnOk = Button(self.panel, wx.ID_OK)
  192. self.btnOk.SetDefault()
  193. self.Bind(wx.EVT_BUTTON, self.OnOk, self.btnOk)
  194. self.Bind(wx.EVT_BUTTON, self.OnCancel, self.btnCancel)
  195. self._layout()
  196. self.SetSize((500, -1))
  197. def _layout(self):
  198. cmdSizer = wx.StaticBoxSizer(self.cmdBox, wx.VERTICAL)
  199. cmdSizer.Add(self.cmd_prompt, proportion=1, flag=wx.EXPAND)
  200. labelSizer = wx.StaticBoxSizer(self.labelBox, wx.VERTICAL)
  201. gridSizer = wx.GridBagSizer(hgap=5, vgap=5)
  202. gridSizer.Add(
  203. StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Label:")),
  204. flag=wx.ALIGN_CENTER_VERTICAL,
  205. pos=(0, 0),
  206. )
  207. gridSizer.Add(self.label, pos=(0, 1), flag=wx.EXPAND)
  208. gridSizer.Add(
  209. StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Comment:")),
  210. flag=wx.ALIGN_CENTER_VERTICAL,
  211. pos=(1, 0),
  212. )
  213. gridSizer.Add(self.comment, pos=(1, 1), flag=wx.EXPAND)
  214. gridSizer.AddGrowableRow(1)
  215. gridSizer.AddGrowableCol(1)
  216. labelSizer.Add(gridSizer, proportion=1, flag=wx.EXPAND)
  217. btnSizer = wx.StdDialogButtonSizer()
  218. btnSizer.AddButton(self.btnCancel)
  219. btnSizer.AddButton(self.btnOk)
  220. btnSizer.Realize()
  221. mainSizer = wx.BoxSizer(wx.VERTICAL)
  222. mainSizer.Add(self.search, proportion=0, flag=wx.EXPAND | wx.ALL, border=3)
  223. mainSizer.Add(
  224. cmdSizer,
  225. proportion=1,
  226. flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP,
  227. border=3,
  228. )
  229. mainSizer.Add(
  230. labelSizer,
  231. proportion=1,
  232. flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP,
  233. border=3,
  234. )
  235. mainSizer.Add(btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
  236. self.panel.SetSizer(mainSizer)
  237. mainSizer.Fit(self)
  238. self.Layout()
  239. def GetPanel(self):
  240. """Get dialog panel"""
  241. return self.panel
  242. def _getCmd(self):
  243. line = self.cmd_prompt.GetCurLine()[0].strip()
  244. if len(line) == 0:
  245. cmd = list()
  246. else:
  247. cmd = utils.split(str(line))
  248. return cmd
  249. def GetCmd(self):
  250. """Get command"""
  251. return self._command
  252. def GetLabel(self):
  253. """Get label and comment"""
  254. return self.label.GetValue(), self.comment.GetValue()
  255. def ValidateCmd(self, cmd):
  256. if len(cmd) < 1:
  257. GError(
  258. parent=self,
  259. message=_(
  260. "Command not defined.\n\n" "Unable to add new action to the model."
  261. ),
  262. )
  263. return False
  264. if cmd[0] not in globalvar.grassCmd:
  265. GError(
  266. parent=self,
  267. message=_(
  268. "'%s' is not a GRASS tool.\n\n"
  269. "Unable to add new action to the model."
  270. )
  271. % cmd[0],
  272. )
  273. return False
  274. return True
  275. def OnCommand(self, cmd):
  276. """Command in prompt confirmed"""
  277. if self.ValidateCmd(cmd):
  278. self._command = cmd
  279. self.EndModal(wx.ID_OK)
  280. def OnOk(self, event):
  281. """Button 'OK' pressed"""
  282. cmd = self._getCmd()
  283. if self.ValidateCmd(cmd):
  284. self._command = cmd
  285. self.EndModal(wx.ID_OK)
  286. def OnCancel(self, event):
  287. """Cancel pressed, close window"""
  288. self.Hide()
  289. def Reset(self):
  290. """Reset dialog"""
  291. self.search.Reset()
  292. self.label.SetValue("")
  293. self.comment.SetValue("")
  294. self.cmd_prompt.OnCmdErase(None)
  295. self.cmd_prompt.SetFocus()
  296. class ModelRelationDialog(wx.Dialog):
  297. """Relation properties dialog"""
  298. def __init__(
  299. self,
  300. parent,
  301. shape,
  302. id=wx.ID_ANY,
  303. title=_("Relation properties"),
  304. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  305. **kwargs,
  306. ):
  307. self.parent = parent
  308. self.shape = shape
  309. options = self._getOptions()
  310. if not options:
  311. self.valid = False
  312. return
  313. self.valid = True
  314. wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)
  315. self.SetIcon(
  316. wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"), wx.BITMAP_TYPE_ICO)
  317. )
  318. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  319. self.fromBox = StaticBox(
  320. parent=self.panel, id=wx.ID_ANY, label=" %s " % _("From")
  321. )
  322. self.toBox = StaticBox(parent=self.panel, id=wx.ID_ANY, label=" %s " % _("To"))
  323. self.option = wx.ComboBox(
  324. parent=self.panel, id=wx.ID_ANY, style=wx.CB_READONLY, choices=options
  325. )
  326. self.option.Bind(wx.EVT_COMBOBOX, self.OnOption)
  327. self.btnCancel = Button(self.panel, wx.ID_CANCEL)
  328. self.btnOk = Button(self.panel, wx.ID_OK)
  329. self.btnOk.Enable(False)
  330. self._layout()
  331. def _layout(self):
  332. mainSizer = wx.BoxSizer(wx.VERTICAL)
  333. fromSizer = wx.StaticBoxSizer(self.fromBox, wx.VERTICAL)
  334. self._layoutShape(shape=self.shape.GetFrom(), sizer=fromSizer)
  335. toSizer = wx.StaticBoxSizer(self.toBox, wx.VERTICAL)
  336. self._layoutShape(shape=self.shape.GetTo(), sizer=toSizer)
  337. btnSizer = wx.StdDialogButtonSizer()
  338. btnSizer.AddButton(self.btnCancel)
  339. btnSizer.AddButton(self.btnOk)
  340. btnSizer.Realize()
  341. mainSizer.Add(fromSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
  342. mainSizer.Add(
  343. toSizer,
  344. proportion=0,
  345. flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
  346. border=5,
  347. )
  348. mainSizer.Add(
  349. btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5
  350. )
  351. self.panel.SetSizer(mainSizer)
  352. mainSizer.Fit(self.panel)
  353. self.Layout()
  354. self.SetSize(self.GetBestSize())
  355. def _layoutShape(self, shape, sizer):
  356. if isinstance(shape, ModelData):
  357. sizer.Add(
  358. StaticText(
  359. parent=self.panel,
  360. id=wx.ID_ANY,
  361. label=_("Data: %s") % shape.GetLog(),
  362. ),
  363. proportion=1,
  364. flag=wx.EXPAND | wx.ALL,
  365. border=5,
  366. )
  367. elif isinstance(shape, ModelAction):
  368. gridSizer = wx.GridBagSizer(hgap=5, vgap=5)
  369. gridSizer.Add(
  370. StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Command:")),
  371. pos=(0, 0),
  372. )
  373. gridSizer.Add(
  374. StaticText(parent=self.panel, id=wx.ID_ANY, label=shape.GetLabel()),
  375. pos=(0, 1),
  376. )
  377. gridSizer.Add(
  378. StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Option:")),
  379. flag=wx.ALIGN_CENTER_VERTICAL,
  380. pos=(1, 0),
  381. )
  382. gridSizer.Add(self.option, pos=(1, 1))
  383. sizer.Add(gridSizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
  384. def _getOptions(self):
  385. """Get relevant options"""
  386. items = []
  387. fromShape = self.shape.GetFrom()
  388. if not isinstance(fromShape, ModelData):
  389. GError(
  390. parent=self.parent,
  391. message=_(
  392. "Relation doesn't start with data item.\n" "Unable to add relation."
  393. ),
  394. )
  395. return items
  396. toShape = self.shape.GetTo()
  397. if not isinstance(toShape, ModelAction):
  398. GError(
  399. parent=self.parent,
  400. message=_(
  401. "Relation doesn't point to GRASS command.\n"
  402. "Unable to add relation."
  403. ),
  404. )
  405. return items
  406. prompt = fromShape.GetPrompt()
  407. task = toShape.GetTask()
  408. for p in task.get_options()["params"]:
  409. if p.get("prompt", "") == prompt and "name" in p:
  410. items.append(p["name"])
  411. if not items:
  412. GError(
  413. parent=self.parent,
  414. message=_("No relevant option found.\n" "Unable to add relation."),
  415. )
  416. return items
  417. def GetOption(self):
  418. """Get selected option"""
  419. return self.option.GetStringSelection()
  420. def IsValid(self):
  421. """Check if relation is valid"""
  422. return self.valid
  423. def OnOption(self, event):
  424. """Set option"""
  425. if event.GetString():
  426. self.btnOk.Enable()
  427. else:
  428. self.btnOk.Enable(False)
  429. class ModelItemDialog(wx.Dialog):
  430. """Abstract item properties dialog"""
  431. def __init__(
  432. self,
  433. parent,
  434. shape,
  435. title,
  436. id=wx.ID_ANY,
  437. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  438. **kwargs,
  439. ):
  440. self.parent = parent
  441. self.shape = shape
  442. wx.Dialog.__init__(self, parent, id, title=title, style=style, **kwargs)
  443. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  444. self.condBox = StaticBox(
  445. parent=self.panel, id=wx.ID_ANY, label=" %s " % _("Condition")
  446. )
  447. self.condText = TextCtrl(
  448. parent=self.panel, id=wx.ID_ANY, value=shape.GetLabel()
  449. )
  450. self.itemList = ItemCheckListCtrl(
  451. parent=self.panel,
  452. columns=[_("Label"), _("Command")],
  453. shape=shape,
  454. frame=parent,
  455. )
  456. self.itemList.Populate(self.parent.GetModel().GetItems())
  457. self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
  458. self.btnOk = Button(parent=self.panel, id=wx.ID_OK)
  459. self.btnOk.SetDefault()
  460. def _layout(self):
  461. """Do layout (virtual method)"""
  462. pass
  463. def GetCondition(self):
  464. """Get loop condition"""
  465. return self.condText.GetValue()
  466. class ModelLoopDialog(ModelItemDialog):
  467. """Loop properties dialog"""
  468. def __init__(
  469. self,
  470. parent,
  471. shape,
  472. id=wx.ID_ANY,
  473. title=_("Loop properties"),
  474. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  475. **kwargs,
  476. ):
  477. ModelItemDialog.__init__(self, parent, shape, title, style=style, **kwargs)
  478. self.listBox = StaticBox(
  479. parent=self.panel, id=wx.ID_ANY, label=" %s " % _("List of items in loop")
  480. )
  481. self.btnSeries = Button(parent=self.panel, id=wx.ID_ANY, label=_("Series"))
  482. self.btnSeries.SetToolTip(_("Define map series as condition for the loop"))
  483. self.btnSeries.Bind(wx.EVT_BUTTON, self.OnSeries)
  484. self._layout()
  485. self.SetMinSize(self.GetSize())
  486. self.SetSize((500, 400))
  487. def _layout(self):
  488. """Do layout"""
  489. sizer = wx.BoxSizer(wx.VERTICAL)
  490. condSizer = wx.StaticBoxSizer(self.condBox, wx.HORIZONTAL)
  491. condSizer.Add(self.condText, proportion=1, flag=wx.ALL, border=3)
  492. condSizer.Add(self.btnSeries, proportion=0, flag=wx.EXPAND)
  493. listSizer = wx.StaticBoxSizer(self.listBox, wx.VERTICAL)
  494. listSizer.Add(self.itemList, proportion=1, flag=wx.EXPAND | wx.ALL, border=3)
  495. btnSizer = wx.StdDialogButtonSizer()
  496. btnSizer.AddButton(self.btnCancel)
  497. btnSizer.AddButton(self.btnOk)
  498. btnSizer.Realize()
  499. sizer.Add(condSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=3)
  500. sizer.Add(
  501. listSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=3
  502. )
  503. sizer.Add(
  504. btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5
  505. )
  506. self.panel.SetSizer(sizer)
  507. sizer.Fit(self.panel)
  508. self.Layout()
  509. def GetItems(self):
  510. """Get list of selected actions"""
  511. return self.itemList.GetItems()
  512. def OnSeries(self, event):
  513. """Define map series as condition"""
  514. dialog = MapLayersDialogForModeler(
  515. parent=self, title=_("Define series of maps")
  516. )
  517. if dialog.ShowModal() != wx.ID_OK:
  518. dialog.Destroy()
  519. return
  520. cond = dialog.GetDSeries()
  521. if not cond:
  522. cond = "map in {}".format(list(map(str, dialog.GetMapLayers())))
  523. self.condText.SetValue(cond)
  524. dialog.Destroy()
  525. class ModelConditionDialog(ModelItemDialog):
  526. """Condition properties dialog"""
  527. def __init__(
  528. self,
  529. parent,
  530. shape,
  531. id=wx.ID_ANY,
  532. title=_("If-else properties"),
  533. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  534. **kwargs,
  535. ):
  536. ModelItemDialog.__init__(self, parent, shape, title, style=style, **kwargs)
  537. self.listBoxIf = StaticBox(
  538. parent=self.panel,
  539. id=wx.ID_ANY,
  540. label=" %s " % _("List of items in 'if' block"),
  541. )
  542. self.itemListIf = self.itemList
  543. self.itemListIf.SetName("IfBlockList")
  544. self.listBoxElse = StaticBox(
  545. parent=self.panel,
  546. id=wx.ID_ANY,
  547. label=" %s " % _("List of items in 'else' block"),
  548. )
  549. self.itemListElse = ItemCheckListCtrl(
  550. parent=self.panel,
  551. columns=[_("Label"), _("Command")],
  552. shape=shape,
  553. frame=parent,
  554. )
  555. self.itemListElse.SetName("ElseBlockList")
  556. self.itemListElse.Populate(self.parent.GetModel().GetItems())
  557. self._layout()
  558. self.SetMinSize(self.GetSize())
  559. self.SetSize((500, 400))
  560. def _layout(self):
  561. """Do layout"""
  562. sizer = wx.BoxSizer(wx.VERTICAL)
  563. condSizer = wx.StaticBoxSizer(self.condBox, wx.VERTICAL)
  564. condSizer.Add(self.condText, proportion=1, flag=wx.EXPAND)
  565. listIfSizer = wx.StaticBoxSizer(self.listBoxIf, wx.VERTICAL)
  566. listIfSizer.Add(self.itemListIf, proportion=1, flag=wx.EXPAND)
  567. listElseSizer = wx.StaticBoxSizer(self.listBoxElse, wx.VERTICAL)
  568. listElseSizer.Add(self.itemListElse, proportion=1, flag=wx.EXPAND)
  569. btnSizer = wx.StdDialogButtonSizer()
  570. btnSizer.AddButton(self.btnCancel)
  571. btnSizer.AddButton(self.btnOk)
  572. btnSizer.Realize()
  573. sizer.Add(condSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=3)
  574. sizer.Add(
  575. listIfSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=3
  576. )
  577. sizer.Add(
  578. listElseSizer, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=3
  579. )
  580. sizer.Add(
  581. btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5
  582. )
  583. self.panel.SetSizer(sizer)
  584. sizer.Fit(self.panel)
  585. self.Layout()
  586. def OnCheckItemIf(self, index, flag):
  587. """Item in if-block checked/unchecked"""
  588. if flag is False:
  589. return
  590. aId = int(self.itemListIf.GetItem(index, 0).GetText())
  591. if aId in self.itemListElse.GetItems()["checked"]:
  592. self.itemListElse.CheckItemById(aId, False)
  593. def OnCheckItemElse(self, index, flag):
  594. """Item in else-block checked/unchecked"""
  595. if flag is False:
  596. return
  597. aId = int(self.itemListElse.GetItem(index, 0).GetText())
  598. if aId in self.itemListIf.GetItems()["checked"]:
  599. self.itemListIf.CheckItemById(aId, False)
  600. def GetItems(self):
  601. """Get items"""
  602. return {"if": self.itemListIf.GetItems(), "else": self.itemListElse.GetItems()}
  603. class ModelListCtrl(ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.TextEditMixin):
  604. def __init__(
  605. self,
  606. parent,
  607. columns,
  608. frame,
  609. id=wx.ID_ANY,
  610. columnsNotEditable=[],
  611. style=wx.LC_REPORT | wx.BORDER_NONE | wx.LC_HRULES | wx.LC_VRULES,
  612. **kwargs,
  613. ):
  614. """List of model variables"""
  615. self.parent = parent
  616. self.columns = columns
  617. self.shape = None
  618. self.frame = frame
  619. self.columnNotEditable = columnsNotEditable
  620. ListCtrl.__init__(self, parent, id=id, style=style, **kwargs)
  621. listmix.ListCtrlAutoWidthMixin.__init__(self)
  622. listmix.TextEditMixin.__init__(self)
  623. i = 0
  624. for col in columns:
  625. self.InsertColumn(i, col)
  626. self.SetColumnWidth(i, wx.LIST_AUTOSIZE_USEHEADER)
  627. i += 1
  628. self.itemDataMap = {} # requested by sorter
  629. self.itemCount = 0
  630. self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnBeginEdit)
  631. self.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnEndEdit)
  632. self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick)
  633. self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnRightUp) # wxMSW
  634. self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp) # wxGTK
  635. def OnBeginEdit(self, event):
  636. """Editing of item started"""
  637. column = event.GetColumn()
  638. if self.columnNotEditable and column in self.columnNotEditable:
  639. event.Veto()
  640. self.SetItemState(
  641. event.GetIndex(),
  642. wx.LIST_STATE_SELECTED,
  643. wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED,
  644. )
  645. else:
  646. event.Allow()
  647. def OnEndEdit(self, event):
  648. """Finish editing of item"""
  649. pass
  650. def GetListCtrl(self):
  651. """Used by ColumnSorterMixin"""
  652. return self
  653. def OnColClick(self, event):
  654. """Click on column header (order by)"""
  655. event.Skip()
  656. class VariableListCtrl(ModelListCtrl):
  657. def __init__(self, parent, columns, **kwargs):
  658. """List of model variables"""
  659. ModelListCtrl.__init__(self, parent, columns, **kwargs)
  660. self.SetColumnWidth(2, 200) # default value
  661. def GetData(self):
  662. """Get list data"""
  663. return self.itemDataMap
  664. def Populate(self, data):
  665. """Populate the list"""
  666. self.itemDataMap = dict()
  667. i = 0
  668. for name, values in six.iteritems(data):
  669. self.itemDataMap[i] = [
  670. name,
  671. values["type"],
  672. values.get("value", ""),
  673. values.get("description", ""),
  674. ]
  675. i += 1
  676. self.itemCount = len(self.itemDataMap.keys())
  677. self.DeleteAllItems()
  678. i = 0
  679. for name, vtype, value, desc in six.itervalues(self.itemDataMap):
  680. index = self.InsertItem(i, name)
  681. self.SetItem(index, 0, name)
  682. self.SetItem(index, 1, vtype)
  683. self.SetItem(index, 2, value)
  684. self.SetItem(index, 3, desc)
  685. self.SetItemData(index, i)
  686. i += 1
  687. def Append(self, name, vtype, value, desc):
  688. """Append new item to the list
  689. :return: None on success
  690. :return: error string
  691. """
  692. for iname, ivtype, ivalue, idesc in six.itervalues(self.itemDataMap):
  693. if iname == name:
  694. return (
  695. _(
  696. "Variable <%s> already exists in the model. "
  697. "Adding variable failed."
  698. )
  699. % name
  700. )
  701. index = self.InsertItem(self.GetItemCount(), name)
  702. self.SetItem(index, 0, name)
  703. self.SetItem(index, 1, vtype)
  704. self.SetItem(index, 2, value)
  705. self.SetItem(index, 3, desc)
  706. self.SetItemData(index, self.itemCount)
  707. self.itemDataMap[self.itemCount] = [name, vtype, value, desc]
  708. self.itemCount += 1
  709. return None
  710. def OnRemove(self, event):
  711. """Remove selected variable(s) from the model"""
  712. item = self.GetFirstSelected()
  713. while item != -1:
  714. self.DeleteItem(item)
  715. del self.itemDataMap[item]
  716. item = self.GetFirstSelected()
  717. self.parent.UpdateModelVariables()
  718. event.Skip()
  719. def OnRemoveAll(self, event):
  720. """Remove all variable(s) from the model"""
  721. dlg = wx.MessageBox(
  722. parent=self,
  723. message=_("Do you want to delete all variables from " "the model?"),
  724. caption=_("Delete variables"),
  725. style=wx.YES_NO | wx.CENTRE,
  726. )
  727. if dlg != wx.YES:
  728. return
  729. self.DeleteAllItems()
  730. self.itemDataMap = dict()
  731. self.parent.UpdateModelVariables()
  732. def OnEndEdit(self, event):
  733. """Finish editing of item"""
  734. itemIndex = event.GetIndex()
  735. columnIndex = event.GetColumn()
  736. nameOld = self.GetItem(itemIndex, 0).GetText()
  737. if columnIndex == 0: # TODO
  738. event.Veto()
  739. self.itemDataMap[itemIndex][columnIndex] = event.GetText()
  740. self.parent.UpdateModelVariables()
  741. def OnReload(self, event):
  742. """Reload list of variables"""
  743. self.Populate(self.parent.parent.GetModel().GetVariables())
  744. def OnRightUp(self, event):
  745. """Mouse right button up"""
  746. if not hasattr(self, "popupID1"):
  747. self.popupID1 = NewId()
  748. self.popupID2 = NewId()
  749. self.popupID3 = NewId()
  750. self.Bind(wx.EVT_MENU, self.OnRemove, id=self.popupID1)
  751. self.Bind(wx.EVT_MENU, self.OnRemoveAll, id=self.popupID2)
  752. self.Bind(wx.EVT_MENU, self.OnReload, id=self.popupID3)
  753. # generate popup-menu
  754. menu = Menu()
  755. menu.Append(self.popupID1, _("Delete selected"))
  756. menu.Append(self.popupID2, _("Delete all"))
  757. if self.GetFirstSelected() == -1:
  758. menu.Enable(self.popupID1, False)
  759. menu.Enable(self.popupID2, False)
  760. menu.AppendSeparator()
  761. menu.Append(self.popupID3, _("Reload"))
  762. self.PopupMenu(menu)
  763. menu.Destroy()
  764. class ItemListCtrl(ModelListCtrl):
  765. def __init__(self, parent, columns, frame, disablePopup=False, **kwargs):
  766. """List of model actions"""
  767. self.disablePopup = disablePopup
  768. ModelListCtrl.__init__(self, parent, columns, frame, **kwargs)
  769. self.itemIdMap = list()
  770. self.SetColumnWidth(0, 100)
  771. self.SetColumnWidth(1, 75)
  772. if len(self.columns) >= 3:
  773. self.SetColumnWidth(2, 100)
  774. def GetData(self):
  775. """Get list data"""
  776. return self.itemDataMap
  777. def Populate(self, data):
  778. """Populate the list"""
  779. self.itemDataMap = dict()
  780. self.itemIdMap = list()
  781. if self.shape:
  782. items = self.frame.GetModel().GetItems(objType=ModelAction)
  783. if isinstance(self.shape, ModelCondition):
  784. if self.GetLabel() == "ElseBlockList":
  785. shapeItems = map(
  786. lambda x: x.GetId(), self.shape.GetItems(items)["else"]
  787. )
  788. else:
  789. shapeItems = map(
  790. lambda x: x.GetId(), self.shape.GetItems(items)["if"]
  791. )
  792. else:
  793. shapeItems = map(lambda x: x.GetId(), self.shape.GetItems(items))
  794. else:
  795. shapeItems = list()
  796. i = 0
  797. if len(self.columns) == 2: # ItemCheckList
  798. checked = list()
  799. for action in data:
  800. if isinstance(action, ModelData) or action == self.shape:
  801. continue
  802. self.itemIdMap.append(action.GetId())
  803. if len(self.columns) == 2:
  804. self.itemDataMap[i] = [action.GetLabel(), action.GetLog()]
  805. aId = action.GetBlockId()
  806. if action.GetId() in shapeItems:
  807. checked.append(aId)
  808. else:
  809. checked.append(None)
  810. else:
  811. bId = action.GetBlockId()
  812. if not bId:
  813. bId = _("No")
  814. else:
  815. bId = _("Yes")
  816. options = action.GetParameterizedParams()
  817. params = []
  818. for f in options["flags"]:
  819. params.append("-{0}".format(f["name"]))
  820. for p in options["params"]:
  821. params.append(p["name"])
  822. self.itemDataMap[i] = [
  823. action.GetLabel(),
  824. bId,
  825. ",".join(params),
  826. action.GetLog(),
  827. ]
  828. i += 1
  829. self.itemCount = len(self.itemDataMap.keys())
  830. self.DeleteAllItems()
  831. i = 0
  832. if len(self.columns) == 2:
  833. for name, desc in six.itervalues(self.itemDataMap):
  834. index = self.InsertItem(i, str(i))
  835. self.SetItem(index, 0, name)
  836. self.SetItem(index, 1, desc)
  837. self.SetItemData(index, i)
  838. if checked[i]:
  839. self.CheckItem(index, True)
  840. i += 1
  841. else:
  842. for name, inloop, param, desc in six.itervalues(self.itemDataMap):
  843. index = self.InsertItem(i, str(i))
  844. self.SetItem(index, 0, name)
  845. self.SetItem(index, 1, inloop)
  846. self.SetItem(index, 2, param)
  847. self.SetItem(index, 3, desc)
  848. self.SetItemData(index, i)
  849. i += 1
  850. def OnRemove(self, event):
  851. """Remove selected action(s) from the model"""
  852. model = self.frame.GetModel()
  853. canvas = self.frame.GetCanvas()
  854. item = self.GetFirstSelected()
  855. while item != -1:
  856. self.DeleteItem(item)
  857. del self.itemDataMap[item]
  858. action = model.GetItem(item + 1) # action indices starts at 1
  859. if not action:
  860. item = self.GetFirstSelected()
  861. continue
  862. canvas.RemoveShapes([action])
  863. self.frame.ModelChanged()
  864. item = self.GetFirstSelected()
  865. canvas.Refresh()
  866. event.Skip()
  867. def OnEndEdit(self, event):
  868. """Finish editing of item"""
  869. itemIndex = event.GetIndex()
  870. columnIndex = event.GetColumn()
  871. self.itemDataMap[itemIndex][columnIndex] = event.GetText()
  872. action = self.frame.GetModel().GetItem(itemIndex + 1)
  873. if not action:
  874. event.Veto()
  875. return
  876. action.SetLabel(label=event.GetText())
  877. self.frame.ModelChanged()
  878. def OnReload(self, event=None):
  879. """Reload list of actions"""
  880. self.Populate(self.frame.GetModel().GetItems(objType=ModelAction))
  881. def OnRightUp(self, event):
  882. """Mouse right button up"""
  883. if self.disablePopup:
  884. return
  885. if not hasattr(self, "popupId"):
  886. self.popupID = dict()
  887. self.popupID["remove"] = NewId()
  888. self.popupID["reload"] = NewId()
  889. self.Bind(wx.EVT_MENU, self.OnRemove, id=self.popupID["remove"])
  890. self.Bind(wx.EVT_MENU, self.OnReload, id=self.popupID["reload"])
  891. # generate popup-menu
  892. menu = Menu()
  893. menu.Append(self.popupID["remove"], _("Delete selected"))
  894. if self.GetFirstSelected() == -1:
  895. menu.Enable(self.popupID["remove"], False)
  896. menu.AppendSeparator()
  897. menu.Append(self.popupID["reload"], _("Reload"))
  898. self.PopupMenu(menu)
  899. menu.Destroy()
  900. def MoveItems(self, items, up):
  901. """Move items in the list
  902. :param items: list of items to move
  903. :param up: True to move up otherwise down
  904. """
  905. if len(items) < 1:
  906. return
  907. if items[0] == 0 and up:
  908. del items[0]
  909. if len(items) < 1:
  910. return
  911. if items[-1] == len(self.itemDataMap.keys()) - 1 and not up:
  912. del items[-1]
  913. if len(items) < 1:
  914. return
  915. model = self.frame.GetModel()
  916. modelActions = model.GetItems(objType=ModelAction)
  917. idxList = dict()
  918. itemsToSelect = list()
  919. for i in items:
  920. if up:
  921. idx = i - 1
  922. else:
  923. idx = i + 1
  924. itemsToSelect.append(idx)
  925. idxList[model.GetItemIndex(modelActions[i])] = model.GetItemIndex(
  926. modelActions[idx]
  927. )
  928. # reorganize model items
  929. model.ReorderItems(idxList)
  930. model.Normalize()
  931. self.Populate(model.GetItems(objType=ModelAction))
  932. # re-selected originally selected item
  933. for item in itemsToSelect:
  934. self.SetItemState(
  935. item,
  936. wx.LIST_STATE_SELECTED,
  937. wx.LIST_STATE_SELECTED | wx.LIST_STATE_FOCUSED,
  938. )
  939. class ItemCheckListCtrl(ItemListCtrl, CheckListCtrlMixin):
  940. def __init__(self, parent, shape, columns, frame, **kwargs):
  941. self.parent = parent
  942. self.frame = frame
  943. ItemListCtrl.__init__(self, parent, columns, frame, disablePopup=True, **kwargs)
  944. CheckListCtrlMixin.__init__(self)
  945. self.SetColumnWidth(0, 100)
  946. self.shape = shape
  947. def OnBeginEdit(self, event):
  948. """Disable editing"""
  949. event.Veto()
  950. def OnCheckItem(self, index, flag):
  951. """Item checked/unchecked"""
  952. name = self.GetLabel()
  953. if name == "IfBlockList" and self.window:
  954. self.window.OnCheckItemIf(index, flag)
  955. elif name == "ElseBlockList" and self.window:
  956. self.window.OnCheckItemElse(index, flag)
  957. def GetItems(self):
  958. """Get list of selected actions"""
  959. ids = {"checked": list(), "unchecked": list()}
  960. # action ids start at 1
  961. for i in range(self.GetItemCount()):
  962. if self.IsItemChecked(i):
  963. ids["checked"].append(self.itemIdMap[i])
  964. else:
  965. ids["unchecked"].append(self.itemIdMap[i])
  966. return ids
  967. def CheckItemById(self, aId, flag):
  968. """Check/uncheck given item by id"""
  969. for i in range(self.GetItemCount()):
  970. iId = int(self.GetItem(i, 0).GetText())
  971. if iId == aId:
  972. self.CheckItem(i, flag)
  973. break