-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingBlockTree.py
233 lines (214 loc) · 10.2 KB
/
SortingBlockTree.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
import traceback
import re
from PyQt5 import QtCore, QtGui, QtWidgets
class LibrarySortingFilter:
def __init__(self, filter_type: str = '*', filter_data: str = None):
self.type = filter_type
self.data = filter_data
def __str__(self):
return self.type+':{}'.format(self.data)
def __repr__(self):
return self.__str__()
class HomeWindowSortingBlockTree:
sorting_block_tree_actual_filter = 'all'
sorting_block_tree_actual_filters = []
def sorting_block_tree_init(self) -> None:
"""
sorting_block_tree_init
:return: None
"""
self.sorting_block_tree.itemClicked.connect(self.sorting_block_tree_item_activated)
self.sorting_block_search_button.clicked.connect(self.sorting_block_tree_toogle_search_zone)
self.sorting_block_search_edit.setMaximumWidth(0)
self.sorting_block_search_edit.returnPressed.connect(self.sorting_block_tree_research)
def sorting_block_tree_load_data(self):
try:
# Clean sub-tree Authors ans Series
while self.sorting_block_tree.topLevelItem(1).childCount() > 0:
self.sorting_block_tree.topLevelItem(1).removeChild(self.sorting_block_tree.topLevelItem(1).child(0))
while self.sorting_block_tree.topLevelItem(2).childCount() > 0:
self.sorting_block_tree.topLevelItem(2).removeChild(self.sorting_block_tree.topLevelItem(2).child(0))
while self.sorting_block_tree.topLevelItem(3).childCount() > 0:
self.sorting_block_tree.topLevelItem(3).removeChild(self.sorting_block_tree.topLevelItem(3).child(0))
authors = self.BDD.get_authors()
series = self.BDD.get_series()
tags = self.BDD.get_tags()
for author in authors:
item = QtWidgets.QTreeWidgetItem(self.sorting_block_tree.topLevelItem(1))
item.setText(0, author)
item.setText(1, 'authors:{}'.format(author))
item.setToolTip(0, author)
for serie in series:
item = QtWidgets.QTreeWidgetItem(self.sorting_block_tree.topLevelItem(2))
item.setText(0, serie)
item.setText(1, 'series:{}'.format(serie))
item.setToolTip(0, serie)
for tag in tags:
item = QtWidgets.QTreeWidgetItem(self.sorting_block_tree.topLevelItem(3))
item.setText(0, tag.capitalize())
item.setText(1, 'tags:{}'.format(tag.capitalize()))
item.setToolTip(0, tag.capitalize())
except Exception:
traceback.print_exc()
def sorting_block_tree_item_activated(self, item, column):
try:
self.sorting_block_tree_set_filter(item.text(1))
except Exception:
traceback.print_exc()
def sorting_block_tree_set_filter(self, filter: str = None) -> None:
try:
if filter is None or filter.strip() == '':
return
filter_tab = [filter]
if filter == 'all' or filter == '*':
self.sorting_block_tree_actual_filters.clear()
self.sorting_block_tree_actual_filters.append(LibrarySortingFilter('*', None))
else:
self.sorting_block_tree_del_filter('*', False)
filter_tab = filter.split(':', 1)
found = False
for index in range(0, len(self.sorting_block_tree_actual_filters)):
if self.sorting_block_tree_actual_filters[index].type == filter_tab[0]:
self.sorting_block_tree_actual_filters[index].data = filter_tab[1]
found = True
if found is False:
try:
self.sorting_block_tree_actual_filters.append(LibrarySortingFilter(filter_tab[0], filter_tab[1]))
except:
traceback.print_exc()
self.sorting_block_tree_parse_filters()
except Exception:
traceback.print_exc()
def sorting_block_tree_del_filter(self, filter: str = None, filter_print: bool = True) -> bool:
if filter is None or filter.strip() == '':
return False
ender = -1
for index in range(0, len(self.sorting_block_tree_actual_filters)):
if self.sorting_block_tree_actual_filters[index].type == filter:
ender = index
break
if ender >= 0:
self.sorting_block_tree_actual_filters.pop(ender)
if filter_print is True:
self.sorting_block_tree_parse_filters()
return True
def sorting_block_tree_del_filter_button(self) -> None:
filter = self.sender().property('filter')
self.sorting_block_tree_del_filter(filter)
def sorting_block_tree_parse_filters(self) -> None:
try:
self.central_block_table.clearSelection()
all = False
if len(self.sorting_block_tree_actual_filters) < 1:
all = True
else:
for filter in self.sorting_block_tree_actual_filters:
if filter.type == '*':
all = True
break
self.clearLayout(self.sorting_block_search_area_contents)
if all is True:
self.load_books(self.BDD.get_books())
alayout = QtWidgets.QHBoxLayout()
alabel = QtWidgets.QLabel()
alabel.setText('*')
alabel.setMaximumHeight(20)
alayout.addWidget(alabel)
self.sorting_block_search_area_contents.addLayout(alayout)
self.sorting_block_search_area.hide()
else:
self.sorting_block_search_area.show()
for filter in self.sorting_block_tree_actual_filters:
alayout = QtWidgets.QHBoxLayout()
alabel = QtWidgets.QLabel()
alabel.setText(filter.type + ' : ' + filter.data)
alabel.setToolTip(filter.data)
alabel.setWordWrap(True)
# alabel.setMaximumHeight(20)
alayout.addWidget(alabel)
abutton = QtWidgets.QPushButton()
abutton.setText("X")
abutton.setProperty('filter', filter.type)
abutton.setMinimumSize(20, 20)
abutton.setMaximumSize(20, 20)
abutton.clicked.connect(self.sorting_block_tree_del_filter_button)
alayout.addWidget(abutton)
self.sorting_block_search_area_contents.addLayout(alayout)
books = self.BDD.get_books()
end_books = []
for book in books:
cptYes = 0
cptNo = 0
ok = True
for filter in self.sorting_block_tree_actual_filters:
if filter.type == 'authors':
if book['authors'] != filter.data:
ok = False
cptNo += 1
else:
cptYes += 1
elif filter.type == 'series':
if filter.data == "[ ]":
if book['series'] is None or book['series'].strip() == '':
cptYes += 1
else:
ok = False
cptNo += 1
else:
if book['series'] != filter.data:
ok = False
cptNo += 1
else:
cptYes += 1
elif filter.type == 'tags':
filter.data = filter.data.lower()
tags = book['tags'].lower()
if tags == filter.data:
cptYes += 1
elif ';'+filter.data+';' in tags:
cptYes += 1
elif tags.startswith(filter.data+';'):
cptYes += 1
elif tags.endswith(';'+filter.data):
cptYes += 1
else:
cptNo += 1
elif filter.type == 'search':
fd = filter.data.lower()
i = book['title'].lower()
s = book['series'].lower()
a = book['authors'].lower()
t = book['tags'].lower()
if fd not in i and fd not in s and fd not in a and fd not in t:
ok = False
cptNo += 1
else:
cptYes += 1
else:
cptNo += 1
if cptYes > cptNo:
end_books.append(book)
self.load_books(end_books)
except Exception:
traceback.print_exc()
def clearLayout(self, layout):
if layout is not None:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget is not None:
widget.deleteLater()
else:
self.clearLayout(item.layout())
def sorting_block_tree_toogle_search_zone(self) -> None:
w = self.sorting_block_search_edit.width()
if w <= 0:
self.sorting_block_search_edit.setMaximumWidth(999999)
self.sorting_block_search_edit.setFocus()
else:
self.sorting_block_search_edit.setMaximumWidth(0)
def sorting_block_tree_research(self):
text = self.sorting_block_search_edit.text()
self.sorting_block_search_edit.setMaximumWidth(0)
self.sorting_block_search_edit.setText('')
self.sorting_block_tree_set_filter('search:'+text)