-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_search.py
More file actions
406 lines (333 loc) · 14.5 KB
/
gui_search.py
File metadata and controls
406 lines (333 loc) · 14.5 KB
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
import sys
from PyQt5.QtWidgets import QApplication, QFrame, QComboBox, QWidget, QVBoxLayout, QPushButton, QDialog, QLabel, QLineEdit, QAbstractItemView, QHBoxLayout, QListView, QMessageBox, QStyledItemDelegate
from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex, QSize, pyqtSignal
from PyQt5.QtGui import QColor, QPainter, QFont
class FilterStruct():
def __init__(self,
column="",
min_val="",
max_val="",
str_val="",
datastr=None,
qmodelindex=None):
self.column = column
self.min_val = min_val
self.max_val = max_val
self.str_val = str_val
condition = ''
if qmodelindex:
condition = qmodelindex.data()
elif datastr:
condition = datastr
if condition:
if '∈' in condition:
self.column = condition.split()[2]
self.str_val = condition.split()[0]
else:
self.column = condition.split()[2]
self.min_val = condition.split()[0]
self.max_val = condition.split()[4]
if self.min_val:
self.is_minmax_mode = True
elif self.str_val:
self.is_minmax_mode = False
else:
assert False, "Erorr"
def to_string(self):
if self.min_val:
return f"{self.min_val} < {self.column} < {self.max_val}"
else:
return f"{self.str_val} ∈ {self.column}"
class ConditionDialog(QDialog):
def __init__(self, parent, column_list, original_qmodelindex=None):
super().__init__()
self.is_minmax_mode = True
self._parent = parent
self.original_qmodelindex = original_qmodelindex
self.setWindowTitle("조건 추가")
layout = QVBoxLayout()
self.column_label = QLabel("열(라벨):")
self.column_combobox = QComboBox()
self.column_combobox.addItems(column_list)
self.mode_label = QLabel("비교 모드:")
self.mode_combobox = QComboBox()
self.mode_combobox.addItems(['크기 비교', '문자 포함'])
self.mode_combobox.currentIndexChanged.connect(
self.on_moderadio_changed)
self.frame_minmax = QFrame()
layout_minmax = QVBoxLayout()
self.frame_minmax.setLayout(layout_minmax)
self.min_label = QLabel("이상:")
self.min_input = QLineEdit()
self.max_label = QLabel("이하:")
self.max_input = QLineEdit()
layout_minmax.addWidget(self.min_label)
layout_minmax.addWidget(self.min_input)
layout_minmax.addWidget(self.max_label)
layout_minmax.addWidget(self.max_input)
self.frame_str = QFrame()
layout_str = QVBoxLayout()
self.frame_str.setLayout(layout_str)
self.str_label = QLabel("다음 텍스트를 포함:")
self.str_input = QLineEdit()
layout_str.addWidget(self.str_label)
layout_str.addWidget(self.str_input)
self.frame_str.setVisible(False)
self.confirm_button = QPushButton("확인")
self.confirm_button.clicked.connect(self.confirm_condition)
empty_frame = QFrame()
empty_frame.setStyleSheet("QFrame { border: none; }")
layout.addWidget(self.column_label)
layout.addWidget(self.column_combobox)
layout.addWidget(self.mode_label)
layout.addWidget(self.mode_combobox)
layout.addWidget(self.frame_minmax)
layout.addWidget(self.frame_str)
layout.addWidget(empty_frame, stretch=999)
layout.addWidget(self.confirm_button)
if original_qmodelindex:
self.remove_button = QPushButton("제거")
self.remove_button.clicked.connect(self.remove_condition)
layout.addWidget(self.remove_button)
fs = FilterStruct(qmodelindex=original_qmodelindex)
column = fs.column
if not (column in column_list):
column_list.append(column)
self.column_combobox.setCurrentText(column)
self.change_searchmode(fs.is_minmax_mode)
self.mode_combobox.setCurrentIndex(0 if fs.is_minmax_mode else 1)
if fs.is_minmax_mode:
self.min_input.setText(fs.min_val or "")
self.max_input.setText(fs.max_val or "")
else:
self.str_input.setText(fs.str_val or "")
self.setLayout(layout)
def on_moderadio_changed(self, v):
self.change_searchmode((v == 0))
def change_searchmode(self, is_minmax_mode):
self.is_minmax_mode = is_minmax_mode
self.frame_minmax.setVisible(is_minmax_mode)
self.frame_str.setVisible(not is_minmax_mode)
def confirm_condition(self):
column = self.column_combobox.currentText()
if self.is_minmax_mode:
min_val = self.min_input.text().strip()
max_val = self.max_input.text().strip()
if self._check_condition_value_minmax(column, min_val, max_val):
item_text = FilterStruct(
column=column, min_val=min_val, max_val=max_val).to_string()
self._parent.add_condition(
item_text, self.original_qmodelindex)
self.close()
else:
str_val = self.str_input.text().strip()
if self._check_condition_value_str(column, str_val):
item_text = FilterStruct(
column=column, str_val=str_val).to_string()
self._parent.add_condition(
item_text, self.original_qmodelindex)
self.close()
def remove_condition(self):
self._parent.remove_condition(self.original_qmodelindex)
self.close()
def _check_condition_value_str(self, column, str_val):
if not column:
QMessageBox.warning(self, "경고", "열(라벨)의 이름을 입력해주세요.")
return False
if not str_val:
QMessageBox.warning(self, "경고", "검색할 값을 넣어주세요.")
return False
if len(str_val) < 1 or len(str_val) > 120:
QMessageBox.warning(
self, "경고", "검색할 문자는 1자 이상, 120자 이하여야합니다.")
return False
return True
def _check_condition_value_minmax(self, column, min_val, max_val):
if not column:
QMessageBox.warning(self, "경고", "열(라벨)의 이름을 입력해주세요.")
return False
if not min_val or not max_val:
QMessageBox.warning(
self, "경고", "이상/이하의 숫자를 입력해 주세요.")
return False
try:
min_val = float(min_val)
max_val = float(max_val)
except ValueError:
QMessageBox.warning(
self, "경고", "이상/이하 칸에 올바른 숫자를 입력해주세요.")
return False
if len(column) < 1 or len(column) > 120:
QMessageBox.warning(
self, "경고", "열(라벨)의 이름은 1자 이상, 120자 이하여야합니다.")
return False
if min_val > max_val:
QMessageBox.warning(
self, "경고", "이상 칸의 숫자가 이하 칸의 숫자보다 큽니다.")
return False
return True
class CircularListModel(QAbstractListModel):
def __init__(self, data=[], parent=None):
super().__init__(parent)
self._data = data
def rowCount(self, parent=QModelIndex()):
return len(self._data)
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self._data[index.row()]
def setData(self, index, value, role=Qt.EditRole):
if role == Qt.EditRole:
self.beginRemoveRows(QModelIndex(), index.row(), index.row())
self._data[index.row()] = value
self.dataChanged.emit(index, index)
self.endRemoveRows()
return True
return False
def editItem(self, index, newValue):
if index.isValid() and 0 <= index.row() < len(self._data):
self.setData(index, newValue)
def addItem(self, item):
self.beginInsertRows(QModelIndex(), len(self._data), len(self._data))
self._data.append(item)
self.endInsertRows()
def removeRow(self, index):
if not index.isValid() or index.row() >= len(self._data):
return False
self.beginRemoveRows(QModelIndex(), index.row(), index.row())
del self._data[index.row()]
self.endRemoveRows()
return True
def removeLastItem(self):
if self.rowCount() > 0:
self.beginRemoveRows(self.createIndex(
0, 0), self.rowCount() - 1, self.rowCount() - 1)
del self._data[-1]
self.endRemoveRows()
def clearAll(self):
self.beginResetModel()
self._data.clear()
self.endResetModel()
class CustomDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
painter.save()
# Draw outline for each item
pen = painter.pen()
pen.setColor(QColor(0, 150, 136)) # Outline color
pen.setWidth(2) # Outline width
painter.setPen(pen)
painter.drawRoundedRect(option.rect, 5, 5)
# Call the default paint implementation
super().paint(painter, option, index)
painter.restore()
class PlaceholderTableView(QListView):
def __init__(self):
super().__init__()
self.placeholder_text = ""
def set_info_text(self, text):
self.placeholder_text = text
def paintEvent(self, event):
super().paintEvent(event)
if self.model() is not None and self.model().rowCount() > 0:
return
painter = QPainter(self.viewport())
painter.save()
col = QColor("#009688") # self.palette().placeholderText().color()
painter.setPen(col)
font = QFont()
font.setBold(True)
# font.setPointSize(14)
painter.setFont(font)
fm = self.fontMetrics()
elided_text = fm.elidedText(
self.placeholder_text, Qt.ElideRight, self.viewport().width()
)
painter.drawText(self.viewport().rect(),
Qt.AlignCenter, elided_text)
painter.restore()
class SearchWidget(QWidget):
on_condition_changed = pyqtSignal(list) # TODO hardcoded
def __init__(self):
super().__init__()
self.layout = QHBoxLayout()
self.add_condition_button = QPushButton("추가")
self.add_condition_button.setEnabled(False)
self.add_condition_button.clicked.connect(self.show_condition_dialog)
self.layout.addWidget(self.add_condition_button)
self.internal_model = CircularListModel() # 초기 아이템 설정
list_view = PlaceholderTableView()
list_view.setModel(self.internal_model)
list_view.setItemDelegate(CustomDelegate())
list_view.setIconSize(QSize(100, 100)) # 아이콘 크기 조절
# list_view.setViewMode(QListView.IconMode)
list_view.setMovement(QListView.Static) # 아이템 이동 금지
list_view.setResizeMode(QListView.Adjust) # 크기 자동 조절
list_view.setFixedHeight(30)
list_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
list_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
list_view.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
list_view.horizontalScrollBar().setSingleStep(10)
list_view.setSizeAdjustPolicy(QListView.AdjustToContents)
list_view.setSpacing(4)
list_view.setFlow(QListView.Flow.LeftToRight)
list_view.doubleClicked.connect(self.show_edit_dialog)
self.list_view = list_view
self.layout.addWidget(list_view)
self.enter_condition_button = QPushButton("반영")
self.enter_condition_button.setEnabled(False)
self.enter_condition_button.clicked.connect(
self.on_click_entercondition)
self.layout.addWidget(self.enter_condition_button)
self.setLayout(self.layout)
def show_condition_dialog(self):
dialog = ConditionDialog(self, self.columns)
dialog.exec_()
def show_edit_dialog(self, item):
dialog = ConditionDialog(self, self.columns, item)
dialog.exec_()
def initialize(self, columns):
self.internal_model.clearAll()
self.set_columns(columns)
self.add_condition_button.setEnabled(True)
self.enter_condition_button.setEnabled(True)
self.enter_condition_button.setStyleSheet(
"QPushButton{color: #009688;}")
def set_columns(self, columns):
self.columns = columns
# original_qmodelindex가 존재하면 edit함.
def add_condition(self, condition, original_qmodelindex=None):
if not original_qmodelindex:
self.internal_model.addItem(condition)
else:
self.internal_model.editItem(original_qmodelindex, condition)
self.enter_condition_button.setStyleSheet("QPushButton{color: red;}")
def remove_condition(self, target_index):
self.internal_model.removeRow(target_index)
def set_info_text(self, text):
self.list_view.set_info_text(text)
# on_click_entercondition으로부터 시작해, gui에서 실행된다.
# 반영 버튼을 눌렀을때 gui가 dm에게 전달해 condition을 변경하려고 시도한다.
# 시도가 실패한 경우, dm이 원래 가지고 있던 조건들을 original_cond로 돌려준다.
# 그럼 on_edit_failed는 지금까지 설정되어있던 조건들을 다 날리고
# dm에 들어가 있던 조건을 새로 설정한다.
def on_edit_failed(self, original_cond):
self.internal_model.clearAll()
for cond in original_cond:
self.add_condition(cond)
def on_click_entercondition(self):
self.on_condition_changed.emit(
self.internal_model._data.copy())
self.enter_condition_button.setStyleSheet(
"QPushButton{color: #009688;}")
if __name__ == '__main__':
app = QApplication(sys.argv)
search_widget = SearchWidget()
search_widget.initialize(
['Columcsacsaasdasdasdsadscn3', 'Cn2', 'Columcsacsacn3'])
# search_widget.add_condition("1 < col < 2")
# search_widget.add_condition("1 < col < 2")
# search_widget.add_condition("1 < col < 2")
# search_widget.add_condition("1 < col < 2")
search_widget.resize(800, 50)
search_widget.show()
search_widget.show_condition_dialog()
sys.exit(app.exec_())