dialogs.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. """!
  2. @package iclass::toolbars
  3. @brief wxIClass dialogs
  4. Classes:
  5. - dialogs::IClassGroupDialog
  6. - dialogs::IClassMapDialog
  7. - dialogs::IClassCategoryManagerDialog
  8. - dialogs::CategoryListCtrl
  9. - dialogs::IClassSignatureFileDialog
  10. (C) 2006-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Vaclav Petras <wenzeslaus gmail.com>
  15. @author Anna Kratochvilova <kratochanna gmail.com>
  16. """
  17. import os
  18. import wx
  19. import wx.lib.mixins.listctrl as listmix
  20. import wx.lib.scrolledpanel as scrolled
  21. from core import globalvar
  22. from gui_core.dialogs import ElementDialog, GroupDialog
  23. from gui_core import gselect
  24. from iclass.statistics import Statistics, BandStatistics
  25. import grass.script as grass
  26. class IClassGroupDialog(ElementDialog):
  27. """!Dialog for imagery group selection"""
  28. def __init__(self, parent, group = None, title = _("Select imagery group"), id = wx.ID_ANY):
  29. """!
  30. Does post init and layout.
  31. @param gui parent
  32. @param title dialog window title
  33. @param id wx id
  34. """
  35. ElementDialog.__init__(self, parent, title, label = _("Name of imagery group:"))
  36. self.element = gselect.Select(parent = self.panel, type = 'group',
  37. mapsets = [grass.gisenv()['MAPSET']],
  38. size = globalvar.DIALOG_GSELECT_SIZE)
  39. if group:
  40. self.element.SetValue(group)
  41. self.editGroup = wx.Button(parent = self.panel, id = wx.ID_ANY,
  42. label = _("Create/edit group..."))
  43. self.editGroup.Bind(wx.EVT_BUTTON, self.OnEditGroup)
  44. self.PostInit()
  45. self.__Layout()
  46. self.SetMinSize(self.GetSize())
  47. def __Layout(self):
  48. """!Do layout"""
  49. self.dataSizer.Add(self.element, proportion = 0,
  50. flag = wx.EXPAND | wx.ALL, border = 5)
  51. self.dataSizer.Add(self.editGroup, proportion = 0,
  52. flag = wx.ALL, border = 5)
  53. self.panel.SetSizer(self.sizer)
  54. self.sizer.Fit(self)
  55. def GetGroup(self):
  56. """!Returns selected group"""
  57. return self.GetElement()
  58. def OnEditGroup(self, event):
  59. """!Launch edit group dialog"""
  60. dlg = GroupDialog(parent = self, defaultGroup = self.element.GetValue())
  61. dlg.ShowModal()
  62. gr = dlg.GetSelectedGroup()
  63. if gr in dlg.GetExistGroups():
  64. self.element.SetValue(gr)
  65. dlg.Destroy()
  66. class IClassMapDialog(ElementDialog):
  67. """!Dialog for adding raster map"""
  68. def __init__(self, parent, title = _("Add raster map"), id = wx.ID_ANY):
  69. """!
  70. Does post init and layout.
  71. @param gui parent
  72. @param title dialog window title
  73. @param id wx id
  74. """
  75. ElementDialog.__init__(self, parent, title, label = _("Name of raster map:"))
  76. self.element = gselect.Select(parent = self.panel, type = 'raster',
  77. size = globalvar.DIALOG_GSELECT_SIZE)
  78. #self.firstMapCheck = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  79. #label = _("Add raster map to Training display"))
  80. #self.secondMapCheck = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
  81. #label = _("Add raster map to Preview display"))
  82. # in user settings
  83. #self.firstMapCheck.SetValue(True)
  84. #self.secondMapCheck.SetValue(True)
  85. self.PostInit()
  86. self.__Layout()
  87. self.SetMinSize(self.GetSize())
  88. def __Layout(self):
  89. """!Do layout"""
  90. self.dataSizer.Add(self.element, proportion = 0,
  91. flag = wx.EXPAND | wx.ALL, border = 5)
  92. #self.dataSizer.Add(self.firstMapCheck, proportion = 0,
  93. #flag = wx.ALL, border = 5)
  94. #self.dataSizer.Add(self.secondMapCheck, proportion = 0,
  95. #flag = wx.ALL, border = 5)
  96. self.panel.SetSizer(self.sizer)
  97. self.sizer.Fit(self)
  98. def GetRasterMap(self):
  99. """!Returns selected raster map"""
  100. return self.GetElement()
  101. #def IfAddToFirstMap(self):
  102. #return self.firstMapCheck.IsChecked()
  103. #def IfAddToSecondMap(self):
  104. #return self.secondMapCheck.IsChecked()
  105. class IClassCategoryManagerDialog(wx.Dialog):
  106. """!Dialog for managing categories (classes).
  107. Alows adding, deleting class and changing its name and color.
  108. """
  109. def __init__(self, parent, title = _("Class manager"), id = wx.ID_ANY):
  110. """!
  111. Does post init and layout.
  112. @param gui parent
  113. @param title dialog window title
  114. @param id wx id
  115. """
  116. wx.Dialog.__init__(self, parent = parent, title = title, id = id)
  117. panel = wx.Panel(parent = self, id = wx.ID_ANY)
  118. mainSizer = wx.BoxSizer(wx.VERTICAL)
  119. box = wx.StaticBox(panel, id = wx.ID_ANY,
  120. label = " %s " % _("Classes"))
  121. sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  122. gridSizer = wx.GridBagSizer(hgap = 5, vgap = 5)
  123. gridSizer.AddGrowableCol(0)
  124. gridSizer.AddGrowableRow(2)
  125. self.catList = CategoryListCtrl(panel, mapwindow = parent, statistics = parent.statisticsDict,
  126. statisticsList = parent.statisticsList)
  127. addButton = wx.Button(panel, id = wx.ID_ADD)
  128. deleteButton = wx.Button(panel, id = wx.ID_DELETE)
  129. gridSizer.Add(item = self.catList, pos = (0, 0), span = (3, 1), flag = wx.EXPAND)
  130. gridSizer.Add(item = addButton, pos = (0, 1), flag = wx.EXPAND)
  131. gridSizer.Add(item = deleteButton, pos = (1, 1), flag = wx.EXPAND)
  132. sizer.Add(item = gridSizer, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  133. mainSizer.Add(item = sizer, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
  134. btnSizer = wx.BoxSizer(wx.HORIZONTAL)
  135. closeButton = wx.Button(panel, id = wx.ID_CLOSE)
  136. btnSizer.Add(item = wx.Size(-1, -1), proportion = 1, flag = wx.EXPAND)
  137. btnSizer.Add(item = closeButton, proportion = 0, flag = wx.ALIGN_RIGHT)
  138. mainSizer.Add(item = btnSizer, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
  139. addButton.Bind(wx.EVT_BUTTON, self.OnAddCategory)
  140. deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteCategory)
  141. closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
  142. self.Bind(wx.EVT_CLOSE, self.OnClose)
  143. panel.SetSizer(mainSizer)
  144. mainSizer.Fit(panel)
  145. self.SetSize((-1, 250))
  146. self.Layout()
  147. def OnAddCategory(self, event):
  148. self.catList.AddCategory()
  149. def OnDeleteCategory(self, event):
  150. self.catList.DeleteCategory()
  151. def OnClose(self, event):
  152. self.catList.UpdateChoice()
  153. if not isinstance(event, wx.CloseEvent):
  154. self.Destroy()
  155. event.Skip()
  156. class CategoryListCtrl(wx.ListCtrl,
  157. listmix.ListCtrlAutoWidthMixin,
  158. listmix.TextEditMixin):
  159. """! Widget for controling list of classes (categories).
  160. CategoryListCtrl updates choice in mapwindow and removes raster map
  161. when deleting class (category).
  162. It uses virtual data in the terms of @c wx.ListCtrl.
  163. @todo statistics and categories are managed here directly,
  164. it could be better to use some interface
  165. @todo delete vector features after deleting class
  166. """
  167. def __init__(self, parent, mapwindow, statistics, statisticsList, id = wx.ID_ANY):
  168. """!
  169. @param parent gui parent
  170. @param mapwindow mapwindow instance with iclass toolbar and remove raster method
  171. @param statistics dictionary of statistics (defined in statistics.py)
  172. @param statisticsList list of statistics
  173. @param id wx id
  174. """
  175. wx.ListCtrl.__init__(self, parent, id,
  176. style = wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES)
  177. self.columns = ((_('Class name'), 'name'),
  178. (_('Color'), 'color'))
  179. self.Populate(columns = self.columns)
  180. self.mapWindow = mapwindow
  181. self.statisticsDict = statistics
  182. self.statisticsList = statisticsList
  183. self.SetItemCount(len(statisticsList))
  184. listmix.ListCtrlAutoWidthMixin.__init__(self)
  185. listmix.TextEditMixin.__init__(self)
  186. self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnEdit)
  187. def SetVirtualData(self, row, column, text):
  188. attr = self.columns[column][1]
  189. setattr(self.statisticsDict[self.statisticsList[row]], attr, text)
  190. self.UpdateChoice()
  191. toolbar = self.mapWindow.toolbars['iClass']
  192. toolbar.choice.SetSelection(row)
  193. if attr == 'name':
  194. self.mapWindow.UpdateRasterName(text, toolbar.GetSelectedCategoryIdx())
  195. self.mapWindow.UpdateChangeState(changes = True)
  196. def Populate(self, columns):
  197. for i, col in enumerate(columns):
  198. self.InsertColumn(i, col[0])#wx.LIST_FORMAT_RIGHT
  199. self.SetColumnWidth(0, 100)
  200. self.SetColumnWidth(1, 100)
  201. def AddCategory(self):
  202. st = Statistics()
  203. if self.statisticsList:
  204. cat = max(self.statisticsList) + 1
  205. else:
  206. cat = 1
  207. defaultName = 'class' + '_' + str(cat) # intentionally not translatable
  208. defaultColor = '0:0:0'
  209. st.SetBaseStatistics(cat = cat, name = defaultName, color = defaultColor)
  210. self.statisticsDict[cat] = st
  211. self.statisticsList.append(cat)
  212. self.SetItemCount(len(self.statisticsList))
  213. self.UpdateChoice()
  214. self.mapWindow.UpdateChangeState(changes = True)
  215. def DeleteCategory(self):
  216. indexList = sorted(self.GetSelectedIndices(), reverse = True)
  217. for i in indexList:
  218. # remove temporary raster
  219. name = self.statisticsDict[self.statisticsList[i]].rasterName
  220. self.mapWindow.RemoveTempRaster(name)
  221. del self.statisticsDict[self.statisticsList[i]]
  222. del self.statisticsList[i]
  223. self.SetItemCount(len(self.statisticsList))
  224. self.UpdateChoice()
  225. self.mapWindow.UpdateChangeState(changes = True)
  226. # delete vector items!
  227. def UpdateChoice(self):
  228. toolbar = self.mapWindow.toolbars['iClass']
  229. name = toolbar.GetSelectedCategoryName()
  230. catNames = []
  231. for cat in self.statisticsList:
  232. catNames.append(self.statisticsDict[cat].name)
  233. toolbar.SetCategories(catNames = catNames, catIdx = self.statisticsList)
  234. if name in catNames:
  235. toolbar.choice.SetStringSelection(name)
  236. elif catNames:
  237. toolbar.choice.SetSelection(0)
  238. if toolbar.choice.IsEmpty():
  239. toolbar.EnableControls(False)
  240. else:
  241. toolbar.EnableControls(True)
  242. # don't forget to update maps, histo, ...
  243. def GetSelectedIndices(self, state = wx.LIST_STATE_SELECTED):
  244. indices = []
  245. lastFound = -1
  246. while True:
  247. index = self.GetNextItem(lastFound, wx.LIST_NEXT_ALL, state)
  248. if index == -1:
  249. break
  250. else:
  251. lastFound = index
  252. indices.append(index)
  253. return indices
  254. def OnEdit(self, event):
  255. currentItem = event.m_itemIndex
  256. currentCol = event.m_col
  257. if currentCol == 1:
  258. dlg = wx.ColourDialog(self)
  259. dlg.GetColourData().SetChooseFull(True)
  260. if dlg.ShowModal() == wx.ID_OK:
  261. color = dlg.GetColourData().GetColour().Get()
  262. color = ':'.join(map(str, color))
  263. self.SetVirtualData(currentItem, currentCol, color)
  264. dlg.Destroy()
  265. event.Skip()
  266. def OnGetItemText(self, item, col):
  267. cat = self.statisticsList[item]
  268. return getattr(self.statisticsDict[cat], self.columns[col][1])
  269. def OnGetItemImage(self, item):
  270. return -1
  271. def OnGetItemAttr(self, item):
  272. return None
  273. class IClassSignatureFileDialog(wx.Dialog):
  274. def __init__(self, parent, group, file = None, title = _("Save signature file"), id = wx.ID_ANY,
  275. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
  276. **kwargs):
  277. """!Dialog for saving signature file
  278. @param parent window
  279. @param group group name
  280. @param file signature file name
  281. @param title window title
  282. """
  283. wx.Dialog.__init__(self, parent, id, title, style = style, **kwargs)
  284. self.fileName = file
  285. env = grass.gisenv()
  286. # inconsistent group and subgroup name
  287. # path: grassdata/nc_spm_08/landsat/group/test_group/subgroup/test_group@landsat/sig/sigFile
  288. self.baseFilePath = os.path.join(env['GISDBASE'],
  289. env['LOCATION_NAME'],
  290. env['MAPSET'],
  291. 'group', group.split('@')[0], # !
  292. 'subgroup', group,
  293. 'sig')
  294. self.panel = wx.Panel(parent = self, id = wx.ID_ANY)
  295. self.btnCancel = wx.Button(parent = self.panel, id = wx.ID_CANCEL)
  296. self.btnOK = wx.Button(parent = self.panel, id = wx.ID_OK)
  297. self.btnOK.SetDefault()
  298. self.btnOK.Enable(False)
  299. self.__layout()
  300. self.fileNameCtrl.Bind(wx.EVT_TEXT, self.OnTextChanged)
  301. self.OnTextChanged(None)
  302. def OnTextChanged(self, event):
  303. """!Name for signature file given"""
  304. file = self.fileNameCtrl.GetValue()
  305. if len(file) > 0:
  306. self.btnOK.Enable(True)
  307. else:
  308. self.btnOK.Enable(False)
  309. path = os.path.join(self.baseFilePath, file)
  310. self.filePathText.SetLabel(path)
  311. bestSize = self.pathPanel.GetBestVirtualSize()
  312. self.pathPanel.SetVirtualSize(bestSize)
  313. self.pathPanel.Scroll(*bestSize)
  314. def __layout(self):
  315. """!Do layout"""
  316. sizer = wx.BoxSizer(wx.VERTICAL)
  317. dataSizer = wx.BoxSizer(wx.VERTICAL)
  318. dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  319. label = _("Enter name of signature file:")),
  320. proportion = 0, flag = wx.ALL, border = 3)
  321. self.fileNameCtrl = wx.TextCtrl(parent = self.panel, id = wx.ID_ANY, size = (400, -1))
  322. if self.fileName:
  323. self.fileNameCtrl.SetValue(self.fileName)
  324. dataSizer.Add(item = self.fileNameCtrl,
  325. proportion = 0, flag = wx.ALL | wx.EXPAND, border = 3)
  326. dataSizer.Add(item = wx.StaticText(parent = self.panel, id = wx.ID_ANY,
  327. label = _("Signature file path:")),
  328. proportion = 0, flag = wx.ALL, border = 3)
  329. self.pathPanel = scrolled.ScrolledPanel(self.panel, size = (-1, 40))
  330. pathSizer = wx.BoxSizer()
  331. self.filePathText = wx.StaticText(parent = self.pathPanel, id = wx.ID_ANY,
  332. label = self.baseFilePath)
  333. pathSizer.Add(self.filePathText, proportion = 1, flag = wx.ALL | wx.EXPAND, border = 1)
  334. self.pathPanel.SetupScrolling(scroll_x = True, scroll_y = False)
  335. self.pathPanel.SetSizer(pathSizer)
  336. dataSizer.Add(item = self.pathPanel,
  337. proportion = 0, flag = wx.ALL | wx.EXPAND, border = 3)
  338. # buttons
  339. btnSizer = wx.StdDialogButtonSizer()
  340. btnSizer.AddButton(self.btnCancel)
  341. btnSizer.AddButton(self.btnOK)
  342. btnSizer.Realize()
  343. sizer.Add(item = dataSizer, proportion = 1,
  344. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  345. sizer.Add(item = btnSizer, proportion = 0,
  346. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  347. self.panel.SetSizer(sizer)
  348. sizer.Fit(self)
  349. self.SetMinSize(self.GetSize())
  350. def GetFileName(self, fullPath = False):
  351. """!Returns signature file name
  352. @param fullPath return full path of sig. file
  353. """
  354. if fullPath:
  355. return os.path.join(self.baseFilePath, self.fileNameCtrl.GetValue())
  356. return self.fileNameCtrl.GetValue()