diff --git a/__main__.py b/__main__.py index 9de7736..90864e2 100755 --- a/__main__.py +++ b/__main__.py @@ -635,10 +635,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): current_tab.metadata[ 'position']['current_chapter'] = event + 1 + current_tab.metadata[ + 'position']['is_read'] = False if model_index: 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 current_tab.change_chapter_tocBox() @@ -968,6 +970,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): openAction = context_menu.addAction( QtGui.QIcon.fromTheme('view-readermode'), 'Start reading') + editAction = None if len(selected_indexes) == 1: editAction = context_menu.addAction( QtGui.QIcon.fromTheme('edit-rename'), 'Edit') @@ -1020,21 +1023,38 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.delete_books(selected_indexes) 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: - metadata = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 7) - metadata2 = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 3) - print(metadata2['position']) - # self.lib_ref.view_model.setData( - # model_index, current_tab.metadata['position'], QtCore.Qt.UserRole + 7) + metadata = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 3) + book_hash = self.lib_ref.view_model.data(i, QtCore.Qt.UserRole + 6) + position = metadata['position'] + + 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): self.libraryFilterMenu.clear() diff --git a/database.py b/database.py index b36ba54..29eaa85 100644 --- a/database.py +++ b/database.py @@ -185,10 +185,9 @@ class DatabaseFunctions: sql_command = 'UPDATE books SET ' update_data = [] for i in metadata_dict.items(): - if i[1]: - sql_command += i[0] + ' = ?, ' - bin_data = generate_binary(i[0], i[1]) - update_data.append(bin_data) + sql_command += i[0] + ' = ?, ' + bin_data = generate_binary(i[0], i[1]) + update_data.append(bin_data) sql_command = sql_command[:-2] sql_command += ' WHERE Hash = ?' diff --git a/library.py b/library.py index 162c6f4..643e892 100644 --- a/library.py +++ b/library.py @@ -126,7 +126,7 @@ class Library: item.setData(tags, QtCore.Qt.UserRole + 4) item.setData(file_exists, QtCore.Qt.UserRole + 5) 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(date_added, QtCore.Qt.UserRole + 9) item.setData(last_accessed, QtCore.Qt.UserRole + 12) diff --git a/metadatadialog.py b/metadatadialog.py index 21c2c25..ad54ee9 100644 --- a/metadatadialog.py +++ b/metadatadialog.py @@ -114,20 +114,20 @@ class MetadataUI(QtWidgets.QDialog, metadata.Ui_Dialog): book_item.setData(tags, QtCore.Qt.UserRole + 4) 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) database_dict = { 'Title': title, 'Author': author, 'Year': year, - 'Tags': tags, - 'CoverImage': self.cover_for_database} + 'Tags': tags} + + 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_dict, book_hash) diff --git a/models.py b/models.py index 4fccd1e..7d44b11 100644 --- a/models.py +++ b/models.py @@ -104,7 +104,8 @@ class TableProxyModel(QtCore.QSortFilterProxyModel): return_pixmap = None 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: return_pixmap = pie_chart.pixmapper( diff --git a/widgets.py b/widgets.py index 0fe31d9..a23361f 100644 --- a/widgets.py +++ b/widgets.py @@ -44,9 +44,10 @@ class Tab(QtWidgets.QWidget): self.metadata['last_accessed'] = QtCore.QDateTime().currentDateTime() - position = self.metadata['position'] - if position: - current_chapter = position['current_chapter'] + if self.metadata['position']: + if self.metadata['position']['is_read']: + self.generate_position(True) + current_chapter = self.metadata['position']['current_chapter'] else: self.generate_position() current_chapter = 1 @@ -174,23 +175,31 @@ class Tab(QtWidgets.QWidget): if search_text: self.contentView.find(search_text) - text_cursor = self.contentView.textCursor() - text_cursor.clearSelection() - self.contentView.setTextCursor(text_cursor) + text_cursor = self.contentView.textCursor() + text_cursor.clearSelection() + self.contentView.setTextCursor(text_cursor) if switch_widgets: self.window().tabWidget.setCurrentWidget(previous_widget) - def generate_position(self): - total_chapters = len(self.metadata['content'].keys()) + def generate_position(self, is_read=False): # TODO # 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'] = { - 'current_chapter': 1, + 'current_chapter': current_chapter, 'total_chapters': total_chapters, - 'scroll_value': 0, - 'last_visible_text': None} + 'scroll_value': scroll_value, + 'last_visible_text': None, + 'is_read': is_read} def generate_keyboard_shortcuts(self): self.next_chapter = QtWidgets.QShortcut( @@ -437,7 +446,7 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView): self.image_pixmap.load(image_path) self.resizeEvent() - def resizeEvent(self, event=None): + def resizeEvent(self, *args): if not self.image_pixmap: return @@ -497,7 +506,7 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView): scroll_increment = int((maximum - 0) / 2) self.verticalScrollBar().setValue(vertical + scroll_increment) - def mouseMoveEvent(self, event): + def mouseMoveEvent(self, *args): self.setCursor(QtCore.Qt.ArrowCursor) self.parent.mouse_hide_timer.start(3000) @@ -649,7 +658,11 @@ class LibraryDelegate(QtWidgets.QStyledItemDelegate): option = option.__class__(option) 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 # 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) if position: - current_chapter = position['current_chapter'] - total_chapters = position['total_chapters'] + if is_read: + 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( current_chapter, total_chapters, self.temp_dir, 36)