File: dialogs.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 (849 lines) | stat: -rw-r--r-- 27,015 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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
"""
@package iclass.dialogs

@brief wxIClass dialogs

Classes:
 - dialogs::IClassGroupDialog
 - dialogs::IClassMapDialog
 - dialogs::IClassCategoryManagerDialog
 - dialogs::CategoryListCtrl
 - dialogs::IClassSignatureFileDialog
 - dialogs::IClassExportAreasDialog

(C) 2006-2011 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 Vaclav Petras <wenzeslaus gmail.com>
@author Anna Kratochvilova <kratochanna gmail.com>
"""

import os
import wx

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

from core import globalvar
from core.settings import UserSettings
from core.gcmd import GError, RunCommand, GMessage
from gui_core.dialogs import SimpleDialog, GroupDialog
from gui_core import gselect
from gui_core.widgets import SimpleValidator
from gui_core.wrap import (
    CheckBox,
    Button,
    StaticText,
    StaticBox,
    TextCtrl,
    Menu,
    NewId,
    ListCtrl,
)

import grass.script as grass


class IClassGroupDialog(SimpleDialog):
    """Dialog for imagery group selection"""

    def __init__(
        self,
        parent,
        group=None,
        subgroup=None,
        title=_("Select imagery group"),
        id=wx.ID_ANY,
    ):
        """
        Does post init and layout.

        :param parent: gui parent
        :param title: dialog window title
        :param id: wx id
        """
        SimpleDialog.__init__(self, parent, title)

        self.use_subg = True

        self.groupSelect = gselect.Select(
            parent=self.panel,
            type="group",
            mapsets=[grass.gisenv()["MAPSET"]],
            size=globalvar.DIALOG_GSELECT_SIZE,
            validator=SimpleValidator(callback=self.ValidatorCallback),
        )

        # TODO use when subgroup will be optional
        # self.subg_chbox = wx.CheckBox(parent = self.panel, id = wx.ID_ANY,
        #                              label = _("Use subgroup"))
        self.subGroupSelect = gselect.SubGroupSelect(parent=self.panel)

        self.groupSelect.SetFocus()
        if group:
            self.groupSelect.SetValue(group)
            self.GroupSelected()
        if subgroup:
            self.subGroupSelect.SetValue(subgroup)

        self.editGroup = Button(
            parent=self.panel, id=wx.ID_ANY, label=_("Create/edit group...")
        )

        self.editGroup.Bind(wx.EVT_BUTTON, self.OnEditGroup)
        self.groupSelect.GetTextCtrl().Bind(
            wx.EVT_TEXT, lambda event: wx.CallAfter(self.GroupSelected)
        )

        self.warning = _("Name of imagery group is missing.")
        self._layout()
        self.SetMinSize(self.GetSize())

    def _layout(self):
        """Do layout"""
        self.dataSizer.Add(
            StaticText(self.panel, id=wx.ID_ANY, label=_("Name of imagery group:")),
            proportion=0,
            flag=wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT,
            border=5,
        )
        self.dataSizer.Add(
            self.groupSelect, proportion=0, flag=wx.EXPAND | wx.ALL, border=5
        )

        # TODO use when subgroup will be optional
        # self.dataSizer.Add(self.subg_chbox, proportion = 0,
        # flag = wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, border = 5)

        self.dataSizer.Add(
            StaticText(self.panel, id=wx.ID_ANY, label=_("Name of imagery subgroup:")),
            proportion=0,
            flag=wx.EXPAND | wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT,
            border=5,
        )
        self.dataSizer.Add(
            self.subGroupSelect, proportion=0, flag=wx.EXPAND | wx.ALL, border=5
        )

        self.dataSizer.Add(self.editGroup, proportion=0, flag=wx.ALL, border=5)

        self.panel.SetSizer(self.sizer)
        self.sizer.Fit(self)

        # TODO use when subgroup will be optional
        # self.subg_panel.Show(False)
        # self.subg_chbox.Bind(wx.EVT_CHECKBOX, self.OnSubgChbox)

    def OnSubgChbox(self, event):
        self.use_subg = self.subg_chbox.GetValue()

        if self.use_subg:
            self.subg_panel.Show()
            # self.SubGroupSelected()
        else:
            self.subg_panel.Hide()
            # self.GroupSelected()
        self.SetMinSize(self.GetBestSize())
        self.Layout()

    def GetData(self):
        """Returns selected group and subgroup"""

        if self.use_subg:
            ret = (self.groupSelect.GetValue(), self.subGroupSelect.GetValue())
        else:
            ret = (self.groupSelect.GetValue(), None)

        return ret

    def OnEditGroup(self, event):
        """Launch edit group dialog"""
        g, s = self.GetData()
        dlg = GroupDialog(parent=self, defaultGroup=g, defaultSubgroup=s)

        dlg.ShowModal()
        gr, s = dlg.GetSelectedGroup()
        if gr in dlg.GetExistGroups():
            self.groupSelect.SetValue(gr)
            self.GroupSelected()
            wx.CallAfter(self.subGroupSelect.SetValue, s)
        dlg.Destroy()

    def GroupSelected(self):
        group = self.GetSelectedGroup()
        self.subGroupSelect.Insert(group)

    def GetSelectedGroup(self):
        """Return currently selected group (without mapset)"""
        return self.groupSelect.GetValue().split("@")[0]

    def GetGroupBandsErr(self, parent):
        """Get list of raster bands which are in the subgroup of group with both
        having same name. If the group does not exist, or it does not contain any bands
        in subgroup with same name, error dialog is shown.
        """
        gr, s = self.GetData()

        group = grass.find_file(name=gr, element="group")

        bands = []
        g = group["name"]

        if g:
            if self.use_subg:
                if s == "":
                    GError(_("Please choose a subgroup."), parent=parent)
                    return bands
                if s not in self.GetSubgroups(g):
                    GError(
                        _("Subgroup <%s> not found in group <%s>") % (s, g),
                        parent=parent,
                    )
                    return bands

            bands = self.GetGroupBands(g, s)
            if not bands:
                if self.use_subg:
                    GError(
                        _("No data found in subgroup <%s> of group <%s>.\n" ".")
                        % (s, g),
                        parent=parent,
                    )

                else:
                    GError(_("No data found in group <%s>.\n" ".") % g, parent=parent)
        else:
            GError(_("Group <%s> not found") % gr, parent=parent)

        return bands

    def GetGroupBands(self, group, subgroup):
        """Get list of raster bands which are in the subgroup of group with both having
        same name."""

        kwargs = {}
        if subgroup:
            kwargs["subgroup"] = subgroup

        res = RunCommand("i.group", flags="g", group=group, read=True, **kwargs).strip()
        bands = None
        if res.split("\n")[0]:
            bands = res.split("\n")

        return bands

    def GetSubgroups(self, group):
        return RunCommand("i.group", group=group, read=True, flags="sg").splitlines()


class IClassMapDialog(SimpleDialog):
    """Dialog for adding raster/vector map"""

    def __init__(self, parent, title, element):
        """

        :param parent: gui parent
        :param title: dialog title
        :param element: element type ('raster', 'vector')
        """

        SimpleDialog.__init__(self, parent, title=title)

        self.elementType = element
        self.element = gselect.Select(
            parent=self.panel,
            type=element,
            size=globalvar.DIALOG_GSELECT_SIZE,
            validator=SimpleValidator(callback=self.ValidatorCallback),
        )
        self.element.SetFocus()

        self.warning = _("Name of map is missing.")
        self._layout()
        self.SetMinSize(self.GetSize())

    def _layout(self):
        """Do layout"""
        if self.elementType == "raster":
            label = _("Name of raster map:")
        elif self.elementType == "vector":
            label = _("Name of vector map:")
        self.dataSizer.Add(
            StaticText(self.panel, id=wx.ID_ANY, label=label),
            proportion=0,
            flag=wx.EXPAND | wx.ALL,
            border=5,
        )
        self.dataSizer.Add(
            self.element, proportion=0, flag=wx.EXPAND | wx.ALL, border=5
        )

        self.panel.SetSizer(self.sizer)
        self.sizer.Fit(self)

    def GetMap(self):
        """Returns selected raster/vector map"""
        return self.element.GetValue()


class IClassCategoryManagerDialog(wx.Dialog):
    """Dialog for managing categories (classes).

    Allows adding, deleting class and changing its name and color.
    """

    def __init__(self, parent, title=_("Class manager"), id=wx.ID_ANY):
        """
        Does post init and layout.

        :param parent: gui parent
        :param title: dialog window title
        :param id: wx id
        """
        wx.Dialog.__init__(self, parent=parent, title=title, id=id)

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

        mainSizer = wx.BoxSizer(wx.VERTICAL)
        box = StaticBox(panel, id=wx.ID_ANY, label=" %s " % _("Classes"))
        sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
        gridSizer = wx.GridBagSizer(hgap=5, vgap=5)
        self.catList = CategoryListCtrl(
            panel, mapwindow=parent, stats_data=parent.stats_data
        )
        addButton = Button(panel, id=wx.ID_ADD)
        deleteButton = Button(panel, id=wx.ID_DELETE)

        gridSizer.Add(self.catList, pos=(0, 0), span=(3, 1), flag=wx.EXPAND)
        gridSizer.Add(addButton, pos=(0, 1), flag=wx.EXPAND)
        gridSizer.Add(deleteButton, pos=(1, 1), flag=wx.EXPAND)

        gridSizer.AddGrowableCol(0)
        gridSizer.AddGrowableRow(2)
        sizer.Add(gridSizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
        mainSizer.Add(sizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)

        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        closeButton = Button(panel, id=wx.ID_CLOSE)
        btnSizer.Add(wx.Size(-1, -1), proportion=1, flag=wx.EXPAND)
        btnSizer.Add(closeButton, proportion=0)
        mainSizer.Add(btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

        addButton.Bind(wx.EVT_BUTTON, self.OnAddCategory)
        deleteButton.Bind(wx.EVT_BUTTON, self.OnDeleteCategory)
        closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        panel.SetSizer(mainSizer)
        mainSizer.Fit(self)

        self.SetSize((400, 250))

        self.Layout()

    def OnAddCategory(self, event):
        if self.parent.stats_data.GetCategories():
            cat = max(self.parent.stats_data.GetCategories()) + 1
        else:
            cat = 1
        # intentionally not translatable
        defaultName = "class" + "_" + str(cat)
        defaultColor = "0:0:0"
        self.catList.AddCategory(cat=cat, name=defaultName, color=defaultColor)

    def OnDeleteCategory(self, event):
        self.catList.DeleteCategory()

    def OnClose(self, event):
        self.catList.DeselectAll()

        self.Hide()
        # if not isinstance(event, wx.CloseEvent):
        # self.Destroy()

        # event.Skip()

    def GetListCtrl(self):
        """Returns list widget"""
        return self.catList


class CategoryListCtrl(ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.TextEditMixin):
    """Widget for controlling list of classes (categories).

    CategoryListCtrl updates choice in mapwindow and removes raster map
    when deleting class (category).
    It uses virtual data in the terms of @c wx.ListCtrl.

    .. todo::
        delete vector features after deleting class
    """

    def __init__(self, parent, mapwindow, stats_data, id=wx.ID_ANY):
        """
        :param parent: gui parent
        :param mapwindow: mapwindow instance with iclass toolbar and remove raster
                          method
        :param stats_data: StatisticsData instance (defined in statistics.py)
        :param id: wx id
        """
        ListCtrl.__init__(
            self,
            parent,
            id,
            style=wx.LC_REPORT | wx.LC_VIRTUAL | wx.LC_HRULES | wx.LC_VRULES,
        )
        self.columns = ((_("Class name"), "name"), (_("Color"), "color"))
        self.Populate(columns=self.columns)
        self.mapWindow = mapwindow
        self.stats_data = stats_data
        self.SetItemCount(len(self.stats_data.GetCategories()))

        self.rightClickedItemIdx = wx.NOT_FOUND

        listmix.ListCtrlAutoWidthMixin.__init__(self)

        listmix.TextEditMixin.__init__(self)

        self.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnEdit)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnCategorySelected)

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

        self.stats_data.statisticsAdded.connect(self.Update)
        self.stats_data.statisticsDeleted.connect(self.Update)
        self.stats_data.allStatisticsDeleted.connect(self.Update)
        self.stats_data.statisticsSet.connect(self.Update)

    def Update(self):
        self.SetItemCount(len(self.stats_data.GetCategories()))

    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 = self.stats_data.GetCategories()[row]
        self.stats_data.GetStatistics(cat).SetStatistics(stats={attr: text})

        toolbar = self.mapWindow.toolbars["iClass"]
        toolbar.choice.SetSelection(row)
        self.Select(row)

        if attr == "name":
            self.mapWindow.UpdateRasterName(text, toolbar.GetSelectedCategoryIdx())

        self.mapWindow.UpdateChangeState(changes=True)

    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, cat, name, color):
        """Add category record (used when importing areas)"""

        self.stats_data.AddStatistics(cat, name, color)
        self.SetItemCount(len(self.stats_data.GetCategories()))

        self.mapWindow.UpdateChangeState(changes=True)

    def DeleteCategory(self):
        indexList = sorted(self.GetSelectedIndices(), reverse=True)
        del_cats = []
        cats = self.stats_data.GetCategories()

        for i in indexList:
            # remove temporary raster
            cat = cats[i]
            stat = self.stats_data.GetStatistics(cat)

            name = stat.rasterName
            self.mapWindow.RemoveTempRaster(name)

            del_cats.append(cat)
            self.stats_data.DeleteStatistics(cat)

        self.SetItemCount(len(self.stats_data.GetCategories()))

        self.mapWindow.UpdateChangeState(changes=True)

        self.mapWindow.DeleteAreas(cats=del_cats)

    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 OnEdit(self, event):
        currentItem = event.GetIndex()
        currentCol = event.GetColumn()
        if currentCol == 1:
            col = self.OnGetItemText(currentItem, currentCol)
            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.SetVirtualData(currentItem, currentCol, color)
            dlg.Destroy()
            wx.CallAfter(self.SetFocus)

        event.Skip()

    def OnCategorySelected(self, event):
        """Highlight selected areas"""
        indexList = self.GetSelectedIndices()
        sel_cats = []
        cats = self.stats_data.GetCategories()
        for i in indexList:
            sel_cats.append(cats[i])

        self.mapWindow.HighlightCategory(sel_cats)
        if event:
            event.Skip()

    def OnClassRightUp(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

        if not hasattr(self, "popupZoomtoAreas"):
            self.popupZoomtoAreas = NewId()
            self.Bind(wx.EVT_MENU, self.OnZoomToAreasByCat, id=self.popupZoomtoAreas)

        # generate popup-menu
        menu = Menu()
        menu.Append(
            self.popupZoomtoAreas, _("Zoom to training areas of selected class")
        )

        self.PopupMenu(menu)
        menu.Destroy()

    def OnZoomToAreasByCat(self, event):
        """Zoom to areas of given category"""
        cat = self.stats_data.GetCategories()[self.rightClickedItemIdx]
        self.mapWindow.ZoomToAreasByCat(cat)

    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):
        cat = self.stats_data.GetCategories()[item]
        stat = self.stats_data.GetStatistics(cat)
        return getattr(stat, self.columns[col][1])

    def OnGetItemImage(self, item):
        return -1

    def OnGetItemAttr(self, item):
        """Set correct class color for a item"""
        back_c = wx.Colour(*map(int, self.OnGetItemText(item, 1).split(":")))
        text_c = wx.Colour(*ContrastColor(back_c))

        # 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 ContrastColor(color):
    """Decides which value should have text to be contrast with background color
        (bright bg -> black, dark bg -> white)

    .. todo::
        could be useful by other apps, consider moving it into gui_core
    """
    # gacek,
    # https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
    a = 1 - (0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]) / 255

    if a < 0.5:
        d = 0
    else:
        d = 255
    # maybe return just bool if text should be dark or bright
    return (d, d, d)


class IClassSignatureFileDialog(wx.Dialog):
    def __init__(
        self,
        parent,
        file=None,
        title=_("Save signature file"),
        id=wx.ID_ANY,
        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
        **kwargs,
    ):
        """Dialog for saving signature file

        :param parent: window
        :param file: signature file name
        :param title: window title
        """
        wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)

        self.fileName = file

        env = grass.gisenv()

        # inconsistent group and subgroup name
        # path:
        # grassdata/nc_spm_08/landsat/signatures/sig/sigFile
        self.baseFilePath = os.path.join(
            env["GISDBASE"], env["LOCATION_NAME"], env["MAPSET"], "signatures", "sig"
        )
        self.panel = wx.Panel(parent=self, id=wx.ID_ANY)

        self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
        self.btnOK = Button(parent=self.panel, id=wx.ID_OK)
        self.btnOK.SetDefault()
        self.btnOK.Enable(False)

        self.__layout()

        self.fileNameCtrl.Bind(wx.EVT_TEXT, self.OnTextChanged)
        self.OnTextChanged(None)

    def OnTextChanged(self, event):
        """Name for signature file given"""
        file = self.fileNameCtrl.GetValue()
        if len(file) > 0:
            self.btnOK.Enable(True)
        else:
            self.btnOK.Enable(False)

        path = os.path.join(self.baseFilePath, file)
        self.filePathText.SetLabel(path)
        bestSize = self.pathPanel.GetBestVirtualSize()
        self.pathPanel.SetVirtualSize(bestSize)
        self.pathPanel.Scroll(*bestSize)

    def __layout(self):
        """Do layout"""
        sizer = wx.BoxSizer(wx.VERTICAL)

        dataSizer = wx.BoxSizer(wx.VERTICAL)

        dataSizer.Add(
            StaticText(
                parent=self.panel,
                id=wx.ID_ANY,
                label=_("Enter name of signature file:"),
            ),
            proportion=0,
            flag=wx.ALL,
            border=3,
        )
        self.fileNameCtrl = TextCtrl(parent=self.panel, id=wx.ID_ANY, size=(400, -1))
        if self.fileName:
            self.fileNameCtrl.SetValue(self.fileName)
        dataSizer.Add(
            self.fileNameCtrl, proportion=0, flag=wx.ALL | wx.EXPAND, border=3
        )

        dataSizer.Add(
            StaticText(
                parent=self.panel, id=wx.ID_ANY, label=_("Signature file path:")
            ),
            proportion=0,
            flag=wx.ALL,
            border=3,
        )

        self.pathPanel = scrolled.ScrolledPanel(self.panel, size=(-1, 40))
        pathSizer = wx.BoxSizer()
        self.filePathText = StaticText(
            parent=self.pathPanel, id=wx.ID_ANY, label=self.baseFilePath
        )
        pathSizer.Add(
            self.filePathText, proportion=1, flag=wx.ALL | wx.EXPAND, border=1
        )
        self.pathPanel.SetupScrolling(scroll_x=True, scroll_y=False)
        self.pathPanel.SetSizer(pathSizer)

        dataSizer.Add(self.pathPanel, proportion=0, flag=wx.ALL | wx.EXPAND, border=3)

        # buttons
        btnSizer = wx.StdDialogButtonSizer()
        btnSizer.AddButton(self.btnCancel)
        btnSizer.AddButton(self.btnOK)
        btnSizer.Realize()

        sizer.Add(dataSizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)

        sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

        self.panel.SetSizer(sizer)
        sizer.Fit(self)

        self.SetMinSize(self.GetSize())

    def GetFileName(self, fullPath=False):
        """Returns signature file name

        :param fullPath: return full path of sig. file
        """
        if fullPath:
            return os.path.join(self.baseFilePath, self.fileNameCtrl.GetValue())

        return self.fileNameCtrl.GetValue()


class IClassExportAreasDialog(wx.Dialog):
    def __init__(
        self,
        parent,
        vectorName=None,
        title=_("Export training areas"),
        id=wx.ID_ANY,
        style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
        **kwargs,
    ):
        """Dialog for export of training areas to vector layer

        :param parent: window
        :param vectorName: name of vector layer for export
        :param title: window title
        """
        wx.Dialog.__init__(self, parent, id, title, style=style, **kwargs)

        self.vectorName = vectorName
        self.panel = wx.Panel(parent=self, id=wx.ID_ANY)

        self.btnCancel = Button(parent=self.panel, id=wx.ID_CANCEL)
        self.btnOK = Button(parent=self.panel, id=wx.ID_OK)
        self.btnOK.SetDefault()
        self.btnOK.Enable(False)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnOK)

        self.__layout()

        self.vectorNameCtrl.Bind(wx.EVT_TEXT, self.OnTextChanged)
        self.OnTextChanged(None)
        wx.CallAfter(self.vectorNameCtrl.SetFocus)

    def OnTextChanged(self, event):
        """Name of new vector map given.

        Enable/disable OK button.
        """
        file = self.vectorNameCtrl.GetValue()
        if len(file) > 0:
            self.btnOK.Enable(True)
        else:
            self.btnOK.Enable(False)

    def __layout(self):
        """Do layout"""
        sizer = wx.BoxSizer(wx.VERTICAL)

        dataSizer = wx.BoxSizer(wx.VERTICAL)

        dataSizer.Add(
            StaticText(
                parent=self.panel,
                id=wx.ID_ANY,
                label=_("Enter name of new vector map:"),
            ),
            proportion=0,
            flag=wx.ALL,
            border=3,
        )
        self.vectorNameCtrl = gselect.Select(
            parent=self.panel,
            type="vector",
            mapsets=[grass.gisenv()["MAPSET"]],
            size=globalvar.DIALOG_GSELECT_SIZE,
        )
        if self.vectorName:
            self.vectorNameCtrl.SetValue(self.vectorName)
        dataSizer.Add(
            self.vectorNameCtrl, proportion=0, flag=wx.ALL | wx.EXPAND, border=3
        )
        self.withTableCtrl = CheckBox(
            parent=self.panel, id=wx.ID_ANY, label=_("Export attribute table")
        )
        self.withTableCtrl.SetValue(True)
        self.withTableCtrl.SetToolTip(
            _("Export attribute table containing" " computed statistical data")
        )

        dataSizer.Add(self.withTableCtrl, proportion=0, flag=wx.ALL, border=3)

        # buttons
        btnSizer = wx.StdDialogButtonSizer()
        btnSizer.AddButton(self.btnCancel)
        btnSizer.AddButton(self.btnOK)
        btnSizer.Realize()

        sizer.Add(dataSizer, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)

        sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

        self.panel.SetSizer(sizer)
        sizer.Fit(self)

        self.SetMinSize(self.GetSize())

    def GetVectorName(self):
        """Returns vector name"""
        return self.vectorNameCtrl.GetValue()

    def WithTable(self):
        """Returns true if attribute table should be exported too"""
        return self.withTableCtrl.IsChecked()

    def OnOK(self, event):
        """Checks if map exists and can be overwritten."""
        overwrite = UserSettings.Get(group="cmd", key="overwrite", subkey="enabled")
        vName = self.GetVectorName()
        res = grass.find_file(vName, element="vector")
        if res["fullname"] and overwrite is False:
            qdlg = wx.MessageDialog(
                parent=self,
                message=_(
                    "Vector map <%s> already exists."
                    " Do you want to overwrite it?" % vName
                ),
                caption=_("Vector <%s> exists" % vName),
                style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION | wx.CENTRE,
            )
            if qdlg.ShowModal() == wx.ID_YES:
                event.Skip()
            qdlg.Destroy()
        else:
            event.Skip()