-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.py
411 lines (282 loc) · 14.1 KB
/
MainWindow.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
#!/usr/bin/env python
from PyQt5.QtCore import Qt
from PyQt5 import QtCore, QtWidgets, QtGui
import config.const as const
from network.DownloadManager import DownloadManager
from ficparser import FicParser
import FicCard
import FicMain
import os, pickle
from model.StoryModel import FanficModel
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.loadedFicModels = []
self.currentReadingFic = None
self.currentReadingChapterIndex = None
self.centralWidget = QtWidgets.QWidget()
self.centralLayout = QtWidgets.QGridLayout()
self.tabWidget = QtWidgets.QTabWidget()
# tab 1 - reading tab
self.splitter = QtWidgets.QSplitter(Qt.Vertical)
self.readNavWidget = QtWidgets.QWidget()
self.readNavWidgetLayout = QtWidgets.QGridLayout()
self.nextChapButton = QtWidgets.QPushButton()
self.textInfoLabel = QtWidgets.QLabel("<NO TEXT>")
self.chaptersComboBox = QtWidgets.QComboBox()
self.prevChapButton = QtWidgets.QPushButton()
self.htmlTextBrowser = QtWidgets.QTextBrowser()
# tab 2- library stuff
self.tabLibContainer = QtWidgets.QWidget()
self.tabLibCardContainer = QtWidgets.QWidget()
self.tabLibContainerLayout = QtWidgets.QGridLayout()
self.tabLibCardContainerLayout = QtWidgets.QGridLayout()
self.filterAreaWidget = QtWidgets.QWidget()
self.filterAreaLayout = QtWidgets.QHBoxLayout()
self.filterFandomComboBoxLabel = QtWidgets.QLabel("Fandom:")
self.filterFandomComboBox = QtWidgets.QComboBox()
self.filterTagsEditLabel = QtWidgets.QLabel("Tags:")
self.filterTagsEdit = QtWidgets.QLineEdit()
self.filterTagsEditCompleter = None#QtWidgets.QCompleter()
self.butFilterFics = QtWidgets.QPushButton("Filter")
self.butPopulateLibrary = QtWidgets.QPushButton("&Populate")
self.tabLibScrollArea = QtWidgets.QScrollArea()
# tab 3 - Downloading stuff stuff
self.tabDlContainer = QtWidgets.QWidget()
self.tabDlLayout = QtWidgets.QGridLayout()
self.tabDlFicContainer = QtWidgets.QWidget()
self.tabDlFicLayout = QtWidgets.QVBoxLayout()
self.urlBar = QtWidgets.QLineEdit()
self.butDownloadFic = QtWidgets.QPushButton("&Add to Queue", self)
self.butActualDownload = QtWidgets.QPushButton("&Download All", self)
self.listDownloadFicWidgets = []
# tab 4- temporary, for
self.tabFicDetailsContainer = QtWidgets.QWidget()
self.tabFicDetailsLayout = QtWidgets.QVBoxLayout()
# Other stuff
self.downloadManager = DownloadManager(self)
self.initializeGUI()
self.postInitialization()
#FIXME: temp fix for using cloudflare, match timings
def tempDLGetButton(self):
return self.butActualDownload
def initializeGUI(self):
self.centralWidget.setLayout(self.centralLayout)
# -- TAB INITIALIZATION -- #
container_1 = self.createReadingTab()
container_2 = self.createLibraryTab()
container_3 = self.createDownloadTab()
container_4 = self.createDetailsTab()
self.tabWidget.addTab(container_1, "Read")
self.tabWidget.addTab(container_2, "Library")
self.tabWidget.addTab(container_3, "Download Manager")
self.tabWidget.addTab(container_4, "Fic Details")
# -- TAB INITIALIZATION -- #
self.centralLayout.addWidget(self.tabWidget, 0, 0)
self.setCentralWidget(self.centralWidget)
self.setWindowTitle("ForWantOfANail")
self.setMinimumSize(*const.MIN_WIN_DIMENSIONS)
def addFicDownloadWidget(self, widget):
# the download manager should handle the updating
self.tabDlFicLayout.addWidget(widget)
def createReadingTab(self):
self.readNavWidget.setLayout(self.readNavWidgetLayout)
self.readNavWidgetLayout.setVerticalSpacing(0)
# set button icons and other styles
self.textInfoLabel.setStyleSheet("text-align: center; padding: 0 auto;")
style_1 = QtWidgets.QCommonStyle()
self.prevChapButton.setIcon(style_1.standardIcon(QtWidgets.QStyle.SP_ArrowLeft))
self.nextChapButton.setIcon(style_1.standardIcon(QtWidgets.QStyle.SP_ArrowRight))
self.chaptersComboBox.setMinimumWidth(const.MIN_DROP_DOWN_WIDTH)
#connect
self.prevChapButton.released.connect(self.handleNavPrevPage)
self.nextChapButton.released.connect(self.handleNavNextPage)
self.chaptersComboBox.currentIndexChanged.connect(self.handleChangeDropDownTitle)
# html alignment
self.htmlTextBrowser.setAlignment(Qt.AlignLeft)
self.readNavWidgetLayout.addWidget(self.textInfoLabel, 0, 1, Qt.AlignHCenter)
self.readNavWidgetLayout.addWidget(self.prevChapButton, 1, 0, Qt.AlignLeft)
self.readNavWidgetLayout.addWidget(self.chaptersComboBox, 1, 1, Qt.AlignHCenter)
self.readNavWidgetLayout.addWidget(self.nextChapButton, 1, 2, Qt.AlignRight)
self.splitter.addWidget(self.readNavWidget)
self.splitter.addWidget(self.htmlTextBrowser)
return self.splitter
def createDownloadTab(self):
self.tabDlContainer.setLayout(self.tabDlLayout)
self.tabDlFicContainer.setLayout(self.tabDlFicLayout)
self.tabDlLayout.setVerticalSpacing(0)
self.tabDlLayout.setContentsMargins(0, 0, 0, 0)
self.tabDlFicLayout.setSpacing(5)
self.tabDlFicLayout.setContentsMargins(0, 0, 0, 0) # l, t, r, b
self.tabDlFicLayout.setAlignment(Qt.AlignTop)
# url example
self.urlBar.setPlaceholderText("e.g. https://www.fanfiction.net/s/4390285/1/Searching-for-Disaster")
self.tabDlLayout.addWidget(self.urlBar, 0, 0)
self.tabDlLayout.addWidget(self.butDownloadFic, 0, 1)
self.tabDlLayout.addWidget(self.butActualDownload, 1, 1)
self.tabDlLayout.addWidget(self.tabDlFicContainer, 2, 0)
# button.released is a PYQT_SIGNAL
self.butDownloadFic.released.connect(self.handleFicDownload)
self.butActualDownload.released.connect(self.handleActualDownload)
return self.tabDlContainer
def createLibraryTab(self):
self.tabLibContainer.setLayout(self.tabLibContainerLayout)
self.tabLibContainerLayout.setVerticalSpacing(0)
self.tabLibContainerLayout.setAlignment(Qt.AlignTop)
self.tabLibCardContainer.setLayout(self.tabLibCardContainerLayout)
self.tabLibCardContainerLayout.setVerticalSpacing(5)
self.tabLibCardContainerLayout.setAlignment(Qt.AlignTop)
self.tabLibScrollArea.setWidget(self.tabLibCardContainer)
self.tabLibScrollArea.setWidgetResizable(True)
self.filterAreaWidget.setLayout(self.filterAreaLayout)
self.filterAreaLayout.addWidget(self.filterFandomComboBoxLabel)
self.filterAreaLayout.addWidget(self.filterFandomComboBox)
self.filterAreaLayout.addWidget(self.filterTagsEditLabel)
self.filterAreaLayout.addWidget(self.filterTagsEdit)
self.filterAreaLayout.addWidget(self.butFilterFics)
self.filterAreaLayout.addWidget(self.butPopulateLibrary)
self.tabLibContainerLayout.addWidget(self.filterAreaWidget, 0, 0)
#self.tabLibContainerLayout.addWidget(self.butPopulateLibrary, 0, 1)
self.tabLibContainerLayout.addWidget(self.tabLibScrollArea, 1, 0)
self.butPopulateLibrary.released.connect(self.handleFicPopulate)
self.butFilterFics.released.connect(self.handleFicFilter)
return self.tabLibContainer
def createDetailsTab(self):
self.tabFicDetailsContainer.setLayout(self.tabFicDetailsLayout)
return self.tabFicDetailsContainer
def postInitialization(self):
if not os.path.exists(const.DEFAULT_META_PATH):
os.makedirs(const.DEFAULT_META_PATH)
if not os.path.exists(const.DEFAULT_FILE_PATH):
os.makedirs(const.DEFAULT_FILE_PATH)
self.handleFicPopulate()
self.resetFilterElements()
self.tabWidget.setCurrentIndex(1)
def resetFilterElements(self):
self.filterFandomComboBox.clear()
fandom_dict = dict() # Fandom => no. of fics
tag_set = set()
for fic in self.loadedFicModels:
fandom_dict[fic.metadata.fandom] = fandom_dict.get(fic.metadata.fandom, 0) + 1
tag_set.union(fic.tags)
for fic in fic.metadata.fandomsCrossover:
fandom_dict[fic] = fandom_dict.get(fic, 0) + 1
fandom_list = [f"{fandom} ({count})" for fandom, count in fandom_dict.items()]
fandom_list.sort()
# filter for all fandoms at top | effectively a non-filter
fandom_list.insert(0, const.FANDOM_ALL)
self.filterFandomComboBox.addItems(fandom_list)
self.filterTagsEditCompleter = QtWidgets.QCompleter(list(tag_set), self)
self.filterTagsEdit.setCompleter(self.filterTagsEditCompleter)
#--- SIGNALS --
def handleFicPopulate(self):
self.clearLayout(self.tabLibCardContainerLayout)
target_files = os.listdir(const.DEFAULT_META_PATH)
tempLoadedFicModels = []
for filename in target_files:
ind = filename.rfind(".")
if filename[ind:] == ".json":
# Un-pickle and add
path = os.path.join(const.DEFAULT_META_PATH, filename)
with open(path, "r") as fi:
json_string = fi.read()
ficModel = FanficModel()
ficModel.deserialize(json_string)
# due to __eq__ magic method in FanficModel
if ficModel not in self.loadedFicModels:
tempLoadedFicModels.append(ficModel)
self.loadedFicModels.extend(tempLoadedFicModels)
# sort alphabetically
self.loadedFicModels.sort(key = lambda fic: fic.metadata.title)
for idx, model in enumerate(self.loadedFicModels):
widget = FicCard.FicCard(self, model)
row, col = self.getGridCoordsFromIndex(idx)
self.tabLibCardContainerLayout.addWidget(widget, row, col)
self.resetFilterElements()
def handleFicFilter(self):
self.clearLayout(self.tabLibCardContainerLayout)
fics = self.loadedFicModels
index = self.filterFandomComboBox.currentIndex()
# remove parens
fi_fandom = self.filterFandomComboBox.itemText(index)
rind = fi_fandom.rfind("(")
if rind != -1:
fi_fandom = fi_fandom[:rind].strip()
fi_tags = self.filterTagsEdit.text().lstrip().rstrip()
if fi_tags != '':
fi_tags = set(fi_tags.split(" "))
else:
fi_tags = set([])
if fi_fandom != '' and fi_fandom != const.FANDOM_ALL:
fics = [fic for fic in fics if fic.metadata.fandom == fi_fandom or fi_fandom in fic.metadata.fandomsCrossover]
if fi_tags:
fics = [fic for fic in fics if fi_tags.issubset(fic.tags)]
for idx, model in enumerate(fics):
widget = FicCard.FicCard(self, model)
row, col = self.getGridCoordsFromIndex(idx)
self.tabLibCardContainerLayout.addWidget(widget, row, col)
def handleFicDownload(self):
text = self.urlBar.text()
if text == "":
pass
self.downloadManager.appendRawFic(self.urlBar.text())
def handleActualDownload(self):
self.downloadManager.startDownloads()
def handleNavNextPage(self):
if self.currentReadingChapterIndex == self.currentReadingFic.metadata.numChapters - 1:
return
self.currentReadingChapterIndex += 1
self.setReadingPage(self.currentReadingChapterIndex)
def handleNavPrevPage(self):
if self.currentReadingChapterIndex == 0:
return
self.currentReadingChapterIndex -= 1
self.setReadingPage(self.currentReadingChapterIndex)
def handleChangeDropDownTitle(self, index):
self.setReadingPage(index)
# --- utils
def showFicDetails(self, ficModel):
#self.tabFicDetails = FicMain.FicDescriptionWidget(ficModel)
child = self.tabFicDetailsLayout.takeAt(0)
if child and child.widget() is not None:
child.widget().deleteLater()
self.tabFicDetailsLayout.addWidget(FicMain.FicDescriptionWidget(ficModel))
self.tabFicDetailsLayout.update()
self.tabWidget.setCurrentIndex(3)
def setReadingHTML(self, file_path):
with open(file_path, "r") as f:
dat = f.read()
par = FicParser.FicParser(dat)
mainHTML = par.getMainStory()
self.htmlTextBrowser.setHtml(mainHTML)
def setReadingPage(self, index):
self.currentReadingChapterIndex = index
self.setReadingHTML(self.currentReadingFic.realFiles[index])
self.chaptersComboBox.setCurrentIndex(index)
ficName = self.currentReadingFic.metadata.title
#chapName = self.currentReadingFic.metadata.chapterTitles[index]
label_text = ficName #+ "\n" + chapName
self.textInfoLabel.setText(label_text)
def setReadingFic(self, ficModel):
self.currentReadingFic = ficModel
#populate combobox
self.chaptersComboBox.clear()
for title in self.currentReadingFic.metadata.chapterTitles:
self.chaptersComboBox.addItem(title)
#self.currentReadingChapterIndex = 0
self.setReadingPage(0)
self.tabWidget.setCurrentIndex(0)
#self.setReadingHTML(self.currentReadingFic.realFiles[0])
def getGridCoordsFromIndex(self, index):
row = index // 3
col = index % 3
return (row, col)
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())