Add feature to download selected books in GUI

This commit is contained in:
nightroan
2023-02-17 01:36:59 -08:00
parent d0b7a05b86
commit 9bdd717282
3 changed files with 51 additions and 10 deletions

View File

@@ -84,7 +84,11 @@
</widget>
</item>
<item>
<widget class="QTableView" name="bookView"/>
<widget class="QTableView" name="bookView">
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
@@ -116,6 +120,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="selectedButton">
<property name="text">
<string>Download Selected</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="dedrmCkb">
<property name="text">

View File

@@ -3,7 +3,7 @@
################################################################################
## Form generated from reading UI file 'kindle.ui'
##
## Created by: Qt User Interface Compiler version 6.3.0
## Created by: Qt User Interface Compiler version 6.3.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
@@ -15,7 +15,7 @@ from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QCheckBox, QDialog, QGroupBox,
from PySide6.QtWidgets import (QAbstractItemView, QApplication, QCheckBox, QDialog, QGroupBox,
QHBoxLayout, QHeaderView, QLabel, QLineEdit,
QPlainTextEdit, QPushButton, QRadioButton, QSizePolicy,
QSpinBox, QTableView, QTextBrowser, QVBoxLayout,
@@ -67,6 +67,7 @@ class Ui_MainDialog(object):
self.bookView = QTableView(self.listBox)
self.bookView.setObjectName(u"bookView")
self.bookView.setSelectionBehavior(QAbstractItemView.SelectRows)
self.verticalLayout_5.addWidget(self.bookView)
@@ -92,6 +93,11 @@ class Ui_MainDialog(object):
self.horizontalLayout_4.addWidget(self.downloadButton)
self.selectedButton = QPushButton(self.listBox)
self.selectedButton.setObjectName(u"selectedButton")
self.horizontalLayout_4.addWidget(self.selectedButton)
self.dedrmCkb = QCheckBox(self.listBox)
self.dedrmCkb.setObjectName(u"dedrmCkb")
@@ -280,6 +286,7 @@ class Ui_MainDialog(object):
self.outDirEdit.setText(QCoreApplication.translate("MainDialog", u"DOWNLOADS", None))
self.browseButton.setText(QCoreApplication.translate("MainDialog", u"\u6d4f\u89c8...", None))
self.downloadButton.setText(QCoreApplication.translate("MainDialog", u"\u4e0b\u8f7d\u5168\u90e8", None))
self.selectedButton.setText(QCoreApplication.translate("MainDialog", u"Download Selected", None))
self.dedrmCkb.setText(QCoreApplication.translate("MainDialog", u"DeDRM", None))
self.groupBox_2.setTitle(QCoreApplication.translate("MainDialog", u"\u8f93\u51fa", None))
self.logBrowser.setHtml(QCoreApplication.translate("MainDialog", u"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"

View File

@@ -33,6 +33,7 @@ class Book(NamedTuple):
asin: str
filetype: str
done: bool
selected: bool
class Worker(QtCore.QObject):
@@ -82,7 +83,7 @@ class KindleMainDialog(QtWidgets.QDialog):
# self.setup_logger()
self.book_model = BookItemModel(self.ui.bookView, [], ["序号", "书名", "作者"])
self.ui.bookView.setModel(self.book_model)
self.ui.bookView.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
#self.ui.bookView.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection) #allow selection so user can choose items to download
self.ui.bookView.horizontalHeader().setSectionResizeMode(
1, QtWidgets.QHeaderView.Stretch
)
@@ -97,6 +98,7 @@ class KindleMainDialog(QtWidgets.QDialog):
self.ui.browseButton.clicked.connect(self.on_browse_dir)
self.ui.fetchButton.clicked.connect(self.on_fetch_books)
self.ui.downloadButton.clicked.connect(self.on_download_books)
self.ui.selectedButton.clicked.connect(self.on_download_selected_books)
def show_error(self, message):
msg = QtWidgets.QErrorMessage(self)
@@ -185,7 +187,7 @@ class KindleMainDialog(QtWidgets.QDialog):
def log(self, message):
self.ui.logBrowser.append(message)
def on_download_books(self):
def download_books(self, mode="all"):
if not self.setup_kindle():
return
if not os.path.exists(self.kindle.out_dir):
@@ -193,7 +195,7 @@ class KindleMainDialog(QtWidgets.QDialog):
if not os.path.exists(self.kindle.out_dedrm_dir):
os.makedirs(self.kindle.out_dedrm_dir)
self.thread = QtCore.QThread()
iterable = self.book_model.data_to_download()
iterable = self.book_model.data_to_download(mode)
total = len(iterable)
self.kindle.total_to_download = total
self.worker = Worker(iterable, self.kindle)
@@ -214,6 +216,13 @@ class KindleMainDialog(QtWidgets.QDialog):
self.thread.finished.connect(self.on_finish_download)
self.thread.start()
def on_download_books(self):
self.download_books("all")
def on_download_selected_books(self):
self.book_model.mark_selected(self.ui.bookView.selectionModel().selectedRows(column=0))
self.download_books("selected")
def on_finish_download(self):
self.ui.downloadButton.setEnabled(True)
QtWidgets.QMessageBox.information(self, "下载完成", "下载完成")
@@ -238,7 +247,18 @@ class BookItemModel(QtCore.QAbstractTableModel):
def mark_done(self, idx):
if idx >= len(self._data):
return
self._data[idx] = Book(*self._data[idx][:-1], done=True)
self._data[idx] = self._data[idx]._replace(done=True)
self.layoutAboutToBeChanged.emit()
self.dataChanged.emit(
self.createIndex(idx, 0), self.createIndex(idx, self.columnCount(0))
)
self.layoutChanged.emit()
# Mark which books to download
def mark_selected(self, rows):
for index in rows:
idx = self.data(index, QtCore.Qt.DisplayRole)
self._data[idx-1] = self._data[idx-1]._replace(selected=True)
self.layoutAboutToBeChanged.emit()
self.dataChanged.emit(
self.createIndex(idx, 0), self.createIndex(idx, self.columnCount(0))
@@ -246,7 +266,7 @@ class BookItemModel(QtCore.QAbstractTableModel):
self.layoutChanged.emit()
def updateData(self, data):
self._data = [Book(i, *row, False) for i, row in enumerate(data, 1)]
self._data = [Book(i, *row, False, False) for i, row in enumerate(data, 1)]
self.layoutAboutToBeChanged.emit()
self.dataChanged.emit(
self.createIndex(0, 0),
@@ -254,8 +274,11 @@ class BookItemModel(QtCore.QAbstractTableModel):
)
self.layoutChanged.emit()
def data_to_download(self):
return [item for item in self._data if not item.done]
def data_to_download(self, mode="all"):
if mode == "all":
return [item for item in self._data if not item.done]
elif mode == "selected":
return [item for item in self._data if (not item.done) and (item.selected)]
def data(self, index, role):
if not index.isValid():