dialogs.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. """
  2. @package iscatt.dialogs
  3. @brief Dialogs widgets.
  4. Classes:
  5. - dialogs::AddScattPlotDialog
  6. - dialogs::ExportCategoryRaster
  7. - dialogs::SettingsDialog
  8. - dialogs::ManageBusyCursorMixin
  9. - dialogs::RenameClassDialog
  10. (C) 2013 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 Stepan Turek <stepan.turek seznam.cz> (mentor: Martin Landa)
  14. """
  15. import six
  16. import wx
  17. from gui_core.gselect import Select
  18. import wx.lib.colourselect as csel
  19. import grass.script as grass
  20. from core import globalvar
  21. from core.gcmd import GMessage
  22. from core.settings import UserSettings
  23. from gui_core.dialogs import SimpleDialog
  24. from gui_core.wrap import SpinCtrl, Button, StaticText, StaticBox, TextCtrl
  25. class AddScattPlotDialog(wx.Dialog):
  26. def __init__(self, parent, bands, check_bands_callback, id=wx.ID_ANY):
  27. wx.Dialog.__init__(self, parent, title=_("Add scatter plots"), id=id)
  28. self.bands = bands
  29. self.x_band = None
  30. self.y_band = None
  31. self.chb_callback = check_bands_callback
  32. self.added_bands_ids = []
  33. self.sel_bands_ids = []
  34. self._createWidgets()
  35. def _createWidgets(self):
  36. self.labels = {}
  37. self.params = {}
  38. self.band_1_label = StaticText(parent=self, id=wx.ID_ANY, label=_("x axis:"))
  39. self.band_1_ch = wx.ComboBox(
  40. parent=self,
  41. id=wx.ID_ANY,
  42. choices=self.bands,
  43. style=wx.CB_READONLY,
  44. size=(350, 30),
  45. )
  46. self.band_2_label = StaticText(parent=self, id=wx.ID_ANY, label=_("y axis:"))
  47. self.band_2_ch = wx.ComboBox(
  48. parent=self,
  49. id=wx.ID_ANY,
  50. choices=self.bands,
  51. style=wx.CB_READONLY,
  52. size=(350, 30),
  53. )
  54. self.scattsBox = wx.ListBox(
  55. parent=self,
  56. id=wx.ID_ANY,
  57. size=(-1, 150),
  58. style=wx.LB_MULTIPLE | wx.LB_NEEDED_SB,
  59. )
  60. # buttons
  61. self.btn_add = Button(parent=self, id=wx.ID_ADD)
  62. self.btn_remove = Button(parent=self, id=wx.ID_REMOVE)
  63. self.btn_close = Button(parent=self, id=wx.ID_CANCEL)
  64. self.btn_ok = Button(parent=self, id=wx.ID_OK)
  65. self._layout()
  66. def _layout(self):
  67. border = wx.BoxSizer(wx.VERTICAL)
  68. dialogSizer = wx.BoxSizer(wx.VERTICAL)
  69. regionSizer = wx.BoxSizer(wx.HORIZONTAL)
  70. dialogSizer.Add(
  71. self._addSelectSizer(title=self.band_1_label, sel=self.band_1_ch)
  72. )
  73. dialogSizer.Add(
  74. self._addSelectSizer(title=self.band_2_label, sel=self.band_2_ch)
  75. )
  76. dialogSizer.Add(
  77. self.btn_add, proportion=0, flag=wx.TOP | wx.ALIGN_RIGHT, border=5
  78. )
  79. box = StaticBox(
  80. self,
  81. id=wx.ID_ANY,
  82. label=" %s " % _("Bands of scatter plots to be added (x y):"),
  83. )
  84. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  85. sizer.Add(self.scattsBox, proportion=1, flag=wx.EXPAND | wx.TOP, border=5)
  86. sizer.Add(self.btn_remove, proportion=0, flag=wx.TOP | wx.ALIGN_RIGHT, border=5)
  87. dialogSizer.Add(sizer, proportion=1, flag=wx.EXPAND | wx.TOP, border=5)
  88. # buttons
  89. self.btnsizer = wx.BoxSizer(orient=wx.HORIZONTAL)
  90. self.btnsizer.Add(
  91. self.btn_close,
  92. proportion=0,
  93. flag=wx.RIGHT | wx.LEFT | wx.ALIGN_CENTER,
  94. border=10,
  95. )
  96. self.btnsizer.Add(
  97. self.btn_ok,
  98. proportion=0,
  99. flag=wx.RIGHT | wx.LEFT | wx.ALIGN_CENTER,
  100. border=10,
  101. )
  102. dialogSizer.Add(
  103. self.btnsizer, proportion=0, flag=wx.ALIGN_CENTER | wx.TOP, border=5
  104. )
  105. border.Add(
  106. dialogSizer, proportion=0, flag=wx.LEFT | wx.RIGHT | wx.BOTTOM, border=10
  107. )
  108. self.SetSizer(border)
  109. self.Layout()
  110. self.Fit()
  111. # bindings
  112. self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
  113. self.btn_ok.Bind(wx.EVT_BUTTON, self.OnOk)
  114. self.btn_add.Bind(wx.EVT_BUTTON, self.OnAdd)
  115. self.btn_remove.Bind(wx.EVT_BUTTON, self.OnRemoveLayer)
  116. def OnOk(self, event):
  117. if not self.GetBands():
  118. GMessage(parent=self, message=_("No scatter plots selected."))
  119. return
  120. event.Skip()
  121. def _addSelectSizer(self, title, sel):
  122. """Helper layout function."""
  123. selSizer = wx.BoxSizer(orient=wx.VERTICAL)
  124. selTitleSizer = wx.BoxSizer(wx.HORIZONTAL)
  125. selTitleSizer.Add(title, proportion=1, flag=wx.TOP | wx.EXPAND, border=5)
  126. selSizer.Add(selTitleSizer, proportion=0, flag=wx.EXPAND)
  127. selSizer.Add(sel, proportion=1, flag=wx.EXPAND | wx.TOP, border=5)
  128. return selSizer
  129. def GetBands(self):
  130. """Get layers"""
  131. return self.sel_bands_ids
  132. def OnClose(self, event):
  133. """Close dialog"""
  134. if not self.IsModal():
  135. self.Destroy()
  136. event.Skip()
  137. def OnRemoveLayer(self, event):
  138. """Remove layer from listbox"""
  139. while self.scattsBox.GetSelections():
  140. sel = self.scattsBox.GetSelections()[0]
  141. self.scattsBox.Delete(sel)
  142. self.sel_bands_ids.pop(sel)
  143. def OnAdd(self, event):
  144. """"""
  145. b_x = self.band_1_ch.GetSelection()
  146. b_y = self.band_2_ch.GetSelection()
  147. if b_x < 0 or b_y < 0:
  148. GMessage(parent=self, message=_("Select both x and y bands."))
  149. return
  150. if b_y == b_x:
  151. GMessage(parent=self, message=_("Selected bands must be different."))
  152. return
  153. if [b_x, b_y] in self.sel_bands_ids or [b_y, b_x] in self.sel_bands_ids:
  154. GMessage(
  155. parent=self,
  156. message=_(
  157. "Scatter plot with same bands combination (regardless x y order) "
  158. "has been already added into the list."
  159. ),
  160. )
  161. return
  162. if not self.chb_callback(b_x, b_y):
  163. return
  164. self.sel_bands_ids.append([b_x, b_y])
  165. b_x_str = self.band_1_ch.GetStringSelection()
  166. b_y_str = self.band_2_ch.GetStringSelection()
  167. text = b_x_str + " " + b_y_str
  168. self.scattsBox.Append(text)
  169. event.Skip()
  170. class ExportCategoryRaster(wx.Dialog):
  171. def __init__(
  172. self,
  173. parent,
  174. title,
  175. rasterName=None,
  176. id=wx.ID_ANY,
  177. style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  178. **kwargs,
  179. ):
  180. """Dialog for export of category raster.
  181. :param parent: window
  182. :param str rasterName name of vector layer for export
  183. :param title: window title
  184. """
  185. wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)
  186. self.rasterName = rasterName
  187. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  188. self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
  189. self.btnOK = Button(parent=self.panel, id=wx.ID_OK)
  190. self.btnOK.SetDefault()
  191. self.btnOK.Enable(False)
  192. self.btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
  193. self.__layout()
  194. self.vectorNameCtrl.Bind(wx.EVT_TEXT, self.OnTextChanged)
  195. self.OnTextChanged(None)
  196. wx.CallAfter(self.vectorNameCtrl.SetFocus)
  197. def OnTextChanged(self, event):
  198. """Name of new vector map given.
  199. Enable/diable OK button.
  200. """
  201. file = self.vectorNameCtrl.GetValue()
  202. if len(file) > 0:
  203. self.btnOK.Enable(True)
  204. else:
  205. self.btnOK.Enable(False)
  206. def __layout(self):
  207. """Do layout"""
  208. sizer = wx.BoxSizer(wx.VERTICAL)
  209. dataSizer = wx.BoxSizer(wx.VERTICAL)
  210. dataSizer.Add(
  211. StaticText(
  212. parent=self.panel,
  213. id=wx.ID_ANY,
  214. label=_("Enter name of new vector map:"),
  215. ),
  216. proportion=0,
  217. flag=wx.ALL,
  218. border=3,
  219. )
  220. self.vectorNameCtrl = Select(
  221. parent=self.panel,
  222. type="raster",
  223. mapsets=[grass.gisenv()["MAPSET"]],
  224. size=globalvar.DIALOG_GSELECT_SIZE,
  225. )
  226. if self.rasterName:
  227. self.vectorNameCtrl.SetValue(self.rasterName)
  228. dataSizer.Add(
  229. self.vectorNameCtrl, proportion=0, flag=wx.ALL | wx.EXPAND, border=3
  230. )
  231. # buttons
  232. btnSizer = wx.StdDialogButtonSizer()
  233. btnSizer.AddButton(self.btnCancel)
  234. btnSizer.AddButton(self.btnOK)
  235. btnSizer.Realize()
  236. sizer.Add(
  237. dataSizer, proportion=1, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5
  238. )
  239. sizer.Add(
  240. btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5
  241. )
  242. self.panel.SetSizer(sizer)
  243. sizer.Fit(self)
  244. self.SetMinSize(self.GetSize())
  245. def GetRasterName(self):
  246. """Returns vector name"""
  247. return self.vectorNameCtrl.GetValue()
  248. def OnOK(self, event):
  249. """Checks if map exists and can be overwritten."""
  250. overwrite = UserSettings.Get(group="cmd", key="overwrite", subkey="enabled")
  251. rast_name = self.GetRasterName()
  252. res = grass.find_file(rast_name, element="cell")
  253. if res["fullname"] and overwrite is False:
  254. qdlg = wx.MessageDialog(
  255. parent=self,
  256. message=_(
  257. "Raster map <%s> already exists."
  258. " Do you want to overwrite it?" % rast_name
  259. ),
  260. caption=_("Raster <%s> exists" % rast_name),
  261. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION | wx.CENTRE,
  262. )
  263. if qdlg.ShowModal() == wx.ID_YES:
  264. event.Skip()
  265. qdlg.Destroy()
  266. else:
  267. event.Skip()
  268. class SettingsDialog(wx.Dialog):
  269. def __init__(
  270. self,
  271. parent,
  272. id,
  273. title,
  274. scatt_mgr,
  275. pos=wx.DefaultPosition,
  276. size=wx.DefaultSize,
  277. style=wx.DEFAULT_DIALOG_STYLE,
  278. ):
  279. """Settings dialog"""
  280. wx.Dialog.__init__(self, parent, id, title, pos, size, style)
  281. self.scatt_mgr = scatt_mgr
  282. maxValue = 1e8
  283. self.parent = parent
  284. self.settings = {}
  285. settsLabels = {}
  286. self.settings["show_ellips"] = wx.CheckBox(
  287. parent=self, id=wx.ID_ANY, label=_("Show confidence ellipses")
  288. )
  289. show_ellips = UserSettings.Get(
  290. group="scatt", key="ellipses", subkey="show_ellips"
  291. )
  292. self.settings["show_ellips"].SetValue(show_ellips)
  293. self.colorsSetts = {
  294. "sel_pol": ["selection", _("Selection polygon color:")],
  295. "sel_pol_vertex": ["selection", _("Color of selection polygon vertex:")],
  296. "sel_area": ["selection", _("Selected area color:")],
  297. }
  298. for settKey, sett in six.iteritems(self.colorsSetts):
  299. settsLabels[settKey] = StaticText(parent=self, id=wx.ID_ANY, label=sett[1])
  300. col = UserSettings.Get(group="scatt", key=sett[0], subkey=settKey)
  301. self.settings[settKey] = csel.ColourSelect(
  302. parent=self, id=wx.ID_ANY, colour=wx.Colour(col[0], col[1], col[2], 255)
  303. )
  304. self.sizeSetts = {
  305. "snap_tresh": ["selection", _("Snapping threshold in pixels:")],
  306. "sel_area_opacty": ["selection", _("Selected area opacity:")],
  307. }
  308. for settKey, sett in six.iteritems(self.sizeSetts):
  309. settsLabels[settKey] = StaticText(parent=self, id=wx.ID_ANY, label=sett[1])
  310. self.settings[settKey] = SpinCtrl(parent=self, id=wx.ID_ANY, min=0, max=100)
  311. size = int(UserSettings.Get(group="scatt", key=sett[0], subkey=settKey))
  312. self.settings[settKey].SetValue(size)
  313. # buttons
  314. self.btnSave = Button(self, wx.ID_SAVE)
  315. self.btnApply = Button(self, wx.ID_APPLY)
  316. self.btnClose = Button(self, wx.ID_CLOSE)
  317. self.btnApply.SetDefault()
  318. # bindings
  319. self.btnApply.Bind(wx.EVT_BUTTON, self.OnApply)
  320. self.btnApply.SetToolTip(_("Apply changes for the current session"))
  321. self.btnSave.Bind(wx.EVT_BUTTON, self.OnSave)
  322. self.btnSave.SetToolTip(
  323. _(
  324. "Apply and save changes to user settings file (default for next sessions)"
  325. )
  326. )
  327. self.btnClose.Bind(wx.EVT_BUTTON, self.OnClose)
  328. self.btnClose.SetToolTip(_("Close dialog"))
  329. # Layout
  330. # Analysis result style layout
  331. self.SetMinSize(self.GetBestSize())
  332. sizer = wx.BoxSizer(wx.VERTICAL)
  333. sel_pol_box = StaticBox(
  334. parent=self, id=wx.ID_ANY, label=" %s " % _("Selection style:")
  335. )
  336. selPolBoxSizer = wx.StaticBoxSizer(sel_pol_box, wx.VERTICAL)
  337. gridSizer = wx.GridBagSizer(vgap=1, hgap=1)
  338. row = 0
  339. setts = dict()
  340. setts.update(self.colorsSetts)
  341. setts.update(self.sizeSetts)
  342. settsOrder = [
  343. "sel_pol",
  344. "sel_pol_vertex",
  345. "sel_area",
  346. "sel_area_opacty",
  347. "snap_tresh",
  348. ]
  349. for settKey in settsOrder:
  350. sett = setts[settKey]
  351. gridSizer.Add(
  352. settsLabels[settKey], flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0)
  353. )
  354. gridSizer.Add(
  355. self.settings[settKey],
  356. flag=wx.ALIGN_RIGHT | wx.ALL,
  357. border=5,
  358. pos=(row, 1),
  359. )
  360. row += 1
  361. gridSizer.AddGrowableCol(1)
  362. selPolBoxSizer.Add(gridSizer, flag=wx.EXPAND)
  363. ell_box = StaticBox(
  364. parent=self, id=wx.ID_ANY, label=" %s " % _("Ellipses settings:")
  365. )
  366. ellPolBoxSizer = wx.StaticBoxSizer(ell_box, wx.VERTICAL)
  367. gridSizer = wx.GridBagSizer(vgap=1, hgap=1)
  368. sett = setts[settKey]
  369. row = 0
  370. gridSizer.Add(
  371. self.settings["show_ellips"], flag=wx.ALIGN_CENTER_VERTICAL, pos=(row, 0)
  372. )
  373. gridSizer.AddGrowableCol(0)
  374. ellPolBoxSizer.Add(gridSizer, flag=wx.EXPAND)
  375. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  376. btnSizer.Add(self.btnApply, flag=wx.LEFT | wx.RIGHT, border=5)
  377. btnSizer.Add(self.btnSave, flag=wx.LEFT | wx.RIGHT, border=5)
  378. btnSizer.Add(self.btnClose, flag=wx.LEFT | wx.RIGHT, border=5)
  379. sizer.Add(selPolBoxSizer, flag=wx.EXPAND | wx.ALL, border=5)
  380. sizer.Add(ellPolBoxSizer, flag=wx.EXPAND | wx.ALL, border=5)
  381. sizer.Add(btnSizer, flag=wx.EXPAND | wx.ALL, border=5, proportion=0)
  382. self.SetSizer(sizer)
  383. sizer.Fit(self)
  384. def OnSave(self, event):
  385. """Button 'Save' pressed"""
  386. self.UpdateSettings()
  387. fileSettings = {}
  388. UserSettings.ReadSettingsFile(settings=fileSettings)
  389. fileSettings["scatt"] = UserSettings.Get(group="scatt")
  390. UserSettings.SaveToFile(fileSettings)
  391. self.Close()
  392. def UpdateSettings(self):
  393. chanaged_setts = []
  394. for settKey, sett in six.iteritems(self.colorsSetts):
  395. col = tuple(self.settings[settKey].GetColour())
  396. col_s = UserSettings.Get(group="scatt", key=sett[0], subkey=settKey)
  397. if col_s != col:
  398. UserSettings.Set(group="scatt", key=sett[0], subkey=settKey, value=col)
  399. chanaged_setts.append([settKey, sett[0]])
  400. for settKey, sett in six.iteritems(self.sizeSetts):
  401. val = self.settings[settKey].GetValue()
  402. val_s = UserSettings.Get(group="scatt", key=sett[0], subkey=settKey)
  403. if val_s != val:
  404. UserSettings.Set(group="scatt", key=sett[0], subkey=settKey, value=val)
  405. chanaged_setts.append([settKey, sett[0]])
  406. val = self.settings["show_ellips"].IsChecked()
  407. val_s = UserSettings.Get(group="scatt", key="ellipses", subkey="show_ellips")
  408. if val != val_s:
  409. UserSettings.Set(
  410. group="scatt", key="ellipses", subkey="show_ellips", value=val
  411. )
  412. chanaged_setts.append(["ellipses", "show_ellips"])
  413. if chanaged_setts:
  414. self.scatt_mgr.SettingsUpdated(chanaged_setts)
  415. def OnApply(self, event):
  416. """Button 'Apply' pressed"""
  417. self.UpdateSettings()
  418. # self.Close()
  419. def OnClose(self, event):
  420. """Button 'Cancel' pressed"""
  421. self.Close()
  422. class ManageBusyCursorMixin:
  423. def __init__(self, window):
  424. self.win = window
  425. self.is_busy = False
  426. self.cur_inside = False
  427. self.win.Bind(wx.EVT_ENTER_WINDOW, self.OnEnter)
  428. self.win.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
  429. def OnLeave(self, event):
  430. self.cur_inside = False
  431. self.busy_cur = None
  432. def OnEnter(self, event):
  433. self.cur_inside = True
  434. if self.is_busy:
  435. self.busy_cur = wx.BusyCursor()
  436. def UpdateCur(self, busy):
  437. self.is_busy = busy
  438. if self.cur_inside and self.is_busy:
  439. self.busy_cur = wx.BusyCursor()
  440. return
  441. self.busy_cur = None
  442. class RenameClassDialog(SimpleDialog):
  443. def __init__(self, parent, old_name, title=("Change class name")):
  444. SimpleDialog.__init__(self, parent, title)
  445. self.name = TextCtrl(self.panel, id=wx.ID_ANY)
  446. self.name.SetValue(old_name)
  447. self.dataSizer.Add(self.name, proportion=0, flag=wx.EXPAND | wx.ALL, border=1)
  448. self.panel.SetSizer(self.sizer)
  449. self.name.SetMinSize((200, -1))
  450. self.sizer.Fit(self)
  451. def GetNewName(self):
  452. return self.name.GetValue()