-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeAFtool.py
858 lines (747 loc) · 38.8 KB
/
LeAFtool.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
import logging.config
import os, sys
from sys import path as syspath, executable
import subprocess
from pathlib import Path
import yaml
import pandas as pd
from collections import OrderedDict
from PyQt5.QtCore import Qt, QCoreApplication, pyqtSlot, QObject, pyqtSignal, QSize
from PyQt5.QtGui import QIcon, QPixmap, QColor
from PyQt5.Qsci import QsciScintilla, QsciLexerYAML
import PyQt5.QtWidgets as qt
from functools import partial
import PyIPSDK
python_exe = sys.executable
# auto add Explorer in PYTHONPATH
explorer_path = Path(PyIPSDK.getPyIPSDKDir()).parent.parent.joinpath("Explorer", "Interface")
syspath.insert(0, explorer_path.as_posix())
# import explorer variables/functions
import UsefullVariables as vrb
import UsefullWidgets as wgt
import UsefullFunctions as fct
import DatabaseFunction as Dfct
# add plugin LeAFtool to PYTHONPATH
syspath.insert(0, Path(vrb.folderMacroInterface + "/LeAFtool/").as_posix())
# Import LeAFtool class
from Leaftool_addons.DrawCut import DrawCutParams
from Leaftool_addons.MachineLearning import MachineLearningParams
from Leaftool_addons.commonWidget import style, scroll_style, return_default_folder, TableWidget, TwoListSelection, FileSelectorLeaftool, Documentator, check_values
# from Leaftool_addons.cmd_LeAFtool import LeAFtool
# configure logger
logging.config.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'stdout_handler': {
'level': f'{"DEBUG"}',
'class': "logging.StreamHandler",
'formatter': '',
'stream': 'ext://sys.stdout',
},
},
'loggers': {
"": {
'handlers': ['stdout_handler'],
'propagate': True,
},
}
})
logger = logging.getLogger('LeAFtool GUI')
class QTextEditLogger(logging.Handler, QObject):
appendPlainText = pyqtSignal(str)
def __init__(self, parent):
super().__init__()
QObject.__init__(self)
self.widget = qt.QPlainTextEdit(parent)
self.widget.setLineWrapMode(qt.QPlainTextEdit.NoWrap)
self.widget.setReadOnly(True)
self.widget.setStyleSheet('background-color:#4c4c4c;border:1px;border-style:solid;border-color:#999;')
# self.widget.setFixedWidth(int(1000 * vrb.ratio))
self.appendPlainText.connect(self.widget.appendPlainText)
def emit(self, record):
message = self.format(record)
if "DEBUG" in message.upper():
self.widget.setStyleSheet('background-color:#4c4c4c;border:1px;border-style:solid;border-color:#999;color: #ffffff;')
elif "INFO" in message.upper():
self.widget.setStyleSheet('background-color:#4c4c4c;border:1px;border-style:solid;border-color:#999;color: #48dada;')
elif "WARNING" in message.upper():
self.widget.setStyleSheet('background-color:#4c4c4c;border:1px;border-style:solid;border-color:#999;color: #f5f500;')
elif "ERROR" in message.upper():
self.widget.setStyleSheet('background-color:#4c4c4c;border:1px;border-style:solid;border-color:#999;color: #ff0000;')
elif "CRITICAL" in message.upper():
self.widget.setStyleSheet('background-color:#4c4c4c;border:1px;border-style:solid;border-color:#999;color: #aa0000;')
if "[" in message[:5]:
self.appendPlainText.emit(f"{message.rstrip()[5:-4]}")
elif "\n" in message:
self.appendPlainText.emit(f"{message.rstrip()}")
else:
self.appendPlainText.emit(f"{message}")
qt.QApplication.processEvents()
def clear(self):
self.widget.clear()
class YAMLEditor(QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setLexer(QsciLexerYAML(self))
self.setReadOnly(True)
# Set the zoom factor, the factor is in points.
self.zoomTo(2)
# line numbers margin colors
self.setMarginsBackgroundColor(QColor("#323232"))
self.setMarginsForegroundColor(QColor("#323232"))
# Use boxes as folding visual
self.setFolding(self.BoxedTreeFoldStyle)
# Braces matching
self.setBraceMatching(self.SloppyBraceMatch)
# folding margin colors (foreground,background)
self.setFoldMarginColors(QColor("#929292"),
QColor("#323232"))
# Show whitespace to help detect whitespace errors
self.setWhitespaceVisibility(True)
self.setIndentationGuides(True)
class ToolsActivation(qt.QGroupBox):
"""add tools selection layer"""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.loading = False
# Layout Style
self.setTitle("Globals Parameters")
self.setStyleSheet(style)
self.layout = qt.QGridLayout()
# self.layout.setSizeConstraint(qt.QVBoxLayout.SetDefaultConstraint)
self.layout.setContentsMargins(10, 0, 10, 5)
self.setLayout(self.layout)
self.setFixedWidth(int(1000 * vrb.ratio))
# self.setAutoFillBackground(True)
# Widgets
self.plant_model_label = qt.QLabel()
self.plant_model_label.setText("Plant model:")
self.plant_model_label.setFixedHeight(int(30 * vrb.ratio))
self.plant_model_label.setWhatsThis(self.parent.parent.dico_doc["PLANT_MODEL"])
self.plant_model_label.setStatusTip(self.parent.parent.dico_doc_str["PLANT_MODEL"])
self.plant_model = qt.QComboBox()
self.plant_model.addItems(["leaf", "banana"])
self.plant_model.setFixedSize(int(100 * vrb.ratio), int(25 * vrb.ratio))
self.plant_model.setWhatsThis(self.parent.parent.dico_doc["PLANT_MODEL"])
self.plant_model.setStatusTip(self.parent.parent.dico_doc_str["PLANT_MODEL"])
self.show_meta_checkbox = qt.QCheckBox()
self.show_meta_checkbox.setText("Show Meta Section")
self.show_meta_checkbox.setChecked(True)
self.show_meta_checkbox.setFixedWidth(int(150 * vrb.ratio))
self.show_meta_checkbox.setWhatsThis(self.parent.parent.dico_doc["show_meta"])
self.show_meta_checkbox.setStatusTip(self.parent.parent.dico_doc_str["show_meta"])
icon_size = 35
self.draw_label = qt.QLabel()
self.draw_label.setText("Draw")
self.draw_label.setFixedSize(int(55 * vrb.ratio), int(icon_size * vrb.ratio))
self.draw = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/draw.png")
self.draw.setCheckable(True)
self.draw.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.draw.setWhatsThis(self.parent.parent.dico_doc["draw"])
self.draw.setStatusTip(self.parent.parent.dico_doc_str["draw"])
self.cut_label = qt.QLabel()
self.cut_label.setText("Cut")
self.cut_label.setFixedSize(int(55 * vrb.ratio), int(icon_size * vrb.ratio))
self.cut = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/cut.png")
self.cut.setCheckable(True)
self.cut.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.cut.setWhatsThis(self.parent.parent.dico_doc["cut"])
self.cut.setStatusTip(self.parent.parent.dico_doc_str["cut"])
self.ml_label = qt.QLabel()
self.ml_label.setText("ML")
self.ml_label.setFixedSize(int(55 * vrb.ratio), int(icon_size * vrb.ratio))
self.ml = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/ml.png")
self.ml.setCheckable(True)
self.ml.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.ml.setWhatsThis(self.parent.parent.dico_doc["ml"])
self.ml.setStatusTip(self.parent.parent.dico_doc_str["ml"])
self.merge_label = qt.QLabel()
self.merge_label.setText("Merge")
self.merge_label.setFixedSize(int(60 * vrb.ratio), int(icon_size * vrb.ratio))
self.merge = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/merge.png")
self.merge.setCheckable(True)
self.merge.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.merge.setWhatsThis(self.parent.parent.dico_doc["merge"])
self.merge.setStatusTip(self.parent.parent.dico_doc_str["merge"])
self.csv_file = FileSelectorLeaftool(label="CSV file:", file=True)
self.csv_file.setWhatsThis(self.parent.parent.dico_doc["csv_file"])
self.csv_file.setStatusTip(self.parent.parent.dico_doc_str["csv_file"])
self.list_selection = TwoListSelection()
self.list_selection.setWhatsThis(self.parent.parent.dico_doc["rename"])
self.list_selection.setStatusTip(self.parent.parent.dico_doc_str["rename"])
# self.list_selection.setAutoFillBackground(True)
self.list_selection.setMaximumHeight(int(65 * vrb.ratio))
# Position widgets
self.layout.addWidget(self.plant_model_label, 0, 0, Qt.AlignLeft)
self.layout.addWidget(self.plant_model, 0, 1, Qt.AlignLeft)
self.layout.addWidget(self.show_meta_checkbox, 0, 2, Qt.AlignLeft)
# Layout Style
tools_group = qt.QGroupBox()
tools_group.setTitle("Tools activation")
tools_group.setStyleSheet(style)
tools_group.layout = qt.QGridLayout()
tools_group.layout.setContentsMargins(5, 10, 0, 0)
tools_group.setLayout(tools_group.layout)
tools_group.layout.addWidget(self.draw, 0, 0, Qt.AlignRight)
tools_group.layout.addWidget(self.draw_label, 0, 1, Qt.AlignLeft)
tools_group.layout.addWidget(self.cut, 0, 2, Qt.AlignRight)
tools_group.layout.addWidget(self.cut_label, 0, 3, Qt.AlignLeft)
tools_group.layout.addWidget(self.ml, 0, 4, Qt.AlignRight)
tools_group.layout.addWidget(self.ml_label, 0, 5, Qt.AlignLeft)
tools_group.layout.addWidget(self.merge, 0, 6, Qt.AlignRight)
tools_group.layout.addWidget(self.merge_label, 0, 7, Qt.AlignLeft)
tools_group.setFixedWidth(int(600 * vrb.ratio))
self.layout.addWidget(tools_group, 0, 3, Qt.AlignTop)
# Meta group
self.meta_group = qt.QGroupBox()
self.meta_group.setTitle("Meta Infos")
self.meta_group.setStyleSheet(style)
self.meta_group.layout = qt.QVBoxLayout()
# self.meta_group.layout.setSizeConstraint(qt.QVBoxLayout.SetDefaultConstraint)
self.meta_group.layout.setContentsMargins(10, 10, 10, 10)
self.meta_group.setLayout(self.meta_group.layout)
self.meta_group.layout.addWidget(self.csv_file)
self.meta_group.layout.addWidget(self.list_selection)
self.meta_group.setFixedWidth(int(1000 * vrb.ratio))
# self.meta_group.setAutoFillBackground(True)
# self.layout.addWidget(self.meta_group, 1, 0, 1, 4)
# connections
self.draw.clicked.connect(self.update_activation_tools)
self.cut.clicked.connect(self.update_activation_tools)
self.ml.clicked.connect(self.update_activation_tools)
self.merge.clicked.connect(self.update_activation_tools)
self.plant_model.currentIndexChanged.connect(self.update_activation_tools)
self.csv_file.lineEditFile.textChanged.connect(partial(self.parent.check_path, from_object=self.csv_file.lineEditFile, keys_list="'csv_file'"))
# self.list_selection.mInput.itemSelectionChanged.connect(self.update_activation_tools)
# self.list_selection.mOuput.itemSelectionChanged.connect(self.update_activation_tools)
self.list_selection.mBtnMoveToAvailable.clicked.connect(self.update_activation_tools)
self.list_selection.mBtnMoveToSelected.clicked.connect(self.update_activation_tools)
self.list_selection.mButtonToAvailable.clicked.connect(self.update_activation_tools)
self.list_selection.mButtonToSelected.clicked.connect(self.update_activation_tools)
self.list_selection.mBtnUp.clicked.connect(self.update_activation_tools)
self.list_selection.mBtnDown.clicked.connect(self.update_activation_tools)
self.show_meta_checkbox.stateChanged.connect(self.show_meta_section)
self.show_meta_section()
def show_meta_section(self):
self.meta_group.setVisible(self.show_meta_checkbox.isChecked())
def upload_activation_tools(self):
try:
self.loading = True
if self.parent.dict_for_yaml["PLANT_MODEL"] not in ["banana", "leaf"]:
self.parent.logger.error(
f"Error: arguments PLANT_MODEL:'{self.parent.dict_for_yaml['PLANT_MODEL']}' is not allow, please use only 'leaf' or 'banana', please reload valid file")
else:
self.plant_model.setCurrentText(self.parent.dict_for_yaml["PLANT_MODEL"])
# check all boolean values
message = check_values(dico_params=self.parent.dict_for_yaml,
primary_key="RUNSTEP",
secondary_key_list=["draw", "cut", "ML", "merge"],
type_value=bool,
default_error=False)
if message:
self.parent.logger.error(message)
self.draw.setChecked(bool(self.parent.dict_for_yaml["RUNSTEP"]["draw"]))
self.cut.setChecked(bool(self.parent.dict_for_yaml["RUNSTEP"]["cut"]))
self.ml.setChecked(bool(self.parent.dict_for_yaml["RUNSTEP"]["ML"]))
self.merge.setChecked(bool(self.parent.dict_for_yaml["RUNSTEP"]["merge"]))
self.csv_file.lineEditFile.setText(self.parent.dict_for_yaml["csv_file"])
self.list_selection.clean_list()
self.list_selection.add_right_elements(self.parent.dict_for_yaml["rename"])
self.loading = False
except KeyError as e:
self.parent.logger.error(f"ERROR: Key {e} is not found on file")
self.parent.dict_for_yaml = self.parent.dict_backup
def update_activation_tools(self):
if not self.loading:
self.parent.dict_for_yaml["RUNSTEP"]["cut"] = self.cut.isChecked()
self.parent.dict_for_yaml["RUNSTEP"]["draw"] = self.draw.isChecked()
self.parent.layer_draw_cut.show_draw_params()
self.parent.dict_for_yaml["RUNSTEP"]["ML"] = self.ml.isChecked()
self.parent.dict_for_yaml["RUNSTEP"]["merge"] = self.merge.isChecked()
self.parent.layer_ml_merge.show_ml_merge_params()
self.parent.dict_for_yaml["PLANT_MODEL"] = self.plant_model.currentText()
self.parent.dict_for_yaml["csv_file"] = self.csv_file.lineEditFile.text()
self.update_header_csv()
self.parent.dict_for_yaml["rename"] = self.list_selection.get_right_elements()
self.parent.preview_config.setText(self.parent.export_use_yaml)
def disable_running_bottom(self):
# if all disable, disable run
if (not self.parent.layer_tools.draw.isChecked() \
and not self.parent.layer_tools.cut.isChecked() \
and not self.parent.layer_tools.ml.isChecked() \
and not self.parent.layer_tools.merge.isChecked()) or self.parent.warning_found:
self.parent.layer_leaftool_params.run.setDisabled(True)
self.parent.layer_leaftool_params.save.setDisabled(True)
else:
self.parent.layer_leaftool_params.run.setDisabled(False)
self.parent.layer_leaftool_params.save.setDisabled(False)
# self.csv_file.setVisible(self.parent.layer_tools.draw.isChecked())
# self.csv_file.setVisible(self.parent.layer_tools.cut.isChecked())
# self.csv_file.setVisible(self.parent.layer_tools.ml.isChecked())
def update_header_csv(self):
if self.parent.dict_for_yaml["csv_file"] and Path(self.parent.dict_for_yaml["csv_file"]).exists():
header_txt = Path(self.parent.dict_for_yaml["csv_file"]).open("r").readline().strip()
sep_dict = {",": header_txt.count(","),
";": header_txt.count(";"),
".": header_txt.count("."),
"\t": header_txt.count("\t")
}
csv_separator = max(sep_dict, key=sep_dict.get)
header_list = header_txt.split(csv_separator)
left_list = self.list_selection.get_left_elements()
right_list = self.list_selection.get_right_elements()
# if header_list not equal left_list+right_list
if len(header_list) != len(left_list+right_list):
if len(left_list) == 0:
self.list_selection.add_left_elements(list(set(header_list) - set(right_list)))
else:
self.list_selection.add_left_elements(header_list)
else:
self.list_selection.clean_list()
class LeaftoolParams(qt.QGroupBox):
def __init__(self, parent):
super().__init__()
self.parent = parent
# Layout Style
self.setTitle("LeAFtool")
self.setStyleSheet(style)
# self.setAutoFillBackground(True)
self.layout = qt.QGridLayout()
# self.layout.setSizeConstraint(qt.QVBoxLayout.SetMinAndMaxSize)
self.setMaximumHeight(int(80*vrb.ratio))
self.layout.setContentsMargins(10, 10, 10, 10)
self.setLayout(self.layout)
self.setFixedWidth(int(1000 * vrb.ratio))
self.loading = False
# Widgets
icon_size = 35
self.save_label = qt.QLabel()
self.save_label.setText("Save YAML:")
self.save = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/Save_As.png")
self.save.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.save.setWhatsThis(self.parent.parent.dico_doc["save"])
self.save.setStatusTip(self.parent.parent.dico_doc_str["save"])
self.upload_label = qt.QLabel()
self.upload_label.setText("Upload YAML:")
self.upload = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/upload.png")
self.upload.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.upload.setWhatsThis(self.parent.parent.dico_doc["upload"])
self.upload.setStatusTip(self.parent.parent.dico_doc_str["upload"])
self.run_label = qt.QLabel()
self.run_label.setText("Run:")
self.run = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/run.png")
self.run.setCheckable(True)
self.run.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.run.setWhatsThis(self.parent.parent.dico_doc["run"])
self.run.setStatusTip(self.parent.parent.dico_doc_str["run"])
self.preview_yaml_checkbox = qt.QCheckBox()
self.preview_yaml_checkbox.setChecked(False)
self.preview_yaml_checkbox.setText("Preview YAML")
self.preview_yaml_checkbox.setFixedSize(int(120 * vrb.ratio), int(30 * vrb.ratio))
self.preview_yaml_checkbox.setWhatsThis(self.parent.parent.dico_doc["preview"])
self.preview_yaml_checkbox.setStatusTip(self.parent.parent.dico_doc_str["preview"])
self.debug_checkbox = qt.QCheckBox()
self.debug_checkbox.setChecked(False)
self.debug_checkbox.setText("Debug")
self.debug_checkbox.setFixedSize(int(120 * vrb.ratio), int(30 * vrb.ratio))
self.debug_checkbox.setWhatsThis(self.parent.parent.dico_doc["debug"])
self.debug_checkbox.setStatusTip(self.parent.parent.dico_doc_str["debug"])
self.clean_label = qt.QLabel()
self.clean_label.setText("Reset:")
self.clean = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/clean.png")
self.clean.setCheckable(True)
self.clean.setFixedSize(int(icon_size * vrb.ratio), int(icon_size * vrb.ratio))
self.clean.setWhatsThis(self.parent.parent.dico_doc["clean"])
self.clean.setStatusTip(self.parent.parent.dico_doc_str["clean"])
# Position Widgets
self.layout.addWidget(self.upload_label, 0, 0, Qt.AlignRight)
self.layout.addWidget(self.upload, 0, 1, Qt.AlignLeft)
self.layout.addWidget(self.save_label, 0, 2, Qt.AlignRight)
self.layout.addWidget(self.save, 0, 3, Qt.AlignLeft)
self.layout.addWidget(self.run_label, 0, 4, Qt.AlignRight)
self.layout.addWidget(self.run, 0, 5, Qt.AlignLeft)
self.layout.addWidget(self.clean_label, 0, 6, Qt.AlignRight)
self.layout.addWidget(self.clean, 0, 7, Qt.AlignLeft)
self.layout.addWidget(self.preview_yaml_checkbox, 0, 8, Qt.AlignCenter)
self.layout.addWidget(self.debug_checkbox, 0, 9, Qt.AlignCenter)
self.layout.addWidget(self.parent.parent.whatsThisButton, 0, 10, Qt.AlignCenter)
# Init state
self.hide_preview_yaml()
# Connections
self.preview_yaml_checkbox.clicked.connect(self.hide_preview_yaml)
self.debug_checkbox.clicked.connect(self.update_debug)
def hide_preview_yaml(self):
if self.preview_yaml_checkbox.isChecked():
self.parent.preview_config.setVisible(True)
else:
self.parent.preview_config.setVisible(False)
def update_debug(self):
try:
if not self.loading:
self.parent.dict_for_yaml["debug"] = bool(self.debug_checkbox.isChecked())
self.parent.preview_config.setText(self.parent.export_use_yaml)
except KeyError as e:
self.parent.logger.error(f"ERROR: Key {e} is not found on file")
self.parent.dict_for_yaml = self.parent.dict_backup
def upload_debug(self):
self.loading = True
self.debug_checkbox.setChecked(bool(self.parent.dict_for_yaml["debug"]))
self.loading = False
class RunLeAFtool(qt.QWidget):
""""""
def __init__(self, parent):
super().__init__()
self.parent = parent
self.dict_for_yaml = {}
self.dict_backup = {}
self.logger = logger
self.leaftool = None
self.connect = None
self.running_process = None
self.warning_found = False
# Layout Style
self.layout = qt.QGridLayout()
self.layout.setSizeConstraint(qt.QVBoxLayout.SetMinAndMaxSize)
self.setLayout(self.layout)
self.setContentsMargins(0, 0, 0, 0)
style_global = fct.getStyleSheet()
self.setStyleSheet(style_global)
self.setStyleSheet(scroll_style)
# Create the text output widget.
self.process = QTextEditLogger(self)
self.logger.addHandler(self.process)
self.logger.setLevel(logging.DEBUG)
# add preview of yaml file
self.preview_config = qt.QPlainTextEdit()
self.preview_config = YAMLEditor()
self.preview_config.setMinimumWidth(500)
self.preview_config.setAutoFillBackground(True)
self.preview_config.setWhatsThis(self.parent.dico_doc["preview_edit"])
self.preview_config.setStatusTip(self.parent.dico_doc_str["preview_edit"])
# add path config file
self.yaml_path = vrb.folderMacroInterface + "/LeAFtool/config.yaml"
self.default_yaml_path = vrb.folderMacroInterface + "/LeAFtool/config.yaml"
# Add layer part
self.layer_tools = ToolsActivation(parent=self)
self.layer_draw_cut = DrawCutParams(parent=self)
self.layer_leaftool_params = LeaftoolParams(parent=self)
self.layer_ml_merge = MachineLearningParams(parent=self)
# # size policy
not_resize = self.layer_ml_merge.sizePolicy()
not_resize.setRetainSizeWhenHidden(True)
self.layer_ml_merge.setSizePolicy(not_resize)
not_resize = self.layer_draw_cut.sizePolicy()
not_resize.setRetainSizeWhenHidden(True)
self.layer_draw_cut.setSizePolicy(not_resize)
self.layout.addWidget(self.layer_leaftool_params, 0, 0, 1, 2)
self.layout.addWidget(self.layer_tools, 1, 0, 1, 2)
self.layout.addWidget(self.layer_tools.meta_group, 2, 0, 1, 2)
self.layout.addWidget(self.preview_config, 0, 3, 5, 1)
self.layout.addWidget(self.layer_draw_cut, 3, 0)
self.layout.addWidget(self.layer_ml_merge, 3, 1)
self.layout.addWidget(self.process.widget, 4, 0, 1, 2)
not_resize = self.sizePolicy()
not_resize.setRetainSizeWhenHidden(True)
self.setSizePolicy(not_resize)
self.load_yaml()
# self.setAutoFillBackground(True)
# Layer leaftool params connection
self.layer_leaftool_params.upload.clicked.connect(self.upload_yaml)
self.layer_leaftool_params.save.clicked.connect(self.save_yaml)
self.layer_leaftool_params.run.clicked.connect(self.change_run_state)
self.layer_leaftool_params.clean.clicked.connect(self.reset_error)
qt.QApplication.instance().focusChanged.connect(self.on_focus_changed)
def reset_error(self):
"""reset error on load params"""
self.process.clear()
self.get_warning()
self.update_all()
def change_run_state(self):
if self.layer_leaftool_params.run.isChecked():
if self.save_yaml():
self.layer_leaftool_params.run_label.setText("Stop:")
self.layer_leaftool_params.run.pixmap = QPixmap(vrb.folderMacroInterface + "/LeAFtool/Images/stop.png")
self.layer_leaftool_params.run.resizeEvent(None)
self.start_threads()
else:
self.layer_leaftool_params.run.setChecked(False)
else:
self.layer_leaftool_params.run_label.setText("Run:")
self.layer_leaftool_params.run.pixmap = QPixmap(vrb.folderMacroInterface + "/LeAFtool/Images/run.png")
self.layer_leaftool_params.run.resizeEvent(None)
self.layer_leaftool_params.run.setChecked(False)
self.abort_workers()
def start_threads(self):
self.process.clear()
# self.leaftool = LeAFtool(config_file=self.yaml_path)
# cmd = f"{python_exe} {Path(__file__).parent.joinpath('Leaftool_addons', 'cmd_LeAFtool.py')} -c {Path(self.yaml_path).resolve()}"
# self.running_process = subprocess.Popen("exec " + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=os.environ)
cmd = f"source /etc/profile; {executable} {Path(__file__).parent.joinpath('Leaftool_addons', 'cmd_LeAFtool.py')} -c {Path(self.yaml_path).resolve()}"
self.logger.info(cmd)
self.running_process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, executable="/bin/bash")
with self.running_process:
for line in iter(self.running_process.stdout.readline, b''):
line = line.decode("utf-8").rstrip().rstrip()
if line:
self.logger.info(line)
self.layer_leaftool_params.run.setChecked(False)
del self.running_process
self.running_process = None
self.change_run_state()
def abort_workers(self):
if self.running_process:
self.logger.info('Asking to abort')
self.running_process.kill()
del self.running_process
self.running_process = None
self.logger.info("kill Thread success")
def load_yaml(self):
self.process.clear()
with open(self.yaml_path, "r") as file_config:
self.dict_for_yaml = yaml.load(file_config, Loader=yaml.Loader)
with open(self.default_yaml_path, "r") as file_config:
self.dict_backup = yaml.load(file_config, Loader=yaml.Loader)
# self.mask_yaml()
# from pprint import pprint as pp
# pp(self.dict_for_yaml)
# print(f"{'#'*15}\ndict_backup\n{'#'*15}\n{self.dict_backup}")
self.upload_all()
@pyqtSlot("QWidget*", "QWidget*")
def on_focus_changed(self, old, now):
self.update_all()
self.get_warning()
self.layer_tools.disable_running_bottom()
# pass
def get_warning(self):
"""Get warning on txt"""
txt = self.process.widget.toPlainText()
if "warning" in txt.lower() or "error" in txt.lower():
self.warning_found = True
else:
self.warning_found = False
self.process.clear()
def update_all(self):
self.layer_tools.update_activation_tools()
self.layer_draw_cut.update_draw_cut_params()
self.layer_ml_merge.update_ml_params()
self.layer_leaftool_params.update_debug()
self.preview_config.setText(self.export_use_yaml)
self.dict_backup = self.dict_for_yaml.copy()
def upload_all(self):
self.layer_tools.upload_activation_tools()
self.layer_draw_cut.upload_draw_cut_params()
self.layer_ml_merge.upload_ml_params()
self.layer_leaftool_params.upload_debug()
self.update_all()
def mask_yaml(self):
# Remove section on YAML if not activated
# For ML/merge
# If not ML AND not merge delete
if not self.dict_for_yaml["RUNSTEP"]["ML"] and not self.dict_for_yaml["RUNSTEP"]["merge"] and "ML" in self.dict_for_yaml:
self.dict_backup.update({"ML": self.dict_for_yaml["ML"]})
del self.dict_for_yaml["ML"]
# if key ML not in dict and ML or MERGE is TRUE
elif "ML" not in self.dict_for_yaml and (self.dict_for_yaml["RUNSTEP"]["ML"] or self.dict_for_yaml["RUNSTEP"]["merge"]):
self.dict_for_yaml.update({"ML": self.dict_backup["ML"]})
if not self.dict_for_yaml["RUNSTEP"]["merge"] and "MERGE" in self.dict_for_yaml:
self.dict_backup.update({"MERGE": self.dict_for_yaml["MERGE"]})
del self.dict_for_yaml["MERGE"]
elif self.dict_for_yaml["RUNSTEP"]["merge"] and "MERGE" not in self.dict_for_yaml:
self.dict_for_yaml.update({"MERGE": self.dict_backup["MERGE"]})
# For Draw/cut
if not self.dict_for_yaml["RUNSTEP"]["draw"] and not self.dict_for_yaml["RUNSTEP"]["cut"] and "DRAW-CUT" in self.dict_for_yaml:
self.dict_backup.update({"DRAW-CUT": self.dict_for_yaml["DRAW-CUT"]})
del self.dict_for_yaml["DRAW-CUT"]
elif "DRAW-CUT" not in self.dict_for_yaml and (self.dict_for_yaml["RUNSTEP"]["draw"] or self.dict_for_yaml["RUNSTEP"]["cut"]):
self.dict_for_yaml.update({"DRAW-CUT": self.dict_backup["DRAW-CUT"]})
@property
def export_use_yaml(self):
"""Use to print a dump config.yaml with corrected parameters"""
def represent_dictionary_order(self, dict_data):
return self.represent_mapping('tag:yaml.org,2002:map', dict_data.items())
def setup_yaml():
yaml.add_representer(OrderedDict, represent_dictionary_order)
# self.mask_yaml()
setup_yaml()
return yaml.dump(self.dict_for_yaml, default_flow_style=False, sort_keys=False, indent=4)
def upload_yaml(self):
defaultFolder = return_default_folder()
try:
default_filename = list(Path(defaultFolder).glob("*.yaml"))[0].as_posix()
except IndexError:
default_filename = defaultFolder
filename = qt.QFileDialog.getOpenFileName(self, "Select your file", default_filename, "yaml file (*.yaml)")
filename = filename[0]
if filename != "" and filename:
self.yaml_path = filename
self.load_yaml()
Dfct.SubElement(vrb.userPathElement, "ImportImages").text = Path(filename).parent.as_posix()
Dfct.saveXmlElement(vrb.userPathElement, vrb.folderInformation + "/UserPath.mho", forceSave=True)
else:
self.logger.error("Error: Please select file")
def save_yaml(self):
defaultFolder = return_default_folder()
default_filename = Path(defaultFolder).joinpath("config.yaml").as_posix()
filename = qt.QFileDialog.getSaveFileName(self, "Save config yaml file", default_filename, "yaml file(*.yaml)")
filename = filename[0]
if filename != "" and filename:
self.yaml_path = filename
with open(self.yaml_path, "w", encoding='UTF-8', newline='\n') as write_yaml:
write_yaml.write(self.export_use_yaml)
Dfct.SubElement(vrb.userPathElement, "ImportImages").text = Path(filename).parent.as_posix()
Dfct.saveXmlElement(vrb.userPathElement, vrb.folderInformation + "/UserPath.mho", forceSave=True)
return True
else:
return False
def check_path(self, from_object, keys_list):
def clean_warning(keys_list):
txt = self.process.widget.toPlainText()
if keys_list in txt:
txt = "\n".join([elm for elm in txt.split("\n") if keys_list not in elm])
self.process.clear()
self.logger.warning(txt)
path_str = from_object.text()
if path_str:
if Path(path_str).exists():
from_object.setStyleSheet("background-color: #606060;")
clean_warning(keys_list)
else:
self.logger.warning(f"Warning: arguments {keys_list}: '{path_str}' doesn't exist")
from_object.setStyleSheet("background-color: darkRed;")
elif path_str == "":
from_object.setStyleSheet("background-color: #606060;")
clean_warning(keys_list)
class MainInterface(qt.QMainWindow):
"""
"""
def __init__(self):
super().__init__()
self.main = qt.QWidget()
self.setCentralWidget(self.main)
# Add documentation mode:
self.documentator = Documentator()
self.dico_doc = self.documentator.dico_doc
self.dico_doc_str = self.documentator.dico_doc_str
self.whatsThisButton = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/help.png")
self.whatsThisButton.setFixedSize(int(40 * vrb.ratio), int(40 * vrb.ratio))
self.whatsThisButton.clicked.connect(qt.QWhatsThis.enterWhatsThisMode)
self.whatsThisButton.setToolTip("Click on this button and then on another object to get the documentation")
self.statusbar = self.statusBar()
self.statusbar.setSizeGripEnabled(False)
# Layout Style
self.setAttribute(Qt.WA_DeleteOnClose)
self.layout = qt.QVBoxLayout(self.main)
# self.layout.setSizeConstraint(1)
self.main.setContentsMargins(0, 5, 5, 5)
self.main.setAutoFillBackground(True)
style_global = fct.getStyleSheet()
self.setStyleSheet(style_global)
# Add title and logo
self.setWindowTitle("LeAFtool")
self.setWindowIcon(QIcon(vrb.folderMacroInterface + "/LeAFtool/Images/favicon.png"))
self.logo_label = qt.QLabel(self)
self.logo_img = QPixmap(vrb.folderMacroInterface + "/LeAFtool/Images/LeAFtool-long.png")
self.logo_img = self.logo_img.scaledToHeight(80, mode=Qt.FastTransformation)
self.logo_label.setPixmap(self.logo_img)
self.logo_label.setAlignment(Qt.AlignCenter)
self.logo_label.setMaximumHeight(80)
# Initialize the 2 tab screen
self.tabs = qt.QTabWidget()
self.tabs.setTabPosition(qt.QTabWidget.West)
# FIRST TAB PAGE
self.tab1 = RunLeAFtool(parent=self)
# SECOND TAB PAGE
self.csv_file = FileSelectorLeaftool(label="CSV file:", file=True)
self.table_final = TableWidget()
self.table_final.setAutoFillBackground(True)
# SECOND TAB Layout Style
table_group = qt.QGroupBox()
table_group.setTitle("Merge Results")
table_group.setStyleSheet(style)
table_group.layout = qt.QVBoxLayout()
table_group.layout.setContentsMargins(10, 10, 10, 10)
table_group.setLayout(table_group.layout)
table_group.layout.addWidget(self.csv_file)
table_group.layout.addWidget(self.table_final)
# table_group.setAutoFillBackground(True)
self.tab2 = table_group
# Add th 2 tables to object TABS
self.tabs.addTab(self.tab1, " Run LeAFtool ")
self.tabs.addTab(self.tab2, " Explore Results ")
# add TABS to layout
self.layout.addWidget(self.logo_label, Qt.AlignCenter)
self.layout.addWidget(self.tabs)
# Edit connection
self.csv_file.lineEditFile.textChanged.connect(self.update_table)
def update_table(self):
csv_file = self.csv_file.lineEditFile.text()
if csv_file and Path(csv_file).exists():
with open(csv_file, "r") as csv:
header_txt = csv.readline().rstrip()
sep_dict = {",": header_txt.count(","),
";": header_txt.count(";"),
# ".": header_txt.count("."),
"\t": header_txt.count("\t")
}
csv_separator = max(sep_dict, key=sep_dict.get)
path_images = Path(csv_file).parent
df = pd.read_csv(csv_file, index_col=None, header=[0], sep=csv_separator)
ddict = df.to_dict(orient='list')
self.table_final.loadDictionary(ddict, path_images)
else:
self.table_final.clear()
def openWidget():
app = QCoreApplication.instance()
name = app.applicationName()
app.setObjectName("LeAFtool")
app.setApplicationName("LeAFtool")
app.setApplicationDisplayName("LeAFtool")
# SplashScreen
pixmap = QPixmap(vrb.folderMacroInterface + "/LeAFtool/Images/LeAFtool-long.png")
pixmap = pixmap.scaled(700, 700, aspectRatioMode=Qt.KeepAspectRatio, transformMode=Qt.SmoothTransformation)
splashScreen = qt.QSplashScreen(pixmap)
splashScreen.setFixedSize(700, 700)
splashScreen.show()
# Load App interface
main_interface = MainInterface()
main_interface.setWindowIcon(QIcon(vrb.folderMacroInterface + "/LeAFtool/Images/favicon.png"))
splashScreen.finish(main_interface)
main_interface.show()
if name == "LeAFtool.py":
app.exec()
###############################################################################################
# For adding button on Explorer
mainWindow = vrb.mainWindow
if mainWindow:
groupMenu = mainWindow.groupMenu
button = wgt.PushButtonImage(vrb.folderMacroInterface + "/LeAFtool/Images/favicon.png")
button.setFixedSize(30 * vrb.ratio, 30 * vrb.ratio)
groupMenu.layoutBar1.addWidget(button, 0, vrb.numMacro, Qt.AlignVCenter)
vrb.numMacro += 1
button.clicked.connect(openWidget)
###############################################################################################
# MAIN
if __name__ == '__main__':
openWidget()
# app = QCoreApplication.instance()
# if app is None:
# app = qt.QApplication([])
#
# splashScreen = seeSplashScreen()
# app.processEvents()
# app.setWindowIcon(QIcon(vrb.folderMacroInterface + "/LeAFtool/Images/favicon.png"))
#
# qt.QApplication.setStyle(qt.QStyleFactory.create('Fusion')) # <- Choose the style
# main_interface = MainInterface()
#
# # main_interface = FileSelectorLeaftool("test")
# # main_interface = DataExplorer()
# # main_interface = ToolsActivation(parent=RunLeAFtool)
# # main_interface = RunLeAFtool()
# # main_interface = NumberLineEditLabel(constraint="Natural", text="0", label="Y pieces:")
# # main_interface.showFullScreen()
# splashScreen.finish(main_interface)
# main_interface.show()
# app.exec_()