Implement Mark (un)Read functionality

This commit is contained in:
BasioMeusPuga
2018-03-06 23:10:03 +05:30
parent 45811b57cb
commit 710ec98c0a
6 changed files with 85 additions and 46 deletions

View File

@@ -635,10 +635,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
current_tab.metadata[ current_tab.metadata[
'position']['current_chapter'] = event + 1 'position']['current_chapter'] = event + 1
current_tab.metadata[
'position']['is_read'] = False
if model_index: if model_index:
self.lib_ref.view_model.setData( self.lib_ref.view_model.setData(
model_index, current_tab.metadata['position'], QtCore.Qt.UserRole + 7) model_index, current_tab.metadata, QtCore.Qt.UserRole + 3)
# Go on to change the value of the Table of Contents box # Go on to change the value of the Table of Contents box
current_tab.change_chapter_tocBox() current_tab.change_chapter_tocBox()
@@ -968,6 +970,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
openAction = context_menu.addAction( openAction = context_menu.addAction(
QtGui.QIcon.fromTheme('view-readermode'), 'Start reading') QtGui.QIcon.fromTheme('view-readermode'), 'Start reading')
editAction = None
if len(selected_indexes) == 1: if len(selected_indexes) == 1:
editAction = context_menu.addAction( editAction = context_menu.addAction(
QtGui.QIcon.fromTheme('edit-rename'), 'Edit') QtGui.QIcon.fromTheme('edit-rename'), 'Edit')
@@ -1020,21 +1023,38 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.delete_books(selected_indexes) self.delete_books(selected_indexes)
if action == readAction or action == unreadAction: if action == readAction or action == unreadAction:
# TODO
# This will take some doing
# Best idea: Have another field in the db that returns an "is read"
# Have that supersede all other considerations of position
# Use that to generate a position in the tab when the file is opened
# OR
# Simulate file opening and closure
for i in selected_indexes: for i in selected_indexes:
metadata = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 7) metadata = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 3)
metadata2 = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 3) book_hash = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 6)
print(metadata2['position']) position = metadata['position']
# self.lib_ref.view_model.setData(
# model_index, current_tab.metadata['position'], QtCore.Qt.UserRole + 7) if position:
if action == readAction:
position['is_read'] = True
position['scroll_value'] = 1
elif action == unreadAction:
position['is_read'] = False
position['current_chapter'] = 1
position['scroll_value'] = 0
else:
position = {}
if action == readAction:
position['is_read'] = True
metadata['position'] = position
self.lib_ref.view_model.setData(i, metadata, QtCore.Qt.UserRole + 3)
last_accessed_time = None
if action == readAction:
last_accessed_time = QtCore.QDateTime().currentDateTime()
self.lib_ref.view_model.setData(i, last_accessed_time, QtCore.Qt.UserRole + 12)
database_dict = {
'Position': position,
'LastAccessed': last_accessed_time}
database.DatabaseFunctions(
self.database_path).modify_metadata(database_dict, book_hash)
def generate_library_filter_menu(self, directory_list=None): def generate_library_filter_menu(self, directory_list=None):
self.libraryFilterMenu.clear() self.libraryFilterMenu.clear()

View File

@@ -185,10 +185,9 @@ class DatabaseFunctions:
sql_command = 'UPDATE books SET ' sql_command = 'UPDATE books SET '
update_data = [] update_data = []
for i in metadata_dict.items(): for i in metadata_dict.items():
if i[1]: sql_command += i[0] + ' = ?, '
sql_command += i[0] + ' = ?, ' bin_data = generate_binary(i[0], i[1])
bin_data = generate_binary(i[0], i[1]) update_data.append(bin_data)
update_data.append(bin_data)
sql_command = sql_command[:-2] sql_command = sql_command[:-2]
sql_command += ' WHERE Hash = ?' sql_command += ' WHERE Hash = ?'

View File

@@ -126,7 +126,7 @@ class Library:
item.setData(tags, QtCore.Qt.UserRole + 4) item.setData(tags, QtCore.Qt.UserRole + 4)
item.setData(file_exists, QtCore.Qt.UserRole + 5) item.setData(file_exists, QtCore.Qt.UserRole + 5)
item.setData(i[8], QtCore.Qt.UserRole + 6) # File hash item.setData(i[8], QtCore.Qt.UserRole + 6) # File hash
item.setData(position, QtCore.Qt.UserRole + 7) item.setData(position, QtCore.Qt.UserRole + 30)
item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed? item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed?
item.setData(date_added, QtCore.Qt.UserRole + 9) item.setData(date_added, QtCore.Qt.UserRole + 9)
item.setData(last_accessed, QtCore.Qt.UserRole + 12) item.setData(last_accessed, QtCore.Qt.UserRole + 12)

View File

@@ -114,20 +114,20 @@ class MetadataUI(QtWidgets.QDialog, metadata.Ui_Dialog):
book_item.setData(tags, QtCore.Qt.UserRole + 4) book_item.setData(tags, QtCore.Qt.UserRole + 4)
book_item.setToolTip(tooltip_string) book_item.setToolTip(tooltip_string)
if self.cover_for_database:
self.parent.cover_loader(
book_item, self.cover_for_database)
self.parent.lib_ref.update_proxymodels()
self.hide()
book_hash = book_item.data(QtCore.Qt.UserRole + 6) book_hash = book_item.data(QtCore.Qt.UserRole + 6)
database_dict = { database_dict = {
'Title': title, 'Title': title,
'Author': author, 'Author': author,
'Year': year, 'Year': year,
'Tags': tags, 'Tags': tags}
'CoverImage': self.cover_for_database}
if self.cover_for_database:
database_dict['CoverImage'] = self.cover_for_database
self.parent.cover_loader(
book_item, self.cover_for_database)
self.parent.lib_ref.update_proxymodels()
self.hide()
database.DatabaseFunctions(self.database_path).modify_metadata( database.DatabaseFunctions(self.database_path).modify_metadata(
database_dict, book_hash) database_dict, book_hash)

View File

@@ -104,7 +104,8 @@ class TableProxyModel(QtCore.QSortFilterProxyModel):
return_pixmap = None return_pixmap = None
file_exists = item.data(QtCore.Qt.UserRole + 5) file_exists = item.data(QtCore.Qt.UserRole + 5)
position = item.data(QtCore.Qt.UserRole + 7) metadata = item.data(QtCore.Qt.UserRole + 3)
position = metadata['position']
if not file_exists: if not file_exists:
return_pixmap = pie_chart.pixmapper( return_pixmap = pie_chart.pixmapper(

View File

@@ -44,9 +44,10 @@ class Tab(QtWidgets.QWidget):
self.metadata['last_accessed'] = QtCore.QDateTime().currentDateTime() self.metadata['last_accessed'] = QtCore.QDateTime().currentDateTime()
position = self.metadata['position'] if self.metadata['position']:
if position: if self.metadata['position']['is_read']:
current_chapter = position['current_chapter'] self.generate_position(True)
current_chapter = self.metadata['position']['current_chapter']
else: else:
self.generate_position() self.generate_position()
current_chapter = 1 current_chapter = 1
@@ -174,23 +175,31 @@ class Tab(QtWidgets.QWidget):
if search_text: if search_text:
self.contentView.find(search_text) self.contentView.find(search_text)
text_cursor = self.contentView.textCursor() text_cursor = self.contentView.textCursor()
text_cursor.clearSelection() text_cursor.clearSelection()
self.contentView.setTextCursor(text_cursor) self.contentView.setTextCursor(text_cursor)
if switch_widgets: if switch_widgets:
self.window().tabWidget.setCurrentWidget(previous_widget) self.window().tabWidget.setCurrentWidget(previous_widget)
def generate_position(self): def generate_position(self, is_read=False):
total_chapters = len(self.metadata['content'].keys())
# TODO # TODO
# Calculate lines to incorporate into progress # Calculate lines to incorporate into progress
total_chapters = len(self.metadata['content'].keys())
current_chapter = 1
scroll_value = 0
if is_read:
current_chapter = total_chapters
scroll_value = 1
self.metadata['position'] = { self.metadata['position'] = {
'current_chapter': 1, 'current_chapter': current_chapter,
'total_chapters': total_chapters, 'total_chapters': total_chapters,
'scroll_value': 0, 'scroll_value': scroll_value,
'last_visible_text': None} 'last_visible_text': None,
'is_read': is_read}
def generate_keyboard_shortcuts(self): def generate_keyboard_shortcuts(self):
self.next_chapter = QtWidgets.QShortcut( self.next_chapter = QtWidgets.QShortcut(
@@ -437,7 +446,7 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView):
self.image_pixmap.load(image_path) self.image_pixmap.load(image_path)
self.resizeEvent() self.resizeEvent()
def resizeEvent(self, event=None): def resizeEvent(self, *args):
if not self.image_pixmap: if not self.image_pixmap:
return return
@@ -497,7 +506,7 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView):
scroll_increment = int((maximum - 0) / 2) scroll_increment = int((maximum - 0) / 2)
self.verticalScrollBar().setValue(vertical + scroll_increment) self.verticalScrollBar().setValue(vertical + scroll_increment)
def mouseMoveEvent(self, event): def mouseMoveEvent(self, *args):
self.setCursor(QtCore.Qt.ArrowCursor) self.setCursor(QtCore.Qt.ArrowCursor)
self.parent.mouse_hide_timer.start(3000) self.parent.mouse_hide_timer.start(3000)
@@ -649,7 +658,11 @@ class LibraryDelegate(QtWidgets.QStyledItemDelegate):
option = option.__class__(option) option = option.__class__(option)
file_exists = index.data(QtCore.Qt.UserRole + 5) file_exists = index.data(QtCore.Qt.UserRole + 5)
position = index.data(QtCore.Qt.UserRole + 7) metadata = index.data(QtCore.Qt.UserRole + 3)
position = metadata['position']
if position:
is_read = position['is_read']
# The shadow pixmap currently is set to 420 x 600 # The shadow pixmap currently is set to 420 x 600
# Only draw the cover shadow in case the setting is enabled # Only draw the cover shadow in case the setting is enabled
@@ -675,8 +688,14 @@ class LibraryDelegate(QtWidgets.QStyledItemDelegate):
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index) QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
if position: if position:
current_chapter = position['current_chapter'] if is_read:
total_chapters = position['total_chapters'] current_chapter = total_chapters = 100
else:
try:
current_chapter = position['current_chapter']
total_chapters = position['total_chapters']
except KeyError:
return
read_icon = pie_chart.pixmapper( read_icon = pie_chart.pixmapper(
current_chapter, total_chapters, self.temp_dir, 36) current_chapter, total_chapters, self.temp_dir, 36)