-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools_qt.py
1609 lines (1274 loc) · 53.9 KB
/
tools_qt.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
"""
This file is part of Giswater 3
The program is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
"""
# -*- coding: utf-8 -*-
import inspect
import os
import operator
import sys
import subprocess
import traceback
from typing import Dict, Literal, Optional
import webbrowser
from functools import partial
from encodings.aliases import aliases
from warnings import warn
from sip import isdeleted
from qgis.PyQt.QtCore import QDate, QDateTime, QSortFilterProxyModel, QStringListModel, QTime, Qt, QRegExp, pyqtSignal,\
QPersistentModelIndex, QCoreApplication, QTranslator, QEvent, QLocale
from qgis.PyQt.QtGui import QPixmap, QDoubleValidator, QTextCharFormat, QFont, QIcon
from qgis.PyQt.QtSql import QSqlTableModel
from qgis.PyQt.QtWidgets import QAction, QLineEdit, QComboBox, QWidget, QDoubleSpinBox, QCheckBox, QLabel, QTextEdit, \
QDateEdit, QAbstractItemView, QCompleter, QDateTimeEdit, QTableView, QSpinBox, QTimeEdit, QPushButton, \
QPlainTextEdit, QRadioButton, QSizePolicy, QSpacerItem, QFileDialog, QGroupBox, QMessageBox, QTabWidget, QToolBox, \
QToolButton, QDialog, QGridLayout
from qgis.core import QgsExpression, QgsProject, QgsLayerTreeLayer
from qgis.gui import QgsDateTimeEdit
from qgis.utils import iface
from . import tools_log, tools_os, tools_qgis, tools_db
from . import lib_vars
from .ui.ui_manager import DialogTextUi
translator = QTranslator()
dlg_text = DialogTextUi()
class GwExtendedQLabel(QLabel):
clicked = pyqtSignal()
def __init(self, parent):
QLabel.__init__(self, parent)
def mouseReleaseEvent(self, ev):
self.clicked.emit()
class GwHyperLinkLabel(QLabel):
clicked = pyqtSignal()
def __init__(self):
QLabel.__init__(self)
self.setStyleSheet("QLabel{color:blue; text-decoration: underline;}")
def mouseReleaseEvent(self, ev):
self.clicked.emit()
self.setStyleSheet("QLabel{color:purple; text-decoration: underline;}")
class GwHyperLinkLineEdit(QLineEdit):
clicked = pyqtSignal()
def __init__(self):
QLabel.__init__(self)
self.setStyleSheet("QLineEdit{color:blue; text-decoration: underline;}")
def mouseReleaseEvent(self, ev):
if self.isReadOnly():
self.clicked.emit()
self.setStyleSheet("QLineEdit { background: rgb(242, 242, 242); color:purple; text-decoration: underline; border: none;}")
class GwEditDialog(QDialog):
"""
Dialog with just one widget (QLineEdit, QTextEdit, QComboBox, QCheckBox).
Use example:
```
edit_dialog = GwEditDialog(dialog, title=f"Edit {header}",
label_text=f"Set new '{header}' value for result '{result_id}':",
widget_type="QTextEdit", initial_value=value)
if edit_dialog.exec_() == QDialog.Accepted:
new_value = edit_dialog.get_value()
self._update_data(result_id, columnname, new_value)
```
"""
def __init__(self, parent=None, title="Edit", label_text="", widget_type="QLineEdit", options=None, initial_value=None):
super(GwEditDialog, self).__init__(parent)
self.setWindowTitle(title)
self.layout = QGridLayout(self)
# Add the label
self.label = QLabel(label_text, self)
self.layout.addWidget(self.label, 0, 0, 1, 2)
# Create the widget based on the type
if widget_type == "QLineEdit":
self.widget = QLineEdit(self)
elif widget_type == "QTextEdit":
self.widget = QTextEdit(self)
elif widget_type == "QComboBox":
self.widget = QComboBox(self)
if options:
self.widget.addItems(options)
elif widget_type == "QCheckBox":
self.widget = QCheckBox(self)
else:
raise ValueError("Unsupported widget type")
self.layout.addWidget(self.widget, 1, 0, 1, 2)
if initial_value is not None:
self.set_value(initial_value)
# Add buttons
self.accept_button = QPushButton("Accept", self)
self.cancel_button = QPushButton("Cancel", self)
self.accept_button.clicked.connect(self.accept)
self.cancel_button.clicked.connect(self.reject)
self.layout.addWidget(self.accept_button, 2, 0)
self.layout.addWidget(self.cancel_button, 2, 1)
def get_value(self):
if isinstance(self.widget, QLineEdit):
return self.widget.text()
elif isinstance(self.widget, QTextEdit):
return self.widget.toPlainText()
elif isinstance(self.widget, QComboBox):
return self.widget.currentText()
elif isinstance(self.widget, QCheckBox):
return self.widget.isChecked()
else:
return None
def set_value(self, value):
set_widget_text(self, self.widget, value)
QtMatchFlag = Literal['starts', 'contains', 'ends', 'exact', 'regex']
match_flags: Dict[QtMatchFlag, Qt.MatchFlag] = {
'starts': Qt.MatchStartsWith,
'contains': Qt.MatchContains,
'ends': Qt.MatchEndsWith,
'exact': Qt.MatchExactly,
'regex': Qt.MatchRegularExpression,
}
def fill_combo_box(dialog, widget, rows, allow_nulls=True, clear_combo=True):
warn('This method is deprecated, use fill_combo_values instead.', DeprecationWarning, stacklevel=2)
if rows is None:
return
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QComboBox, widget)
if clear_combo:
widget.clear()
if allow_nulls:
widget.addItem('')
for row in rows:
if len(row) > 1:
elem = row[0]
user_data = row[1]
else:
elem = row[0]
user_data = None
if elem is not None:
try:
if type(elem) is int or type(elem) is float:
widget.addItem(str(elem), user_data)
else:
widget.addItem(elem, user_data)
except Exception:
widget.addItem(str(elem), user_data)
def fill_combo_box_list(dialog, widget, list_object, allow_nulls=True, clear_combo=True):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QComboBox, widget)
if widget is None:
return None
if clear_combo:
widget.clear()
if allow_nulls:
widget.addItem('')
for elem in list_object:
widget.addItem(str(elem))
def get_calendar_date(dialog, widget, date_format="yyyy/MM/dd", datetime_format="yyyy/MM/dd hh:mm:ss"):
date = None
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return
if type(widget) is QDateEdit:
date = widget.date().toString(date_format)
elif type(widget) is QDateTimeEdit:
date = widget.dateTime().toString(datetime_format)
elif type(widget) is QgsDateTimeEdit and widget.displayFormat() in \
('dd/MM/yyyy', 'yyyy/MM/dd', 'dd-MM-yyyy', 'yyyy-MM-dd'):
date = widget.dateTime().toString(date_format)
elif type(widget) is QgsDateTimeEdit and widget.displayFormat() in ('dd/MM/yyyy hh:mm:ss', 'yyyy/MM/dd hh:mm:ss'):
date = widget.dateTime().toString(datetime_format)
return date
def set_calendar(dialog, widget, date, default_current_date=True):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return
if lib_vars.date_format in ("dd/MM/yyyy", "dd-MM-yyyy", "yyyy/MM/dd", "yyyy-MM-dd"):
widget.setDisplayFormat(lib_vars.date_format)
if type(widget) is QDateEdit \
or (type(widget) is QgsDateTimeEdit and widget.displayFormat() in
('dd/MM/yyyy', 'yyyy/MM/dd', 'dd-MM-yyyy', 'yyyy-MM-dd')):
if date is None:
if default_current_date:
date = QDate.currentDate()
else:
date = QDate.fromString('01-01-2000', 'dd-MM-yyyy')
widget.setDate(date)
elif type(widget) is QDateTimeEdit \
or (type(widget) is QgsDateTimeEdit and widget.displayFormat() in
('dd/MM/yyyy hh:mm:ss', 'yyyy/MM/dd hh:mm:ss', 'dd-MM-yyyy hh:mm:ss', 'yyyy-MM-dd hh:mm:ss')):
if date is None:
date = QDateTime.currentDateTime()
widget.setDateTime(date)
def set_time(dialog, widget, time):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return
if type(widget) is QTimeEdit:
if time is None:
time = QTime(00, 00, 00)
widget.setTime(time)
def get_widget(dialog, widget):
if isdeleted(dialog):
return None
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
return widget
def get_widget_type(dialog, widget):
if isdeleted(dialog):
return None
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return None
return type(widget)
def get_widget_value(dialog, widget):
value = None
if isdeleted(dialog):
return value
if type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return value
if type(widget) in (QDoubleSpinBox, QLineEdit, QSpinBox, QTextEdit, GwHyperLinkLineEdit):
value = get_text(dialog, widget, return_string_null=False)
elif type(widget) is QComboBox:
value = get_combo_value(dialog, widget, 0)
elif type(widget) is QCheckBox:
value = is_checked(dialog, widget)
if value is not None:
value = str(value).lower()
elif type(widget) is QgsDateTimeEdit:
value = get_calendar_date(dialog, widget)
return value
def get_text(dialog, widget, add_quote=False, return_string_null=True):
if isdeleted(dialog):
return None
if type(widget) is str:
widget = dialog.findChild(QWidget, widget)
text = None
if widget:
if type(widget) in (QLineEdit, QPushButton, QLabel, GwHyperLinkLabel, GwHyperLinkLineEdit):
text = widget.text()
elif type(widget) in (QDoubleSpinBox, QSpinBox):
# When the QDoubleSpinbox contains decimals, for example 2,0001 when collecting the value,
# the spinbox itself sends 2.0000999999, as in reality we only want, maximum 4 decimal places, we round up,
# thus fixing this small failure of the widget
text = round(widget.value(), 4)
elif type(widget) in (QTextEdit, QPlainTextEdit):
text = widget.toPlainText()
elif type(widget) is QComboBox:
text = widget.currentText()
elif type(widget) is QCheckBox:
value = is_checked(dialog, widget)
if type(value) is bool:
text = str(value)
else:
text = None
else:
return None
if text in (None, '') and return_string_null:
text = "null"
elif text in (None, ''):
text = ""
if add_quote and text != "null":
text = "'" + text + "'"
else:
if return_string_null:
text = "null"
else:
text = ""
return text
def set_widget_text(dialog, widget, text):
if type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return
if type(widget) in (QLabel, QLineEdit, QTextEdit, QPushButton):
if str(text) == 'None':
text = ""
widget.setText(f"{text}")
elif type(widget) is QPlainTextEdit:
if str(text) == 'None':
text = ""
widget.insertPlainText(f"{text}")
elif type(widget) is QDoubleSpinBox or type(widget) is QSpinBox:
if text == 'None' or text == 'null':
text = 0
widget.setValue(float(text))
elif type(widget) is QComboBox:
set_selected_item(dialog, widget, text)
elif type(widget) is QTimeEdit:
set_time(dialog, widget, text)
elif type(widget) is QCheckBox:
set_checked(dialog, widget, text)
def is_checked(dialog, widget):
if type(widget) is str:
widget = dialog.findChild(QCheckBox, widget)
if widget is None:
widget = dialog.findChild(QRadioButton, widget)
checked = False
if widget:
state = widget.checkState()
if state == 0:
checked = False
elif state == 1:
checked = None
elif state == 2:
checked = True
return checked
def set_checked(dialog, widget, checked=True):
if str(checked) in ('true', 't', 'True'):
checked = True
elif str(checked) in ('false', 'f', 'False'):
checked = False
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return
if type(widget) is QCheckBox or type(widget) is QRadioButton:
widget.setChecked(bool(checked))
def get_selected_item(dialog, widget, return_string_null=True):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QComboBox, widget)
if return_string_null:
widget_text = "null"
else:
widget_text = ""
if widget:
if widget.currentText():
widget_text = widget.currentText()
return widget_text
def set_selected_item(dialog, widget, text):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QComboBox, widget)
if widget:
index = widget.findText(text)
if index == -1:
index = 0
widget.setCurrentIndex(index)
def set_current_index(dialog, widget, index):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QComboBox, widget)
if widget:
if index == -1:
index = 0
widget.setCurrentIndex(index)
def set_widget_visible(dialog, widget, visible=True):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget:
widget.setVisible(visible)
def set_widget_enabled(dialog, widget, enabled=True):
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget:
widget.setEnabled(enabled)
def add_image(dialog, widget, cat_shape):
""" Set pictures for UD """
element = cat_shape.lower()
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget is None:
return
if type(widget) is QLabel:
plugin_dir = os.path.dirname(__file__)
pic_file = os.path.join(plugin_dir, f'resources{os.sep}png', '' + element + '')
pixmap = QPixmap(pic_file)
widget.setPixmap(pixmap)
widget.show()
def set_autocompleter(combobox, list_items=None):
""" Iterate over the items in the QCombobox, create a list,
create the model, and set the model according to the list
"""
if list_items is None:
list_items = [combobox.itemText(i) for i in range(combobox.count())]
proxy_model = QSortFilterProxyModel(combobox)
_set_model_by_list(list_items, proxy_model)
combobox.editTextChanged.connect(partial(filter_by_list, combobox, proxy_model))
# Set up the completer without changing the combobox's model
completer = QCompleter(proxy_model, combobox)
completer.setCompletionColumn(0)
completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
combobox.setCompleter(completer)
def filter_by_list(combobox, proxy_model, text):
""" Filter the list based on the text input """
proxy_model.setFilterFixedString(text)
if combobox.completer():
combobox.completer().complete()
combobox.completer().popup().hide()
def get_combo_value(dialog, widget, index=0, add_quote=False):
""" Get item data of current index of the @widget """
value = -1
if add_quote:
value = ''
if type(widget) is str or type(widget) is str:
widget = dialog.findChild(QWidget, widget)
if widget:
if type(widget) is QComboBox:
current_index = widget.currentIndex()
elem = widget.itemData(current_index)
if index == -1:
return elem
value = elem[index]
return value
def set_combo_value(combo, value, index, add_new=True):
"""
Set text to combobox populate with more than 1 item for row
:param combo: QComboBox widget to manage
:param value: element to show
:param index: index to compare
:param add_new: if True it will add the value even if it's not in the combo
"""
for i in range(0, combo.count()):
elem = combo.itemData(i)
if elem is not None and value == str(elem[index]):
combo.setCurrentIndex(i)
return True
# Add new value if @value not in combo
if add_new and value not in ("", None, 'None', 'none', '-1', -1):
new_elem = []
# Control if the QComboBox has been previously filled
if combo.count() > 0:
for x in range(len(combo.itemData(0))):
new_elem.append("")
else:
new_elem.append("")
new_elem.append("")
new_elem[0] = value
new_elem[1] = f"({value})"
combo.addItem(new_elem[1], new_elem)
combo.setCurrentIndex(combo.count() - 1)
return False
def fill_combo_values(combo, rows, index_to_show=1, combo_clear=True, sort_combo=True, sort_by=1, add_empty=False, selected_id=None, index_to_compare=None):
"""
Populate @combo with list @rows and show field @index_to_show
:param combo: QComboBox widget to fill (QComboBox)
:param rows: the data that'll fill the combo
:param index_to_show: the index of the row to show (int)
:param combo_clear: whether it should clear the combo or not (bool)
:param sort_combo: whether it should sort the items or not (bool)
:param sort_by: sort combo by this column (int)
:param add_empty: add an empty element as first item (bool)
:param selected_id: The value to be set as selected in the ComboBox (str or int)
:param index_to_compare: Index to compare `selected_id` with the id or value in this combo widget (int).
"""
records = []
if rows is None:
rows = [['', '']]
if sort_by > len(rows[0]) - 1:
sort_by = 1
for row in rows:
elem = []
for x in range(0, len(row)):
elem.append(row[x])
records.append(elem)
combo.blockSignals(True)
if combo_clear:
combo.clear()
records_sorted = records
try:
if sort_combo:
records_sorted = sorted(records, key=operator.itemgetter(sort_by))
except Exception:
pass
finally:
if add_empty:
records_sorted.insert(0, ['', ''])
for record in records_sorted:
combo.addItem(str(record[index_to_show]), record)
combo.blockSignals(False)
if None not in (selected_id, index_to_compare):
set_combo_value(combo, selected_id, index_to_compare)
def set_combo_item_unselectable_by_id(qcombo, list_id=[]):
""" Make items of QComboBox visibles but not selectable"""
for x in range(0, qcombo.count()):
if x in list_id:
index = qcombo.model().index(x, 0)
qcombo.model().setData(index, 0, Qt.UserRole - 1)
def set_combo_item_selectable_by_id(qcombo, list_id=[]):
""" Make items of QComboBox selectable """
for x in range(0, qcombo.count()):
if x in list_id:
index = qcombo.model().index(x, 0)
qcombo.model().setData(index, (1 | 32), Qt.UserRole - 1)
def set_combo_item_select_unselectable(qcombo, list_id=[], column=0, opt=0):
"""
Make items of QComboBox visibles but not selectable
:param qcombo: QComboBox widget to manage (QComboBox)
:param list_id: list of strings to manage ex. ['1','3','...'] or ['word1', 'word3','...'] (list)
:param column: column where to look up the values in the list (int)
:param opt: 0 -> item not selectable // (1 | 32) -> item selectable (int)
"""
for x in range(0, qcombo.count()):
elem = qcombo.itemData(x)
if str(elem[column]) in list_id:
index = qcombo.model().index(x, 0)
qcombo.model().setData(index, opt, Qt.UserRole - 1)
def remove_tab(tab_widget, tab_name):
""" Look in @tab_widget for a tab with @tab_name and remove it """
for x in range(0, tab_widget.count()):
if tab_widget.widget(x).objectName() == tab_name:
tab_widget.removeTab(x)
break
def enable_tab_by_tab_name(tab_widget, tab_name, enable):
""" Look in @tab_widget for a tab with @tab_name and remove it """
for x in range(0, tab_widget.count()):
if tab_widget.widget(x).objectName() == tab_name:
tab_widget.setTabEnabled(x, enable)
break
def double_validator(widget, min_=-9999999, max_=9999999, decimals=2, notation=QDoubleValidator().StandardNotation,
locale=None):
"""
Create and apply a validator for doubles to ensure the number is within a maximum and minimum values
:param widget: Widget to apply the validator
:param min_: Minimum value (int)
:param max_: Maximum value (int)
:param decimals: Number of decimals (int)
:param notation: StandardNotation or ScientificNotation
:param locale: Locale to define decimal separator and more (QLocale)
"""
validator = QDoubleValidator(min_, max_, decimals)
validator.setNotation(notation)
if locale is None:
locale = QLocale("en_US")
validator.setLocale(locale)
widget.setValidator(validator)
def enable_dialog(dialog, enable, ignore_widgets=['', None]):
widget_list = dialog.findChildren(QWidget)
for widget in widget_list:
if str(widget.objectName()) not in ignore_widgets:
if type(widget) in (QSpinBox, QDoubleSpinBox, QLineEdit):
widget.setReadOnly(not enable)
if enable:
widget.setStyleSheet(None)
else:
widget.setStyleSheet("QWidget { background: rgb(242, 242, 242);"
" color: rgb(100, 100, 100)}")
elif type(widget) in (QComboBox, QCheckBox, QPushButton, QgsDateTimeEdit, QTableView):
widget.setEnabled(enable)
def set_tableview_config(widget, selection=QAbstractItemView.SelectRows, edit_triggers=QTableView.NoEditTriggers,
sectionResizeMode=3, stretchLastSection=True, sortingEnabled=True,
selectionMode=QAbstractItemView.ExtendedSelection):
""" Set QTableView configurations """
widget.setSelectionBehavior(selection)
widget.setSelectionMode(selectionMode)
widget.horizontalHeader().setSectionResizeMode(sectionResizeMode)
widget.horizontalHeader().setStretchLastSection(stretchLastSection)
widget.horizontalHeader().setMinimumSectionSize(100)
widget.setEditTriggers(edit_triggers)
widget.setSortingEnabled(sortingEnabled)
def get_col_index_by_col_name(qtable, column_name):
""" Return column index searching by column name """
model = qtable.model()
columns_dict = qtable.property('columns')
if not columns_dict:
columns_dict = {model.headerData(i, Qt.Horizontal): model.headerData(i, Qt.Horizontal) for i in range(model.columnCount())}
qtable.setProperty('columns', columns_dict)
column_index = -1
try:
record = model.record(0)
column_index = record.indexOf(column_name)
except AttributeError:
for x in range(0, model.columnCount()):
if columns_dict.get(model.headerData(x, Qt.Horizontal)) == column_name:
column_index = x
break
if column_index == -1:
column_index = None
return column_index
def get_tab_index_by_tab_name(qtabwidget: QTabWidget, tab_name: str) -> Optional[int]:
""" Return tab index searching by tab name """
tab_index = -1
try:
for idx in range(qtabwidget.count()):
if qtabwidget.widget(idx).objectName() == tab_name:
tab_index = idx
break
except Exception as e:
tools_log.log_error("Tab not found.", parameter=tab_name)
if tab_index == -1:
tab_index = None
return tab_index
def onCellChanged(table, row, column):
""" Function to be connected to a QTableWidget cellChanged signal.
Note: row & column parameters are passed by the signal """
# Add a new row if the edited row is the last one
if row >= (table.rowCount()-1):
headers = [n for n in range(0, table.rowCount()+1)]
table.insertRow(table.rowCount())
table.setVerticalHeaderLabels(headers)
# Remove "last" row (empty one) if the real last row is empty
elif row == (table.rowCount()-2):
for n in range(0, table.columnCount()):
item = table.item(row, n)
if item is not None:
if item.data(0) not in (None, ''):
return
table.setRowCount(table.rowCount()-1)
def set_completer_object(completer, model, widget, list_items, max_visible=10):
""" Set autocomplete of widget @table_object + "_id"
getting id's from selected @table_object.
WARNING: Each QLineEdit needs their own QCompleter and their own QStringListModel!!!
"""
# Set completer and model: add autocomplete in the widget
completer.setCaseSensitivity(Qt.CaseInsensitive)
completer.setMaxVisibleItems(max_visible)
widget.setCompleter(completer)
completer.setCompletionMode(1)
model.setStringList(list_items)
completer.setModel(model)
# Connect the textChanged signal of the widget to a custom slot that hides the popup
widget.textChanged.connect(partial(on_text_changed, completer))
def on_text_changed(completer):
completer.popup().hide()
def set_action_checked(action, enabled, dialog=None):
if type(action) is str and dialog is not None:
action = dialog.findChild(QAction, action)
try:
action.setChecked(enabled)
except RuntimeError:
pass
def set_calendar_empty(widget):
""" Set calendar empty when click inner button of QgsDateTimeEdit because aesthetically it looks better"""
widget.displayNull(False) # False is for 'updateCalendar' parameter. If True it sets a default date instead of NULL
def add_horizontal_spacer():
widget = QSpacerItem(10, 10, QSizePolicy.Expanding, QSizePolicy.Minimum)
return widget
def add_verticalspacer():
widget = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
return widget
def check_expression_filter(expr_filter, log_info=False):
""" Check if expression filter @expr is valid """
if log_info:
tools_log.log_info(expr_filter)
expr = QgsExpression(expr_filter)
if expr.hasParserError():
message = "Expression Error"
tools_log.log_warning(message, parameter=expr_filter)
return False, expr
return True, expr
def fill_table(qtable, table_name, expr_filter=None, edit_strategy=QSqlTableModel.OnManualSubmit,
sort_order=Qt.AscendingOrder):
""" Set a model with selected filter. Attach that model to selected table
:param qtable: tableview where set the model (QTableView)
:param table_name: database table name or view name (String)
:param expr_filter: expression to filter the model (String)
:param edit_strategy: (QSqlTableModel.OnFieldChange, QSqlTableModel.OnManualSubmit, QSqlTableModel.OnRowChange)
:param sort_order: can be 0 or 1 (Qt.AscendingOrder or Qt.AscendingOrder)
:return:
"""
if lib_vars.schema_name and lib_vars.schema_name not in table_name:
table_name = f"{lib_vars.schema_name}.{table_name}"
# Set model
model = QSqlTableModel(db=lib_vars.qgis_db_credentials)
model.setTable(table_name)
model.setEditStrategy(edit_strategy)
model.setSort(0, sort_order)
if expr_filter is not None:
model.setFilter(expr_filter)
model.select()
# Check for errors
if model.lastError().isValid():
if 'Unable to find table' in model.lastError().text():
tools_db.reset_qsqldatabase_connection()
else:
tools_qgis.show_warning(f"fill_table: {model.lastError().text()}")
# Attach model to tableview
qtable.setModel(model)
def set_lazy_init(widget, lazy_widget=None, lazy_init_function=None):
"""Apply the init function related to the model. It's necessary
a lazy init because model is changed everytime is loaded."""
if lazy_widget is None:
return
if widget != lazy_widget:
return
lazy_init_function(lazy_widget)
def filter_by_id(dialog, widget_table, widget_txt, table_object, field_object_id="id"):
object_id = get_text(dialog, widget_txt)
if object_id != 'null':
expr = f"{field_object_id}::text ILIKE '%{object_id}%'"
# Refresh model with selected filter
widget_table.model().setFilter(expr)
widget_table.model().select()
else:
fill_table(widget_table, lib_vars.schema_name + "." + table_object)
def set_selection_behavior(dialog):
# Get objects of type: QTableView
widget_list = dialog.findChildren(QTableView)
for widget in widget_list:
widget.setSelectionBehavior(QAbstractItemView.SelectRows)
widget.horizontalHeader().setSectionResizeMode(3)
widget.horizontalHeader().setStretchLastSection(True)
def get_folder_path(dialog, widget):
""" Get folder path """
# Check if selected folder exists. Set default value if necessary
folder_path = get_text(dialog, widget)
if folder_path is None or folder_path == 'null' or not os.path.exists(folder_path):
folder_path = os.path.expanduser("~")
# Open dialog to select folder
os.chdir(folder_path)
file_dialog = QFileDialog()
file_dialog.setFileMode(QFileDialog.Directory)
message = "Select folder"
folder_path = file_dialog.getExistingDirectory(
parent=None, caption=tr(message), directory=folder_path)
if folder_path:
set_widget_text(dialog, widget, str(folder_path))
def hide_void_groupbox(dialog):
""" Rceives a dialog, searches it all the QGroupBox, looks 1 to 1 if the grb have widgets, if it does not have
(if it is empty), hides the QGroupBox
:param dialog: QDialog or QMainWindow
:return: Dictionario with names of hidden QGroupBox
"""
grb_list = {}
grbox_list = dialog.findChildren(QGroupBox)
for grbox in grbox_list:
widget_list = grbox.findChildren(QWidget)
if len(widget_list) == 0:
grb_list[grbox.objectName()] = 0
grbox.setVisible(False)
return grb_list
def set_completer_lineedit(qlineedit, list_items):
""" Set a completer into a QLineEdit
:param qlineedit: Object where to set the completer (QLineEdit)
:param list_items: List of items to set into the completer (List)["item1","item2","..."]
"""
completer = QCompleter()
completer.setCaseSensitivity(Qt.CaseInsensitive)
completer.setMaxVisibleItems(10)
completer.setCompletionMode(0)
completer.setFilterMode(Qt.MatchContains)
completer.popup().setStyleSheet("color: black;")
qlineedit.setCompleter(completer)
model = QStringListModel()
model.setStringList(list_items)
completer.setModel(model)
def set_completer_rows(widget, rows, filter_mode: QtMatchFlag = 'starts'):
""" Set a completer into a widget
:param widget: Object where to set the completer (QLineEdit)
:param rows: rows to set into the completer (List)["item1","item2","..."]
"""
list_values = []
if rows is not None:
for row in rows:
list_values.append(str(row[0]))
# Set completer and model: add autocomplete in the widget
completer = QCompleter()
completer.setCaseSensitivity(Qt.CaseInsensitive)
completer.setFilterMode(match_flags.get(filter_mode))
widget.setCompleter(completer)
model = QStringListModel()
model.setStringList(list_values)
completer.setModel(model)
def add_combo_on_tableview(qtable, rows, field, widget_pos, combo_values):
""" Set one column of a QtableView as QComboBox with values from database.
:param qtable: QTableView to fill
:param rows: List of items to set QComboBox (["..", "..."])
:param field: Field to set QComboBox (String)
:param widget_pos: Position of the column where we want to put the QComboBox (integer)
:param combo_values: List of items to populate QComboBox (["..", "..."])
:return:
"""
for x in range(0, len(rows)):
combo = QComboBox()
row = rows[x]
# Populate QComboBox
fill_combo_values(combo, combo_values)
# Set QCombobox to wanted item
set_combo_value(combo, str(row[field]), 1)
# Get index and put QComboBox into QTableView at index position
idx = qtable.model().index(x, widget_pos)
qtable.setIndexWidget(idx, combo)
# noinspection PyUnresolvedReferences
combo.currentIndexChanged.connect(partial(set_status, combo, qtable, x, widget_pos))