This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadvanced_search.py
424 lines (306 loc) · 15.6 KB
/
advanced_search.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
#!/usr/bin/python
# coding: utf-8
import sys
import os
from PyQt5 import QtGui, QtCore, QtWidgets
from log import MyLog
from line_icon import ButtonLineIcon
import functions
class AdvancedSearch(QtWidgets.QDialog):
"""Class to perform advanced searches"""
def __init__(self, parent=None):
super(AdvancedSearch, self).__init__(parent)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.parent = parent
self.resource_dir, DATA_PATH = functions.getRightDirs()
# Condition to use a specific logger if
# module started in standalone
if parent is None:
self.logger = MyLog("activity.log")
self.test = True
else:
self.logger = self.parent.l
self.test = False
self.options = QtCore.QSettings(DATA_PATH + "/config/searches.ini",
QtCore.QSettings.IniFormat)
# List to store the lineEdit, with the value of
# the search fields
self.fields_list = []
self.initUI()
self.defineSlots()
self.restoreSettings()
def restoreSettings(self):
"""Restore the right number of tabs"""
for query in self.options.childGroups():
# Don't create a search tab for the to read list
if query == "ToRead":
continue
self.tabs.addTab(self.createForm(), query)
# Try to restore the geometry of the AdvancedSearch window
try:
self.restoreGeometry(self.options.value("window_geometry"))
except TypeError:
self.logger.debug("Can't restore window geometry for AdvancedSearch")
def closeEvent(self, event):
"""Actions to perform when closing the window.
Mainly saves the window geometry"""
self.logger.debug("Saving windows state for AdvancedSearch")
self.options.setValue("window_geometry", self.saveGeometry())
super(AdvancedSearch, self).closeEvent(event)
def defineSlots(self):
"""Establish the slots"""
self.button_search_and_save.clicked.connect(self.saveSearch)
self.tabs.currentChanged.connect(self.tabChanged)
self.button_delete_search.clicked.connect(self.deleteSearch)
self.destroyed.connect(self.closeEvent)
def deleteSearch(self):
"""Slot to delete a query"""
# Get the title of the search, get the group with
# the same name in searches.ini, and clear the group
tab_title = self.tabs.tabText(self.tabs.currentIndex())
self.options.beginGroup(tab_title)
# Re-initialize the keys
self.options.remove("")
self.options.endGroup()
self.tabs.removeTab(self.tabs.currentIndex())
if not self.test:
for index in range(self.parent.onglets.count()):
if self.parent.onglets.tabText(index) == tab_title:
self.parent.list_tables_in_tabs.remove(self.parent.onglets.widget(index))
self.parent.onglets.removeTab(index)
self.parent.onglets.setCurrentIndex(0)
break
def buildSearch(self):
"""Build the query"""
# Get all the lineEdit from the current tab
lines = self.tabs.currentWidget().findChildren(QtWidgets.QLineEdit)
radios = self.tabs.currentWidget().findChildren(QtWidgets.QRadioButton)
# Clean the fields of tailing comma
topic_entries = [line.text()[:-1] if line.text() and line.text()[-1] == ',' else line.text() for line in lines[0:2]]
author_entries = [line.text()[:-1] if line.text() and line.text()[-1] == ',' else line.text() for line in lines[2:4]]
radio_states = [radio.isChecked() for radio in radios]
base = functions.buildSearch(topic_entries, author_entries,
radio_states)
return base
def tabChanged(self):
"""Method called when tab is changed.
Fill the fields with the good data"""
# Get the current tab number
index = self.tabs.currentIndex()
tab_title = self.tabs.tabText(index)
# Get the lineEdit objects of the current search tab displayed
lines = self.tabs.currentWidget().findChildren(QtWidgets.QLineEdit)
topic_entries = [line for line in lines[0:2]]
author_entries = [line for line in lines[2:4]]
radios = self.tabs.currentWidget().findChildren(QtWidgets.QRadioButton)
if index != 0:
# Change the buttons at the button if the tab is
# a tab dedicated to search edition
self.button_delete_search.show()
topic_entries_options = self.options.value("{0}/topic_entries".format(tab_title), None)
if topic_entries_options is not None:
topic_entries = [line.setText(value) for line, value in zip(topic_entries, topic_entries_options)]
author_entries_options = self.options.value("{0}/author_entries".format(tab_title), None)
if author_entries_options is not None:
author_entries = [line.setText(value) for line, value in zip(author_entries, author_entries_options)]
radio_states = self.options.value("{0}/radio_states".format(tab_title), None)
radio_states = [True if element == 'true' else False for element in radio_states]
if radio_states is not None:
[radio.setChecked(value) for radio, value in zip(radios, radio_states)]
else:
self.button_delete_search.hide()
def saveSearch(self):
"""Slot to save a query"""
lines = self.tabs.currentWidget().findChildren(QtWidgets.QLineEdit)
radios = self.tabs.currentWidget().findChildren(QtWidgets.QRadioButton)
# Get the name of the current tab. Used to determine if the current
# tab is the "new query" tab
tab_title = self.tabs.tabText(self.tabs.currentIndex())
topic_entries = [line.text() for line in lines[0:2]]
author_entries = [line.text() for line in lines[2:4]]
radio_states = [radio.isChecked() for radio in radios]
# Build the query string
base = self.buildSearch()
if not base:
return
# Creating a new search
if tab_title == "New query":
# Get the search name with a dialogBox, if the user pushed the
# save button
name_search = QtWidgets.QInputDialog.getText(self, "Search name",
"Save your search as:")
if "/" in name_search:
name_search = name_search.replace("/", "-")
if not name_search[1] or name_search[0] == "":
return
else:
name_search = name_search[0]
if name_search in self.options.childGroups():
# Display an error message if the search name is already used
QtWidgets.QMessageBox.critical(self, "Saving search", "You already have a search called like this",
QtWidgets.QMessageBox.Ok, defaultButton=QtWidgets.QMessageBox.Ok)
self.logger.debug("This search name is already used")
return
else:
self.tabs.addTab(self.createForm(), name_search)
if not self.test:
self.parent.createSearchTab(name_search, base,
topic_entries,
author_entries,
radio_states)
self.parent.loadNotifications()
# Clear the fields when perform search
for line in lines:
line.clear()
# Modifying and saving an existing search
else:
name_search = tab_title
if not self.test:
self.parent.createSearchTab(name_search, base, topic_entries,
author_entries, radio_states,
update=True)
self.logger.debug("Saving the search")
self.options.beginGroup(name_search)
# Re-initialize the keys
self.options.remove("")
if topic_entries != [''] * 2:
self.options.setValue("topic_entries", topic_entries)
if author_entries != [''] * 2:
self.options.setValue("author_entries", author_entries)
if base:
self.options.setValue("sql_query", base)
self.options.setValue("radio_states", radio_states)
self.options.endGroup()
self.options.sync()
def showInfo(self, field_type):
if field_type == 1:
# Generic message fot the field tooltips
mes = """
Insert comma(s) between keywords. Ex: heparin sulfate, \
heparinase. If 'Any' is checked, will match any keyword. If 'All' \
is checked, will match all the keywords.\nWildcards (*) are \
accepted. Ex: heparin*.\nFilters are case insensitive.
"""
if field_type == 2:
# Generic message fot the field tooltips
mes = """
Insert comma(s) between keywords. Ex: heparin sulfate, \
heparinase.\nWildcards (*) are accepted. Ex: heparin*.\nFilters \
are case insensitive.
"""
elif field_type == 3:
# Generic message fot the authors tooltips
mes = """Insert comma(s) between keywords. Ex: Jean-Patrick \
Francoia, Laurent Vial. If 'Any' is checked, will match any \
author. If 'All' is checked, will match all the authors. \
Wildcards (*) are accepted. Ex: J* Francoia. \nFilters are case \
insensitive.\nFirst name comes before last name. Ex: Linus \
Pauling or L* Pauling.
"""
elif field_type == 4:
# Generic message fot the authors tooltips
mes = """Insert comma(s) between keywords. Ex: Jean-Patrick \
Francoia, Laurent Vial. Wildcards (*) are accepted. \
Ex: J* Francoia. \nFilters are case insensitive.\nFirst name \
comes before last name. Ex: Linus Pauling or L* Pauling.
"""
# Clean the tabs in the message (tabs are 4 spaces)
mes = mes.replace(" ", "")
QtWidgets.QMessageBox.information(self, "Information", mes,
QtWidgets.QMessageBox.Ok)
def createForm(self):
# ------------------------ NEW SEARCH TAB -----------------------------
# Main widget of the tab, with a grid layout
widget_query = QtWidgets.QWidget()
vbox_query = QtWidgets.QVBoxLayout()
widget_query.setLayout(vbox_query)
vbox_query.addStretch(1)
# ------------- TOPIC ----------------------------------
# Create a groupbox for the topic
group_topic = QtWidgets.QGroupBox("Topic")
grid_topic = QtWidgets.QGridLayout()
group_topic.setLayout(grid_topic)
# Add the topic groupbox to the global vbox
vbox_query.addWidget(group_topic)
# Create 3 lines, with their label: AND, OR, NOT
label_topic_include = QtWidgets.QLabel("Include:")
line_topic_include = ButtonLineIcon(os.path.join(self.resource_dir,
'images/info'))
line_topic_include.buttonClicked.connect(lambda: self.showInfo(1))
group_radio_topic = QtWidgets.QButtonGroup()
radio_topic_any = QtWidgets.QRadioButton("Any")
radio_topic_any.setChecked(True)
radio_topic_all = QtWidgets.QRadioButton("All")
group_radio_topic.addButton(radio_topic_any)
group_radio_topic.addButton(radio_topic_all)
label_topic_exclude = QtWidgets.QLabel("Exclude:")
line_topic_exclude = QtWidgets.QLineEdit()
line_topic_exclude = ButtonLineIcon(os.path.join(self.resource_dir,
'images/info'))
line_topic_exclude.buttonClicked.connect(lambda: self.showInfo(2))
# Organize the lines and the lab within the grid
# addWidget (self, QWidget, int row, int column, int rowSpan, int columnSpan, Qt.Alignment alignment = 0)
grid_topic.addWidget(label_topic_include, 0, 0)
grid_topic.addWidget(line_topic_include, 0, 1)
grid_topic.addWidget(radio_topic_any, 0, 2)
grid_topic.addWidget(radio_topic_all, 0, 3)
grid_topic.addWidget(label_topic_exclude, 1, 0)
grid_topic.addWidget(line_topic_exclude, 1, 1)
vbox_query.addStretch(1)
# ------------- AUTHORS ----------------------------------
# Create a groupbox for the authors
group_author = QtWidgets.QGroupBox("Author(s)")
grid_author = QtWidgets.QGridLayout()
group_author.setLayout(grid_author)
# Add the author groupbox to the global vbox
vbox_query.addWidget(group_author)
label_author_include = QtWidgets.QLabel("Include:")
line_author_include = QtWidgets.QLineEdit()
line_author_include = ButtonLineIcon(os.path.join(self.resource_dir,
'images/info'))
line_author_include.buttonClicked.connect(lambda: self.showInfo(3))
group_radio_author = QtWidgets.QButtonGroup()
radio_author_any = QtWidgets.QRadioButton("Any")
radio_author_any.setChecked(True)
radio_author_all = QtWidgets.QRadioButton("All")
group_radio_author.addButton(radio_author_any)
group_radio_author.addButton(radio_author_all)
label_author_not = QtWidgets.QLabel("Exclude:")
line_author_exclude = QtWidgets.QLineEdit()
line_author_exclude = ButtonLineIcon(os.path.join(self.resource_dir,
'images/info'))
line_author_exclude.buttonClicked.connect(lambda: self.showInfo(4))
grid_author.addWidget(label_author_include, 0, 0)
grid_author.addWidget(line_author_include, 0, 1)
grid_author.addWidget(radio_author_any, 0, 2)
grid_author.addWidget(radio_author_all, 0, 3)
grid_author.addWidget(label_author_not, 1, 0)
grid_author.addWidget(line_author_exclude, 1, 1)
vbox_query.addStretch(1)
line_topic_include.returnPressed.connect(self.saveSearch)
line_topic_exclude.returnPressed.connect(self.saveSearch)
line_author_include.returnPressed.connect(self.saveSearch)
line_author_exclude.returnPressed.connect(self.saveSearch)
return widget_query
def initUI(self):
"""Handles the display"""
self.setWindowTitle('Advanced Search')
self.tabs = QtWidgets.QTabWidget()
query = self.createForm()
self.tabs.addTab(query, "New query")
# ----------------- BUTTONS -----------------------------------------
self.button_delete_search = QtWidgets.QPushButton("Delete search", self)
self.button_search_and_save = QtWidgets.QPushButton("Save search", self)
# ------------------------ ASSEMBLING ---------------------------------
# Create a global vbox, and stack the main widget + the search button
self.vbox_global = QtWidgets.QVBoxLayout()
self.vbox_global.addWidget(self.tabs)
self.vbox_global.addWidget(self.button_delete_search)
self.button_delete_search.hide()
self.vbox_global.addWidget(self.button_search_and_save)
self.setLayout(self.vbox_global)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
obj = AdvancedSearch()
sys.exit(app.exec_())