File: frame.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (721 lines) | stat: -rw-r--r-- 22,792 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
"""
@package iscatt.frame

@brief Main scatter plot widgets.

Classes:
 - frame::IClassIScattPanel
 - frame::IScattDialog
 - frame::MapDispIScattPanel
 - frame::ScatterPlotsPanel
 - frame::CategoryListCtrl

(C) 2013 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Stepan Turek <stepan.turek seznam.cz> (mentor: Martin Landa)
"""

import os

import wx
import wx.lib.scrolledpanel as scrolled
import wx.lib.mixins.listctrl as listmix

from core import globalvar
from core.gcmd import GError, GMessage

from gui_core.dialogs import SetOpacityDialog
from gui_core.wrap import StaticBox, Menu, ListCtrl
from iscatt.controllers import ScattsManager
from iscatt.toolbars import MainToolbar, EditingToolbar, CategoryToolbar
from iscatt.iscatt_core import idScattToidBands
from iscatt.dialogs import ManageBusyCursorMixin, RenameClassDialog
from iscatt.plots import ScatterPlotWidget
from iclass.dialogs import ContrastColor

try:
    from agw import aui
except ImportError:
    import wx.lib.agw.aui as aui


class IClassIScattPanel(wx.Panel, ManageBusyCursorMixin):
    def __init__(self, parent, giface, iclass_mapwin=None, id=wx.ID_ANY):
        # wx.SplitterWindow.__init__(self, parent = parent, id = id,
        #                           style = wx.SP_LIVE_UPDATE)
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
        ManageBusyCursorMixin.__init__(self, window=self)

        self.scatt_mgr = self._createScattMgr(
            guiparent=parent, giface=giface, iclass_mapwin=iclass_mapwin
        )

        # toobars
        self.toolbars = {}
        self.toolbars["mainToolbar"] = self._createMainToolbar()
        self.toolbars["editingToolbar"] = EditingToolbar(
            parent=self, scatt_mgr=self.scatt_mgr
        )

        self._createCategoryPanel(self)

        self.plot_panel = ScatterPlotsPanel(self, self.scatt_mgr)

        self.mainsizer = wx.BoxSizer(wx.VERTICAL)
        self.mainsizer.Add(self.toolbars["mainToolbar"], proportion=0, flag=wx.EXPAND)
        self.mainsizer.Add(
            self.toolbars["editingToolbar"], proportion=0, flag=wx.EXPAND
        )
        self.mainsizer.Add(
            self.catsPanel, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5
        )
        self.mainsizer.Add(self.plot_panel, proportion=1, flag=wx.EXPAND)

        self.catsPanel.Hide()
        self.toolbars["editingToolbar"].Hide()

        self.SetSizer(self.mainsizer)

        self.scatt_mgr.computingStarted.connect(lambda: self.UpdateCur(busy=True))
        self.scatt_mgr.renderingStarted.connect(lambda: self.UpdateCur(busy=True))
        self.scatt_mgr.renderingFinished.connect(lambda: self.UpdateCur(busy=False))

        # self.SetSashGravity(0.5)
        # self.SplitHorizontally(self.head_panel, self.plot_panel, -50)
        self.Layout()

    def CloseWindow(self):
        self.scatt_mgr.CleanUp()

    def UpdateCur(self, busy):
        self.plot_panel.SetBusy(busy)
        ManageBusyCursorMixin.UpdateCur(self, busy)

    def _selCatInIScatt(self):
        return False

    def _createMainToolbar(self):
        return MainToolbar(parent=self, scatt_mgr=self.scatt_mgr)

    def _createScattMgr(self, guiparent, giface, iclass_mapwin):
        return ScattsManager(guiparent=self, giface=giface, iclass_mapwin=iclass_mapwin)

    def NewScatterPlot(self, scatt_id, transpose):
        return self.plot_panel.NewScatterPlot(scatt_id, transpose)

    def ShowPlotEditingToolbar(self, show):
        self.toolbars["editingToolbar"].Show(show)
        self.Layout()

    def ShowCategoryPanel(self, show):
        self.catsPanel.Show(show)

        # if show:
        #    self.SetSashSize(5)
        # else:
        #    self.SetSashSize(0)
        self.plot_panel.SetVirtualSize(self.plot_panel.GetBestVirtualSize())
        self.Layout()

    def _createCategoryPanel(self, parent):
        self.catsPanel = wx.Panel(parent=parent)
        self.cats_list = CategoryListCtrl(
            parent=self.catsPanel,
            cats_mgr=self.scatt_mgr.GetCategoriesManager(),
            sel_cats_in_iscatt=self._selCatInIScatt(),
        )

        self.catsPanel.SetMinSize((-1, 100))
        self.catsPanel.SetInitialSize((-1, 150))

        box_capt = StaticBox(
            parent=self.catsPanel,
            id=wx.ID_ANY,
            label=" %s " % _("Classes"),
        )
        catsSizer = wx.StaticBoxSizer(box_capt, wx.VERTICAL)

        self.toolbars["categoryToolbar"] = self._createCategoryToolbar(self.catsPanel)

        catsSizer.Add(self.cats_list, proportion=1, flag=wx.EXPAND | wx.TOP, border=5)
        if self.toolbars["categoryToolbar"]:
            catsSizer.Add(self.toolbars["categoryToolbar"], proportion=0)

        self.catsPanel.SetSizer(catsSizer)

    def _createCategoryToolbar(self, parent):
        return CategoryToolbar(
            parent=parent, scatt_mgr=self.scatt_mgr, cats_list=self.cats_list
        )


class IScattDialog(wx.Dialog):
    def __init__(
        self,
        parent,
        giface,
        title=_("Interactive Scatter Plot Tool"),
        id=wx.ID_ANY,
        style=wx.DEFAULT_FRAME_STYLE,
        **kwargs,
    ):
        wx.Dialog.__init__(self, parent, id, style=style, title=title, **kwargs)
        self.SetIcon(
            wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"), wx.BITMAP_TYPE_ICO)
        )

        self.iscatt_panel = MapDispIScattPanel(self, giface)

        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.Add(self.iscatt_panel, proportion=1, flag=wx.EXPAND)

        self.SetSizer(mainsizer)

        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

        self.SetMinSize((300, 300))

    def OnCloseWindow(self, event):
        event.Skip()
        # self.


class MapDispIScattPanel(IClassIScattPanel):
    def __init__(self, parent, giface, id=wx.ID_ANY, **kwargs):
        IClassIScattPanel.__init__(self, parent=parent, giface=giface, id=id, **kwargs)

    def _createScattMgr(self, guiparent, giface, iclass_mapwin):
        return ScattsManager(guiparent=self, giface=giface)

    def _createMainToolbar(self):
        return MainToolbar(
            parent=self, scatt_mgr=self.scatt_mgr, opt_tools=["add_group"]
        )

    def _selCatInIScatt(self):
        return True


class ScatterPlotsPanel(scrolled.ScrolledPanel):
    def __init__(self, parent, scatt_mgr, id=wx.ID_ANY):
        scrolled.ScrolledPanel.__init__(self, parent)
        self.SetupScrolling(scroll_x=False, scroll_y=True, scrollToTop=False)

        self.scatt_mgr = scatt_mgr

        self.mainPanel = wx.Panel(parent=self, id=wx.ID_ANY)

        # self._createCategoryPanel()
        # Fancy gui
        self._mgr = aui.AuiManager(self.mainPanel)
        # self._mgr.SetManagedWindow(self)

        self._mgr.Update()

        self._doLayout()
        self.Bind(wx.EVT_SCROLLWIN, self.OnScroll)
        self.Bind(wx.EVT_SCROLL_CHANGED, self.OnScrollChanged)

        self.Bind(aui.EVT_AUI_PANE_CLOSE, self.OnPlotPaneClosed)

        dlgSize = (-1, 400)
        # self.SetBestSize(dlgSize)
        # self.SetInitialSize(dlgSize)
        self.SetAutoLayout(1)
        # fix goutput's pane size (required for Mac OSX)
        # if self.gwindow:
        #    self.gwindow.SetSashPosition(int(self.GetSize()[1] * .75))
        self.ignore_scroll = 0
        self.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheel)

        self.scatt_i = 1
        self.scatt_id_scatt_i = {}
        self.transpose = {}
        self.scatts = {}

        self.Bind(wx.EVT_CLOSE, self.OnClose)

        self.scatt_mgr.cursorPlotMove.connect(self.CursorPlotMove)

    def SetBusy(self, busy):
        for scatt in self.scatts.values():
            scatt.UpdateCur(busy)

    def CursorPlotMove(self, x, y, scatt_id):
        try:
            x = int(round(x))
            y = int(round(y))
            coords = True
        except:
            coords = False

        pane = self._getPane(scatt_id)
        caption = self._creteCaption(scatt_id)
        if coords:
            caption += "   %d, %d" % (x, y)

        pane.Caption(caption)
        self._mgr.RefreshCaptions()

    def _getPane(self, scatt_id):
        scatt_i = self.scatt_id_scatt_i[scatt_id]
        name = self._getScatterPlotName(scatt_i)
        return self._mgr.GetPane(name)

    def ScatterPlotClosed(self, scatt_id):
        scatt_i = self.scatt_id_scatt_i[scatt_id]

        name = self._getScatterPlotName(scatt_i)
        pane = self._mgr.GetPane(name)

        del self.scatt_id_scatt_i[scatt_id]
        del self.scatts[scatt_id]

        if pane.IsOk():
            pane.DestroyOnClose()
            self._mgr.ClosePane(pane)
        self._mgr.Update()

    def OnMouseWheel(self, event):
        # TODO very ugly find some better solution
        self.ignore_scroll = 3
        event.Skip()

    def ScrollChildIntoView(self, child):
        # For aui manager it does not work and returns position always to the
        # top -> deactivated.
        pass

    def OnPlotPaneClosed(self, event):
        if isinstance(event.pane.window, ScatterPlotWidget):
            event.pane.window.CleanUp()

    def OnScrollChanged(self, event):
        wx.CallAfter(self.Layout)

    def OnScroll(self, event):
        if self.ignore_scroll > 0:
            self.ignore_scroll -= 1
        else:
            event.Skip()

        # wx.CallAfter(self._mgr.Update)
        # wx.CallAfter(self.Layout)

    def _doLayout(self):
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        mainsizer.Add(self.mainPanel, proportion=1, flag=wx.EXPAND)
        self.SetSizer(mainsizer)

        self.Layout()
        self.SetupScrolling()

    def OnClose(self, event):
        """Close dialog"""
        # TODO: why todo here, why print? just delete?
        print("closed")
        self.scatt_mgr.CleanUp()
        self.Destroy()

    def OnSettings(self, event):
        pass

    def _newScatterPlotName(self, scatt_id):
        name = self._getScatterPlotName(self.scatt_i)
        self.scatt_id_scatt_i[scatt_id] = self.scatt_i
        self.scatt_i += 1
        return name

    def _getScatterPlotName(self, i):
        name = "scatter plot %d" % i
        return name

    def NewScatterPlot(self, scatt_id, transpose):
        # TODO needs to be resolved (should be in this class)

        scatt = ScatterPlotWidget(
            parent=self.mainPanel,
            scatt_mgr=self.scatt_mgr,
            scatt_id=scatt_id,
            transpose=transpose,
        )
        scatt.plotClosed.connect(self.ScatterPlotClosed)
        self.transpose[scatt_id] = transpose

        caption = self._creteCaption(scatt_id)
        self._mgr.AddPane(
            scatt,
            aui.AuiPaneInfo()
            .Dockable(True)
            .Floatable(True)
            .Name(self._newScatterPlotName(scatt_id))
            .MinSize((-1, 300))
            .Caption(caption)
            .Center()
            .Position(1)
            .MaximizeButton(True)
            .MinimizeButton(True)
            .CaptionVisible(True)
            .CloseButton(True)
            .Layer(0),
        )

        self._mgr.Update()

        self.SetVirtualSize(self.GetBestVirtualSize())
        self.Layout()

        self.scatts[scatt_id] = scatt

        return scatt

    def _creteCaption(self, scatt_id):
        transpose = self.transpose[scatt_id]
        bands = self.scatt_mgr.GetBands()

        # TODO too low level
        b1_id, b2_id = idScattToidBands(scatt_id, len(bands))

        x_b = bands[b1_id].split("@")[0]
        y_b = bands[b2_id].split("@")[0]

        if transpose:
            tmp = x_b
            x_b = y_b
            y_b = tmp

        return "%s x: %s y: %s" % (_("scatter plot"), x_b, y_b)

    def GetScattMgr(self):
        return self.scatt_mgr


class CategoryListCtrl(ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, cats_mgr, sel_cats_in_iscatt, id=wx.ID_ANY):
        ListCtrl.__init__(
            self,
            parent,
            id,
            style=wx.LC_REPORT
            | wx.LC_VIRTUAL
            | wx.LC_HRULES
            | wx.LC_VRULES
            | wx.LC_SINGLE_SEL
            | wx.LC_NO_HEADER,
        )
        self.columns = ((_("Class name"), "name"),)
        # (_('Color'), 'color'))

        self.sel_cats_in_iscatt = sel_cats_in_iscatt

        self.Populate(columns=self.columns)

        self.cats_mgr = cats_mgr
        self.SetItemCount(len(self.cats_mgr.GetCategories()))

        self.rightClickedItemIdx = wx.NOT_FOUND

        listmix.ListCtrlAutoWidthMixin.__init__(self)

        # listmix.TextEditMixin.__init__(self)

        self.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.OnCategoryRightUp)  # wxMSW
        self.Bind(wx.EVT_RIGHT_UP, self.OnCategoryRightUp)  # wxGTK

        # self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnEdit)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSel)

        self.cats_mgr.setCategoryAttrs.connect(self.Update)
        self.cats_mgr.deletedCategory.connect(self.Update)
        self.cats_mgr.addedCategory.connect(self.Update)

    def Update(self, **kwargs):
        self.SetItemCount(len(self.cats_mgr.GetCategories()))
        if len(self.cats_mgr.GetCategories()):
            self.RefreshItems(0, len(self.cats_mgr.GetCategories()) - 1)

    def SetVirtualData(self, row, column, text):
        attr = self.columns[column][1]
        if attr == "name":
            try:
                text.encode("ascii")
            except UnicodeEncodeError:
                GMessage(parent=self, message=_("Please use only ASCII characters."))
                return

        cat_id = self.cats_mgr.GetCategories()[row]

        self.cats_mgr.setCategoryAttrs.disconnect(self.Update)
        self.cats_mgr.SetCategoryAttrs(cat_id, {attr: text})
        self.cats_mgr.setCategoryAttrs.connect(self.Update)

        self.Select(row)

    def Populate(self, columns):
        for i, col in enumerate(columns):
            self.InsertColumn(i, col[0])  # wx.LIST_FORMAT_RIGHT

        # self.SetColumnWidth(0, 100)
        # self.SetColumnWidth(1, 100)

    def AddCategory(self):
        self.cats_mgr.addedCategory.disconnect(self.Update)
        cat_id = self.cats_mgr.AddCategory()
        self.cats_mgr.addedCategory.connect(self.Update)

        if cat_id < 0:
            GError(_("Maximum limit of categories number was reached."))
            return
        self.SetItemCount(len(self.cats_mgr.GetCategories()))

    def DeleteCategory(self):
        indexList = sorted(self.GetSelectedIndices(), reverse=True)
        cats = []
        for i in indexList:
            # remove temporary raster
            cat_id = self.cats_mgr.GetCategories()[i]

            cats.append(cat_id)

            self.cats_mgr.deletedCategory.disconnect(self.Update)
            self.cats_mgr.DeleteCategory(cat_id)
            self.cats_mgr.deletedCategory.connect(self.Update)

        self.SetItemCount(len(self.cats_mgr.GetCategories()))

    def OnSel(self, event):
        if self.sel_cats_in_iscatt:
            indexList = self.GetSelectedIndices()
            sel_cats = []
            cats = self.cats_mgr.GetCategories()
            for i in indexList:
                sel_cats.append(cats[i])

            if sel_cats:
                self.cats_mgr.SetSelectedCat(sel_cats[0])
        event.Skip()

    def GetSelectedIndices(self, state=wx.LIST_STATE_SELECTED):
        indices = []
        lastFound = -1
        while True:
            index = self.GetNextItem(lastFound, wx.LIST_NEXT_ALL, state)
            if index == -1:
                break
            else:
                lastFound = index
                indices.append(index)
        return indices

    def DeselectAll(self):
        """Deselect all items"""
        indexList = self.GetSelectedIndices()
        for i in indexList:
            self.Select(i, on=0)

        # no highlight
        self.OnCategorySelected(None)

    def OnGetItemText(self, item, col):
        attr = self.columns[col][1]
        cat_id = self.cats_mgr.GetCategories()[item]

        return self.cats_mgr.GetCategoryAttrs(cat_id)[attr]

    def OnGetItemImage(self, item):
        return -1

    def OnGetItemAttr(self, item):
        cat_id = self.cats_mgr.GetCategories()[item]

        cattr = self.cats_mgr.GetCategoryAttrs(cat_id)

        if cattr["show"]:
            c = cattr["color"]

            back_c = wx.Colour(*map(int, c.split(":")))
            text_c = wx.Colour(*ContrastColor(back_c))
        else:
            back_c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTION)
            text_c = wx.SystemSettings.GetColour(wx.SYS_COLOUR_INACTIVECAPTIONTEXT)

        # if it is in scope of the method, gui falls, using self solved it
        self.l = wx.ItemAttr()
        self.l.SetBackgroundColour(back_c)
        self.l.SetTextColour(text_c)
        return self.l

    def OnCategoryRightUp(self, event):
        """Show context menu on right click"""
        item, flags = self.HitTest((event.GetX(), event.GetY()))
        if item != wx.NOT_FOUND and flags & wx.LIST_HITTEST_ONITEM:
            self.rightClickedItemIdx = item

        # generate popup-menu
        cat_idx = self.rightClickedItemIdx

        cats = self.cats_mgr.GetCategories()
        cat_id = cats[cat_idx]
        showed = self.cats_mgr.GetCategoryAttrs(cat_id)["show"]

        menu = Menu()

        item = menu.Append(wx.ID_ANY, _("Rename class"))
        self.Bind(wx.EVT_MENU, self.OnRename, item)

        item = menu.Append(wx.ID_ANY, _("Set color"))
        self.Bind(wx.EVT_MENU, self.OnSetColor, item)

        item = menu.Append(wx.ID_ANY, _("Change opacity level"))
        self.Bind(wx.EVT_MENU, self.OnPopupOpacityLevel, item)

        if showed:
            text = _("Hide")
        else:
            text = _("Show")

        item = menu.Append(wx.ID_ANY, text)
        self.Bind(
            wx.EVT_MENU,
            lambda event: self._setCatAttrs(cat_id=cat_id, attrs={"show": not showed}),
            item,
        )

        menu.AppendSeparator()

        item = menu.Append(wx.ID_ANY, _("Move to top"))
        self.Bind(wx.EVT_MENU, self.OnMoveTop, item)
        if cat_idx == 0:
            menu.Enable(item.GetId(), False)

        item = menu.Append(wx.ID_ANY, _("Move to bottom"))
        self.Bind(wx.EVT_MENU, self.OnMoveBottom, item)
        if cat_idx == len(cats) - 1:
            menu.Enable(item.GetId(), False)

        menu.AppendSeparator()

        item = menu.Append(wx.ID_ANY, _("Move category up"))
        self.Bind(wx.EVT_MENU, self.OnMoveUp, item)
        if cat_idx == 0:
            menu.Enable(item.GetId(), False)

        item = menu.Append(wx.ID_ANY, _("Move category down"))
        self.Bind(wx.EVT_MENU, self.OnMoveDown, item)
        if cat_idx == len(cats) - 1:
            menu.Enable(item.GetId(), False)

        menu.AppendSeparator()

        item = menu.Append(wx.ID_ANY, _("Export class raster"))
        self.Bind(wx.EVT_MENU, self.OnExportCatRast, item)

        self.PopupMenu(menu)
        menu.Destroy()

    def OnExportCatRast(self, event):
        """Export training areas"""
        # TODO
        #   GMessage(parent=self, message=_("No class raster to export."))
        #    return

        cat_idx = self.rightClickedItemIdx
        cat_id = self.cats_mgr.GetCategories()[cat_idx]

        self.cats_mgr.ExportCatRast(cat_id)

    def OnMoveUp(self, event):
        cat_idx = self.rightClickedItemIdx
        cat_id = self.cats_mgr.GetCategories()[cat_idx]
        self.cats_mgr.ChangePosition(cat_id, cat_idx - 1)
        self.RefreshItems(0, len(self.cats_mgr.GetCategories()))

    def OnMoveDown(self, event):
        cat_idx = self.rightClickedItemIdx
        cat_id = self.cats_mgr.GetCategories()[cat_idx]
        self.cats_mgr.ChangePosition(cat_id, cat_idx + 1)
        self.RefreshItems(0, len(self.cats_mgr.GetCategories()))

    def OnMoveTop(self, event):
        cat_idx = self.rightClickedItemIdx
        cat_id = self.cats_mgr.GetCategories()[cat_idx]
        self.cats_mgr.ChangePosition(cat_id, 0)
        self.RefreshItems(0, len(self.cats_mgr.GetCategories()))

    def OnMoveBottom(self, event):
        cat_idx = self.rightClickedItemIdx
        cats = self.cats_mgr.GetCategories()
        cat_id = cats[cat_idx]
        self.cats_mgr.ChangePosition(cat_id, len(cats) - 1)
        self.RefreshItems(0, len(self.cats_mgr.GetCategories()))

    def OnSetColor(self, event):
        """Popup opacity level indicator"""
        cat_idx = self.rightClickedItemIdx
        cat_id = self.cats_mgr.GetCategories()[cat_idx]

        col = self.cats_mgr.GetCategoryAttrs(cat_id)["color"]
        col = map(int, col.split(":"))

        col_data = wx.ColourData()
        col_data.SetColour(wx.Colour(*col))

        dlg = wx.ColourDialog(self, col_data)
        dlg.GetColourData().SetChooseFull(True)

        if dlg.ShowModal() == wx.ID_OK:
            color = dlg.GetColourData().GetColour().Get()
            color = ":".join(map(str, color))
            self.cats_mgr.SetCategoryAttrs(cat_id, {"color": color})

        dlg.Destroy()

    def OnPopupOpacityLevel(self, event):
        """Popup opacity level indicator"""

        cat_id = self.cats_mgr.GetCategories()[self.rightClickedItemIdx]
        cat_attrs = self.cats_mgr.GetCategoryAttrs(cat_id)
        value = cat_attrs["opacity"] * 100
        name = cat_attrs["name"]

        dlg = SetOpacityDialog(
            self, opacity=value, title=_("Change opacity of class <%s>" % name)
        )

        dlg.applyOpacity.connect(
            lambda value: self._setCatAttrs(cat_id=cat_id, attrs={"opacity": value})
        )
        dlg.CentreOnParent()

        if dlg.ShowModal() == wx.ID_OK:
            self._setCatAttrs(cat_id=cat_id, attrs={"opacity": value})

        dlg.Destroy()

    def OnRename(self, event):
        cat_id = self.cats_mgr.GetCategories()[self.rightClickedItemIdx]
        cat_attrs = self.cats_mgr.GetCategoryAttrs(cat_id)

        dlg = RenameClassDialog(self, old_name=cat_attrs["name"])
        dlg.CentreOnParent()

        while True:
            if dlg.ShowModal() == wx.ID_OK:
                name = dlg.GetNewName().strip()
                if not name:
                    GMessage(parent=self, message=_("Empty name was inserted."))
                else:
                    self.cats_mgr.SetCategoryAttrs(cat_id, {"name": name})
                    break
            else:
                break

        dlg.Destroy()

    def _setCatAttrs(self, cat_id, attrs):
        self.cats_mgr.setCategoryAttrs.disconnect(self.Update)
        self.cats_mgr.SetCategoryAttrs(cat_id, attrs)
        self.cats_mgr.setCategoryAttrs.connect(self.Update)