Move cover loading and culling to guifunctions module

This commit is contained in:
BasioMeusPuga
2018-03-24 00:30:58 +05:30
parent f63b6627b2
commit 0f963b20f9
6 changed files with 111 additions and 100 deletions

View File

@@ -37,7 +37,7 @@ from lector.widgets import Tab
from lector.delegates import LibraryDelegate
from lector.threaded import BackGroundTabUpdate, BackGroundBookAddition, BackGroundBookDeletion
from lector.library import Library
from lector.guifunctions import QImageFactory
from lector.guifunctions import QImageFactory, CoverLoadingAndCulling
from lector.settings import Settings
from lector.settingsdialog import SettingsUI
from lector.metadatadialog import MetadataUI
@@ -125,13 +125,17 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
# Application wide temporary directory
self.temp_dir = QtCore.QTemporaryDir()
# Init the Library
self.lib_ref = Library(self)
# Initialize Cover loading functions
# Must be after the Library init
self.cover_functions = CoverLoadingAndCulling(self)
# Init the culling timer
self.culling_timer = QtCore.QTimer()
self.culling_timer.setSingleShot(True)
self.culling_timer.timeout.connect(self.cull_covers)
# Init the Library
self.lib_ref = Library(self)
self.culling_timer.timeout.connect(self.cover_functions.cull_covers)
# Toolbar display
# Maybe make this a persistent option
@@ -333,98 +337,10 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.move_on()
def cull_covers(self, event=None):
blank_pixmap = QtGui.QPixmap()
blank_pixmap.load(':/images/blank.png') # Keep this. Removing it causes the
# listView to go blank on a resize
all_indexes = set()
for i in range(self.lib_ref.item_proxy_model.rowCount()):
all_indexes.add(self.lib_ref.item_proxy_model.index(i, 0))
y_range = list(range(0, self.listView.viewport().height(), 100))
y_range.extend((-20, self.listView.viewport().height() + 20))
x_range = range(0, self.listView.viewport().width(), 80)
visible_indexes = set()
for i in y_range:
for j in x_range:
this_index = self.listView.indexAt(QtCore.QPoint(j, i))
visible_indexes.add(this_index)
invisible_indexes = all_indexes - visible_indexes
for i in invisible_indexes:
model_index = self.lib_ref.item_proxy_model.mapToSource(i)
this_item = self.lib_ref.view_model.item(model_index.row())
if this_item:
this_item.setIcon(QtGui.QIcon(blank_pixmap))
this_item.setData(False, QtCore.Qt.UserRole + 8)
hash_index_dict = {}
hash_list = []
for i in visible_indexes:
model_index = self.lib_ref.item_proxy_model.mapToSource(i)
book_hash = self.lib_ref.view_model.data(
model_index, QtCore.Qt.UserRole + 6)
cover_displayed = self.lib_ref.view_model.data(
model_index, QtCore.Qt.UserRole + 8)
if book_hash and not cover_displayed:
hash_list.append(book_hash)
hash_index_dict[book_hash] = model_index
all_covers = database.DatabaseFunctions(
self.database_path).fetch_covers_only(hash_list)
for i in all_covers:
book_hash = i[0]
cover = i[1]
model_index = hash_index_dict[book_hash]
book_item = self.lib_ref.view_model.item(model_index.row())
self.cover_loader(book_item, cover)
def start_culling_timer(self):
if self.settings['perform_culling']:
self.culling_timer.start(30)
def load_all_covers(self):
all_covers_db = database.DatabaseFunctions(
self.database_path).fetch_data(
('Hash', 'CoverImage',),
'books',
{'Hash': ''},
'LIKE')
if not all_covers_db:
return
all_covers = {
i[0]: i[1] for i in all_covers_db}
for i in range(self.lib_ref.view_model.rowCount()):
this_item = self.lib_ref.view_model.item(i, 0)
is_cover_already_displayed = this_item.data(QtCore.Qt.UserRole + 8)
if is_cover_already_displayed:
continue
book_hash = this_item.data(QtCore.Qt.UserRole + 6)
cover = all_covers[book_hash]
self.cover_loader(this_item, cover)
def cover_loader(self, item, cover):
img_pixmap = QtGui.QPixmap()
if cover:
img_pixmap.loadFromData(cover)
else:
img_pixmap.load(':/images/NotFound.png')
img_pixmap = img_pixmap.scaled(420, 600, QtCore.Qt.IgnoreAspectRatio)
item.setIcon(QtGui.QIcon(img_pixmap))
item.setData(True, QtCore.Qt.UserRole + 8)
def add_bookmark(self):
if self.tabWidget.currentIndex() != 0:
self.tabWidget.widget(self.tabWidget.currentIndex()).add_bookmark()
@@ -570,7 +486,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self._translate('Main_UI', ' books'))
if not self.settings['perform_culling']:
self.load_all_covers()
self.cover_functions.load_all_covers()
def switch_library_view(self):
if self.libraryToolBar.coverViewButton.isChecked():
@@ -1077,7 +993,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
book_item = self.lib_ref.view_model.item(edit_book.row())
book_cover = database.DatabaseFunctions(
self.database_path).fetch_covers_only([book_hash])[0][1]
self.cover_loader(book_item, book_cover)
self.cover_functions.cover_loader(book_item, book_cover)
cover = self.lib_ref.view_model.item(edit_book.row()).icon()
title = metadata['title']