File: gselect.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (2695 lines) | stat: -rw-r--r-- 96,737 bytes parent folder | download
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
"""
@package gui_core.gselect

@brief Custom control that selects elements

Classes:
 - :class:`Select`
 - :class:`VectorSelect`
 - :class:`ListCtrlComboPopup`
 - :class:`TreeCrtlComboPopup`
 - :class:`VectorDBInfo`
 - :class:`LayerSelect`
 - :class:`DriverSelect`
 - :class:`DatabaseSelect`
 - :class:`TableSelect`
 - :class:`ColumnSelect`
 - :class:`DbaseSelect`
 - :class:`LocationSelect`
 - :class:`MapsetSelect`
 - :class:`SubGroupSelect`
 - :class:`FormatSelect`
 - :class:`GdalSelect`
 - :class:`ProjSelect`
 - :class:`ElementSelect`
 - :class:`OgrTypeSelect`
 - :class:`CoordinatesSelect`
 - :class:`VectorCategorySelect`
 - :class:`SignatureSelect`
 - :class:`SeparatorSelect`

(C) 2007-2014 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 Michael Barton
@author Martin Landa <landa.martin gmail.com>
@author Vaclav Petras <wenzeslaus gmail.com> (menu customization)
@author Stepan Turek <stepan.turek seznam.cz> (CoordinatesSelect, ListCtrlComboPopup)
@author Matej Krejci <matejkrejci gmail.com> (VectorCategorySelect)
"""

import os
import sys
import glob
import copy

import wx
import wx.combo
import wx.lib.buttons as buttons
import wx.lib.filebrowsebutton as filebrowse

from core import globalvar

import grass.script as grass
from grass.script import task as gtask
from grass.exceptions import CalledModuleError
try:
    from grass.pygrass import messages
except ImportError as e:
    print >> sys.stderr, _("Unable to import pyGRASS: %s\n"
                           "Some functionality will be not accessible") % e

from gui_core.widgets import ManageSettingsWidget, CoordinatesValidator

from core.gcmd import RunCommand, GError, GMessage, GWarning
from core.utils    import GetListOfLocations, GetListOfMapsets, \
    GetFormats, rasterFormatExtension, vectorFormatExtension
from core.utils import GetSettingsPath, GetValidLayerName, ListSortLower
from core.utils import GetVectorNumberOfLayers, _
from core.settings import UserSettings
from core.debug import Debug
from gui_core.vselect import VectorSelectBase

from grass.pydispatch.signal import Signal


class Select(wx.combo.ComboCtrl):

    def __init__(
            self, parent, id=wx.ID_ANY, size=globalvar.DIALOG_GSELECT_SIZE,
            type=None, multiple=False, nmaps=1, mapsets=None,
            updateOnPopup=True, onPopup=None, fullyQualified=True,
            extraItems={},
            layerTree=None, validator=wx.DefaultValidator):
        """Custom control to create a ComboBox with a tree control to
        display and select GIS elements within acessible mapsets.
        Elements can be selected with mouse. Can allow multiple
        selections, when argument <em>multiple</em> is True. Multiple
        selections are separated by commas.

        :param type: type of GIS elements ('raster, 'vector', ...)
        :param multiple: True for multiple input
        :param nmaps: number of maps to be entered
        :param mapsets: force list of mapsets (otherwise search path)
        :param updateOnPopup: True for updating list of elements on popup
        :param onPopup: function to be called on Popup
        :param fullyQualified: True to provide fully qualified names (map@mapset)
        :param extraItems: extra items to add (given as dictionary) - see gmodeler for usage
        :param layerTree: show only elements from given layer tree if not None
        :param validator: validator for TextCtrl
        """
        wx.combo.ComboCtrl.__init__(
            self,
            parent=parent,
            id=id,
            size=size,
            validator=validator)
        if globalvar.CheckWxVersion([3]):
            self.SetName("Select")
        else:
            self.GetChildren()[0].SetName("Select")

        self.GetChildren()[0].type = type

        self.tcp = TreeCtrlComboPopup()
        self.SetPopupControl(self.tcp)
        self.SetPopupExtents(0, 100)
        if type:
            self.tcp.SetData(
                type=type,
                mapsets=mapsets,
                multiple=multiple,
                nmaps=nmaps,
                updateOnPopup=updateOnPopup,
                onPopup=onPopup,
                fullyQualified=fullyQualified,
                extraItems=extraItems,
                layerTree=layerTree)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

    def OnKeyDown(self, event):
        """Open popup and send key events to the tree."""
        if self.IsPopupShown():
            # on some configurations we get key down, with some only key up
            # so we are trying to catch either key up or down
            # this mess shouldn't be necessary with wxPython 3
            self.tcp.OnKeyUp(event)
        else:
            if event.GetKeyCode() == wx.WXK_DOWN:
                self.ShowPopup()
            event.Skip()

    def SetElementList(self, type, mapsets=None):
        """Set element list

        :param type: GIS element type
        :param mapsets: list of acceptable mapsets (None for all in search path)
        """
        self.tcp.SetData(type=type, mapsets=mapsets)

    def GetElementList(self):
        """Load elements"""
        self.tcp.GetElementList()

    def SetType(self, etype, multiple=False, nmaps=1,
                mapsets=None, updateOnPopup=True, onPopup=None):
        """Param set element type for widget

        :param etype: element type, see gselect.ElementSelect
        """
        self.tcp.SetData(type=etype, mapsets=mapsets,
                         multiple=multiple, nmaps=nmaps,
                         updateOnPopup=updateOnPopup, onPopup=onPopup)


class VectorSelect(Select):

    def __init__(self, parent, ftype, **kwargs):
        """Custom to create a ComboBox with a tree control to display and
        select vector maps. You can filter the vector maps. If you
        don't need this feature use Select class instead

        :param ftype: filter vector maps based on feature type
        """
        Select.__init__(self, parent=parent, id=wx.ID_ANY,
                        type='vector', **kwargs)

        self.ftype = ftype

        # remove vector maps which do not contain given feature type
        self.tcp.SetFilter(self._isElement)

    def _isElement(self, vectorName):
        """Check if element should be filtered out"""
        try:
            if int(grass.vector_info_topo(vectorName)[self.ftype]) < 1:
                return False
        except KeyError:
            return False

        return True


class ListCtrlComboPopup(wx.combo.ComboPopup):
    """Create a list ComboBox using TreeCtrl with hidden root.

    .. todo::

        use event.EventObject instead of hardcoding (see forms.py)
        https://groups.google.com/forum/#!topic/wxpython-users/pRz6bi0k0XY

    """
    # overridden ComboPopup methods

    def Init(self):
        self.value = []            # for multiple is False ->
        # len(self.value) in [0,1]
        self.curitem = None
        self.multiple = False
        self.updateOnPopup = True
        self.filterItems = []      # limit items based on this list,
        # see layerTree parameter

    def Create(self, parent):
        self.seltree = wx.TreeCtrl(parent, style=wx.TR_HIDE_ROOT
                                   | wx.TR_HAS_BUTTONS
                                   | wx.TR_SINGLE
                                   | wx.TR_LINES_AT_ROOT
                                   | wx.SIMPLE_BORDER
                                   | wx.TR_FULL_ROW_HIGHLIGHT)
        self.seltree.Bind(wx.EVT_MOTION, self.OnMotion)
        self.seltree.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        # the following dummy handler are needed to keep tree events
        # from propagating up to the parent GIS Manager layer tree
        self.seltree.Bind(wx.EVT_TREE_ITEM_EXPANDING, lambda x: None)
        self.seltree.Bind(wx.EVT_TREE_ITEM_COLLAPSED, lambda x: None)
        self.seltree.Bind(wx.EVT_TREE_SEL_CHANGED, lambda x: None)
        self.seltree.Bind(wx.EVT_TREE_DELETE_ITEM, lambda x: None)
        self.seltree.Bind(wx.EVT_TREE_BEGIN_DRAG, lambda x: None)
        self.seltree.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, lambda x: None)
        # navigation in list/tree is handled automatically since wxPython 3
        # for older versions, we have to workaround it and write our own
        # navigation
        if globalvar.CheckWxVersion(version=[3]):
            self.seltree.Bind(
                wx.EVT_TREE_ITEM_ACTIVATED,
                self._onItemConfirmed)
            self.seltree.Bind(wx.EVT_TREE_KEY_DOWN, self._onDismissPopup)
        else:
            self.seltree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, lambda x: None)
            self.seltree.Bind(wx.EVT_KEY_UP, self.OnKeyUp)

    def GetControl(self):
        return self.seltree

    def GetStringValue(self):
        """Get value as a string separated by commas
        """
        return ','.join(self.value)

    def SetStringValue(self, value):
        # this assumes that item strings are unique...
        root = self.seltree.GetRootItem()
        if not root:
            return
        winValue = self.GetCombo().GetValue().strip(',')
        self.value = []
        if winValue:
            self.value = winValue.split(',')

    def OnPopup(self, force=False):
        """Limited only for first selected
        """
        if not force and not self.updateOnPopup:
            return

        # selects map starting according to written text
        inputText = self.GetCombo().GetValue().strip()
        if inputText:
            root = self.seltree.GetRootItem()
            match = self.FindItem(root, inputText, startLetters=True)
            if match.IsOk():
                self.seltree.EnsureVisible(match)
                self.seltree.SelectItem(match)

    def GetAdjustedSize(self, minWidth, prefHeight, maxHeight):
        """Reads UserSettings to get height (which was 200 in old implementation).
        """
        height = UserSettings.Get(
            group='appearance',
            key='gSelectPopupHeight',
            subkey='value')
        return wx.Size(minWidth, min(height, maxHeight))

    def FindItem(self, parentItem, text, startLetters=False):
        """Finds item with given name or starting with given text
        """
        startletters = startLetters
        item, cookie = self.seltree.GetFirstChild(parentItem)
        while wx.TreeItemId.IsOk(item):
            if self.seltree.GetItemText(item) == text:
                return item
            if self.seltree.ItemHasChildren(item):
                item = self.FindItem(item, text, startLetters=startletters)
                if wx.TreeItemId.IsOk(item):
                    return item
            elif startletters and self.seltree.GetItemText(item).startswith(text.split('@', 1)[0]):
                return item
            item, cookie = self.seltree.GetNextChild(parentItem, cookie)
        return wx.TreeItemId()

    def AddItem(self, value):
        root = self.seltree.GetRootItem()
        if not root:
            root = self.seltree.AddRoot("<hidden root>")
        self.seltree.AppendItem(root, text=value)

    def SetItems(self, items):
        root = self.seltree.GetRootItem()
        if not root:
            root = self.seltree.AddRoot("<hidden root>")
        for item in items:
            self.seltree.AppendItem(root, text=item)

    def OnKeyUp(self, event):
        """Enable to select items using keyboard.

        Unused with wxPython 3, can be removed in the future.
        """
        item = self.seltree.GetSelection()
        if event.GetKeyCode() == wx.WXK_DOWN:
            self.seltree.SelectItem(self.seltree.GetNextVisible(item))

        elif event.GetKeyCode() == wx.WXK_UP:
            itemPrev = self.seltree.GetPrevSibling(item)
            self.seltree.SelectItem(itemPrev)

        elif event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Dismiss()

        elif event.GetKeyCode() == wx.WXK_RETURN:
            self.seltree.SelectItem(item)
            self.curitem = item
            self._selectTreeItem(item)
            self.Dismiss()

    def _onDismissPopup(self, event):
        """Hide popup without selecting item on Esc"""
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Dismiss()
        else:
            event.Skip()

    def _selectTreeItem(self, item):
        item_str = self.seltree.GetItemText(item)
        if self.multiple:
            if item_str not in self.value:
                self.value.append(item_str)
        else:
            self.value = [item_str]

    def _onItemConfirmed(self, event):
        item = event.GetItem()
        self._selectTreeItem(item)
        self.Dismiss()

    def OnMotion(self, evt):
        """Have the selection follow the mouse, like in a real combobox
        """
        item, flags = self.seltree.HitTest(evt.GetPosition())
        if item and flags & wx.TREE_HITTEST_ONITEMLABEL:
            self.seltree.SelectItem(item)
            self.curitem = item
        evt.Skip()

    def OnLeftDown(self, evt):
        """Do the combobox selection
        """
        if self.curitem is None:
            return

        self._selectTreeItem(self.curitem)
        self.Dismiss()

        evt.Skip()

    def SetData(self, **kargs):
        """Set object properties"""
        if 'multiple' in kargs:
            self.multiple = kargs['multiple']
        if 'onPopup' in kargs:
            self.onPopup = kargs['onPopup']
        if kargs.get('layerTree', None):
            self.filterItems = []  # reset
            ltype = kargs['type']
            for layer in kargs['layerTree'].GetVisibleLayers(
                    skipDigitized=True):
                if layer.GetType() != ltype:
                    continue
                self.filterItems.append(layer.GetName())

    def DeleteAllItems(self):
        """Delete all items in popup"""
        self.seltree.DeleteAllItems()


class TreeCtrlComboPopup(ListCtrlComboPopup):
    """Create a tree ComboBox for selecting maps and other GIS elements
    in accessible mapsets within the current location
    """
    # overridden ComboPopup methods

    def Init(self):

        ListCtrlComboPopup.Init(self)
        self.nmaps = 1
        self.type = None
        self.mapsets = None
        self.onPopup = None
        self.fullyQualified = True
        self.extraItems = dict()

        self.SetFilter(None)
        self.tgis_error = False

    def SetFilter(self, filter):
        """Set filter for GIS elements, see e.g. VectorSelect"""
        self.filterElements = filter

    def OnPopup(self, force=False):
        """Limited only for first selected"""
        if not force and not self.updateOnPopup:
            return
        if self.onPopup:
            selected, exclude = self.onPopup(self.type)
        else:
            selected = None
            exclude = False

        self.GetElementList(selected, exclude)

        ListCtrlComboPopup.OnPopup(self, force)

    def GetElementList(self, elements=None, exclude=False):
        """Get filtered list of GIS elements in accessible mapsets
        and display as tree with all relevant elements displayed
        beneath each mapset branch
        """
        # update list
        self.seltree.DeleteAllItems()
        if self.type:
            self._getElementList(self.type, self.mapsets, elements, exclude)

        if len(self.value) > 0:
            root = self.seltree.GetRootItem()
            if not root:
                return
            item = self.FindItem(root, self.value[0])
            try:
                self.seltree.EnsureVisible(item)
                self.seltree.SelectItem(item)
            except:
                pass

    def _getElementList(self, element, mapsets=None,
                        elements=None, exclude=False):
        """Get list of GIS elements in accessible mapsets and display as tree
        with all relevant elements displayed beneath each mapset branch

        :param element: GIS element
        :param mapsets: list of acceptable mapsets (None for all mapsets in search path)
        :param elements: list of forced GIS elements
        :param exclude: True to exclude, False for forcing the list (elements)
        """
        # get current mapset
        curr_mapset = grass.gisenv()['MAPSET']

        # map element types to g.list types
        elementdict = {'cell': 'raster',
                       'raster': 'raster',
                       'grid3': 'raster_3d',
                       'raster_3d': 'raster_3d',
                       'vector': 'vector',
                       'paint/labels': 'label',
                       'label': 'label',
                       'windows': 'region',
                       'region': 'region',
                       'group': 'group',
                       'stds': 'stds',
                       'strds': 'strds',
                       'str3ds': 'str3ds',
                       'stvds': 'stvds'}

        # to support multiple elements
        element_list = element.split(',')
        renamed_elements = []
        for elem in element_list:
            if elem not in elementdict:
                self.AddItem(_('Not selectable element'), node=False)
                return
            else:
                renamed_elements.append(elementdict[elem])

        if element in ('stds', 'strds', 'str3ds', 'stvds'):
            if self.tgis_error is False:
                import grass.temporal as tgis
                filesdict = tgis.tlist_grouped(
                    elementdict[element], element == 'stds')
            else:
                filesdict = None
        else:
            filesdict = grass.list_grouped(renamed_elements,
                                           check_search_path=False)

        # add extra items first
        if self.extraItems:
            for group, items in self.extraItems.iteritems():
                node = self.AddItem(group, node=True)
                self.seltree.SetItemTextColour(node, wx.Colour(50, 50, 200))
                for item in items:
                    self.AddItem(item, node=False, parent=node)
                self.seltree.ExpandAllChildren(node)

        # list of mapsets in current location
        if mapsets is None:
            mapsets = grass.mapsets(search_path=True)

        # current mapset first
        if curr_mapset in mapsets and mapsets[0] != curr_mapset:
            mapsets.remove(curr_mapset)
            mapsets.insert(0, curr_mapset)

        first_mapset = None
        for mapset in mapsets:
            mapset_node = self.AddItem(
                _('Mapset') + ': ' + mapset, node=True, mapset=mapset)
            node = mapset_node
            if not first_mapset:
                first_mapset = mapset_node

            self.seltree.SetItemTextColour(mapset_node, wx.Colour(50, 50, 200))
            if mapset not in filesdict:
                continue
            try:
                if isinstance(filesdict[mapset], dict):
                    for elementType in filesdict[mapset].keys():
                        node = self.AddItem(
                            _('Type: ') + elementType,
                            mapset=mapset,
                            node=True,
                            parent=mapset_node)
                        self.seltree.SetItemTextColour(
                            node, wx.Colour(50, 50, 200))
                        elem_list = filesdict[mapset][elementType]
                        self._addItems(
                            elist=elem_list,
                            elements=elements,
                            mapset=mapset,
                            exclude=exclude,
                            node=node)
                else:
                    elem_list = filesdict[mapset]
                    self._addItems(elist=elem_list, elements=elements,
                                   mapset=mapset, exclude=exclude, node=node)
            except Exception as e:
                sys.stderr.write(_("GSelect: invalid item: %s") % e)
                continue

            if self.seltree.ItemHasChildren(mapset_node):
                sel = UserSettings.Get(
                    group='appearance',
                    key='elementListExpand',
                    subkey='selection')
                collapse = True

                if sel == 0:  # collapse all except PERMANENT and current
                    if mapset in ('PERMANENT', curr_mapset):
                        collapse = False
                elif sel == 1:  # collapse all except PERMANENT
                    if mapset == 'PERMANENT':
                        collapse = False
                elif sel == 2:  # collapse all except current
                    if mapset == curr_mapset:
                        collapse = False
                elif sel == 3:  # collapse all
                    pass
                elif sel == 4:  # expand all
                    collapse = False

                if collapse:
                    self.seltree.CollapseAllChildren(mapset_node)
                else:
                    self.seltree.ExpandAllChildren(mapset_node)

        if first_mapset:
            # select first mapset (MSW hack)
            self.seltree.SelectItem(first_mapset)

    # helpers
    def _addItems(self, elist, elements, mapset, exclude, node):
        """Helper function for adding multiple items (maps, stds).

        :param list elist: list of map/stds names
        :param list elements: list of forced elements
        :param str mapset:  mapset name
        :param exclude: True to exclude, False for forcing the list
        :param node: parent node
        """
        elist = grass.natural_sort(elist)
        for elem in elist:
            if elem != '':
                fullqElem = elem + '@' + mapset
                if self.filterItems and fullqElem not in self.filterItems:
                    continue  # skip items missed in self.filterItems

                if elements is not None:
                    if (exclude and fullqElem in elements) or \
                            (not exclude and fullqElem not in elements):
                        continue

                if self.filterElements:
                    if self.filterElements(fullqElem):
                        self.AddItem(
                            elem, mapset=mapset, node=False, parent=node)
                else:
                    self.AddItem(elem, mapset=mapset, node=False, parent=node)

    def AddItem(self, value, mapset=None, node=True, parent=None):
        if not parent:
            root = self.seltree.GetRootItem()
            if not root:
                root = self.seltree.AddRoot("<hidden root>")
            parent = root

        data = {'node': node, 'mapset': mapset}

        item = self.seltree.AppendItem(
            parent, text=value, data=wx.TreeItemData(data))
        return item

    def OnKeyUp(self, event):
        """Enables to select items using keyboard

        Unused with wxPython 3, can be removed in the future.
        """

        item = self.seltree.GetSelection()
        if event.GetKeyCode() == wx.WXK_DOWN:
            self.seltree.SelectItem(self.seltree.GetNextVisible(item))

        # problem with GetPrevVisible
        elif event.GetKeyCode() == wx.WXK_UP:
            if self.seltree.ItemHasChildren(item) and self.seltree.IsExpanded(
                    self.seltree.GetPrevSibling(item)):
                itemPrev = self.seltree.GetLastChild(
                    self.seltree.GetPrevSibling(item))
            else:
                itemPrev = self.seltree.GetPrevSibling(item)
                if not wx.TreeItemId.IsOk(itemPrev):
                    itemPrev = self.seltree.GetItemParent(item)
                    if item == self.seltree.GetFirstChild(
                            self.seltree.GetRootItem())[0]:
                        itemPrev = item
            self.seltree.SelectItem(itemPrev)

        # selects first item starting with the written text in next mapset
        elif event.GetKeyCode() == wx.WXK_TAB:
            selected = self.seltree.GetSelection()
            if self.seltree.ItemHasChildren(selected):
                parent = selected
            else:
                parent = self.seltree.GetItemParent(selected)
            nextSibling = self.seltree.GetNextSibling(parent)
            if wx.TreeItemId.IsOk(nextSibling):
                match = self.FindItem(
                    nextSibling, self.GetCombo().GetValue().strip(), True)
            else:
                match = self.FindItem(
                    self.seltree.GetFirstChild(
                        self.seltree.GetItemParent(parent))[0],
                    self.GetCombo().GetValue().strip(),
                    True)
            self.seltree.SelectItem(match)

        elif event.GetKeyCode() == wx.WXK_RIGHT:
            if self.seltree.ItemHasChildren(item):
                self.seltree.Expand(item)

        elif event.GetKeyCode() == wx.WXK_LEFT:
            if self.seltree.ItemHasChildren(item):
                self.seltree.Collapse(item)

        elif event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Dismiss()

        elif event.GetKeyCode() == wx.WXK_RETURN:
            if self.seltree.GetPyData(item)['node']:
                self.value = []
            else:
                self._selectTreeItem(item)

            self.Dismiss()

    def OnLeftDown(self, evt):
        """Do the combobox selection
        """
        item, flags = self.seltree.HitTest(evt.GetPosition())
        if item and flags & wx.TREE_HITTEST_ONITEMLABEL:
            self.curitem = item

            if self.seltree.GetPyData(item)['node']:
                evt.Skip()
                return

            self._selectTreeItem(item)
            self.Dismiss()

        evt.Skip()

    def _selectTreeItem(self, item):
        fullName = self.seltree.GetItemText(item)
        if self.fullyQualified and self.seltree.GetPyData(item)['mapset']:
            fullName += '@' + self.seltree.GetPyData(item)['mapset']

        if self.multiple:
            self.value.append(fullName)
        else:
            if self.nmaps > 1:  # see key_desc
                if len(self.value) >= self.nmaps:
                    self.value = [fullName]
                else:
                    self.value.append(fullName)
            else:
                self.value = [fullName]

    def _onItemConfirmed(self, event):
        item = event.GetItem()
        if self.seltree.GetPyData(item)['node']:
            self.value = []
        else:
            self._selectTreeItem(item)
        self.Dismiss()

    def SetData(self, **kargs):
        """Set object properties"""
        ListCtrlComboPopup.SetData(self, **kargs)
        if 'type' in kargs:
            self.type = kargs['type']
            if self.type in ('stds', 'strds', 'str3ds', 'stvds'):
                # Initiate the temporal framework. Catch database error
                # and set the error flag for the stds listing.
                import grass.temporal as tgis
                try:
                    tgis.init(True)
                except messages.FatalError as e:
                    sys.stderr.write("Temporal GIS error:\n%s" % e)
                    self.tgis_error = True
        if 'mapsets' in kargs:
            self.mapsets = kargs['mapsets']
        if 'nmaps' in kargs:
            self.nmaps = kargs['nmaps']
        if 'updateOnPopup' in kargs:
            self.updateOnPopup = kargs['updateOnPopup']
        if 'fullyQualified' in kargs:
            self.fullyQualified = kargs['fullyQualified']
        if 'extraItems' in kargs:
            self.extraItems = kargs['extraItems']

    def GetType(self):
        """Get element type
        """
        return self.type


class VectorDBInfo:
    """Class providing information about attribute tables
    linked to a vector map"""

    def __init__(self, map):
        self.map = map

        # dictionary of layer number and associated (driver, database, table)
        self.layers = {}
        # dictionary of table and associated columns (type, length, values,
        # ids)
        self.tables = {}

        if not self._CheckDBConnection():  # -> self.layers
            return

        self._DescribeTables()  # -> self.tables

    def _CheckDBConnection(self):
        """Check DB connection"""
        nuldev = file(os.devnull, 'w+')
        # if map is not defined (happens with vnet initialization) or it
        # doesn't exist
        try:
            self.layers = grass.vector_db(map=self.map, stderr=nuldev)
        except CalledModuleError:
            return False
        finally:  # always close nuldev
            nuldev.close()

        return bool(len(self.layers.keys()) > 0)

    def _DescribeTables(self):
        """Describe linked tables"""
        for layer in self.layers.keys():
            # determine column names and types
            table = self.layers[layer]["table"]
            columns = {}  # {name: {type, length, [values], [ids]}}
            i = 0
            Debug.msg(
                1,
                "gselect.VectorDBInfo._DescribeTables(): table=%s driver=%s database=%s" %
                (self.layers[layer]["table"],
                 self.layers[layer]["driver"],
                 self.layers[layer]["database"]))
            for item in grass.db_describe(
                    table=self.layers[layer]["table"],
                    driver=self.layers[layer]["driver"],
                    database=self.layers[layer]["database"])['cols']:
                name, type, length = item
                # FIXME: support more datatypes
                if type.lower() == "integer":
                    ctype = int
                elif type.lower() == "double precision":
                    ctype = float
                else:
                    ctype = str

                columns[name.strip()] = {'index': i,
                                         'type': type.lower(),
                                         'ctype': ctype,
                                         'length': int(length),
                                         'values': [],
                                         'ids': []}
                i += 1

            # check for key column
            # v.db.connect -g/p returns always key column name lowercase
            if self.layers[layer]["key"] not in columns.keys():
                for col in columns.keys():
                    if col.lower() == self.layers[layer]["key"]:
                        self.layers[layer]["key"] = col.upper()
                        break

            self.tables[table] = columns

        return True

    def Reset(self):
        """Reset"""
        for layer in self.layers:
            table = self.layers[layer]["table"]  # get table desc
            for name in self.tables[table].keys():
                self.tables[table][name]['values'] = []
                self.tables[table][name]['ids'] = []

    def GetName(self):
        """Get vector name"""
        return self.map

    def GetKeyColumn(self, layer):
        """Get key column of given layer

        :param layer: vector layer number
        """
        return str(self.layers[layer]['key'])

    def GetTable(self, layer):
        """Get table name of given layer

        :param layer: vector layer number
        """
        return self.layers[layer]['table']

    def GetDbSettings(self, layer):
        """Get database settins

        :param layer: layer number

        :return: (driver, database)
        """
        return self.layers[layer]['driver'], self.layers[layer]['database']

    def GetTableDesc(self, table):
        """Get table columns

        :param table: table name
        """
        return self.tables[table]


class LayerSelect(wx.ComboBox):

    def __init__(self, parent, id=wx.ID_ANY,
                 size=globalvar.DIALOG_COMBOBOX_SIZE,
                 vector=None, dsn=None, choices=[], all=False, default=None):
        """Creates combo box for selecting vector map layer names

        :param str vector: vector map name (native or connected via v.external)
        :param str dsn: OGR data source name
        """
        super(
            LayerSelect,
            self).__init__(
            parent,
            id,
            size=size,
            choices=choices)

        self.all = all

        self.SetName("LayerSelect")

        # default value
        self.default = default

        self.InsertLayers(vector=vector, dsn=dsn)

    def InsertLayers(self, vector=None, dsn=None):
        """Insert layers for a vector into the layer combobox

        :param str vector: vector map name (native or connected via v.external)
        :param str dsn: OGR data source name
        """
        layers = list()

        if vector:
            layers = GetVectorNumberOfLayers(vector)
        elif dsn:
            ret = RunCommand('v.in.ogr',
                             read=True,
                             quiet=True,
                             flags='l',
                             input=dsn)
            if ret:
                layers = ret.splitlines()

        if self.default:
            if len(layers) == 0:
                layers.insert(0, str(self.default))
            elif self.default not in layers:
                layers.append(self.default)

        if self.all:
            layers.insert(0, '-1')

        if len(layers) > 0:
            self.SetItems(layers)
        else:
            self.Clear()

        self.SetValue("")

        if self.default and self.default in layers:
            self.SetValue(self.default)


class DriverSelect(wx.ComboBox):
    """Creates combo box for selecting database driver.
    """

    def __init__(self, parent, choices, value,
                 id=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=globalvar.DIALOG_LAYER_SIZE, **kargs):

        super(DriverSelect, self).__init__(parent, id, value, pos, size,
                                           choices, style=wx.CB_READONLY)

        self.SetName("DriverSelect")

        self.SetStringSelection(value)


class DatabaseSelect(wx.TextCtrl):
    """Creates combo box for selecting database driver.
    """

    def __init__(self, parent, value='', id=wx.ID_ANY,
                 size=globalvar.DIALOG_TEXTCTRL_SIZE, **kargs):
        super(
            DatabaseSelect,
            self).__init__(
            parent,
            id,
            value,
            size=size,
            **kargs)
        self.SetName("DatabaseSelect")


class TableSelect(wx.ComboBox):
    """Creates combo box for selecting attribute tables from the database
    """

    def __init__(self, parent,
                 id=wx.ID_ANY, value='',
                 size=globalvar.DIALOG_COMBOBOX_SIZE, choices=[], **kargs):
        super(
            TableSelect,
            self).__init__(
            parent,
            id,
            value,
            size=size,
            choices=choices,
            style=wx.CB_READONLY,
            **kargs)
        self.SetName("TableSelect")

        if not choices:
            self.InsertTables()

    def InsertTables(self, driver=None, database=None):
        """Insert attribute tables into combobox"""
        items = []

        if not driver or not database:
            connect = grass.db_connection()

            driver = connect['driver']
            database = connect['database']

        ret = RunCommand('db.tables',
                         flags='p',
                         read=True,
                         driver=driver,
                         database=database)

        if ret:
            for table in ret.splitlines():
                items.append(table)

        self.SetItems(items)
        self.SetValue('')


class ColumnSelect(wx.combo.ComboCtrl):
    """Creates combo box for selecting columns in the attribute table
    for a vector map.

    :param parent: window parent
    :param id: window id
    :param value: default value
    :param size: window size
    :param str vector: vector map name
    :param layer: layer number
    :param multiple: True if it is possible to add multiple columns
    :param param: parameters list (see menuform.py)
    :param kwags: wx.ComboBox parameters
    """

    def __init__(self, parent, id=wx.ID_ANY, value='',
                 size=globalvar.DIALOG_COMBOBOX_SIZE,
                 vector=None, layer=1, multiple=False,
                 param=None, **kwargs):
        self.defaultValue = value
        self.param = param
        self.columns = []

        wx.combo.ComboCtrl.__init__(self, parent, id, size=size, **kwargs)
        self.GetChildren()[0].SetName("ColumnSelect")
        self.GetChildren()[0].type = type

        self.tcp = ListCtrlComboPopup()
        self.SetPopupControl(self.tcp)
        self.tcp.SetData(multiple=multiple)

        if vector:
            self.InsertColumns(vector, layer)
        self.GetChildren()[0].Bind(wx.EVT_KEY_UP, self.OnKeyUp)

    def GetColumns(self):
        return self.columns

    def OnKeyUp(self, event):
        """Shows popupwindow if down arrow key is released"""
        if event.GetKeyCode() == wx.WXK_DOWN and not self.IsPopupShown():
            self.ShowPopup()
        else:
            event.Skip()

    def Clear(self):
        self.tcp.DeleteAllItems()
        self.SetValue('')

    def InsertColumns(self, vector, layer, excludeKey=False,
                      excludeCols=None, type=None, dbInfo=None):
        """Insert columns for a vector attribute table into the columns combobox

        :param str vector: vector name
        :param int layer: vector layer number
        :param excludeKey: exclude key column from the list?
        :param excludeCols: list of columns to be removed from the list
        :param type: only columns of given type (given as list)
        """
        if not dbInfo:
            dbInfo = VectorDBInfo(vector)

        try:
            try:
                layer = int(layer)
            except TypeError:
                # assuming layer 1
                layer = 1
            table = dbInfo.GetTable(layer)
            columnchoices = dbInfo.GetTableDesc(table)
            keyColumn = dbInfo.GetKeyColumn(layer)
            self.columns = len(columnchoices.keys()) * ['']
            for key, val in columnchoices.iteritems():
                self.columns[val['index']] = key
            if excludeKey:  # exclude key column
                self.columns.remove(keyColumn)
            if excludeCols:  # exclude key column
                for key in columnchoices.iterkeys():
                    if key in excludeCols:
                        self.columns.remove(key)
            if type:  # only selected column types
                for key, value in columnchoices.iteritems():
                    if value['type'] not in type:
                        try:
                            self.columns.remove(key)
                        except ValueError:
                            pass
        except (KeyError, ValueError):
            self.columns[:] = []

        # update list
        self.tcp.DeleteAllItems()
        for col in self.columns:
            self.tcp.AddItem(col)

        self.SetValue(self.defaultValue)

        if self.param:
            value = self.param.get('value', '')
            if value != '' and value in self.columns:
                self.SetValue(value)

    def InsertTableColumns(self, table, driver=None, database=None):
        """Insert table columns

        :param str table: table name
        :param str driver: driver name
        :param str database: database name
        """
        self.columns[:] = []

        ret = RunCommand('db.columns',
                         read=True,
                         driver=driver,
                         database=database,
                         table=table)

        if ret:
            self.columns = ret.splitlines()

        # update list
        self.tcp.DeleteAllItems()
        self.SetValue(self.defaultValue)

        for col in self.columns:
            self.tcp.AddItem(col)
        if self.param:
            value = self.param.get('value', '')
            if value != '' and value in self.columns:
                self.SetValue(value)


class DbaseSelect(wx.lib.filebrowsebutton.DirBrowseButton):
    """Widget for selecting GRASS Database"""

    def __init__(self, parent, **kwargs):
        super(
            DbaseSelect,
            self).__init__(
            parent,
            id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE,
            labelText='',
            dialogTitle=_('Choose GIS Data Directory'),
            buttonText=_('Browse'),
            startDirectory=grass.gisenv()['GISDBASE'],
            **kwargs)


class LocationSelect(wx.ComboBox):
    """Widget for selecting GRASS location"""

    def __init__(
            self, parent, id=wx.ID_ANY, size=globalvar.DIALOG_COMBOBOX_SIZE,
            gisdbase=None, **kwargs):
        super(LocationSelect, self).__init__(parent, id, size=size,
                                             style=wx.CB_READONLY, **kwargs)
        self.SetName("LocationSelect")

        if not gisdbase:
            self.gisdbase = grass.gisenv()['GISDBASE']
        else:
            self.gisdbase = gisdbase

        self.SetItems(GetListOfLocations(self.gisdbase))

    def UpdateItems(self, dbase):
        """Update list of locations

        :param str dbase: path to GIS database
        """
        self.gisdbase = dbase
        if dbase:
            self.SetItems(GetListOfLocations(self.gisdbase))
        else:
            self.SetItems([])


class MapsetSelect(wx.combo.ComboCtrl):
    """Widget for selecting GRASS mapset"""

    def __init__(self, parent, id=wx.ID_ANY,
                 size=globalvar.DIALOG_COMBOBOX_SIZE, gisdbase=None,
                 location=None, setItems=True, searchPath=False, new=False,
                 skipCurrent=False, multiple=False, **kwargs):
        style = 0
        # disabled, read-only widget has no TextCtrl children (TODO: rewrite)
        # if not new and not multiple:
        ###     style = wx.CB_READONLY

        wx.combo.ComboCtrl.__init__(self, parent, id, size=size,
                                    style=style, **kwargs)
        self.searchPath = searchPath
        self.skipCurrent = skipCurrent
        self.SetName("MapsetSelect")
        if not gisdbase:
            self.gisdbase = grass.gisenv()['GISDBASE']
        else:
            self.gisdbase = gisdbase

        if not location:
            self.location = grass.gisenv()['LOCATION_NAME']
        else:
            self.location = location

        self.tcp = ListCtrlComboPopup()
        self.SetPopupControl(self.tcp)
        self.tcp.SetData(multiple=multiple)

        if setItems:
            self.tcp.SetItems(self._getMapsets())

    def UpdateItems(self, location, dbase=None):
        """Update list of mapsets for given location

        :param str dbase: path to GIS database (None to use currently
                          selected)
        :param str location: name of location
        """
        if dbase:
            self.gisdbase = dbase
        self.location = location

        self.tcp.DeleteAllItems()

        if location:
            self.tcp.SetItems(self._getMapsets())
        else:
            self.tcp.SetItems([])

    def _getMapsets(self):
        if self.searchPath:
            mlist = RunCommand('g.mapsets',
                               read=True, flags='p',
                               sep='newline').splitlines()
        else:
            mlist = GetListOfMapsets(self.gisdbase, self.location,
                                     selectable=False)

        gisenv = grass.gisenv()
        if self.skipCurrent and \
                gisenv['LOCATION_NAME'] == self.location and \
                gisenv['MAPSET'] in mlist:
            mlist.remove(gisenv['MAPSET'])

        return mlist

    def GetStringSelection(self):
        """For backward compatibility. MapsetSelect changed to allow
        multiple selection, this required to change super-class from
        wx.ComboBox to wx.combo.ComboCtrl"""
        return self.GetValue()

    def SetStringSelection(self, text):
        """For backward compatibility. MapsetSelect changed to allow
        multiple selection, this required to change super-class from
        wx.ComboBox to wx.combo.ComboCtrl"""
        return self.SetValue(text)

    def SetSelection(self, sel=0):
        """For backward compatibility. MapsetSelect changed to allow
        multiple selection, this required to change super-class from
        wx.ComboBox to wx.combo.ComboCtrl"""
        self.SetValue('')  # TODO: implement SetSelection()

    def SetItems(self, items):
        """For backward compatibility. MapsetSelect changed to allow
        multiple selection, this required to change super-class from
        wx.ComboBox to wx.combo.ComboCtrl"""
        self.tcp.DeleteAllItems()
        self.tcp.SetItems(items)


class SubGroupSelect(wx.ComboBox):
    """Widget for selecting subgroups"""

    def __init__(self, parent, id=wx.ID_ANY,
                 size=globalvar.DIALOG_GSELECT_SIZE, **kwargs):
        super(SubGroupSelect, self).__init__(parent, id, size=size,
                                             **kwargs)
        self.SetName("SubGroupSelect")

    def Insert(self, group):
        """Insert subgroups for defined group"""
        if not group:
            return
        gisenv = grass.gisenv()
        try:
            name, mapset = group.split('@', 1)
        except ValueError:
            name = group
            mapset = gisenv['MAPSET']

        mlist = RunCommand('i.group', group=group,
                           read=True, flags='sg').splitlines()
        try:
            self.SetItems(mlist)
        except OSError:
            self.SetItems([])


class FormatSelect(wx.Choice):

    def __init__(self, parent, srcType, ogr=False,
                 size=globalvar.DIALOG_SPIN_SIZE,
                 **kwargs):
        """Widget for selecting external (GDAL/OGR) format

        :param parent: parent window
        :param srcType: source type ('file', 'database', 'protocol')
        :param ogr: True for OGR otherwise GDAL
        """
        super(FormatSelect, self).__init__(parent, id=wx.ID_ANY, size=size,
                                           **kwargs)
        self.SetName("FormatSelect")

        if ogr:
            ftype = 'ogr'
        else:
            ftype = 'gdal'

        formats = list()
        for f in GetFormats()[ftype][srcType].values():
            formats += f
        self.SetItems(formats)

    def GetExtension(self, name):
        """Get file extension by format name"""
        formatToExt = dict()
        formatToExt.update(rasterFormatExtension)
        formatToExt.update(vectorFormatExtension)

        return formatToExt.get(name, '')


class GdalSelect(wx.Panel):

    def __init__(self, parent, panel, ogr=False, link=False, dest=False,
                 exclude=None, settings=True):
        """Widget for selecting GDAL/OGR datasource, format

        .. todo::
             Split into GdalSelect and OgrSelect and optionally to
             GdalSelectOutput, OgrSelectOutput

        :param parent: parent window
        :param bool ogr: use OGR selector instead of GDAL
        :param bool dest: True for output (destination)
        :param default: default type (ignored when dest == True)
        :param exclude: list of types to be excluded
        """
        self.parent = parent
        self.ogr = ogr
        self.link = link
        self.dest = dest
        self._sourceType = None

        wx.Panel.__init__(self, parent=panel, name='GdalSelect')

        self.reloadDataRequired = Signal('GdalSelect.reloadDataRequired')

        self.inputBox = wx.StaticBox(parent=self)
        if dest:
            self.inputBox.SetLabel(" %s " % _("Output settings"))
        else:
            self.inputBox.SetLabel(" %s " % _("Source input"))

        # source type
        sources = list()
        self.sourceMap = {'file': -1,
                          'dir': -1,
                          'db': -1,
                          'pro': -1,
                          'native': -1}
        idx = 0
        if dest:
            sources.append(_("Native"))
            self.sourceMap['native'] = idx
            idx += 1
        if exclude is None:
            exclude = []
        if 'file' not in exclude:
            sources.append(_("File"))
            self.sourceMap['file'] = idx
            idx += 1
        if 'directory' not in exclude:
            sources.append(_("Directory"))
            self.sourceMap['dir'] = idx
            idx += 1
        if 'database' not in exclude:
            sources.append(_("Database"))
            self.sourceMap['db'] = idx
            idx += 1
        if 'protocol' not in exclude:
            sources.append(_("Protocol"))
            self.sourceMap['pro'] = idx
            idx += 1
        self.sourceMapByIdx = {}
        for name, idx in self.sourceMap.items():
            self.sourceMapByIdx[idx] = name

        self.source = wx.RadioBox(parent=self, id=wx.ID_ANY,
                                  style=wx.RA_SPECIFY_COLS,
                                  choices=sources)
        if dest:
            self.source.SetLabel(" %s " % _('Output type'))
        else:
            self.source.SetLabel(" %s " % _('Source type'))

        self.source.SetSelection(0)
        self.source.Bind(
            wx.EVT_RADIOBOX,
            lambda evt: self.SetSourceType(
                self.sourceMapByIdx[
                    evt.GetInt()]))

        self.nativeWidgets = {}
        self.fileWidgets = {}
        self.dirWidgets = {}
        self.dbWidgets = {}
        self.protocolWidgets = {}
        self.pgWidgets = {}

        if ogr:
            fType = 'ogr'
        else:
            fType = 'gdal'

        # file
        fileMask = '%(all)s (*)|*|' % {'all': _('All files')}
        if not ogr:
            extList = rasterFormatExtension
            fileMask += ('%(name)s (*.%(low1)s;*.%(low2)s;*.%(up1)s;*.%(up2)s)|'
                         '*.%(low1)s;*.%(low2)s;*.%(up1)s;*.%(up2)s|' %
                         {'name': 'GeoTIFF', 'low1': 'tif', 'low2': 'tiff', 'up1': 'TIF', 'up2': 'TIFF'})
        else:
            extList = vectorFormatExtension
            fileMask += '%(name)s (*.%(low)s;*.%(up)s)|*.%(low)s;*.%(up)s|' % {
                'name': 'ESRI Shapefile', 'low': 'shp', 'up': 'SHP'}

        for name, ext in sorted(extList.items()):
            if name in ('ESRI Shapefile', 'GeoTIFF'):
                continue
            fileMask += '%(name)s (*.%(low)s;*.%(up)s)|*.%(low)s;*.%(up)s|' % {
                'name': name, 'low': ext.lower(), 'up': ext.upper()}
        fileMask += '%s (*.zip;*.ZIP)|*.zip;*.ZIP|' % _('ZIP files')
        fileMask += '%s (*.gz;*.GZ)|*.gz;*.GZ|' % _('GZIP files')
        fileMask += '%s (*.tar;*.TAR)|*.tar;*.TAR|' % _('TAR files')
        # don't include last '|' - windows and mac throw error
        fileMask += '%s (*.tar.gz;*.TAR.GZ;*.tgz;*.TGZ)|*.tar.gz;*.TAR.GZ;*.tgz;*.TGZ' % _('TARGZ files')
        # only contains formats with extensions hardcoded

        self.filePanel = wx.Panel(parent=self)
        browse = filebrowse.FileBrowseButton(
            parent=self.filePanel,
            id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE,
            labelText=_('File:'),
            dialogTitle=_('Choose file to import'),
            buttonText=_('Browse'),
            startDirectory=os.getcwd(),
            changeCallback=self.OnUpdate,
            fileMask=fileMask)
        browse.GetChildren()[1].SetName('GdalSelectDataSource')
        self.fileWidgets['browse'] = browse
        self.fileWidgets['options'] = wx.TextCtrl(parent=self.filePanel)

        # directory
        self.dirPanel = wx.Panel(parent=self)
        browse = filebrowse.DirBrowseButton(
            parent=self.dirPanel,
            id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE,
            labelText=_('Directory:'),
            dialogTitle=_('Choose input directory'),
            buttonText=_('Browse'),
            startDirectory=os.getcwd(),
            changeCallback=self.OnUpdate)
        browse.GetChildren()[1].SetName('GdalSelectDataSource')

        self.dirWidgets['browse'] = browse
        formatSelect = wx.Choice(parent=self.dirPanel, size=(300, -1))
        self.dirWidgets['format'] = formatSelect
        fileFormats = GetFormats(writableOnly=dest)[fType]['file']
        formatSelect.SetItems(sorted(list(fileFormats)))
        formatSelect.Bind(
            wx.EVT_CHOICE, lambda evt: self.SetExtension(
                self.dirWidgets['format'].GetStringSelection()))
        formatSelect.Bind(wx.EVT_CHOICE, self.OnUpdate)

        self.dirWidgets['extensionLabel'] = wx.StaticText(
            parent=self.dirPanel, label=_("Extension:"))
        self.dirWidgets['extension'] = wx.TextCtrl(parent=self.dirPanel)
        self.dirWidgets['extension'].Bind(wx.EVT_TEXT, self.ExtensionChanged)
        self.dirWidgets['options'] = wx.TextCtrl(parent=self.dirPanel)
        if self.ogr:
            shapefile = 'ESRI Shapefile'
            if shapefile in fileFormats:
                formatSelect.SetStringSelection(shapefile)
                self.SetExtension(shapefile)
        else:
            tiff = 'GeoTIFF'
            if tiff in fileFormats:
                formatSelect.SetStringSelection(tiff)
                self.SetExtension(tiff)

        # database
        self.dbPanel = wx.Panel(parent=self)
        self.dbFormats = GetFormats(writableOnly=dest)[fType]['database']
        dbChoice = wx.Choice(parent=self.dbPanel, choices=self.dbFormats)
        dbChoice.Bind(
            wx.EVT_CHOICE,
            lambda evt: self.SetDatabase(
                db=dbChoice.GetStringSelection()))
        self.dbWidgets['format'] = dbChoice

        browse = filebrowse.FileBrowseButton(
            parent=self.dbPanel,
            id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE,
            labelText=_("Name:"),
            dialogTitle=_('Choose file'),
            buttonText=_('Browse'),
            startDirectory=os.getcwd(),
            changeCallback=self.OnUpdate)
        browse.GetChildren()[1].SetName('GdalSelectDataSource')

        self.dbWidgets['browse'] = browse
        self.dbWidgets['choice'] = wx.Choice(
            parent=self.dbPanel, name='GdalSelectDataSource')
        self.dbWidgets['choice'].Bind(wx.EVT_CHOICE, self.OnUpdate)
        self.dbWidgets['text'] = wx.TextCtrl(
            parent=self.dbPanel, name='GdalSelectDataSource')
        self.dbWidgets['text'].Bind(wx.EVT_TEXT, self.OnUpdate)
        self.dbWidgets['textLabel1'] = wx.StaticText(
            parent=self.dbPanel, label=_("Name:"))
        self.dbWidgets['textLabel2'] = wx.StaticText(
            parent=self.dbPanel, label=_("Name:"))
        self.dbWidgets['featType'] = wx.RadioBox(
            parent=self.dbPanel,
            id=wx.ID_ANY,
            label=" %s " %
            _("Feature type:"),
            choices=[
                _("simple features"),
                _("topological")],
            majorDimension=2,
            style=wx.RA_SPECIFY_COLS)
        if dest:
            self.dbWidgets['featType'].Disable()
        else:
            self.dbWidgets['featType'].Hide()
        browse = filebrowse.DirBrowseButton(
            parent=self.dbPanel,
            id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE,
            labelText=_('Directory:'),
            dialogTitle=_('Choose input directory'),
            buttonText=_('Browse'),
            startDirectory=os.getcwd(),
            changeCallback=self.OnUpdate)
        self.dbWidgets['dirbrowse'] = browse
        self.dbWidgets['options'] = wx.TextCtrl(parent=self.dbPanel)

        # protocol
        self.protocolPanel = wx.Panel(parent=self)
        protocolFormats = GetFormats(writableOnly=self.dest)[fType]['protocol']
        protocolChoice = wx.Choice(
            parent=self.protocolPanel,
            choices=protocolFormats)
        self.protocolWidgets['format'] = protocolChoice

        self.protocolWidgets['text'] = wx.TextCtrl(parent=self.protocolPanel)
        self.protocolWidgets['text'].Bind(wx.EVT_TEXT, self.OnUpdate)
        self.protocolWidgets['options'] = wx.TextCtrl(
            parent=self.protocolPanel)

        # native
        self.nativePanel = wx.Panel(parent=self)

        self._layout()
        sourceType = 'file'
        self.SetSourceType(sourceType)  # needed always to fit dialog size
        if self.dest:
            current = RunCommand('v.external.out',
                                 parent=self,
                                 read=True, parse=grass.parse_key_val,
                                 flags='g')
            if current['format'] == 'native':
                sourceType = 'native'
            elif current['format'] in GetFormats()['ogr']['database']:
                sourceType = 'db'
            else:
                sourceType = 'dir'

        if self.dest:
            wx.CallAfter(self._postInit, sourceType, current)

    def _postInit(self, sourceType, data):
        """Fill in default values."""
        format = data.get('format', '')
        pg = 'conninfo' in data.keys()
        if pg:
            dsn = ''
            for item in data.get('conninfo').split(' '):
                k, v = item.split('=')
                if k == 'dbname':
                    dsn = v
                    break
            optList = list()
            for k, v in data.iteritems():
                if k in ('format', 'conninfo', 'topology'):
                    continue
                optList.append('%s=%s' % (k, v))
            options = ','.join(optList)
        else:
            dsn = data.get('dsn')
            options = data.get('options', '')

        self.SetSourceType(sourceType)
        self.source.SetSelection(self.sourceMap[sourceType])

        dsn = os.path.expandvars(dsn)  # v.external.out uses $HOME
        # fill in default values
        if sourceType == 'dir':
            self.dirWidgets['format'].SetStringSelection(format)
            self.dirWidgets['browse'].SetValue(dsn)
            self.dirWidgets['options'].SetValue(options)
        elif sourceType == 'db':
            self.dbWidgets['format'].SetStringSelection(format)
            self.dbWidgets['options'].SetValue(options)
            name = self._getCurrentDbWidgetName()
            if name == 'choice':
                if dsn in self.dbWidgets[name].GetItems():
                    self.dbWidgets[name].SetStringSelection(dsn)
                if 'topology' in data.keys():
                    self.dbWidgets['featType'].SetSelection(1)
            else:
                self.dbWidgets[name].SetValue(dsn)

    def _layout(self):
        """Layout"""
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        self.changingSizer = wx.StaticBoxSizer(self.inputBox, wx.VERTICAL)

        # file
        paddingSizer = wx.BoxSizer(wx.VERTICAL)
        sizer = wx.GridBagSizer(vgap=5, hgap=10)
        paddingSizer.Add(item=self.fileWidgets['browse'],
                         flag=wx.BOTTOM | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                         border=30)
        sizer.Add(item=paddingSizer, flag=wx.EXPAND, pos=(0, 0), span=(1, 2))
        sizer.AddGrowableCol(0)
        if self.dest:
            sizer.Add(item=wx.StaticText(parent=self.filePanel,
                                         label=_("Creation options:")),
                      flag=wx.ALIGN_CENTER_VERTICAL,
                      pos=(1, 0))
            sizer.Add(item=self.fileWidgets['options'],
                      flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                      pos=(1, 1))

        else:
            self.fileWidgets['options'].Hide()
        self.filePanel.SetSizer(sizer)

        # directory
        sizer = wx.GridBagSizer(vgap=3, hgap=10)
        sizer.Add(item=wx.StaticText(parent=self.dirPanel,
                                     label=_("Format:")),
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 0))
        sizer.Add(item=self.dirWidgets['format'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(0, 1))
        sizer.Add(item=self.dirWidgets['extensionLabel'],
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 2))
        sizer.Add(item=self.dirWidgets['extension'],
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 3))
        sizer.Add(item=self.dirWidgets['browse'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(1, 0), span=(1, 4))
        if self.dest:
            sizer.Add(item=wx.StaticText(parent=self.dirPanel,
                                         label=_("Creation options:")),
                      flag=wx.ALIGN_CENTER_VERTICAL,
                      pos=(2, 0))
            sizer.Add(item=self.dirWidgets['options'],
                      flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                      pos=(2, 1))
            helpBtn = wx.Button(parent=self.dirPanel, id=wx.ID_HELP)
            helpBtn.Bind(wx.EVT_BUTTON, self.OnHelp)
            sizer.Add(item=helpBtn,
                      flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                      pos=(2, 2))

            self.dirWidgets['extensionLabel'].Hide()
            self.dirWidgets['extension'].Hide()
        else:
            self.dirWidgets['options'].Hide()
        sizer.AddGrowableCol(1)
        self.dirPanel.SetSizer(sizer)

        # database
        sizer = wx.GridBagSizer(vgap=1, hgap=5)
        sizer.Add(item=wx.StaticText(parent=self.dbPanel,
                                     label=_("Format:")),
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 0))
        sizer.Add(item=self.dbWidgets['format'],
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 1))
        sizer.Add(item=self.dbWidgets['textLabel1'],
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(1, 0))
        sizer.Add(item=self.dbWidgets['text'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(1, 1), span=(1, 2))
        sizer.Add(item=self.dbWidgets['browse'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(2, 0), span=(1, 3))
        sizer.Add(item=self.dbWidgets['dirbrowse'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(3, 0), span=(1, 2))
        sizer.Add(item=self.dbWidgets['textLabel2'],
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(4, 0))
        sizer.Add(item=self.dbWidgets['choice'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(4, 1), span=(1, 2))
        if self.dest:
            sizer.Add(item=self.dbWidgets['featType'],
                      pos=(0, 2), flag=wx.EXPAND)

            sizer.Add(item=wx.StaticText(parent=self.dbPanel,
                                         label=_("Creation options:")),
                      flag=wx.ALIGN_CENTER_VERTICAL,
                      pos=(5, 0))
            sizer.Add(item=self.dbWidgets['options'],
                      flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                      pos=(5, 1), span=(1, 2))

            # help button
            helpBtn = wx.Button(parent=self.dbPanel, id=wx.ID_HELP)
            helpBtn.Bind(wx.EVT_BUTTON, self.OnHelp)
            sizer.Add(item=helpBtn,
                      pos=(5, 3))

        else:
            self.dbWidgets['options'].Hide()

        self.dbPanel.SetSizer(sizer)
        sizer.SetEmptyCellSize((0, 0))
        sizer.AddGrowableCol(1)

        # protocol
        sizer = wx.GridBagSizer(vgap=3, hgap=3)
        sizer.Add(item=wx.StaticText(parent=self.protocolPanel,
                                     label=_("Format:")),
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 0))
        sizer.Add(item=self.protocolWidgets['format'],
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(0, 1))
        sizer.Add(item=wx.StaticText(parent=self.protocolPanel,
                                     label=_("Protocol:")),
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  pos=(1, 0))
        sizer.Add(item=self.protocolWidgets['text'],
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                  pos=(1, 1))
        if self.dest:
            sizer.Add(item=wx.StaticText(parent=self.protocolPanel,
                                         label=_("Creation options:")),
                      flag=wx.ALIGN_CENTER_VERTICAL,
                      pos=(2, 0))
            sizer.Add(item=self.protocolWidgets['options'],
                      flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND,
                      pos=(2, 1))

        else:
            self.protocolWidgets['options'].Hide()
        sizer.AddGrowableCol(1)
        self.protocolPanel.SetSizer(sizer)

        # native
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(item=wx.StaticText(parent=self.nativePanel,
                                     label=_("No settings available")),
                  flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL | wx.EXPAND, border=5)
        self.nativePanel.SetSizer(sizer)

        for panel in (self.nativePanel, self.filePanel,
                      self.dirPanel, self.dbPanel,
                      self.protocolPanel):

            self.changingSizer.Add(item=panel, proportion=1,
                                   flag=wx.EXPAND)

        self.mainSizer.Add(item=self.source, proportion=0,
                           flag=wx.LEFT | wx.RIGHT | wx.EXPAND, border=5)
        self.mainSizer.Add(item=self.changingSizer, proportion=1,
                           flag=wx.ALL | wx.EXPAND, border=5)
        self.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self)

    def _getExtension(self, name):
        """Get file extension by format name"""
        formatToExt = dict()
        formatToExt.update(rasterFormatExtension)
        formatToExt.update(vectorFormatExtension)

        return formatToExt.get(name, '')

    def SetSourceType(self, sourceType):
        """Set source type (db, file, dir, ...).
        Does not switch radioboxes."""
        self._sourceType = sourceType
        self.changingSizer.Show(
            item=self.filePanel, show=(
                sourceType == 'file'))
        self.changingSizer.Show(
            item=self.nativePanel, show=(
                sourceType == 'native'))
        self.changingSizer.Show(item=self.dirPanel, show=(sourceType == 'dir'))
        self.changingSizer.Show(
            item=self.protocolPanel, show=(
                sourceType == 'pro'))
        self.changingSizer.Show(item=self.dbPanel, show=(sourceType is 'db'))

        self.changingSizer.Layout()

        if sourceType == 'db':
            self.dbWidgets['format'].SetItems(self.dbFormats)
            if self.dbFormats:
                if 'PostgreSQL' in self.dbFormats:
                    self.dbWidgets['format'].SetStringSelection('PostgreSQL')
                else:
                    self.dbWidgets['format'].SetSelection(0)
            self.dbWidgets['format'].Enable()

        if sourceType == 'db':
            db = self.dbWidgets['format'].GetStringSelection()
            self.SetDatabase(db)

        if not self.dest:
            self.reloadDataRequired.emit(listData=None, data=None)
            self._reloadLayers()

    def OnSettingsChanged(self, data):
        """User changed setting"""
        # data list: [type, dsn, format, options]
        if len(data) == 3:
            data.append('')
        elif len < 3:
            return

        self.source.SetSelection(self.sourceMap[data[0]])
        self.SetSourceType(data[0])
        if data[0] == 'file':
            self.fileWidgets['browse'].SetValue(data[1])
            self.fileWidgets['options'].SetValue(data[3])
        elif data[0] == 'dir':
            self.dirWidgets['browse'].SetValue(data[1])
            self.dirWidgets['format'].SetStringSelection(data[2])
            self.dirWidgets['options'].SetValue(data[3])
            self.SetExtension(data[2])
        elif data[0] == 'pro':
            self.protocolWidgets['text'].SetValue(data[2])
            self.protocolWidgets['options'].SetValue(data[3])
        elif data[0] == 'db':
            name = self._getCurrentDbWidgetName()
            if name == 'choice':
                if len(data[1].split(':', 1)) > 1:
                    for item in data[1].split(':', 1)[1].split(','):
                        key, value = item.split('=', 1)
                        if key == 'dbname':
                            self.dbWidgets[name].SetStringSelection(value)
                            break
                else:
                    self.dbWidgets[name].SetStringSelection(data[1])
            else:
                self.dbWidgets[name].SetValue(data[1])
            self.dbWidgets['options'].SetValue(data[3])

        if not self.dest:
            self.reloadDataRequired.emit(listData=None, data=None)
            self._reloadLayers()

    def AttachSettings(self):
        if self.ogr:
            settingsFile = os.path.join(GetSettingsPath(), 'wxOGR')
        else:
            settingsFile = os.path.join(GetSettingsPath(), 'wxGDAL')

        self.settsManager = ManageSettingsWidget(parent=self,
                                                 settingsFile=settingsFile)
        self.settsManager.settingsChanged.connect(self.OnSettingsChanged)
        self.settsManager.settingsSaving.connect(self.OnSettingsSaving)

        # do layout
        self.mainSizer.Insert(0, item=self.settsManager,
                              flag=wx.ALL | wx.EXPAND, border=5)

    def OnSettingsSaving(self, name):
        """Saving data"""
        if not self.GetDsn():
            GMessage(parent=self, message=_(
                "No data source defined, settings are not saved."))
            return

        self.settsManager.SetDataToSave((self._sourceType, self.GetDsn(),
                                         self.GetFormat(), self.GetOptions()))
        self.settsManager.SaveSettings(name)

    def _getExtPatternGlob(self, ext):
        """Get pattern for case-insensitive globing"""
        pattern = '*.'
        for c in ext:
            pattern += '[%s%s]' % (c.lower(), c.upper())
        return pattern

    def _getCurrentDbWidgetName(self):
        """Returns active dns database widget name."""
        for widget in ('browse', 'dirbrowse', 'text', 'choice'):
            if self.dbWidgets[widget].IsShown():
                return widget

    def GetDsn(self):
        """Get datasource name
        """
        if self._sourceType == 'db':
            if self.dbWidgets['format'].GetStringSelection() in(
                    'PostgreSQL', 'PostGIS Raster driver'):

                dsn = 'PG:dbname=%s' % self.dbWidgets[
                    'choice'].GetStringSelection()
            else:
                name = self._getCurrentDbWidgetName()
                if name == 'choice':
                    dsn = self.dbWidgets[name].GetStringSelection()
                else:
                    dsn = self.dbWidgets[name].GetValue()

        else:
            if self._sourceType == 'file':
                dsn = self.fileWidgets['browse'].GetValue()
            elif self._sourceType == 'dir':
                dsn = self.dirWidgets['browse'].GetValue()
            elif self._sourceType == 'pro':
                dsn = self.protocolWidgets['text'].GetValue()
            else:
                dsn = ''
            # check compressed files
            try:
                ext = os.path.splitext(dsn)[1].lower()
            except KeyError:
                ext = None

            if ext == '.zip':
                dsn = '/vsizip/' + dsn
            elif ext == '.gzip':
                dsn = '/vsigzip/' + dsn
            elif ext in ('.tar', '.tar.gz', '.tgz'):
                dsn = '/vsitar/' + dsn

        return dsn

    def SetDatabase(self, db):
        """Update database panel."""
        sizer = self.dbPanel.GetSizer()
        showBrowse = db in ('SQLite', 'Rasterlite')
        showDirbrowse = db in ('FileGDB')
        showChoice = db in ('PostgreSQL', 'PostGIS WKT Raster driver',
                            'PostGIS Raster driver')
        enableFeatType = self.dest and self.ogr and db in ('PostgreSQL')
        showText = not(showBrowse or showChoice or showDirbrowse)

        sizer.Show(self.dbWidgets['browse'], show=showBrowse)
        sizer.Show(self.dbWidgets['dirbrowse'], show=showDirbrowse)
        sizer.Show(self.dbWidgets['choice'], show=showChoice)
        sizer.Show(self.dbWidgets['textLabel2'], show=showChoice)
        sizer.Show(self.dbWidgets['text'], show=showText)
        sizer.Show(self.dbWidgets['textLabel1'], show=showText)
        self.dbWidgets['featType'].Enable(enableFeatType)
        if showChoice:
            # try to get list of PG databases
            dbNames = RunCommand(
                'db.databases',
                parent=self,
                quiet=True,
                read=True,
                driver='pg').splitlines()
            if dbNames is not None:
                self.dbWidgets['choice'].SetItems(sorted(dbNames))
                self.dbWidgets['choice'].SetSelection(0)
            elif grass.find_program('psql', '--help'):
                if not self.dbWidgets['choice'].GetItems():
                    p = grass.Popen(['psql', '-ltA'], stdout=grass.PIPE)
                    ret = p.communicate()[0]
                    if ret:
                        dbNames = list()
                        for line in ret.splitlines():
                            sline = line.split('|')
                            if len(sline) < 2:
                                continue
                            dbname = sline[0]
                            if dbname:
                                dbNames.append(dbname)
                        self.dbWidgets['choice'].SetItems(db)
                        self.dbWidgets['choice'].SetSelection(0)
            else:
                sizer.Show(self.dbWidgets['text'])
                sizer.Show(self.dbWidgets['choice'], False)

        sizer.Layout()

    def OnUpdate(self, event):
        """Update required - load layers."""
        if not self.dest:
            self._reloadLayers()

        event.Skip()

    def _reloadLayers(self):
        """Reload list of layers"""

        def hasRastSameProjAsLocation(dsn):

            ret = RunCommand('r.external',
                             quiet=True,
                             read=True,
                             flags='t',
                             input=dsn)

            # v.external returns info for individual bands, however projection is shared by all bands ->
            # (it is possible to take first line)

            lines = ret.splitlines()
            projectionMatch = '0'
            if lines:
                bandNumber, bandType, projectionMatch = map(
                    lambda x: x.strip(), lines[0].split(','))

            return projectionMatch

        def getProjMatchCaption(projectionMatch):

            if projectionMatch == '0':
                projectionMatchCaption = _("No")
            else:
                projectionMatchCaption = _("Yes")

            return projectionMatchCaption

        dsn = self.GetDsn()
        if not dsn:
            return

        data = list()
        listData = list()
        layerId = 1

        if self.ogr:
            ret = RunCommand('v.external',
                             quiet=True,
                             read=True,
                             flags='t',
                             input=dsn)
            if not ret:
                self.reloadDataRequired.emit(listData=None, data=None)
                return

            layerId = 1
            for line in ret.splitlines():
                layerName, featureType, projectionMatch, geometryColumn = map(
                    lambda x: x.strip(), line.split(','))
                projectionMatchCaption = getProjMatchCaption(projectionMatch)
                grassName = GetValidLayerName(layerName)
                if geometryColumn:
                    featureType = geometryColumn + '/' + featureType
                listData.append(
                    (layerId,
                     layerName,
                     featureType,
                     projectionMatchCaption,
                     grassName))
                data.append(
                    (layerId,
                     layerName,
                     featureType,
                     int(projectionMatch),
                        grassName))
                layerId += 1
        else:
            if self._sourceType == 'file':
                baseName = os.path.basename(dsn)
                grassName = GetValidLayerName(baseName.split('.', -1)[0])
                projectionMatch = hasRastSameProjAsLocation(dsn)
                projectionMatchCaption = getProjMatchCaption(projectionMatch)
                listData.append(
                    (layerId, baseName, projectionMatchCaption, grassName))
                data.append(
                    (layerId, baseName, int(projectionMatch), grassName))
            elif self._sourceType == 'dir':
                ext = self.dirWidgets['extension'].GetValue()
                for filename in glob.glob(os.path.join(
                        dsn, "%s") % self._getExtPatternGlob(ext)):
                    baseName = os.path.basename(filename)
                    grassName = GetValidLayerName(baseName.split('.', -1)[0])
                    projectionMatch = hasRastSameProjAsLocation(filename)
                    projectionMatchCaption = getProjMatchCaption(
                        projectionMatch)
                    listData.append(
                        (layerId, baseName, projectionMatchCaption, grassName))
                    data.append(
                        (layerId, baseName, int(projectionMatch), grassName))
                    layerId += 1

        # emit signal
        self.reloadDataRequired.emit(listData=listData, data=data)

    def ExtensionChanged(self, event):
        if not self.dest:
            # reload layers
            self._reloadLayers()

    def SetExtension(self, name):
        """Extension changed"""
        ext = self._getExtension(name)
        self.dirWidgets['extension'].SetValue(ext)

    def GetType(self):
        """Get source type"""
        return self._sourceType

    def GetFormat(self):
        """Get format as string"""
        if self._sourceType == 'dir':
            format = self.dirWidgets['format'].GetStringSelection()
        elif self._sourceType == 'pro':
            format = self.protocolWidgets['format'].GetStringSelection()
        elif self._sourceType == 'db':
            format = self.dbWidgets['format'].GetStringSelection()
        else:
            format = ''

        return format.replace(' ', '_')

    def GetFormatExt(self):
        """Get format extension"""
        return self._getExtension(self.GetFormat())

    def GetOptions(self):
        """Get creation options"""
        if self._sourceType == 'file':
            options = self.fileWidgets['options'].GetValue()
        elif self._sourceType == 'dir':
            options = self.dirWidgets['options'].GetValue()
        elif self._sourceType == 'pro':
            options = self.protocolWidgets['options'].GetValue()
        elif self._sourceType == 'db':
            if self.dbWidgets['featType'].GetSelection() == 1:
                options = 'topology=yes '
            else:
                options = ''
            options += self.dbWidgets['options'].GetValue()

        return options.strip()

    def OnHelp(self, event):
        """Show related manual page"""
        cmd = ''
        if self.dest:
            if self.ogr:
                cmd = 'v.external.out'
            else:
                cmd = 'r.external.out'
        else:
            if self.link:
                if self.ogr:
                    cmd = 'v.external'
                else:
                    cmd = 'r.external'
            else:
                if self.ogr:
                    cmd = 'v.in.ogr'
                else:
                    cmd = 'r.in.gdal'

        RunCommand('g.manual', entry=cmd)


class ProjSelect(wx.ComboBox):
    """Widget for selecting input raster/vector map used by
    r.proj/v.proj modules."""

    def __init__(self, parent, isRaster, id=wx.ID_ANY,
                 size=globalvar.DIALOG_COMBOBOX_SIZE, **kwargs):
        super(ProjSelect, self).__init__(parent, id, size=size,
                                         style=wx.CB_READONLY, **kwargs)
        self.SetName("ProjSelect")
        self.isRaster = isRaster

    def UpdateItems(self, dbase, location, mapset):
        """Update list of maps

        """
        if not dbase:
            dbase = grass.gisenv()['GISDBASE']
        if not mapset:
            mapset = grass.gisenv()['MAPSET']
        if self.isRaster:
            ret = RunCommand('r.proj',
                             quiet=True,
                             read=True,
                             flags='l',
                             dbase=dbase,
                             location=location,
                             mapset=mapset)
        else:
            ret = RunCommand('v.proj',
                             quiet=True,
                             read=True,
                             flags='l',
                             dbase=dbase,
                             location=location,
                             mapset=mapset)
        listMaps = list()
        if ret:
            for line in ret.splitlines():
                listMaps.append(line.strip())
        ListSortLower(listMaps)

        self.SetItems(listMaps)
        self.SetValue('')


class ElementSelect(wx.Choice):

    def __init__(self, parent, id=wx.ID_ANY, elements=None,
                 size=globalvar.DIALOG_COMBOBOX_SIZE,
                 **kwargs):
        """Widget for selecting GIS element

        :param parent: parent window
        :param elements: filter elements
        """
        super(ElementSelect, self).__init__(parent, id, size=size,
                                            **kwargs)
        self.SetName("ElementSelect")

        task = gtask.parse_interface('g.list')
        p = task.get_param(value='type')
        self.values = p.get('values', [])
        self.valuesDesc = p.get('values_desc', [])

        if elements:
            values = []
            valuesDesc = []
            for idx in range(0, len(self.values)):
                value = self.values[idx]
                if value in elements:
                    values.append(value)
                    valuesDesc.append(self.valuesDesc[idx])
            self.values = values
            self.valuesDesc = valuesDesc

        self.SetItems(self.valuesDesc)

    def GetValue(self, name):
        """Translate value

        :param name: element name
        """
        idx = self.valuesDesc.index(name)
        if idx > -1:
            return self.values[idx]
        return ''


class OgrTypeSelect(wx.Panel):

    def __init__(self, parent, panel, **kwargs):
        """Widget to choose OGR feature type

        :param parent: parent window
        :param panel: wx.Panel instance used as parent window
        """
        wx.Panel.__init__(self, parent=panel, id=wx.ID_ANY)

        self.ftype = wx.Choice(parent=self, id=wx.ID_ANY, size=(
            200, -1), choices=(_("Point"), _("LineString"), _("Polygon")))
        self._layout()

    def _layout(self):
        """Do layout"""
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(item=wx.StaticText(parent=self,
                                     id=wx.ID_ANY,
                                     label=_("Feature type:")),
                  proportion=1,
                  flag=wx.ALIGN_CENTER_VERTICAL,
                  border=5)
        sizer.Add(item=self.ftype,
                  proportion=0,
                  flag=wx.EXPAND | wx.ALIGN_RIGHT)

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

    def GetType(self):
        """Get selected type as string

        :return: feature type as string
        """
        sel = self.ftype.GetSelection()
        if sel == 0:
            return 'point'
        elif sel == 1:
            return 'line'
        elif sel == 2:
            return 'boundary'


class CoordinatesSelect(wx.Panel):

    def __init__(self, parent, giface, multiple=False, **kwargs):
        """Widget to get coordinates from map window  by mouse click

        :param parent: parent window
        :param giface: GRASS interface
        :param multiple: - True if it is possible to insert more coordinates
        """
        self._giface = giface
        self.multiple = multiple
        self.mapWin = None
        self.drawMapWin = None

        super(CoordinatesSelect, self).__init__(parent=parent, id=wx.ID_ANY)

        self.coordsField = wx.TextCtrl(parent=self, id=wx.ID_ANY,
                                       size=globalvar.DIALOG_TEXTCTRL_SIZE,
                                       validator=CoordinatesValidator())

        icon = wx.Bitmap(
            os.path.join(
                globalvar.ICONDIR,
                "grass",
                "pointer.png"))
        self.buttonInsCoords = buttons.ThemedGenBitmapToggleButton(
            parent=self, id=wx.ID_ANY, bitmap=icon, size=globalvar.DIALOG_COLOR_SIZE)
        self.registered = False
        self.buttonInsCoords.Bind(wx.EVT_BUTTON, self._onClick)

        mapdisp = self._giface.GetMapDisplay()
        if mapdisp:
            switcher = mapdisp.GetToolSwitcher()
            switcher.AddCustomToolToGroup(
                group='mouseUse',
                btnId=self.buttonInsCoords.GetId(),
                toggleHandler=self.buttonInsCoords.SetValue)
        self._doLayout()
        self.coordsField.Bind(wx.EVT_TEXT, lambda event: self._draw(delay=1))

    def _doLayout(self):
        self.dialogSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.dialogSizer.Add(item=self.coordsField,
                             proportion=1,
                             flag=wx.EXPAND)
        self.dialogSizer.Add(item=self.buttonInsCoords)
        self.SetSizer(self.dialogSizer)

    def _onClick(self, event):
        """Button for interacitve inserting of coordinates clicked"""

        self.mapWin = self._giface.GetMapWindow()
        if self.buttonInsCoords.GetToggle() and self.mapWin:
            switcher = self._giface.GetMapDisplay().GetToolSwitcher()
            switcher.ToolChanged(self.buttonInsCoords.GetId())
            if self.mapWin.RegisterMouseEventHandler(wx.EVT_LEFT_DOWN,
                                                     self._onMapClickHandler,
                                                     'cross') == False:
                return

            self.registered = True
            self._giface.GetMapDisplay().Raise()
        else:
            if self.mapWin and self.mapWin.UnregisterMouseEventHandler(
                    wx.EVT_LEFT_DOWN, self._onMapClickHandler):
                self.registered = False
                return

    def drawCleanUp(self):
        if self.drawMapWin:
            self.drawMapWin.UnregisterGraphicsToDraw(self.pointsToDraw)

    def _draw(self, delay):
        """Draws points representing inserted coordinates in mapwindow."""
        if self.drawMapWin != self.mapWin:
            self.drawCleanUp()
            if self.mapWin:
                self.drawMapWin = self.mapWin
                self.pointsToDraw = self.drawMapWin.RegisterGraphicsToDraw(
                    graphicsType="point")

        if self.drawMapWin:
            items = self.pointsToDraw.GetAllItems()
            for i in items:
                self.pointsToDraw.DeleteItem(i)

            coords = self._getCoords()
            if coords is not None:
                for i in range(len(coords) / 2):
                    i = i * 2
                    self.pointsToDraw.AddItem(
                        coords=(coords[i], coords[i + 1]))

            self._giface.updateMap.emit(
                render=False, renderVector=False, delay=delay)

    def _getCoords(self):
        """Get list of coordinates.

        :return: None if values are not valid
        """
        if self.coordsField.GetValidator().Validate():
            return self.coordsField.GetValue().split(',')

        return None

    def _onMapClickHandler(self, event):
        """Gets coordinates from mapwindow"""
        if event == "unregistered":
            return

        e, n = self.mapWin.GetLastEN()
        prevCoords = ""

        if self.multiple:
            prevCoords = self.coordsField.GetValue().strip()
            if prevCoords != "":
                prevCoords += ","

        value = prevCoords + str(e) + "," + str(n)
        self.coordsField.SetValue(value)

        self._draw(delay=0)

    def OnClose(self):
        """Unregistrates _onMapClickHandler from mapWin"""
        self.drawCleanUp()
        self._giface.updateMap.emit(render=False, renderVector=False)

        mapdisp = self._giface.GetMapDisplay()
        if mapdisp:
            switcher = mapdisp.GetToolSwitcher()
            switcher.RemoveCustomToolFromGroup(self.buttonInsCoords.GetId())

        if self.mapWin and self.registered:
            self.mapWin.UnregisterMouseEventHandler(wx.EVT_LEFT_DOWN,
                                                    self._onMapClickHandler)

    def GetTextWin(self):
        """Get TextCtrl widget"""
        return self.coordsField


class VectorCategorySelect(wx.Panel):
    """Widget that allows interactive selection of vector features"""

    def __init__(self, parent, giface, task=None):
        super(VectorCategorySelect, self).__init__(parent=parent, id=wx.ID_ANY)
        self.task = task
        self.parent = parent
        self.giface = giface

        self.selectedFeatures = None
        self.registered = False
        self._vectorSelect = None

        self.mapdisp = self.giface.GetMapDisplay()

        self.catsField = wx.TextCtrl(parent=self, id=wx.ID_ANY,
                                     size=globalvar.DIALOG_TEXTCTRL_SIZE)

        icon = wx.Bitmap(
            os.path.join(
                globalvar.ICONDIR,
                "grass",
                "select.png"))
        self.buttonVecSelect = buttons.ThemedGenBitmapToggleButton(
            parent=self, id=wx.ID_ANY, bitmap=icon, size=globalvar.DIALOG_COLOR_SIZE)
        self.buttonVecSelect.Bind(wx.EVT_BUTTON, self._onClick)

        if self.mapdisp:
            switcher = self.mapdisp.GetToolSwitcher()
            switcher.AddCustomToolToGroup(
                group='mouseUse',
                btnId=self.buttonVecSelect.GetId(),
                toggleHandler=self.buttonVecSelect.SetValue)

        self._layout()

    def _isMapSelected(self):
        """Check if layer list contains at least one selected map
        """
        layerList = self.giface.GetLayerList()
        layerSelected = layerList.GetSelectedLayer()
        if layerSelected is None:
            GWarning(
                _("No vector map selected in layer manager. Operation canceled."))
            return False

        return True

    def _chckMap(self):
        """Check if selected map in 'input' widget is the same as selected map in lmgr """
        if self._isMapSelected():
            layerList = self.giface.GetLayerList()
            layerSelected = layerList.GetSelectedLayer()
            inputName = self.task.get_param('input')
            if inputName['value'] != str(layerSelected):
                if inputName['value'] == '' or inputName['value'] is None:
                    GWarning(_("Input vector map is not selected"))
                    return False
                GWarning(
                    _(
                        "Input vector map <%s> and selected map <%s> in layer manager are different. "
                        "Operation canceled.") %
                    (inputName['value'], str(layerSelected)))
                return False
            return True
        return False

    def _onClick(self, evt=None):
        if self.task is not None:
            if not self._chckMap():
                self.buttonVecSelect.SetValue(False)
                return
        else:
            if not self._isMapSelected():
                self.buttonVecSelect.SetValue(False)
                return
        if self._vectorSelect is None:

            if self.mapdisp:
                if self.buttonVecSelect.IsEnabled():
                    switcher = self.mapdisp.GetToolSwitcher()
                    switcher.ToolChanged(self.buttonVecSelect.GetId())

                self._vectorSelect = VectorSelectBase(
                    self.mapdisp, self.giface)
                if self.mapdisp.GetWindow().RegisterMouseEventHandler(
                        wx.EVT_LEFT_DOWN, self._onMapClickHandler, 'cross') == False:
                    return
                self.registered = True
                self.mapdisp.Raise()
        else:
            self.OnClose()

    def OnClose(self, event=None):
        if not self.mapdisp:
            return

        switcher = self.mapdisp.GetToolSwitcher()
        switcher.RemoveCustomToolFromGroup(self.buttonVecSelect.GetId())
        if self._vectorSelect is not None:
            tmp = self._vectorSelect.GetLineStringSelectedCats()
            self._vectorSelect.OnClose()
            self.catsField.SetValue(tmp)
        self._vectorSelect = None

    def _onMapClickHandler(self, event):
        """Update category text input widget"""
        if event == "unregistered":
            return

        if self.task is None:
            if not self._isMapSelected():
                self.OnClose()
            else:
                self.catsField.SetValue(
                    self._vectorSelect.GetLineStringSelectedCats())
        else:
            if not self._chckMap():
                self.OnClose()
            else:
                self.catsField.SetValue(
                    self._vectorSelect.GetLineStringSelectedCats())

    def GetTextWin(self):
        return self.catsField

    def GetValue(self):
        return self.catsField.GetValue()

    def SetValue(self, value):
        self.catsField.SetValue(value)

    def _layout(self):
        self.dialogSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.dialogSizer.Add(item=self.catsField,
                             proportion=1,
                             flag=wx.EXPAND)

        self.dialogSizer.Add(item=self.buttonVecSelect)
        self.SetSizer(self.dialogSizer)


class SignatureSelect(wx.ComboBox):
    """Widget for selecting signatures"""

    def __init__(self, parent, element, id=wx.ID_ANY,
                 size=globalvar.DIALOG_GSELECT_SIZE, **kwargs):
        super(SignatureSelect, self).__init__(parent, id, size=size,
                                              **kwargs)
        self.element = element
        self.SetName("SignatureSelect")

    def Insert(self, group, subgroup=None):
        """Insert signatures for defined group/subgroup

        :param group: group name (can be fully-qualified)
        :param subgroup: non fully-qualified name of subgroup
        """
        if not group:
            return
        gisenv = grass.gisenv()
        try:
            name, mapset = group.split('@', 1)
        except ValueError:
            name = group
            mapset = gisenv['MAPSET']

        path = os.path.join(
            gisenv['GISDBASE'],
            gisenv['LOCATION_NAME'],
            mapset, 'group', name)

        if subgroup:
            path = os.path.join(path, 'subgroup', subgroup)
        try:
            items = list()
            for element in os.listdir(os.path.join(path, self.element)):
                items.append(element)
            self.SetItems(items)
        except OSError:
            self.SetItems([])
        self.SetValue('')


class SeparatorSelect(wx.ComboBox):
    """Widget for selecting seperator"""

    def __init__(self, parent, id=wx.ID_ANY,
                 size=globalvar.DIALOG_GSELECT_SIZE, **kwargs):
        super(SeparatorSelect, self).__init__(parent, id, size=size,
                                              **kwargs)
        self.SetName("SeparatorSelect")
        self.SetItems(['pipe', 'comma', 'space', 'tab', 'newline'])