-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathmdeditorfactory.py
2030 lines (1783 loc) · 72.3 KB
/
mdeditorfactory.py
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
#!/usr/bin/env python3
"""
@package editor
@module g.gui.metadata
@brief base editor, read/write ISO metadata, generator of widgets in editor
Classes:
- editor::MdFileWork
- editor::MdBox
- editor::MdWxDuplicator
- editor::MdItem
- editor::MdNotebookPage
- editor::MdMainEditor
(C) 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 Matej Krejci <matejkrejci gmail.com> (GSoC 2014)
"""
import contextlib
import os
import re
import sys
import tempfile
from subprocess import PIPE
from grass.pygrass.modules import Module
from gui_core.widgets import (
EmailValidator,
IntegerValidator,
NTCValidator,
SimpleValidator,
TimeISOValidator,
)
import wx
import wx.lib.scrolledpanel as scrolled
from . import globalvar
from .mdjinjaparser import JinjaTemplateParser
# =========================================================================
# MD filework
# =========================================================================
ADD_RM_BUTTON_SIZE = (35, 35)
class MdFileWork:
"""initializer of metadata in OWSLib and export OWSLib object to xml by jinja template system"""
def __init__(self, pathToXml=None):
try:
global Environment, FileSystemLoader, etree, GError, GMessage, mdutil
from jinja2 import Environment, FileSystemLoader
from lxml import etree
from core.gcmd import GError, GMessage
from . import mdutil
except ModuleNotFoundError as e:
msg = e.msg
sys.exit(
globalvar.MODULE_NOT_FOUND.format(
lib=msg.split("'")[-2], url=globalvar.MODULE_URL
)
)
self.path = pathToXml
self.owslibInfo = None
def initMD(self, path=None):
"""
@brief initialize metadata
@param path: path to xml
@return: initialized md object by input xml
"""
if path is None:
self.md = mdutil.get_md_metadatamod_inst(md=None)
return self.md
else:
io = open(path, "r")
str1 = ""
for line in io.readlines():
str1 += mdutil.removeNonAscii(line)
io.close()
io1 = open(path, "w")
io1.write(str1)
io1.close()
try:
tree = etree.parse(path)
root = tree.getroot()
self.md = mdutil.get_md_metadatamod_inst(root)
return self.md
except Exception as e:
GError("Error loading xml:\n" + str(e))
def saveToXML(
self,
md,
owsTagList,
jinjaPath,
outPath=None,
xmlOutName=None,
msg=True,
rmTeplate=False,
):
"""
@note creator of xml with using OWSLib md object and jinja template
@param md: owslib.iso.MD_Metadata
@param owsTagList: in case if user is defining template
@param jinjaPath: path to jinja template
@param outPath: path of exported xml
@param xmlOutName: name of exported xml
@param msg: gmesage info after export
@param rmTeplate: remove template after use
@return: initialized md object by input xml
"""
# if output file name is None, use map name and add postfix
self.dirpath = os.path.dirname(os.path.realpath(__file__))
self.md = md
self.owsTagList = owsTagList
if xmlOutName is None:
xmlOutName = "RANDExportMD" # TODO change to name of map
if not xmlOutName.lower().endswith(".xml"):
xmlOutName += ".xml"
# if path is None, use lunch. dir
if not outPath:
outPath = os.path.join(self.dirpath, xmlOutName)
else:
outPath = os.path.join(outPath, xmlOutName)
xml = open(jinjaPath, "r")
str1 = ""
for line in xml.readlines():
line = mdutil.removeNonAscii(line)
str1 += line
xml.close
try:
io = open(jinjaPath, "w")
io.write(str1)
io.close()
except Exception as err:
print(
"WARNING: Cannot check and remove non ascii characters from template err:< %s >"
% err
)
# generating xml using jinja templates
head, tail = os.path.split(jinjaPath)
env = Environment(loader=FileSystemLoader(head))
env.globals.update(zip=zip)
template = env.get_template(tail)
if self.owsTagList is None:
iso_xml = template.render(md=self.md)
else:
iso_xml = template.render(md=self.md, owsTagList=self.owsTagList)
xml_file = xmlOutName
try:
xml_file = open(outPath, "w")
xml_file.write(iso_xml)
xml_file.close()
if msg:
GMessage("File is exported to: %s" % outPath)
if rmTeplate:
os.remove(jinjaPath)
return outPath
except Exception as e:
GError("Error writing xml:\n" + str(e))
# =========================================================================
# CREATE BOX (staticbox+button(optional)
# =========================================================================
class MdBox(wx.Panel):
"""widget(static box) which include metadata items (MdItem)"""
def __init__(self, parent, label="label"):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.label = label
self.mdItems = list()
self.stbox = wx.StaticBox(
self, label=label, id=wx.ID_ANY, style=wx.RAISED_BORDER
)
self.stbox.SetForegroundColour((0, 0, 0))
self.stbox.SetBackgroundColour((200, 200, 200))
self.stbox.SetFont(wx.Font(12, wx.NORMAL, wx.NORMAL, wx.NORMAL))
def addItems(self, items, multi=True, rmMulti=False, isFirstNum=-1):
"""
@param items: editor::MdItems
@param multi: true in case when box has button for duplicating box and included items
@param rmMulti: true in case when box has button for removing box and included items
@param isFirstNum: handling with 'add' and 'remove' button of box.
this param is necessary for generating editor in editor::MdEditor.generateGUI.inBlock()
note: just first generated box has 'add' button (because being mandatory) and next others has
'remove' button
"""
if isFirstNum != 1:
multi = False
rmMulti = True
# if not initialize in jinja template (default is true)
if multi is None:
multi = True
self.panelSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.panelSizer)
self.boxButtonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.panelSizer.AddSpacer(5)
self.panelSizer.Add(self.boxButtonSizer, flag=wx.EXPAND, proportion=1)
self.stBoxSizer = wx.StaticBoxSizer(self.stbox, orient=wx.VERTICAL)
self.boxButtonSizer.Add(self.stBoxSizer, flag=wx.EXPAND, proportion=1)
for item in items:
self.mdItems.append(item)
self.stBoxSizer.Add(item, flag=wx.EXPAND, proportion=1)
self.stBoxSizer.AddSpacer(5)
if multi:
self.addBoxButt = wx.Button(
self, id=wx.ID_ANY, size=ADD_RM_BUTTON_SIZE, label="+"
)
self.boxButtonSizer.Add(self.addBoxButt, 0)
self.addBoxButt.Bind(wx.EVT_BUTTON, self.duplicateBox)
if rmMulti:
self.rmBoxButt = wx.Button(
self, id=wx.ID_ANY, size=ADD_RM_BUTTON_SIZE, label="-"
)
self.boxButtonSizer.Add(self.rmBoxButt, 0)
self.rmBoxButt.Bind(wx.EVT_BUTTON, self.removeBox)
def addDuplicatedItem(self, item):
self.stBoxSizer.Add(
item,
proportion=1,
flag=wx.EXPAND | wx.BOTTOM,
border=5,
)
self.GetParent().Layout()
def getCtrlID(self):
return self.GetId()
def removeBox(self, evt):
for item in self.mdItems:
item.mdDescription.removeMdItem(item)
self.GetParent().removeBox(self)
def removeMdItem(self, mdItem, items):
"""
@param mdItem: object editor::MdItem
@param items: widgets to destroy
"""
mdItem.mdDescription.removeMdItem(mdItem)
for item in items:
try:
item.Destroy()
except:
pass
self.stBoxSizer.Remove(mdItem)
self.GetParent().Layout()
def duplicateBox(self, evt):
duplicator = MdWxDuplicator(self.mdItems, self.GetParent(), self.label)
clonedBox = duplicator.mdBox
self.GetParent().addDuplicatedItem(clonedBox, self.GetId())
# ===============================================================================
# Handling keywords from database
# ===============================================================================
class MdBoxKeywords(MdBox):
def __init__(self, parent, parent2, label):
super(MdBoxKeywords, self).__init__(parent, label)
self.panelSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.panelSizer)
self.boxButtonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.parent2 = parent2
self.panelSizer.Add(self.boxButtonSizer, flag=wx.EXPAND, proportion=1)
self.parent = parent
self.stBoxSizer = wx.StaticBoxSizer(self.stbox, orient=wx.VERTICAL)
self.boxButtonSizer.Add(self.stBoxSizer, flag=wx.EXPAND, proportion=1)
self.itemHolder = []
self.textTMP = None
def addKeywordItem(self, item):
self.stBoxSizer.Add(
item,
proportion=1,
border=5,
flag=wx.EXPAND | wx.BOTTOM,
)
def removeKeywordItem(self, item):
self.parent2.removeKeyfromBox(item, self.textTMP)
self.stBoxSizer.Remove(item)
self.parent.Fit()
# ===============================================================================
# DUPLICATOR OF WIDGETS-mditem
# ===============================================================================
class MdWxDuplicator:
"""duplicator of MdBox and MdItem object"""
def __init__(self, mdItems, parent, boxlabel=None, mdItemOld=None, template=None):
"""
@param mdItems: list of editor::MdItem
@param parent: parent of new duplicated box
@param boxlabel: label of static box
@param mdItemOld: object which will be duplicated
@param template: in case if 'template mode' is on in editor
"""
# duplicate box of items
if boxlabel:
itemList = list()
self.mdBox = MdBox(parent, boxlabel)
for i in mdItems:
try: # check if item has multiple button
i.addItemButt.GetLabel()
multi = True
except:
multi = False
try: # check if chckBoxEdit exists
i.chckBoxEdit.GetValue()
template = True
except:
template = False
i = i.mdDescription # var mdDescription is jinjainfo::MdDescription
mdItem1 = MdItem(
parent=self.mdBox,
item=i,
multiplicity=multi,
isFirstNum=1,
chckBox=template,
)
itemList.append(mdItem1)
i.addMdItem(mdItem1) # add item with using jinjainfo::MDescription
self.mdBox.addItems(itemList, False, True) # fill box
else: # duplicate only MdItem
self.mdItem = MdItem(
parent=parent,
item=mdItems,
multiplicity=False,
rmMulti=True,
isFirstNum=-1,
chckBox=template,
)
try:
if mdItems.inbox is not None:
mdItems.addMdItem(self.mdItem, mdItemOld)
else:
mdItems.addMdItem(self.mdItem)
except:
mdItems.addMdItem(self.mdItem)
# =========================================================================
# METADATA ITEM (label+ctrlText+button(optional)+chckbox(template)
# =========================================================================
class MdItem(wx.BoxSizer):
"""main building blocks of generated GUI of editor"""
def __init__(
self,
parent,
item,
multiplicity=None,
rmMulti=False,
isFirstNum=-1,
chckBox=False,
):
"""
@param item: jinjainfo::MdDescription(initialized by parsing information from jinja template)
@param multiplicity: if true- widget has button for duplicate self
@param rmMulti: if true- widget has button for remove self
@param isFirstNum: handling with 'add' and 'remove' button of box.
this param is necessary for generating editor in editor::MdEditor.generateGUI.inBlock()
note: just first generated box has 'add' button (because being mandatory) and next others has
'remove' button
@param chckBox: in case-True 'template editor' is on and widget has checkbox
"""
wx.BoxSizer.__init__(self, wx.VERTICAL)
self.isValid = False
self.isChecked = False
self.mdDescription = item
self.chckBox = chckBox
self.multiple = multiplicity
self.parent = parent
added = False
if multiplicity is None:
self.multiple = item.multiplicity
if isFirstNum != 1:
self.multiple = False
if isFirstNum != 1 and item.multiplicity:
rmMulti = True
self.tagText = wx.StaticText(parent=parent, id=wx.ID_ANY, label=item.name)
if self.mdDescription.databaseAttr == "language":
self.fillComboDB("language")
added = True
elif self.mdDescription.databaseAttr == "topicCategory":
self.fillComboDB("topicCategory")
added = True
elif self.mdDescription.databaseAttr == "degree":
self.fillComboDB("degree")
added = True
elif self.mdDescription.databaseAttr == "dateType":
self.fillComboDB("dateType")
added = True
elif self.mdDescription.databaseAttr == "role":
self.fillComboDB("role")
added = True
if self.chckBox is False and not added:
if item.multiline is True:
self.valueCtrl = wx.TextCtrl(
parent,
id=wx.ID_ANY,
size=(0, 70),
validator=self.validators(item.type),
style=wx.VSCROLL
| wx.TE_MULTILINE
| wx.TE_WORDWRAP
| wx.TAB_TRAVERSAL
| wx.RAISED_BORDER,
)
else:
self.valueCtrl = wx.TextCtrl(
parent,
id=wx.ID_ANY,
validator=self.validators(item.type),
style=wx.VSCROLL
| wx.TE_DONTWRAP
| wx.TAB_TRAVERSAL
| wx.RAISED_BORDER
| wx.HSCROLL,
)
elif self.chckBox is True and not added:
if item.multiline is True:
self.valueCtrl = wx.TextCtrl(
parent,
id=wx.ID_ANY,
size=(0, 70),
style=wx.VSCROLL
| wx.TE_MULTILINE
| wx.TE_WORDWRAP
| wx.TAB_TRAVERSAL
| wx.RAISED_BORDER,
)
else:
self.valueCtrl = wx.TextCtrl(
parent,
id=wx.ID_ANY,
style=wx.VSCROLL
| wx.TE_DONTWRAP
| wx.TAB_TRAVERSAL
| wx.RAISED_BORDER
| wx.HSCROLL,
)
self.valueCtrl.Bind(wx.EVT_MOTION, self.onMove)
self.valueCtrl.SetExtraStyle(wx.WS_EX_VALIDATE_RECURSIVELY)
if self.multiple:
self.addItemButt = wx.Button(parent, -1, size=ADD_RM_BUTTON_SIZE, label="+")
self.addItemButt.Bind(wx.EVT_BUTTON, self.duplicateItem)
if rmMulti:
self.rmItemButt = wx.Button(parent, -1, size=ADD_RM_BUTTON_SIZE, label="-")
self.rmItemButt.Bind(wx.EVT_BUTTON, self.removeItem)
if self.chckBox:
self.chckBoxEdit = wx.CheckBox(parent, -1, size=(30, 30))
self.chckBoxEdit.Bind(wx.EVT_CHECKBOX, self.onChangeChckBox)
self.chckBoxEdit.SetValue(False)
self.isChecked = False
self.valueCtrl.Disable()
self.createInfo()
self.tip = wx.ToolTip(self.infoTip)
self._addItemLay(item.multiline, rmMulti)
def fillComboDB(self, label):
if label == "language":
lang = [
"Afrikaans",
"Albanian",
"Arabic",
"Armenian",
"Basque",
"Bengali",
"Bulgarian",
"Catalan",
"Cambodian",
"Chinese",
"Croatian",
"Czech",
"Danish",
"Dutch",
"English",
"Estonian",
"Fiji",
"Finnish",
"French",
"Georgian",
"German",
"Greek",
"Gujarati",
"Hebrew",
"Hindi",
"Hungarian",
"Icelandic",
"Indonesian",
"Irish",
"Italian",
"Japanese",
"Javanese",
"Korean",
"Latin",
"Latvian",
"Lithuanian",
"Macedonian",
"Malay",
"Malayalam",
"Maltese",
"Maori",
"Marathi",
"Mongolian",
"Nepali",
"Norwegian",
"Persian",
"Polish",
"Portuguese",
"Punjabi",
"Quechua",
"Romanian",
"Russian",
"Samoan",
"Serbian",
"Slovak",
"Slovenian",
"Spanish",
"Swahili",
"Swedish",
"Tamil",
"Tatar",
"Telugu",
"Thai",
"Tibetan",
"Tonga",
"Turkish",
"Ukrainian",
"Urdu",
"Uzbek",
"Vietnamese",
"Welsh",
"Xhosa",
]
self.valueCtrl = wx.ComboBox(
self.parent,
id=wx.ID_ANY,
)
for lng in lang:
self.valueCtrl.Append(lng)
if label == "topicCategory":
lang = [
"farming",
"biota",
"boundaries",
"climatologyMeteorologyAtmosphere",
"economy",
"elevation",
"enviroment",
"geoscientificInformation",
"health",
"imageryBaseMapsEarthCover",
"intelligenceMilitary",
"inlandWaters",
"location",
"planningCadastre",
"society",
"structure",
"transportation",
"utilitiesCommunication",
]
self.valueCtrl = wx.ComboBox(
self.parent,
id=wx.ID_ANY,
)
for lng in lang:
self.valueCtrl.Append(lng)
if label == "degree":
lang = ["Not evaluated", "Not conformant", "Conformant"]
self.valueCtrl = wx.ComboBox(
self.parent,
id=wx.ID_ANY,
)
for lng in lang:
self.valueCtrl.Append(lng)
if label == "dateType":
lang = ["Date of creation", "Date of last revision", "Date of publication"]
self.valueCtrl = wx.ComboBox(
self.parent,
id=wx.ID_ANY,
)
for lng in lang:
self.valueCtrl.Append(lng)
if label == "role":
lang = [
"Author",
"Custodian",
"Distributor",
"Originator",
"Owner",
"Point of contact",
"Principal Investigation",
"Processor",
"Publisher",
"Resource provider",
"User",
]
self.valueCtrl = wx.ComboBox(
self.parent,
id=wx.ID_ANY,
)
for lng in lang:
self.valueCtrl.Append(lng)
def validators(self, validationStyle):
if validationStyle == "email":
return EmailValidator()
if validationStyle == "integer":
return NTCValidator("DIGIT_ONLY")
if validationStyle == "decimal":
return NTCValidator("DIGIT_ONLY")
if validationStyle == "date":
return TimeISOValidator()
# return EmptyValidator()
return SimpleValidator("")
def onChangeChckBox(self, evt):
"""current implementation of editor mode for defining templates not allowed to check
only single items in static box. There are two cases: all items in box are checked or not.
"""
if self.mdDescription.inbox: # MdItems are in box
try:
items = self.valueCtrl.GetParent().mdItems
if self.isChecked:
self.valueCtrl.Disable()
self.isChecked = False
else:
self.valueCtrl.Enable()
self.isChecked = True
for item in items:
if self.isChecked:
item.valueCtrl.Enable()
item.chckBoxEdit.SetValue(True)
item.isChecked = True
else:
item.valueCtrl.Disable()
item.chckBoxEdit.SetValue(False)
item.isChecked = False
except:
pass
else:
if self.isChecked:
self.valueCtrl.Disable()
self.isChecked = False
else:
self.valueCtrl.Enable()
self.isChecked = True
def onMove(self, evt=None):
self.valueCtrl.SetToolTip(self.tip)
def createInfo(self):
"""Feed for tooltip"""
string = ""
if self.mdDescription.ref is not None:
string += self.mdDescription.ref + "\n\n"
if self.mdDescription.name is not None:
string += "NAME: \n" + self.mdDescription.name + "\n\n"
if self.mdDescription.desc is not None:
string += "DESCRIPTION: \n" + self.mdDescription.desc + "\n\n"
if self.mdDescription.example is not None:
string += "EXAMPLE: \n" + self.mdDescription.example + "\n\n"
if self.mdDescription.type is not None:
string += "DATA TYPE: \n" + self.mdDescription.type + "\n\n"
string += "*" + "\n"
if self.mdDescription.statements is not None:
string += "Jinja template info: \n" + self.mdDescription.statements + "\n"
if self.mdDescription.statements1 is not None:
string += self.mdDescription.statements1 + "\n"
string += "OWSLib info:\n" + self.mdDescription.tag
self.infoTip = string
def removeItem(self, evt):
"""adding all items in self(mdItem) to list and call parent remover"""
ilist = [self.valueCtrl, self.tagText]
try:
ilist.append(self.rmItemButt)
except:
pass
try:
ilist.append(self.chckBoxEdit)
except:
pass
self.valueCtrl.GetParent().removeMdItem(self, ilist)
def duplicateItem(self, evt):
"""add Md item to parent(box or notebook page)"""
parent = self.valueCtrl.GetParent()
# if parent is box
if self.mdDescription.inbox:
duplicator = MdWxDuplicator(
mdItems=self.mdDescription,
parent=parent,
mdItemOld=self,
template=self.chckBox,
)
else:
duplicator = MdWxDuplicator(
mdItems=self.mdDescription, parent=parent, template=self.chckBox
)
clonedMdItem = duplicator.mdItem
# call parent "add" function
self.valueCtrl.GetParent().addDuplicatedItem(clonedMdItem)
def setValue(self, value):
"""Set value & color of widgets
in case if is template creator 'on':
yellow: in case if value is marked by $NULL(by mdgrass::GrassMD)
red: if value is '' or object is not initialized. e.g. if user
read non fully valid INSPIRE xml with INSPIRE jinja template,
the GUI generating mechanism will create GUI according to template
and all missing tags(xml)-gui(TextCtrls) will be marked by red
"""
if value is None or value == "":
if self.chckBox:
self.chckBoxEdit.SetValue(True)
self.isChecked = True
try:
self.onChangeChckBox(None)
self.onChangeChckBox(None)
except:
pass
self.valueCtrl.SetBackgroundColour((245, 204, 230)) # red
self.valueCtrl.SetValue("")
self.valueCtrl.Enable()
elif self.chckBox and value == "$NULL":
self.valueCtrl.SetBackgroundColour((255, 255, 82)) # yellow
self.valueCtrl.SetValue("")
if self.chckBox:
self.chckBoxEdit.SetValue(True)
self.isChecked = True
self.valueCtrl.Enable()
try:
self.onChangeChckBox(None)
self.onChangeChckBox(None)
except:
pass
elif value == "$NULL":
self.valueCtrl.SetValue("")
else:
self.isValid = True
self.valueCtrl.SetValue(value)
def getValue(self):
value = mdutil.replaceXMLReservedChar(self.valueCtrl.GetValue())
value = value.replace("\n", "")
value = value.replace('"', "")
value = value.replace("'", "")
return value
def getCtrlID(self):
return self.valueCtrl.GetId()
def _addItemLay(self, multiline, rmMulti):
self.textFieldSizer = wx.BoxSizer(wx.HORIZONTAL)
if multiline is True:
self.textFieldSizer.Add(self.valueCtrl, proportion=1, flag=wx.EXPAND)
else:
self.textFieldSizer.Add(self.valueCtrl, proportion=1)
if self.multiple:
self.textFieldSizer.Add(self.addItemButt, 0)
if rmMulti:
self.textFieldSizer.Add(self.rmItemButt, 0)
if self.chckBox:
self.textFieldSizer.Add(self.chckBoxEdit, 0)
self.Add(self.tagText, proportion=0)
self.Add(self.textFieldSizer, proportion=0, flag=wx.EXPAND)
class MdItemKeyword(wx.BoxSizer):
def __init__(self, parent, text, keyword, title, keywordObj):
wx.BoxSizer.__init__(self, wx.VERTICAL)
self.isValid = False
self.isChecked = False
self.keywordObj = keywordObj
self.text = wx.StaticText(parent=parent, id=wx.ID_ANY, label=text)
self.parent = parent
self.rmItemButt = wx.Button(parent, -1, size=ADD_RM_BUTTON_SIZE, label="-")
self.rmItemButt.Bind(wx.EVT_BUTTON, self.removeItem)
self.keyword = keyword
self.title = title
# self.createInfo()
# self.tip = wx.ToolTip(self.infoTip)
self.layout()
def getVal(self):
return self.text.GetLabel()
def getKyewordObj(self):
self.keywordObj["keywords"] = self.keyword
self.keywordObj["title"] = self.title
return self.keywordObj
def removeItem(self, evt):
self.parent.textTMP = self.text.GetLabel()
self.textFieldSizer.Clear()
# self.textFieldSizer.Destroy()
self.rmItemButt.Destroy()
self.text.Destroy()
self.parent.removeKeywordItem(self)
def layout(self):
self.textFieldSizer = wx.BoxSizer(wx.HORIZONTAL)
self.textFieldSizer.Add(
self.rmItemButt,
0,
flag=wx.RIGHT,
border=5,
)
self.textFieldSizer.Add(
self.text,
0,
flag=wx.RIGHT | wx.ALIGN_CENTER_VERTICAL,
)
self.Add(self.textFieldSizer, proportion=0, flag=wx.EXPAND)
# =========================================================================
# =========================================================================
# ADD NOTEBOOK PAGE
# =========================================================================
class MdNotebookPage(scrolled.ScrolledPanel):
"""
every notebook page is initialized by jinjainfo::MdDescription.group (label)
"""
def __init__(self, parent):
scrolled.ScrolledPanel.__init__(self, parent=parent, id=wx.ID_ANY)
self.items = []
self._addNotebookPageLay()
self.sizerIndexDict = {}
self.sizerIndex = 0
def _addNotebookPageLay(self):
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.mainSizer)
def _getIndex(self):
"""
index for handling position of editor::MdBox,MdItem in editor::MdNotebookPage(self).
Primary for correct duplicating Boxes or items on notebook page
"""
self.sizerIndex += 1
return self.sizerIndex
def addKeywordObj(self, item):
self.mainSizer.Add(item, proportion=0, flag=wx.EXPAND)
def addItem(self, item):
"""
@param item: can be editor::MdBox or editor::MDItem
"""
if isinstance(item, list):
for i in item:
if isinstance(i, list):
for ii in i:
self.sizerIndexDict[ii.getCtrlID()] = self._getIndex()
self.mainSizer.Add(ii, proportion=0, flag=wx.EXPAND)
else:
self.sizerIndexDict[i.getCtrlID()] = self._getIndex()
self.mainSizer.Add(i, proportion=0, flag=wx.EXPAND)
else:
self.sizerIndexDict[item.getCtrlID()] = self._getIndex()
self.mainSizer.Add(item, proportion=0, flag=wx.EXPAND)
def addDuplicatedItem(self, item, mId):
"""adding duplicated object to sizer to position after parent"""
self.items.append(item)
posIndex = self.sizerIndexDict[mId]
self.mainSizer.Insert(posIndex, item, proportion=0, flag=wx.EXPAND)
self.GetParent().Refresh()
self.Layout()
self.SetupScrolling()
def removeBox(self, box):
box.Destroy()
self.SetSizerAndFit(self.mainSizer)
def removeMdItem(self, mdDes, items):
"""Remove children
@param mdDes: editor::MdItem.mdDescription
@param items: all widgets to remove of MdItem
"""
mdDes.mdDescription.removeMdItem(
mdDes
) # remove from jinjainfi:MdDEscription object
for item in items:
item.Destroy()
self.SetSizerAndFit(self.mainSizer)
# class MdItemKyewords
class MdKeywords(wx.BoxSizer):
def __init__(self, parent, mdObject, mdOWS):
wx.BoxSizer.__init__(self, wx.VERTICAL)
try:
global GMessage
from core.gcmd import GMessage
except ModuleNotFoundError as e:
msg = e.msg
sys.exit(
globalvar.MODULE_NOT_FOUND.format(
lib=msg.split("'")[-2], url=globalvar.MODULE_URL
)
)
self.itemHolder = set()
self.parent = parent
self.keywordsOWSObject = mdOWS
self.comboKeysLabel = wx.StaticText(
parent=self.parent, id=wx.ID_ANY, label="Keywords from repositories"
)
self.comboKeys = wx.ComboBox(parent=self.parent, id=wx.ID_ANY)
self.keysList = wx.TreeCtrl(
parent=self.parent,
id=wx.ID_ANY,
size=(0, 120),
style=wx.TR_FULL_ROW_HIGHLIGHT | wx.TR_DEFAULT_STYLE,
)
self.box = MdBoxKeywords(parent=parent, parent2=self, label="Keywords")
self.memKeys = set()