-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui_helpers.py
154 lines (130 loc) · 4.35 KB
/
ui_helpers.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
from aqt.qt import *
class OverwriteValues:
"""
Possible values for the overwrite config.
"""
OVERWRITE = "Overwrite"
SKIP = "Skip"
APPEND = "Append"
class ConfigKeys:
"""
See config.json
"""
SOURCE_FIELD = "sourceField"
DELIMITER = "delimiter"
LABEL = "label"
QUERY_CONFIGS = "queryConfigs"
SEARCH_TERM = "searchTerm"
TARGET_FIELD = "targetField"
RESULT_COUNT = "resultCount"
WIDTH = "width"
HEIGHT = "height"
OVERWRITE = "overwrite"
# The order in which the keys appear as columns in the query config form.
#
# This is an extremely unideal way to do this (hacky) but basing on the legacy
# code: to extract the values from the form later, we need to know what order
# the columns appear.
# TODO: Add some assertions to check this.
QUERY_CONFIG_KEY_ORDER = (
ConfigKeys.LABEL,
ConfigKeys.SEARCH_TERM,
ConfigKeys.TARGET_FIELD,
ConfigKeys.RESULT_COUNT,
ConfigKeys.OVERWRITE,
ConfigKeys.WIDTH,
ConfigKeys.HEIGHT)
class ConfigDefaults:
RESULT_COUNT = 1
WIDTH = -1
HEIGHT = -1
OVERWRITE = "Skip"
IGNORED = "<ignored>"
# The placeholder value in the search term the user provides.
WORD_PLACEHOLDER = "{}"
COLUMN_LABELS = [
"Label",
"Search Query",
"Target Field",
"Result Count",
"If not empty?",
"",
""]
def make_target_field_select(options, config_value) -> QComboBox:
"""
Makes a select (combo box) that defaults to selecting the provided value
"""
comboBox = QComboBox()
comboBox.setObjectName(ConfigKeys.TARGET_FIELD)
comboBox.addItem(ConfigDefaults.IGNORED)
comboBox.addItems(options)
if config_value in options:
# + 1 because of the "<ignore>" option that we added earlier
comboBox.setCurrentIndex(options.index(config_value) + 1)
return comboBox
def make_result_count_box(config_value) -> QSpinBox:
spinBox = QSpinBox()
spinBox.setMinimum(1)
spinBox.setValue(config_value)
spinBox.setStyleSheet("""
QSpinBox {
width: 24;
}""")
return spinBox
def make_overwrite_select(config_value) -> QComboBox:
select = QComboBox()
select.setObjectName(ConfigKeys.OVERWRITE)
select.addItem(OverwriteValues.SKIP)
select.addItem(OverwriteValues.OVERWRITE)
select.addItem(OverwriteValues.APPEND)
# TODO: Need to match the values correctly here.
select.setCurrentIndex(select.findText(config_value))
return select
def make_dimension_spin_box(config_value, label) -> QHBoxLayout:
"""
Used to make either a width/height spin box with the given label, populated
with the provided value.
"""
hbox = QHBoxLayout()
hbox.addWidget(QLabel("%s:" % label))
spinBox = QSpinBox()
spinBox.setMinimum(-1)
spinBox.setMaximum(9999)
spinBox.setValue(config_value)
spinBox.setAlignment(Qt.AlignmentFlag.AlignRight |
Qt.AlignmentFlag.AlignVCenter)
hbox.addWidget(spinBox)
return hbox
def serialize_config_from_ui(form):
"""
Reads/scrapes the form to get the values of the form as a config so that it
can be saved to disk and persist across uses.
See config.json for the default config.
"""
config = {}
source_field = form.sourceField.currentText()
config[ConfigKeys.SOURCE_FIELD] = source_field
query_configs = []
num_ui_columns = form.gridLayout.columnCount()
assert (num_ui_columns == len(QUERY_CONFIG_KEY_ORDER))
for i in range(1, form.gridLayout.rowCount()):
q = {}
# Well, in general this approach is highly coupled with the UI, but I'm
# too lazy to change it for now, so I'm copying this guy's code.
for j in range(form.gridLayout.columnCount()):
key = QUERY_CONFIG_KEY_ORDER[j]
item = form.gridLayout.itemAtPosition(i, j)
if isinstance(item, QWidgetItem):
item = item.widget()
elif isinstance(item, QLayoutItem):
item = item.itemAt(1).widget()
if isinstance(item, QComboBox):
# Overwrite and target field boxes
q[key] = item.currentText()
elif isinstance(item, QSpinBox):
q[key] = item.value()
else:
q[key] = item.text()
query_configs.append(q)
config[ConfigKeys.QUERY_CONFIGS] = query_configs
return config