--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/eric7/PdfViewer/PdfZoomSelector.py Sat Jan 14 18:25:26 2023 +0100 @@ -0,0 +1,105 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a widget to select a PDF zoom factor. +""" + +from PyQt6.QtCore import pyqtSignal, pyqtSlot, Qt +from PyQt6.QtPdfWidgets import QPdfView +from PyQt6.QtWidgets import QComboBox + + +class PdfZoomSelector(QComboBox): + """ + Class implementing a widget to select a PDF zoom factor. + + @signal zoomFactorChanged(factor) emitted to indicate the selected zoom factor + @signal zoomModeChanged(zoomMode) emitted to indicate the selected zoom mode + """ + zoomFactorChanged = pyqtSignal(float) + zoomModeChanged = pyqtSignal(QPdfView.ZoomMode) + + def __init__(self, parent=None): + """ + Constructor + + @param parent reference to the parent widget (defaults to None) + @type QWidget (optional) + """ + super().__init__(parent) + + self.setEditable(True) + self.setInsertPolicy(QComboBox.InsertPolicy.NoInsert) + + self.addItem(self.tr("Page Width")) + self.addItem(self.tr("Whole Page")) + self.addItem(self.tr("12%")) + self.addItem(self.tr("25%")) + self.addItem(self.tr("33%")) + self.addItem(self.tr("50%")) + self.addItem(self.tr("66%")) + self.addItem(self.tr("75%")) + self.addItem(self.tr("100%")) + self.addItem(self.tr("125%")) + self.addItem(self.tr("150%")) + self.addItem(self.tr("200%")) + self.addItem(self.tr("400%")) + self.addItem(self.tr("800%")) + self.addItem(self.tr("1600%")) + self.addItem(self.tr("2500%")) + self.addItem(self.tr("5000%")) + + self.lineEdit().setAlignment(Qt.AlignmentFlag.AlignCenter) + + self.currentTextChanged.connect(self.__processText) + self.lineEdit().editingFinished.connect(self.__editingFinished) + + @pyqtSlot() + def __editingFinished(self): + """ + Private slot handling the end of entering a zoom factor. + """ + self.__processText(self.lineEdit().text()) + + @pyqtSlot(str) + def __processText(self, text): + """ + Private slot to handle the change of the entered zoom factor. + + @param text text to be handled + @type str + """ + if text == self.tr("Page Width"): + self.zoomModeChanged.emit(QPdfView.ZoomMode.FitToWidth) + elif text == self.tr("Whole Page"): + self.zoomModeChanged.emit(QPdfView.ZoomMode.FitInView) + else: + withoutPercent = text.replace("%", "") + try: + zoomLevel = int(withoutPercent) + factor = zoomLevel / 100.0 + except ValueError: + factor = 1.0 + + self.zoomModeChanged.emit(QPdfView.ZoomMode.Custom) + self.zoomFactorChanged.emit(factor) + + @pyqtSlot() + def reset(self): + """ + Public slot to reset the zoom factor to 100%. + """ + self.setCurrentIndex(8) # index 8 is 100% + + @pyqtSlot(float) + def setZoomFactor(self, zoomFactor): + """ + Public slot to set the current zoom factor. + + @param zoomFactor current zoom factor + @type float + """ + self.setCurrentText(self.tr("{0}%").format(round(zoomFactor * 100)))