dialogs.py 18 KB

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