Fixed a few issues related to printing in the new web browser.

Tue, 27 Sep 2016 19:41:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 27 Sep 2016 19:41:11 +0200
changeset 5181
1948b27d7b21
parent 5176
30547e9a19e3
child 5182
e2782c9a43d4

Fixed a few issues related to printing in the new web browser.

WebBrowser/Tools/PrintToPdfDialog.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserTabWidget.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/Tools/PrintToPdfDialog.py	Sat Sep 24 18:00:30 2016 +0200
+++ b/WebBrowser/Tools/PrintToPdfDialog.py	Tue Sep 27 19:41:11 2016 +0200
@@ -9,7 +9,9 @@
 
 from __future__ import unicode_literals
 
-from PyQt5.QtCore import pyqtSlot, QMarginsF
+import os
+
+from PyQt5.QtCore import pyqtSlot, QMarginsF, QStandardPaths
 from PyQt5.QtGui import QPageLayout, QPageSize
 from PyQt5.QtPrintSupport import QPrinter, QPageSetupDialog
 from PyQt5.QtWidgets import QDialog
@@ -40,6 +42,13 @@
         self.pdfFilePicker.setFilters(self.tr(
             "PDF Files (*.pdf);;"
             "All Files (*)"))
+        if not os.path.isabs(filePath):
+            documentsPath = QStandardPaths.writableLocation(
+                QStandardPaths.DocumentsLocation)
+            if documentsPath:
+                filePath = os.path.join(documentsPath, filePath)
+            else:
+                filePath = os.path.abspath(filePath)
         self.pdfFilePicker.setText(filePath, toNative=True)
         
         self.__currentPageLayout = QPageLayout(
--- a/WebBrowser/WebBrowserTabWidget.py	Sat Sep 24 18:00:30 2016 +0200
+++ b/WebBrowser/WebBrowserTabWidget.py	Tue Sep 27 19:41:11 2016 +0200
@@ -11,7 +11,7 @@
 
 import os
 
-from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QFile
+from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QFile, qVersion
 from PyQt5.QtGui import QIcon, QPixmap, QPainter
 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QMenu, QToolButton, QDialog
 from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
@@ -179,13 +179,15 @@
         self.__tabContextMenu.addAction(
             self.tr('Close All'), self.closeAllBrowsers)
         self.__tabContextMenu.addSeparator()
-        self.__tabContextMenu.addAction(
-            UI.PixmapCache.getIcon("printPreview.png"),
-            self.tr('Print Preview'), self.__tabContextMenuPrintPreview)
-        self.__tabContextMenu.addAction(
-            UI.PixmapCache.getIcon("print.png"),
-            self.tr('Print'), self.__tabContextMenuPrint)
-        if Globals.isLinuxPlatform():
+        if not Globals.isWindowsPlatform():
+            # TODO: implement printing based on printToPdf()
+            self.__tabContextMenu.addAction(
+                UI.PixmapCache.getIcon("printPreview.png"),
+                self.tr('Print Preview'), self.__tabContextMenuPrintPreview)
+            self.__tabContextMenu.addAction(
+                UI.PixmapCache.getIcon("print.png"),
+                self.tr('Print'), self.__tabContextMenuPrint)
+        if Globals.isLinuxPlatform() or qVersion() >= "5.7.0":
             self.__tabContextMenu.addAction(
                 UI.PixmapCache.getIcon("printPdf.png"),
                 self.tr('Print as PDF'), self.__tabContextMenuPrintPdf)
@@ -645,14 +647,27 @@
             from .Tools.PrintToPdfDialog import PrintToPdfDialog
             if not name:
                 name = "printout.pdf"
-            dlg = PrintToPdfDialog(os.path.abspath(name), self)
+            dlg = PrintToPdfDialog(name, self)
             if dlg.exec_() == QDialog.Accepted:
                 filePath, pageLayout = dlg.getData()
                 if filePath:
+                    if os.path.exists(filePath):
+                        res = E5MessageBox.warning(
+                            self,
+                            self.tr("Print to PDF"),
+                            self.tr("""<p>The file <b>{0}</b> exists"""
+                                    """ already. Shall it be"""
+                                    """ overwritten?</p>""").format(filePath),
+                            E5MessageBox.StandardButtons(
+                                E5MessageBox.No |
+                                E5MessageBox.Yes),
+                            E5MessageBox.No)
+                        if res == E5MessageBox.No:
+                            return
                     browser.page().printToPdf(
                         lambda p: self.__pdfGenerated(filePath, p),
                         pageLayout)
-        else:
+        elif Globals.isLinuxPlatform():
             printer = QPrinter(mode=QPrinter.HighResolution)
             if Preferences.getPrinter("ColorMode"):
                 printer.setColorMode(QPrinter.Color)
--- a/WebBrowser/WebBrowserWindow.py	Sat Sep 24 18:00:30 2016 +0200
+++ b/WebBrowser/WebBrowserWindow.py	Tue Sep 27 19:41:11 2016 +0200
@@ -18,7 +18,7 @@
 import sys
 
 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QByteArray, QSize, QTimer, \
-    QUrl, QTextCodec, QProcess, QEvent
+    QUrl, QTextCodec, QProcess, QEvent, qVersion
 from PyQt5.QtGui import QDesktopServices, QKeySequence, QFont, QFontMetrics
 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QSizePolicy, QDockWidget, \
     QComboBox, QLabel, QSplitter, QMenu, QToolButton, QLineEdit, \
@@ -701,22 +701,26 @@
                 bookmarksManager.exportBookmarks)
         self.__actions.append(self.exportBookmarksAct)
         
-        self.printAct = E5Action(
-            self.tr('Print'),
-            UI.PixmapCache.getIcon("print.png"),
-            self.tr('&Print'),
-            QKeySequence(self.tr("Ctrl+P", "File|Print")),
-            0, self, 'webbrowser_file_print')
-        self.printAct.setStatusTip(self.tr('Print the displayed help'))
-        self.printAct.setWhatsThis(self.tr(
-            """<b>Print</b>"""
-            """<p>Print the displayed help text.</p>"""
-        ))
-        if not self.__initShortcutsOnly:
-            self.printAct.triggered.connect(self.__tabWidget.printBrowser)
-        self.__actions.append(self.printAct)
-        
-        if Globals.isLinuxPlatform():
+        if not Globals.isWindowsPlatform():
+            # TODO: implement printing based on printToPdf()
+            self.printAct = E5Action(
+                self.tr('Print'),
+                UI.PixmapCache.getIcon("print.png"),
+                self.tr('&Print'),
+                QKeySequence(self.tr("Ctrl+P", "File|Print")),
+                0, self, 'webbrowser_file_print')
+            self.printAct.setStatusTip(self.tr('Print the displayed help'))
+            self.printAct.setWhatsThis(self.tr(
+                """<b>Print</b>"""
+                """<p>Print the displayed help text.</p>"""
+            ))
+            if not self.__initShortcutsOnly:
+                self.printAct.triggered.connect(self.__tabWidget.printBrowser)
+            self.__actions.append(self.printAct)
+        else:
+            self.printAct = None
+        
+        if Globals.isLinuxPlatform() or qVersion() >= "5.7.0":
             self.printPdfAct = E5Action(
                 self.tr('Print as PDF'),
                 UI.PixmapCache.getIcon("printPdf.png"),
@@ -735,21 +739,24 @@
         else:
             self.printPdfAct = None
         
-        self.printPreviewAct = E5Action(
-            self.tr('Print Preview'),
-            UI.PixmapCache.getIcon("printPreview.png"),
-            self.tr('Print Preview'),
-            0, 0, self, 'webbrowser_file_print_preview')
-        self.printPreviewAct.setStatusTip(self.tr(
-            'Print preview of the displayed help'))
-        self.printPreviewAct.setWhatsThis(self.tr(
-            """<b>Print Preview</b>"""
-            """<p>Print preview of the displayed help text.</p>"""
-        ))
-        if not self.__initShortcutsOnly:
-            self.printPreviewAct.triggered.connect(
-                self.__tabWidget.printPreviewBrowser)
-        self.__actions.append(self.printPreviewAct)
+        if not Globals.isWindowsPlatform():
+            self.printPreviewAct = E5Action(
+                self.tr('Print Preview'),
+                UI.PixmapCache.getIcon("printPreview.png"),
+                self.tr('Print Preview'),
+                0, 0, self, 'webbrowser_file_print_preview')
+            self.printPreviewAct.setStatusTip(self.tr(
+                'Print preview of the displayed help'))
+            self.printPreviewAct.setWhatsThis(self.tr(
+                """<b>Print Preview</b>"""
+                """<p>Print preview of the displayed help text.</p>"""
+            ))
+            if not self.__initShortcutsOnly:
+                self.printPreviewAct.triggered.connect(
+                    self.__tabWidget.printPreviewBrowser)
+            self.__actions.append(self.printPreviewAct)
+        else:
+            self.printPreviewAct = None
         
         self.closeAct = E5Action(
             self.tr('Close'),
@@ -1774,8 +1781,10 @@
         menu.addAction(self.savePageScreenAct)
         menu.addAction(self.saveVisiblePageScreenAct)
         menu.addSeparator()
-        menu.addAction(self.printPreviewAct)
-        menu.addAction(self.printAct)
+        if self.printPreviewAct:
+            menu.addAction(self.printPreviewAct)
+        if self.printAct:
+            menu.addAction(self.printAct)
         if self.printPdfAct:
             menu.addAction(self.printPdfAct)
         menu.addSeparator()
@@ -1941,8 +1950,10 @@
             filetb.addAction(self.saveAsAct)
         filetb.addAction(self.savePageScreenAct)
         filetb.addSeparator()
-        filetb.addAction(self.printPreviewAct)
-        filetb.addAction(self.printAct)
+        if self.printPreviewAct:
+            filetb.addAction(self.printPreviewAct)
+        if self.printAct:
+            filetb.addAction(self.printAct)
         if self.printPdfAct:
             filetb.addAction(self.printPdfAct)
         filetb.addSeparator()

eric ide

mercurial