-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_picker.py
246 lines (199 loc) · 7.42 KB
/
text_picker.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
""" This module contains a script to quickly screen text data
for outliers.
"""
from PyQt4 import QtGui, QtCore
import pickle
import sys
class TextModel(QtGui.QStringListModel):
def __init__(self, strings, *args, **kwargs):
super(TextModel, self).__init__(strings, *args, **kwargs)
self.is_good = [True for _ in strings]
def data(self, index, role):
if role == QtCore.Qt.BackgroundColorRole:
if not self.is_good[index.row()]:
return QtGui.QColor(QtCore.Qt.red)
return super(TextModel, self).data(index, role)
class TextPickerWidget(QtGui.QWidget):
""" This widget takes a dictionary of text bodies
and displays them one by one.
"""
def __init__(self, data, *args, **kwargs):
super(TextPickerWidget, self).__init__(*args, **kwargs)
self.data = {str(key): value for key, value in data.items()}
self.has_saved = False
self.list_view = None
self.list_model = None
self.text_window = None
self.save_button = None
self.close_button = None
self.current_selection = 0
self.keys = list(self.data.keys())
self.init_ui()
self.set_selected(0)
def init_ui(self):
self.setWindowTitle('Filter texts.')
layout = QtGui.QHBoxLayout()
self.list_view = QtGui.QListView(self)
self.list_model = TextModel(self.keys)
self.list_view.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
self.list_view.setModel(self.list_model)
self.list_view.selectionModel().selectionChanged.connect(
self.list_changed
)
self.text_window = QtGui.QTextEdit(self)
self.text_window.setReadOnly(True)
layout.addWidget(self.list_view)
layout.addWidget(self.text_window)
self.setFocusPolicy(QtCore.Qt.NoFocus)
self.save_button = QtGui.QPushButton('Save')
self.save_button.clicked.connect(self.save)
self.close_button = QtGui.QPushButton('Close')
self.close_button.clicked.connect(self.close)
self.base_string = '{{}} / {}'.format(len(self.data))
self.label = QtGui.QLabel()
sub_layout = QtGui.QVBoxLayout()
sub_layout.addWidget(self.label)
sub_layout.addStretch()
sub_layout.addWidget(self.save_button)
sub_layout.addWidget(self.close_button)
layout.addLayout(sub_layout)
self.setLayout(layout)
def close(self):
if not self.has_saved:
question = ('You have not saved.\n\nWould you '
'like to save before closing?')
answer = QtGui.QMessageBox().question(
self, 'Save?', question,
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No |
QtGui.QMessageBox.Abort)
if answer == QtGui.QMessageBox.Yes:
did_save = self.save()
if not did_save:
return
elif answer == QtGui.QMessageBox.Abort:
return
super(TextPickerWidget, self).close()
def save(self):
""" Open dialog and save filtered data to pickle-file.
Returns
-------
bool
True if progress was saved else False.
"""
to_save = dict()
for i, key in enumerate(self.keys):
if self.list_model.is_good[i]:
to_save[key] = self.data[key]
dialog = QtGui.QFileDialog()
fname = dialog.getSaveFileName(self, 'Save file',
filter='*.pickle')
if fname:
with open(fname, 'wb') as f:
pickle.dump(to_save, f)
self.has_saved = True
return True
return False
def list_changed(self, selection):
if selection.indexes():
i = selection.indexes()[0].row()
self.change_display(i)
def change_display(self, i):
self.text_window.clear()
self.current_selection = i
self.text_window.setText(self.data[self.keys[i]])
self.label.setText(self.base_string.format(i + 1))
def set_selected(self, i):
"""
Parameters
----------
i : int
Selected row.
"""
if not (0 <= i < self.list_model.rowCount()):
return
qindex = self.list_model.createIndex(i, 0)
sel_model = self.list_view.selectionModel()
sel_model.clearSelection()
sel_model.setCurrentIndex(qindex, QtGui.QItemSelectionModel.Select)
self.change_display(i)
def keyPressEvent(self, event):
""" On right-key press go to next.
Parameters
----------
event : QtGui.QKeyEvent
Triggered event.
"""
if event.key() == QtCore.Qt.Key_Right:
self.go_to_next()
elif event.key() == QtCore.Qt.Key_Left:
self.go_back()
elif event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.toggle_bad()
def go_to_next(self):
self.set_selected(self.current_selection + 1)
def go_back(self):
self.set_selected(self.current_selection - 1)
def toggle_bad(self):
self.has_saved = False
is_good = self.list_model.is_good[self.current_selection]
self.list_model.is_good[self.current_selection] = not is_good
self.list_view.update()
class PickKeyDialog(QtGui.QDialog):
def __init__(self, keys):
super(PickKeyDialog, self).__init__()
self.keys = keys
self.buttons = list()
self.button_group = None
self.init_ui()
def init_ui(self):
self.setWindowTitle('Pick data to filter.')
layout = QtGui.QVBoxLayout()
for key in self.keys:
button = QtGui.QRadioButton(key)
button.toggled.connect(self.enable_ok)
self.buttons.append(button)
layout.addWidget(button)
self.button_group = QtGui.QDialogButtonBox(
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal
)
self.button_group.button(QtGui.QDialogButtonBox.Ok).clicked.connect(
self.accept
)
self.button_group.button(QtGui.QDialogButtonBox.Cancel).clicked.connect(
self.reject
)
self.button_group.button(QtGui.QDialogButtonBox.Ok).setEnabled(False)
layout.addWidget(self.button_group)
self.setLayout(layout)
def get_values(self):
checked_button = next(b for b in self.buttons if b.isChecked())
return checked_button.text()
def enable_ok(self, toggled):
self.button_group.button(QtGui.QDialogButtonBox.Ok).setEnabled(True)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog = QtGui.QFileDialog()
fname = dialog.getOpenFileName(filter='*.pickle')
if fname:
with open(fname, 'rb') as f:
data = pickle.load(f)
else:
sys.exit()
try:
pick_dialog = PickKeyDialog(data.keys())
except AttributeError:
QtGui.QMessageBox().critical(None, 'Error', 'Wrong format.')
sys.exit(1)
if pick_dialog.exec_():
key = pick_dialog.get_values()
else:
sys.exit()
try:
window = TextPickerWidget(data[key])
except AttributeError:
msg = 'Wrong format. Data must be key, value-pairs.'
QtGui.QMessageBox().critical(None, 'Error', msg)
sys.exit(1)
window.show()
sys.exit(app.exec_())