diff --git a/__main__.py b/__main__.py index a99ab24..28f3e32 100755 --- a/__main__.py +++ b/__main__.py @@ -90,6 +90,9 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.thread = None # Background Thread self.viewModel = None self.current_contentView = None # For fullscreening purposes + self.display_profiles = None + self.current_profile_index = None + # self.comic_profile = None # Initialize application Settings(self).read_settings() # This should populate all variables that need @@ -136,6 +139,13 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.bookToolBar.paddingDown.triggered.connect(self.modify_font) self.bookToolBar.resetProfile.triggered.connect(self.reset_profile) + self.bookToolBar.zoomIn.triggered.connect(self.modify_comic_view) + self.bookToolBar.zoomOut.triggered.connect(self.modify_comic_view) + self.bookToolBar.fitWidth.triggered.connect(self.modify_comic_view) + self.bookToolBar.bestFit.triggered.connect(self.modify_comic_view) + self.bookToolBar.originalSize.triggered.connect(self.modify_comic_view) + self.bookToolBar.comicBGColor.clicked.connect(self.get_color) + self.bookToolBar.colorBoxFG.clicked.connect(self.get_color) self.bookToolBar.colorBoxBG.clicked.connect(self.get_color) self.bookToolBar.tocBox.currentIndexChanged.connect(self.set_toc_position) @@ -336,13 +346,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): def set_fullscreen(self): current_tab = self.tabWidget.currentIndex() current_tab_widget = self.tabWidget.widget(current_tab) - self.current_contentView = current_tab_widget.findChildren( - (QtWidgets.QTextBrowser, QtWidgets.QGraphicsView))[0] - - self.current_contentView.setWindowFlags(QtCore.Qt.Window) - self.current_contentView.setWindowState(QtCore.Qt.WindowFullScreen) - self.current_contentView.show() - self.hide() + current_tab_widget.go_fullscreen() def list_doubleclick(self, myindex): index = self.listView.model().index(myindex.row(), 0) @@ -366,8 +370,13 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): if not file_paths: return + print('Attempting to open: ' + ', '.join(file_paths)) + contents = sorter.BookSorter( - file_paths, 'reading', self.database_path, self.temp_dir.path()).initiate_threads() + file_paths, + 'reading', + self.database_path, + self.temp_dir.path()).initiate_threads() found_a_focusable_tab = False @@ -405,6 +414,11 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): self.bookToolBar.colorBoxBG.setStyleSheet( 'background-color: %s' % color_name) current_profile['background'] = color_name + + elif signal_sender == 'comicBGColor': + self.bookToolBar.comicBGColor.setStyleSheet( + 'background-color: %s' % color_name) + self.comic_profile['background'] = new_color self.bookToolBar.profileBox.setItemData( profile_index, current_profile, QtCore.Qt.UserRole) @@ -441,41 +455,90 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow): profile_index, current_profile, QtCore.Qt.UserRole) self.format_contentView() + def modify_comic_view(self): + signal_sender = self.sender().objectName() + current_tab = self.tabWidget.widget(self.tabWidget.currentIndex()) + + self.bookToolBar.fitWidth.setChecked(False) + self.bookToolBar.bestFit.setChecked(False) + self.bookToolBar.originalSize.setChecked(False) + + if signal_sender == 'zoomOut': + self.comic_profile['zoom_mode'] = 'manualZoom' + self.comic_profile['padding'] += 50 + if self.comic_profile['padding'] * 2 > current_tab.contentView.viewport().width(): + self.comic_profile['padding'] -= 50 + + if signal_sender == 'zoomIn': + self.comic_profile['zoom_mode'] = 'manualZoom' + self.comic_profile['padding'] -= 50 + if self.comic_profile['padding'] < 0: + self.comic_profile['padding'] = 0 + + if signal_sender == 'fitWidth': + self.comic_profile['zoom_mode'] = 'fitWidth' + self.comic_profile['padding'] = 0 + self.bookToolBar.fitWidth.setChecked(True) + + if signal_sender == 'bestFit': + self.comic_profile['zoom_mode'] = 'bestFit' + self.bookToolBar.bestFit.setChecked(True) + + if signal_sender == 'originalSize': + self.comic_profile['zoom_mode'] = 'originalSize' + self.bookToolBar.originalSize.setChecked(True) + + self.format_contentView() + + def format_contentView(self): # TODO # Implement line spacing # See what happens if a font isn't installed - profile_index = self.bookToolBar.profileBox.currentIndex() - current_profile = self.bookToolBar.profileBox.itemData( - profile_index, QtCore.Qt.UserRole) + current_tab = self.tabWidget.widget(self.tabWidget.currentIndex()) - font = current_profile['font'] - foreground = current_profile['foreground'] - background = current_profile['background'] - padding = current_profile['padding'] - font_size = current_profile['font_size'] - - # Change toolbar widgets to match new settings - self.bookToolBar.fontBox.blockSignals(True) - self.bookToolBar.fontSizeBox.blockSignals(True) - self.bookToolBar.fontBox.setCurrentText(font) - self.bookToolBar.fontSizeBox.setCurrentText(str(font_size)) - self.bookToolBar.fontBox.blockSignals(False) - self.bookToolBar.fontSizeBox.blockSignals(False) - - self.bookToolBar.colorBoxFG.setStyleSheet( - 'background-color: %s' % foreground) - self.bookToolBar.colorBoxBG.setStyleSheet( - 'background-color: %s' % background) - - # Do not run when only the library tab is open - if self.tabWidget.count() == 1: + try: + current_metadata = current_tab.metadata + except AttributeError: return - # Change contentView to match new settings - current_tab = self.tabWidget.widget(self.tabWidget.currentIndex()) - current_tab.format_view(font, font_size, foreground, background, padding) + if current_metadata['images_only']: + background = self.comic_profile['background'] + padding = self.comic_profile['padding'] + + self.bookToolBar.comicBGColor.setStyleSheet( + 'background-color: %s' % background.name()) + + current_tab.format_view( + None, None, None, background, padding) + + else: + profile_index = self.bookToolBar.profileBox.currentIndex() + current_profile = self.bookToolBar.profileBox.itemData( + profile_index, QtCore.Qt.UserRole) + + font = current_profile['font'] + foreground = current_profile['foreground'] + background = current_profile['background'] + padding = current_profile['padding'] + font_size = current_profile['font_size'] + + # Change toolbar widgets to match new settings + self.bookToolBar.fontBox.blockSignals(True) + self.bookToolBar.fontSizeBox.blockSignals(True) + self.bookToolBar.fontBox.setCurrentText(font) + self.bookToolBar.fontSizeBox.setCurrentText(str(font_size)) + self.bookToolBar.fontBox.blockSignals(False) + self.bookToolBar.fontSizeBox.blockSignals(False) + + self.bookToolBar.colorBoxFG.setStyleSheet( + 'background-color: %s' % foreground) + self.bookToolBar.colorBoxBG.setStyleSheet( + 'background-color: %s' % background) + + current_tab.format_view( + font, font_size, foreground, background, padding) def reset_profile(self): current_profile_index = self.bookToolBar.profileBox.currentIndex() diff --git a/parsers/cbr.py b/parsers/cbr.py index 2b4e246..db34886 100644 --- a/parsers/cbr.py +++ b/parsers/cbr.py @@ -73,8 +73,9 @@ class ParseCBR: for i in os.walk(extract_path): if i[2]: # Implies files were found image_dir = i[0] - found_images = i[2] - break + add_path_to_file = [ + os.path.join(image_dir, j) for j in i[2]] + found_images.extend(add_path_to_file) if not found_images: print('Found nothing in ' + self.filename) @@ -84,7 +85,7 @@ class ParseCBR: for count, i in enumerate(found_images): page_name = 'Page ' + str(count + 1) - image_path = os.path.join(extract_path, image_dir, i) + image_path = os.path.join(extract_path, i) contents[page_name] = image_path diff --git a/parsers/cbz.py b/parsers/cbz.py index ff19142..1e66e87 100644 --- a/parsers/cbz.py +++ b/parsers/cbz.py @@ -76,8 +76,9 @@ class ParseCBZ: for i in os.walk(extract_path): if i[2]: # Implies files were found image_dir = i[0] - found_images = i[2] - break + add_path_to_file = [ + os.path.join(image_dir, j) for j in i[2]] + found_images.extend(add_path_to_file) if not found_images: print('Found nothing in ' + self.filename) @@ -87,7 +88,7 @@ class ParseCBZ: for count, i in enumerate(found_images): page_name = 'Page ' + str(count + 1) - image_path = os.path.join(extract_path, image_dir, i) + image_path = os.path.join(extract_path, i) contents[page_name] = image_path diff --git a/settings.py b/settings.py index 2733f8f..777a28d 100644 --- a/settings.py +++ b/settings.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import os -from PyQt5 import QtCore +from PyQt5 import QtCore, QtGui class Settings: @@ -36,6 +36,11 @@ class Settings: self.default_profiles = [ default_profile1, default_profile2, default_profile3] + self.default_comic_profile = { + 'padding': 100, # pixel padding on either size + 'background': QtGui.QColor().fromRgb(0, 0, 0), + 'zoom_mode': 'bestFit'} + def read_settings(self): self.settings.beginGroup('mainWindow') self.parent_window.resize(self.settings.value( @@ -56,6 +61,8 @@ class Settings: 'displayProfiles', self.default_profiles) self.parent_window.current_profile_index = int(self.settings.value( 'currentProfileIndex', 0)) + self.parent_window.comic_profile = self.settings.value( + 'comicProfile', self.default_comic_profile) self.settings.endGroup() self.settings.beginGroup('lastOpen') @@ -86,6 +93,7 @@ class Settings: current_profile2, current_profile3]) self.settings.setValue('currentProfileIndex', current_profile_index) + self.settings.setValue('comicProfile', self.parent_window.comic_profile) self.settings.endGroup() current_tab_index = self.parent_window.tabWidget.currentIndex() diff --git a/widgets.py b/widgets.py index e716239..857238b 100644 --- a/widgets.py +++ b/widgets.py @@ -127,26 +127,31 @@ class BookToolBar(QtWidgets.QToolBar): self.zoomIn = QtWidgets.QAction( QtGui.QIcon.fromTheme('zoom-in'), 'Zoom in', self) + self.zoomIn.setObjectName('zoomIn') self.zoomOut = QtWidgets.QAction( QtGui.QIcon.fromTheme('zoom-out'), 'Zoom Out', self) + self.zoomOut.setObjectName('zoomOut') self.fitWidth = QtWidgets.QAction( QtGui.QIcon.fromTheme('zoom-fit-width'), 'Fit Width', self) + self.fitWidth.setObjectName('fitWidth') self.fitWidth.setCheckable(True) self.bestFit = QtWidgets.QAction( QtGui.QIcon.fromTheme('zoom-fit-best'), 'Best Fit', self) + self.bestFit.setObjectName('bestFit') self.bestFit.setCheckable(True) self.originalSize = QtWidgets.QAction( QtGui.QIcon.fromTheme('zoom-original'), 'Original size', self) + self.originalSize.setObjectName('originalSize') self.originalSize.setCheckable(True) - self.comicColorBG = FixedPushButton(self) - self.comicColorBG.setToolTip('Background color') - self.comicColorBG.setObjectName('comicColorBG') + self.comicBGColor = FixedPushButton(self) + self.comicBGColor.setToolTip('Background color') + self.comicBGColor.setObjectName('comicBGColor') self.comicSeparator1 = self.addSeparator() self.addAction(self.zoomIn) @@ -155,7 +160,7 @@ class BookToolBar(QtWidgets.QToolBar): self.addAction(self.bestFit) self.addAction(self.originalSize) self.comicSeparator2 = self.addSeparator() - self.comicBGColorAction = self.addWidget(self.comicColorBG) + self.comicBGColorAction = self.addWidget(self.comicBGColor) self.comicActions = [ self.comicBGColorAction, @@ -211,6 +216,10 @@ class BookToolBar(QtWidgets.QToolBar): def customize_view_on(self): if self.parent().tabWidget.widget( self.parent().tabWidget.currentIndex()).metadata['images_only']: + + # The following might seem redundant, + # but it's necessary for tab switching + for i in self.comicActions: i.setVisible(True) @@ -354,7 +363,6 @@ class Tab(QtWidgets.QWidget): if self.are_we_doing_images_only: # Boolean self.contentView = PliantQGraphicsView(self.window()) self.contentView.loadImage(chapter_content) - self.setStyleSheet("background-color: black;") else: self.contentView = PliantQTextBrowser(self.window()) @@ -408,7 +416,7 @@ class Tab(QtWidgets.QWidget): self.go_fs = QtWidgets.QShortcut( QtGui.QKeySequence('F11'), self.contentView) - self.go_fs.activated.connect(self.window().set_fullscreen) + self.go_fs.activated.connect(self.go_fullscreen) self.exit_fs = QtWidgets.QShortcut( QtGui.QKeySequence('Escape'), self.contentView) @@ -419,6 +427,16 @@ class Tab(QtWidgets.QWidget): # QtGui.QKeySequence('Ctrl+Q'), self.contentView) # self.exit_all.activated.connect(self.sneaky_exit) + def go_fullscreen(self): + if self.contentView.windowState() == QtCore.Qt.WindowFullScreen: + self.exit_fullscreen() + return + + self.contentView.setWindowFlags(QtCore.Qt.Window) + self.contentView.setWindowState(QtCore.Qt.WindowFullScreen) + self.contentView.show() + self.window().hide() + def exit_fullscreen(self): self.window().show() self.contentView.setWindowFlags(QtCore.Qt.Widget) @@ -436,12 +454,17 @@ class Tab(QtWidgets.QWidget): self.contentView.setHtml(required_content) def format_view(self, font, font_size, foreground, background, padding): - self.contentView.setViewportMargins(padding, 0, padding, 0) - if self.are_we_doing_images_only: - self.contentView.setBackgroundBrush( - QtGui.QBrush(QtCore.Qt.black, QtCore.Qt.SolidPattern)) + # Tab color does not need to be set separately in case + # no padding is set for the viewport of a QGraphicsView + # and image resizing in done in the pixmap + my_qbrush = QtGui.QBrush(QtCore.Qt.SolidPattern) + my_qbrush.setColor(background) + self.contentView.setBackgroundBrush(my_qbrush) + self.contentView.resizeEvent() + else: + self.contentView.setViewportMargins(padding, 0, padding, 0) self.contentView.setStyleSheet( "QTextEdit {{font-family: {0}; font-size: {1}px; color: {2}; background-color: {3}}}".format( font, font_size, foreground, background)) @@ -478,19 +501,30 @@ class PliantQGraphicsView(QtWidgets.QGraphicsView): if not self.image_pixmap: return - profile_index = self.main_window.bookToolBar.profileBox.currentIndex() - current_profile = self.main_window.bookToolBar.profileBox.itemData( - profile_index, QtCore.Qt.UserRole) - padding = current_profile['padding'] + zoom_mode = self.main_window.comic_profile['zoom_mode'] + padding = self.main_window.comic_profile['padding'] - available_width = self.viewport().width() - 2 * padding - - if self.image_pixmap.width() > available_width: + if zoom_mode == 'fitWidth': + available_width = self.viewport().width() image_pixmap = self.image_pixmap.scaledToWidth( available_width, QtCore.Qt.SmoothTransformation) - else: + + elif zoom_mode == 'originalSize': image_pixmap = self.image_pixmap + elif zoom_mode == 'bestFit': + available_width = self.viewport().width() + available_height = self.viewport().height() + + image_pixmap = self.image_pixmap.scaled( + available_width, available_height, + QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation) + + elif zoom_mode == 'manualZoom': + available_width = self.viewport().width() - 2 * padding + image_pixmap = self.image_pixmap.scaledToWidth( + available_width, QtCore.Qt.SmoothTransformation) + graphics_scene = QtWidgets.QGraphicsScene() graphics_scene.addPixmap(image_pixmap) @@ -689,7 +723,10 @@ class BackGroundBookAddition(QtCore.QThread): self.database_path = database_path def run(self): - books = sorter.BookSorter(self.file_list, 'addition', self.database_path) + books = sorter.BookSorter( + self.file_list, + 'addition', + self.database_path) parsed_books = books.initiate_threads() database.DatabaseFunctions(self.database_path).add_to_database(parsed_books) self.parent_window.lib_ref.generate_model('addition', parsed_books)