Implement single/double page modes for comics/pdfs

This commit is contained in:
BasioMeusPuga
2018-10-31 10:39:21 +05:30
parent b5349315be
commit 4b9221128c
4 changed files with 117 additions and 48 deletions

5
TODO
View File

@@ -61,10 +61,12 @@ TODO
✓ Make the bookmark dock float over the reading area ✓ Make the bookmark dock float over the reading area
✓ Spacebar should not cut off lines at the top ✓ Spacebar should not cut off lines at the top
✓ Track open bookmark windows so they can be closed quickly at exit ✓ Track open bookmark windows so they can be closed quickly at exit
Double page / column view
✓ For comics
Caching is current non fuctional
Annotations Annotations
✓ Text ✓ Text
Annotation preview in listView Annotation preview in listView
Image
Adjust key navigation according to viewport dimensions Adjust key navigation according to viewport dimensions
Search document using QTextCursor Search document using QTextCursor
Filetypes: Filetypes:
@@ -106,7 +108,6 @@ TODO
Include icons for filetype emblems Include icons for filetype emblems
Comic view modes Comic view modes
Continuous paging Continuous paging
Double pages
Ignore a / the / numbers for sorting purposes Ignore a / the / numbers for sorting purposes
? Add only one file type if multiple are present ? Add only one file type if multiple are present
? Create emblem per filetype ? Create emblem per filetype

View File

@@ -179,6 +179,10 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.bookToolBar.singlePageButton.triggered.connect(self.change_page_view) self.bookToolBar.singlePageButton.triggered.connect(self.change_page_view)
self.bookToolBar.doublePageButton.triggered.connect(self.change_page_view) self.bookToolBar.doublePageButton.triggered.connect(self.change_page_view)
if self.settings['page_view_button'] == 'singlePageButton':
self.bookToolBar.singlePageButton.setChecked(True)
else:
self.bookToolBar.doublePageButton.setChecked(True)
for count, i in enumerate(self.display_profiles): for count, i in enumerate(self.display_profiles):
self.bookToolBar.profileBox.setItemData(count, i, QtCore.Qt.UserRole) self.bookToolBar.profileBox.setItemData(count, i, QtCore.Qt.UserRole)
@@ -766,7 +770,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
def change_page_view(self): def change_page_view(self):
self.settings['page_view_button'] = self.sender().objectName() self.settings['page_view_button'] = self.sender().objectName()
print(self.sender().objectName()) chapter_number = self.bookToolBar.tocBox.currentIndex()
# Switch page to whatever index is selected in the tocBox
current_tab = self.tabWidget.currentWidget()
required_content = current_tab.metadata['content'][chapter_number][1]
current_tab.contentView.loadImage(required_content)
def search_book(self, search_text): def search_book(self, search_text):
current_tab = self.tabWidget.currentIndex() current_tab = self.tabWidget.currentIndex()

View File

@@ -73,36 +73,58 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView):
self.generate_graphicsview_context_menu) self.generate_graphicsview_context_menu)
def loadImage(self, current_page): def loadImage(self, current_page):
# TODO
# For double page view: 2 before, 2 after
all_pages = [i[1] for i in self.parent.metadata['content']] all_pages = [i[1] for i in self.parent.metadata['content']]
current_page_index = all_pages.index(current_page)
double_page_mode = False
if (self.main_window.settings['page_view_button'] == 'doublePageButton'
and (current_page_index != 0 and current_page_index != len(all_pages) - 1)):
double_page_mode = True
def load_page(current_page): def load_page(current_page):
image_pixmap = QtGui.QPixmap() def page_loader(page):
# TODO Maybe pdf image res needs a setting?
pixmap = QtGui.QPixmap()
if self.filetype in ('cbz', 'cbr'): if self.filetype in ('cbz', 'cbr'):
page_data = self.book.read(current_page) page_data = self.book.read(page)
image_pixmap.loadFromData(page_data) pixmap.loadFromData(page_data)
elif self.filetype == 'pdf': elif self.filetype == 'pdf':
page_data = self.book.page(current_page) page_data = self.book.page(current_page)
page_qimage = page_data.renderToImage(400, 400) # TODO Maybe this needs a setting? page_qimage = page_data.renderToImage(400, 400)
image_pixmap.convertFromImage(page_qimage) pixmap.convertFromImage(page_qimage)
return image_pixmap return pixmap
firstPixmap = page_loader(current_page)
if not double_page_mode:
return firstPixmap
next_page = all_pages[current_page_index + 1]
secondPixmap = page_loader(next_page)
bigPixmap = QtGui.QPixmap(
firstPixmap.width() + secondPixmap.width() + 5,
firstPixmap.height())
bigPixmap.fill(QtCore.Qt.transparent)
imagePainter = QtGui.QPainter(bigPixmap)
imagePainter.drawPixmap(0, 0, firstPixmap)
imagePainter.drawPixmap(firstPixmap.width() + 5, 0, secondPixmap)
imagePainter.end()
return bigPixmap
def generate_image_cache(current_page): def generate_image_cache(current_page):
print('Building image cache') print('Building image cache')
current_page_index = all_pages.index(current_page) current_page_index = all_pages.index(current_page)
# Image caching for single and double page views # Image caching for single and double page views
if self.main_window.settings['page_view_button'] == 'singlePageButton':
page_indices = (-1, 0, 1, 2) page_indices = (-1, 0, 1, 2)
else:
# 2 page view index_modifier = 0
page_indices = (-2, -1, 0, 1, 2, 3) if double_page_mode:
index_modifier = 1
for i in page_indices: for i in page_indices:
try: try:
this_page = all_pages[current_page_index + i] this_page = all_pages[current_page_index + i + index_modifier]
this_pixmap = load_page(this_page) this_pixmap = load_page(this_page)
self.image_cache[i + 1] = (this_page, this_pixmap) self.image_cache[i + 1] = (this_page, this_pixmap)
except IndexError: except IndexError:
@@ -131,7 +153,9 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView):
# No return happened so the image isn't in the cache # No return happened so the image isn't in the cache
generate_image_cache(current_page) generate_image_cache(current_page)
if self.main_window.settings['caching_enabled']: # TODO
# Get caching working for double page view
if not double_page_mode and self.main_window.settings['caching_enabled']:
return_pixmap = None return_pixmap = None
while not return_pixmap: while not return_pixmap:
return_pixmap = check_cache(current_page) return_pixmap = check_cache(current_page)
@@ -267,6 +291,18 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView):
viewSubMenu.setIcon( viewSubMenu.setIcon(
self.main_window.QImageFactory.get_image('mail-thread-watch')) self.main_window.QImageFactory.get_image('mail-thread-watch'))
singlePageAction = doublePageAction = 'It\'s hammer time'
if self.main_window.settings['page_view_button'] == 'doublePageButton':
singlePageAction = viewSubMenu.addAction(
self.main_window.QImageFactory.get_image('page-single'),
self._translate('PliantQGraphicsView', 'Single page view'))
else:
doublePageAction = viewSubMenu.addAction(
self.main_window.QImageFactory.get_image('page-double'),
self._translate('PliantQGraphicsView', 'Double page view'))
viewSubMenu.addSeparator()
zoominAction = viewSubMenu.addAction( zoominAction = viewSubMenu.addAction(
self.main_window.QImageFactory.get_image('zoom-in'), self.main_window.QImageFactory.get_image('zoom-in'),
self._translate('PliantQGraphicsView', 'Zoom in (+)')) self._translate('PliantQGraphicsView', 'Zoom in (+)'))
@@ -297,6 +333,12 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView):
action = contextMenu.exec_(self.sender().mapToGlobal(position)) action = contextMenu.exec_(self.sender().mapToGlobal(position))
if action == singlePageAction:
self.main_window.bookToolBar.singlePageButton.trigger()
if action == doublePageAction:
self.main_window.bookToolBar.doublePageButton.trigger()
if action == saveAction: if action == saveAction:
dialog_prompt = self._translate('Main_UI', 'Save page as...') dialog_prompt = self._translate('Main_UI', 'Save page as...')
extension_string = self._translate('Main_UI', 'Images') extension_string = self._translate('Main_UI', 'Images')
@@ -647,8 +689,22 @@ class PliantWidgetsCommonFunctions:
if (current_toc_index < max_toc_index and direction == 1) or ( if (current_toc_index < max_toc_index and direction == 1) or (
current_toc_index > 0 and direction == -1): current_toc_index > 0 and direction == -1):
# Special cases for double page view
def get_modifier():
if (self.main_window.settings['page_view_button'] == 'singlePageButton'
or not self.are_we_doing_images_only):
return 0
if (current_toc_index == 0
or current_toc_index % 2 == 0
or current_toc_index == max_toc_index):
return 0
if current_toc_index % 2 == 1:
return direction
self.main_window.bookToolBar.tocBox.setCurrentIndex( self.main_window.bookToolBar.tocBox.setCurrentIndex(
current_toc_index + direction) current_toc_index + direction + get_modifier())
# Set page position depending on if the chapter number is increasing or decreasing # Set page position depending on if the chapter number is increasing or decreasing
if direction == 1 or was_button_pressed: if direction == 1 or was_button_pressed:

View File

@@ -68,26 +68,6 @@ class BookToolBar(QtWidgets.QToolBar):
self._translate('BookToolBar', 'Reset profile'), self._translate('BookToolBar', 'Reset profile'),
self) self)
# Single and double page view buttons
self.singlePageButton = QtWidgets.QAction(
image_factory.get_image('page-single'),
self._translate('BookToolBar', 'View as single page'),
self)
self.singlePageButton.setObjectName('singlePageButton')
self.singlePageButton.setCheckable(True)
self.doublePageButton = QtWidgets.QAction(
image_factory.get_image('page-double'),
self._translate('BookToolBar', 'View as double page'),
self)
self.doublePageButton.setObjectName('doublePageButton')
self.doublePageButton.setCheckable(True)
self.pageViewButtons = QtWidgets.QActionGroup(self)
self.pageViewButtons.setExclusive(True)
self.pageViewButtons.addAction(self.singlePageButton)
self.pageViewButtons.addAction(self.doublePageButton)
# Add buttons # Add buttons
self.addAction(self.fontButton) self.addAction(self.fontButton)
self.fontButton.setCheckable(True) self.fontButton.setCheckable(True)
@@ -100,9 +80,6 @@ class BookToolBar(QtWidgets.QToolBar):
self.addAction(self.bookmarkButton) self.addAction(self.bookmarkButton)
self.bookmarkButton.setCheckable(True) self.bookmarkButton.setCheckable(True)
self.addSeparator() self.addSeparator()
self.addAction(self.singlePageButton)
self.addAction(self.doublePageButton)
self.addSeparator()
self.addAction(self.distractionFreeButton) self.addAction(self.distractionFreeButton)
self.addAction(self.fullscreenButton) self.addAction(self.fullscreenButton)
@@ -230,6 +207,26 @@ class BookToolBar(QtWidgets.QToolBar):
i.setVisible(False) i.setVisible(False)
# Comic view modification # Comic view modification
# Single and double page view buttons
self.singlePageButton = QtWidgets.QAction(
image_factory.get_image('page-single'),
self._translate('BookToolBar', 'View as single page'),
self)
self.singlePageButton.setObjectName('singlePageButton')
self.singlePageButton.setCheckable(True)
self.doublePageButton = QtWidgets.QAction(
image_factory.get_image('page-double'),
self._translate('BookToolBar', 'View as double page'),
self)
self.doublePageButton.setObjectName('doublePageButton')
self.doublePageButton.setCheckable(True)
self.pageViewButtons = QtWidgets.QActionGroup(self)
self.pageViewButtons.setExclusive(True)
self.pageViewButtons.addAction(self.singlePageButton)
self.pageViewButtons.addAction(self.doublePageButton)
self.zoomIn = QtWidgets.QAction( self.zoomIn = QtWidgets.QAction(
image_factory.get_image('zoom-in'), image_factory.get_image('zoom-in'),
self._translate('BookToolBar', 'Zoom in'), self._translate('BookToolBar', 'Zoom in'),
@@ -265,15 +262,20 @@ class BookToolBar(QtWidgets.QToolBar):
self.comicBGColor.setObjectName('comicBGColor') self.comicBGColor.setObjectName('comicBGColor')
self.comicSeparator1 = self.addSeparator() self.comicSeparator1 = self.addSeparator()
self.addAction(self.singlePageButton)
self.addAction(self.doublePageButton)
self.comicSeparator2 = self.addSeparator()
self.addAction(self.zoomIn) self.addAction(self.zoomIn)
self.addAction(self.zoomOut) self.addAction(self.zoomOut)
self.addAction(self.fitWidth) self.addAction(self.fitWidth)
self.addAction(self.bestFit) self.addAction(self.bestFit)
self.addAction(self.originalSize) self.addAction(self.originalSize)
self.comicSeparator2 = self.addSeparator() self.comicSeparator3 = self.addSeparator()
self.comicBGColorAction = self.addWidget(self.comicBGColor) self.comicBGColorAction = self.addWidget(self.comicBGColor)
self.comicActions = [ self.comicActions = [
self.singlePageButton,
self.doublePageButton,
self.comicBGColorAction, self.comicBGColorAction,
self.zoomIn, self.zoomIn,
self.zoomOut, self.zoomOut,
@@ -281,7 +283,8 @@ class BookToolBar(QtWidgets.QToolBar):
self.bestFit, self.bestFit,
self.originalSize, self.originalSize,
self.comicSeparator1, self.comicSeparator1,
self.comicSeparator2] self.comicSeparator2,
self.comicSeparator3]
for i in self.comicActions: for i in self.comicActions:
i.setVisible(False) i.setVisible(False)