dialogs.py 18 KB

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