9 |
9 |
10 import contextlib |
10 import contextlib |
11 import os |
11 import os |
12 import pathlib |
12 import pathlib |
13 |
13 |
14 from PyQt6.QtCore import QPointF, QSize, Qt, pyqtSignal, pyqtSlot |
14 from PyQt6.QtCore import QBuffer, QByteArray, QIODevice, QPointF, QSize, Qt, pyqtSignal, pyqtSlot |
15 from PyQt6.QtGui import QAction, QActionGroup, QClipboard, QGuiApplication, QKeySequence |
15 from PyQt6.QtGui import QAction, QActionGroup, QClipboard, QGuiApplication, QKeySequence |
16 from PyQt6.QtPdf import QPdfDocument, QPdfLink |
16 from PyQt6.QtPdf import QPdfDocument, QPdfLink |
17 from PyQt6.QtPdfWidgets import QPdfView |
17 from PyQt6.QtPdfWidgets import QPdfView |
18 from PyQt6.QtWidgets import ( |
18 from PyQt6.QtWidgets import ( |
19 QDialog, |
19 QDialog, |
28 |
28 |
29 from eric7 import Preferences |
29 from eric7 import Preferences |
30 from eric7.EricGui import EricPixmapCache |
30 from eric7.EricGui import EricPixmapCache |
31 from eric7.EricGui.EricAction import EricAction |
31 from eric7.EricGui.EricAction import EricAction |
32 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
32 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
|
33 from eric7.EricWidgets.EricApplication import ericApp |
33 from eric7.EricWidgets.EricMainWindow import EricMainWindow |
34 from eric7.EricWidgets.EricMainWindow import EricMainWindow |
34 from eric7.EricWidgets.EricStretchableSpacer import EricStretchableSpacer |
35 from eric7.EricWidgets.EricStretchableSpacer import EricStretchableSpacer |
35 from eric7.Globals import recentNamePdfFiles |
36 from eric7.Globals import recentNamePdfFiles |
|
37 from eric7.RemoteServerInterface import EricServerFileDialog |
36 from eric7.SystemUtilities import FileSystemUtilities, OSUtilities |
38 from eric7.SystemUtilities import FileSystemUtilities, OSUtilities |
37 |
39 |
38 from .PdfInfoWidget import PdfInfoWidget |
40 from .PdfInfoWidget import PdfInfoWidget |
39 from .PdfPageSelector import PdfPageSelector |
41 from .PdfPageSelector import PdfPageSelector |
40 from .PdfSearchWidget import PdfSearchWidget |
42 from .PdfSearchWidget import PdfSearchWidget |
76 self.__fromEric = fromEric |
78 self.__fromEric = fromEric |
77 self.setWindowIcon(EricPixmapCache.getIcon("ericPdf")) |
79 self.setWindowIcon(EricPixmapCache.getIcon("ericPdf")) |
78 |
80 |
79 if not self.__fromEric: |
81 if not self.__fromEric: |
80 self.setStyle(Preferences.getUI("Style"), Preferences.getUI("StyleSheet")) |
82 self.setStyle(Preferences.getUI("Style"), Preferences.getUI("StyleSheet")) |
|
83 |
|
84 self.__remotefsInterface =( |
|
85 ericApp().getObject("EricServer").getServiceInterface("FileSystem") |
|
86 if self.__fromEric |
|
87 else None |
|
88 ) |
81 |
89 |
82 self.__pdfDocument = QPdfDocument(self) |
90 self.__pdfDocument = QPdfDocument(self) |
83 |
91 |
84 self.__cw = QSplitter(Qt.Orientation.Horizontal, self) |
92 self.__cw = QSplitter(Qt.Orientation.Horizontal, self) |
85 self.__cw.setChildrenCollapsible(False) |
93 self.__cw.setChildrenCollapsible(False) |
959 @param fileName path of the PDF file to load |
967 @param fileName path of the PDF file to load |
960 @type str |
968 @type str |
961 """ |
969 """ |
962 canceled = False |
970 canceled = False |
963 err = QPdfDocument.Error.IncorrectPassword |
971 err = QPdfDocument.Error.IncorrectPassword |
|
972 |
|
973 if FileSystemUtilities.isRemoteFileName(fileName): |
|
974 try: |
|
975 data = QByteArray(self.__remotefsInterface.readFile(fileName)) |
|
976 buffer = QBuffer(data) |
|
977 except OSError as err: |
|
978 EricMessageBox.warning( |
|
979 self, |
|
980 self.tr("OpenPDF File"), |
|
981 self.tr( |
|
982 "<p>The PDF file <b>{0}</b> could not be read.</p>" |
|
983 "<p>Reason: {1}</p>" |
|
984 ).format(fileName, str(err)), |
|
985 ) |
|
986 return |
|
987 else: |
|
988 buffer = None |
|
989 |
964 while not canceled and err == QPdfDocument.Error.IncorrectPassword: |
990 while not canceled and err == QPdfDocument.Error.IncorrectPassword: |
965 err = self.__pdfDocument.load(fileName) |
991 if FileSystemUtilities.isRemoteFileName(fileName): |
|
992 buffer.open(QIODevice.OpenModeFlag.ReadOnly) |
|
993 self.__pdfDocument.load(buffer) |
|
994 err = QPdfDocument.Error.None_ |
|
995 else: |
|
996 err = self.__pdfDocument.load(fileName) |
966 if err == QPdfDocument.Error.IncorrectPassword: |
997 if err == QPdfDocument.Error.IncorrectPassword: |
967 password, ok = QInputDialog.getText( |
998 password, ok = QInputDialog.getText( |
968 self, |
999 self, |
969 self.tr("Open PDF File"), |
1000 self.tr("Open PDF File"), |
970 self.tr("Enter password to show the document:"), |
1001 self.tr("Enter password to show the document:"), |
984 ).format(fileName, self.__getErrorString(err)), |
1015 ).format(fileName, self.__getErrorString(err)), |
985 ) |
1016 ) |
986 self.__documentInfoWidget.setFileName("") |
1017 self.__documentInfoWidget.setFileName("") |
987 return |
1018 return |
988 |
1019 |
989 self.__lastOpenPath = os.path.dirname(fileName) |
1020 self.__lastOpenPath = ( |
|
1021 self.__remotefsInterface.dirname(fileName) |
|
1022 if FileSystemUtilities.isRemoteFileName(fileName) |
|
1023 else os.path.dirname(fileName) |
|
1024 ) |
990 self.__setCurrentFile(fileName) |
1025 self.__setCurrentFile(fileName) |
991 |
1026 |
992 documentTitle = self.__pdfDocument.metaData(QPdfDocument.MetaDataField.Title) |
1027 documentTitle = self.__pdfDocument.metaData(QPdfDocument.MetaDataField.Title) |
993 self.__setViewerTitle(documentTitle) |
1028 self.__setViewerTitle(documentTitle) |
994 |
1029 |
1017 and self.__project is not None |
1052 and self.__project is not None |
1018 and self.__project.isOpen() |
1053 and self.__project.isOpen() |
1019 ): |
1054 ): |
1020 self.__lastOpenPath = self.__project.getProjectPath() |
1055 self.__lastOpenPath = self.__project.getProjectPath() |
1021 |
1056 |
1022 fileName = EricFileDialog.getOpenFileName( |
1057 fileName = ( |
1023 self, |
1058 EricServerFileDialog.getOpenFileName( |
1024 self.tr("Open PDF File"), |
1059 self, |
1025 self.__lastOpenPath, |
1060 self.tr("Open PDF File"), |
1026 self.tr("PDF Files (*.pdf);;All Files (*)"), |
1061 self.__lastOpenPath, |
|
1062 self.tr("PDF Files (*.pdf);;All Files (*)"), |
|
1063 ) |
|
1064 if FileSystemUtilities.isRemoteFileName(self.__lastOpenPath) |
|
1065 else EricFileDialog.getOpenFileName( |
|
1066 self, |
|
1067 self.tr("Open PDF File"), |
|
1068 self.__lastOpenPath, |
|
1069 self.tr("PDF Files (*.pdf);;All Files (*)"), |
|
1070 ) |
1027 ) |
1071 ) |
1028 if fileName: |
1072 if fileName: |
1029 self.__loadPdfFile(fileName) |
1073 self.__loadPdfFile(fileName) |
1030 |
1074 |
1031 @pyqtSlot() |
1075 @pyqtSlot() |
1042 and self.__project.isOpen() |
1086 and self.__project.isOpen() |
1043 ): |
1087 ): |
1044 self.__lastOpenPath = self.__project.getProjectPath() |
1088 self.__lastOpenPath = self.__project.getProjectPath() |
1045 |
1089 |
1046 if not fileName: |
1090 if not fileName: |
1047 fileName = EricFileDialog.getOpenFileName( |
1091 fileName = ( |
1048 self, |
1092 EricServerFileDialog.getOpenFileName( |
1049 self.tr("Open PDF File"), |
1093 self, |
1050 self.__lastOpenPath, |
1094 self.tr("Open PDF File"), |
1051 self.tr("PDF Files (*.pdf);;All Files (*)"), |
1095 self.__lastOpenPath, |
|
1096 self.tr("PDF Files (*.pdf);;All Files (*)"), |
|
1097 ) |
|
1098 if FileSystemUtilities.isRemoteFileName(self.__lastOpenPath) |
|
1099 else EricFileDialog.getOpenFileName( |
|
1100 self, |
|
1101 self.tr("Open PDF File"), |
|
1102 self.__lastOpenPath, |
|
1103 self.tr("PDF Files (*.pdf);;All Files (*)"), |
|
1104 ) |
1052 ) |
1105 ) |
1053 if fileName: |
1106 if fileName: |
1054 viewer = PdfViewerWindow( |
1107 viewer = PdfViewerWindow( |
1055 fileName=fileName, |
1108 fileName=fileName, |
1056 parent=self.parent(), |
1109 parent=self.parent(), |