dialogs.py 18 KB

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