Continued porting the web browser. QtWebEngine

Wed, 02 Mar 2016 19:41:59 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 02 Mar 2016 19:41:59 +0100
branch
QtWebEngine
changeset 4792
f55fa7eb3cf3
parent 4791
a1e53a9ffcf3
child 4795
6e0a26c633b2

Continued porting the web browser.

- implemented the print functions in a 'hackish' way because QtWebEngine doesn't support direct printing yet.

Preferences/ConfigurationPages/PrinterPage.py file | annotate | diff | comparison | revisions
Preferences/ConfigurationPages/PrinterPage.ui file | annotate | diff | comparison | revisions
Preferences/__init__.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserTabWidget.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
diff -r a1e53a9ffcf3 -r f55fa7eb3cf3 Preferences/ConfigurationPages/PrinterPage.py
--- a/Preferences/ConfigurationPages/PrinterPage.py	Mon Feb 29 19:43:46 2016 +0100
+++ b/Preferences/ConfigurationPages/PrinterPage.py	Wed Mar 02 19:41:59 2016 +0100
@@ -52,6 +52,8 @@
             Preferences.getPrinter("TopMargin"))
         self.bottomMarginSpinBox.setValue(
             Preferences.getPrinter("BottomMargin"))
+        self.resolutionSpinBox.setValue(
+            Preferences.getPrinter("Resolution"))
         
     def save(self):
         """
@@ -84,6 +86,9 @@
         Preferences.setPrinter(
             "BottomMargin",
             self.bottomMarginSpinBox.value())
+        Preferences.setPrinter(
+            "Resolution",
+            self.resolutionSpinBox.value())
         
     @pyqtSlot()
     def on_printheaderFontButton_clicked(self):
diff -r a1e53a9ffcf3 -r f55fa7eb3cf3 Preferences/ConfigurationPages/PrinterPage.ui
--- a/Preferences/ConfigurationPages/PrinterPage.ui	Mon Feb 29 19:43:46 2016 +0100
+++ b/Preferences/ConfigurationPages/PrinterPage.ui	Wed Mar 02 19:41:59 2016 +0100
@@ -1,56 +1,86 @@
-<ui version="4.0" >
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
  <class>PrinterPage</class>
- <widget class="QWidget" name="PrinterPage" >
-  <property name="geometry" >
+ <widget class="QWidget" name="PrinterPage">
+  <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>446</width>
+    <width>448</width>
     <height>568</height>
    </rect>
   </property>
-  <layout class="QVBoxLayout" >
+  <layout class="QVBoxLayout" name="verticalLayout">
    <item>
-    <widget class="QLabel" name="headerLabel" >
-     <property name="text" >
-      <string>&lt;b>Configure printer settings&lt;/b></string>
+    <widget class="QLabel" name="headerLabel">
+     <property name="text">
+      <string>&lt;b&gt;Configure printer settings&lt;/b&gt;</string>
      </property>
     </widget>
    </item>
    <item>
-    <widget class="Line" name="line7" >
-     <property name="frameShape" >
+    <widget class="Line" name="line7">
+     <property name="frameShape">
       <enum>QFrame::HLine</enum>
      </property>
-     <property name="frameShadow" >
+     <property name="frameShadow">
       <enum>QFrame::Sunken</enum>
      </property>
-     <property name="orientation" >
+     <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
    </item>
    <item>
-    <layout class="QGridLayout" >
-     <item row="1" column="1" colspan="2" >
-      <widget class="QFrame" name="frame_2" >
-       <property name="frameShape" >
+    <layout class="QGridLayout" name="gridLayout">
+     <item row="0" column="0">
+      <widget class="QLabel" name="TextLabel1">
+       <property name="text">
+        <string>Printername:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="1" colspan="3">
+      <widget class="QLineEdit" name="printerNameEdit"/>
+     </item>
+     <item row="1" column="0">
+      <widget class="QLabel" name="TextLabel2">
+       <property name="text">
+        <string>Colour Mode:</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignTop</set>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="1" colspan="3">
+      <widget class="QFrame" name="frame_2">
+       <property name="frameShape">
         <enum>QFrame::NoFrame</enum>
        </property>
-       <layout class="QVBoxLayout" >
-        <property name="margin" >
+       <layout class="QVBoxLayout">
+        <property name="leftMargin">
+         <number>0</number>
+        </property>
+        <property name="topMargin">
+         <number>0</number>
+        </property>
+        <property name="rightMargin">
+         <number>0</number>
+        </property>
+        <property name="bottomMargin">
          <number>0</number>
         </property>
         <item>
-         <widget class="QRadioButton" name="printerColorButton" >
-          <property name="text" >
+         <widget class="QRadioButton" name="printerColorButton">
+          <property name="text">
            <string>Colour</string>
           </property>
          </widget>
         </item>
         <item>
-         <widget class="QRadioButton" name="printerGrayscaleButton" >
-          <property name="text" >
+         <widget class="QRadioButton" name="printerGrayscaleButton">
+          <property name="text">
            <string>Gray Scale</string>
           </property>
          </widget>
@@ -58,28 +88,47 @@
        </layout>
       </widget>
      </item>
-     <item row="2" column="1" colspan="2" >
-      <widget class="QFrame" name="frame" >
-       <property name="frameShape" >
+     <item row="2" column="0">
+      <widget class="QLabel" name="TextLabel4">
+       <property name="text">
+        <string>Page Order:</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignTop</set>
+       </property>
+      </widget>
+     </item>
+     <item row="2" column="1" colspan="3">
+      <widget class="QFrame" name="frame">
+       <property name="frameShape">
         <enum>QFrame::NoFrame</enum>
        </property>
-       <layout class="QVBoxLayout" >
-        <property name="spacing" >
+       <layout class="QVBoxLayout">
+        <property name="spacing">
          <number>6</number>
         </property>
-        <property name="margin" >
+        <property name="leftMargin">
+         <number>0</number>
+        </property>
+        <property name="topMargin">
+         <number>0</number>
+        </property>
+        <property name="rightMargin">
+         <number>0</number>
+        </property>
+        <property name="bottomMargin">
          <number>0</number>
         </property>
         <item>
-         <widget class="QRadioButton" name="printFirstPageFirstButton" >
-          <property name="text" >
+         <widget class="QRadioButton" name="printFirstPageFirstButton">
+          <property name="text">
            <string>First Page First</string>
           </property>
          </widget>
         </item>
         <item>
-         <widget class="QRadioButton" name="printFirstPageLastButton" >
-          <property name="text" >
+         <widget class="QRadioButton" name="printFirstPageLastButton">
+          <property name="text">
            <string>Last Page First</string>
           </property>
          </widget>
@@ -87,88 +136,32 @@
        </layout>
       </widget>
      </item>
-     <item row="0" column="1" colspan="2" >
-      <widget class="QLineEdit" name="printerNameEdit" />
-     </item>
-     <item row="0" column="0" >
-      <widget class="QLabel" name="TextLabel1" >
-       <property name="text" >
-        <string>Printername:</string>
-       </property>
-      </widget>
-     </item>
-     <item row="3" column="0" >
-      <widget class="QLabel" name="TextLabel3" >
-       <property name="text" >
+     <item row="3" column="0">
+      <widget class="QLabel" name="TextLabel3">
+       <property name="text">
         <string>Magnification:</string>
        </property>
       </widget>
      </item>
-     <item row="4" column="1" colspan="2" >
-      <widget class="QLineEdit" name="printheaderFontSample" >
-       <property name="focusPolicy" >
-        <enum>Qt::NoFocus</enum>
-       </property>
-       <property name="text" >
-        <string>Header Font</string>
-       </property>
-       <property name="alignment" >
-        <set>Qt::AlignHCenter</set>
-       </property>
-       <property name="readOnly" >
-        <bool>true</bool>
-       </property>
-      </widget>
-     </item>
-     <item row="4" column="0" >
-      <widget class="QPushButton" name="printheaderFontButton" >
-       <property name="toolTip" >
-        <string>Press to select the font for the page headers</string>
-       </property>
-       <property name="text" >
-        <string>Header Font</string>
-       </property>
-      </widget>
-     </item>
-     <item row="3" column="1" >
-      <widget class="QSpinBox" name="printMagnificationSpinBox" >
-       <property name="minimum" >
+     <item row="3" column="1">
+      <widget class="QSpinBox" name="printMagnificationSpinBox">
+       <property name="minimum">
         <number>-10</number>
        </property>
-       <property name="maximum" >
+       <property name="maximum">
         <number>10</number>
        </property>
-       <property name="value" >
+       <property name="value">
         <number>-3</number>
        </property>
       </widget>
      </item>
-     <item row="1" column="0" >
-      <widget class="QLabel" name="TextLabel2" >
-       <property name="text" >
-        <string>Colour Mode:</string>
-       </property>
-       <property name="alignment" >
-        <set>Qt::AlignTop</set>
-       </property>
-      </widget>
-     </item>
-     <item row="2" column="0" >
-      <widget class="QLabel" name="TextLabel4" >
-       <property name="text" >
-        <string>Page Order:</string>
-       </property>
-       <property name="alignment" >
-        <set>Qt::AlignTop</set>
-       </property>
-      </widget>
-     </item>
-     <item row="3" column="2" >
+     <item row="3" column="2" colspan="2">
       <spacer>
-       <property name="orientation" >
+       <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
-       <property name="sizeHint" stdset="0" >
+       <property name="sizeHint" stdset="0">
         <size>
          <width>251</width>
          <height>20</height>
@@ -176,88 +169,153 @@
        </property>
       </spacer>
      </item>
+     <item row="4" column="0">
+      <widget class="QLabel" name="label">
+       <property name="text">
+        <string>Resolution:</string>
+       </property>
+      </widget>
+     </item>
+     <item row="4" column="1" colspan="2">
+      <widget class="QSpinBox" name="resolutionSpinBox">
+       <property name="toolTip">
+        <string>Select the printer resolution </string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+       </property>
+       <property name="suffix">
+        <string> DPI</string>
+       </property>
+       <property name="maximum">
+        <number>6000</number>
+       </property>
+       <property name="singleStep">
+        <number>50</number>
+       </property>
+      </widget>
+     </item>
+     <item row="4" column="3">
+      <spacer name="horizontalSpacer">
+       <property name="orientation">
+        <enum>Qt::Horizontal</enum>
+       </property>
+       <property name="sizeHint" stdset="0">
+        <size>
+         <width>208</width>
+         <height>20</height>
+        </size>
+       </property>
+      </spacer>
+     </item>
+     <item row="5" column="0">
+      <widget class="QPushButton" name="printheaderFontButton">
+       <property name="toolTip">
+        <string>Press to select the font for the page headers</string>
+       </property>
+       <property name="text">
+        <string>Header Font</string>
+       </property>
+      </widget>
+     </item>
+     <item row="5" column="1" colspan="3">
+      <widget class="QLineEdit" name="printheaderFontSample">
+       <property name="focusPolicy">
+        <enum>Qt::NoFocus</enum>
+       </property>
+       <property name="text">
+        <string>Header Font</string>
+       </property>
+       <property name="alignment">
+        <set>Qt::AlignHCenter</set>
+       </property>
+       <property name="readOnly">
+        <bool>true</bool>
+       </property>
+      </widget>
+     </item>
     </layout>
    </item>
    <item>
-    <layout class="QHBoxLayout" >
+    <layout class="QHBoxLayout">
      <item>
-      <widget class="QGroupBox" name="groupBox" >
-       <property name="title" >
+      <widget class="QGroupBox" name="groupBox">
+       <property name="title">
         <string>Margins</string>
        </property>
-       <layout class="QGridLayout" >
-        <item row="0" column="1" >
-         <widget class="QDoubleSpinBox" name="topMarginSpinBox" >
-          <property name="toolTip" >
+       <layout class="QGridLayout">
+        <item row="0" column="1">
+         <widget class="QDoubleSpinBox" name="topMarginSpinBox">
+          <property name="toolTip">
            <string>Enter the top margin in cm.</string>
           </property>
-          <property name="suffix" >
+          <property name="suffix">
            <string> cm</string>
           </property>
-          <property name="decimals" >
+          <property name="decimals">
            <number>1</number>
           </property>
-          <property name="maximum" >
+          <property name="maximum">
            <double>9.900000000000000</double>
           </property>
-          <property name="singleStep" >
+          <property name="singleStep">
            <double>0.500000000000000</double>
           </property>
          </widget>
         </item>
-        <item row="1" column="0" >
-         <widget class="QDoubleSpinBox" name="leftMarginSpinBox" >
-          <property name="toolTip" >
+        <item row="1" column="0">
+         <widget class="QDoubleSpinBox" name="leftMarginSpinBox">
+          <property name="toolTip">
            <string>Enter the left margin in cm.</string>
           </property>
-          <property name="suffix" >
+          <property name="suffix">
            <string> cm</string>
           </property>
-          <property name="decimals" >
+          <property name="decimals">
            <number>1</number>
           </property>
-          <property name="maximum" >
+          <property name="maximum">
            <double>9.900000000000000</double>
           </property>
-          <property name="singleStep" >
+          <property name="singleStep">
            <double>0.500000000000000</double>
           </property>
          </widget>
         </item>
-        <item row="1" column="2" >
-         <widget class="QDoubleSpinBox" name="rightMarginSpinBox" >
-          <property name="toolTip" >
+        <item row="1" column="2">
+         <widget class="QDoubleSpinBox" name="rightMarginSpinBox">
+          <property name="toolTip">
            <string>Enter the right margin in cm.</string>
           </property>
-          <property name="suffix" >
+          <property name="suffix">
            <string> cm</string>
           </property>
-          <property name="decimals" >
+          <property name="decimals">
            <number>1</number>
           </property>
-          <property name="maximum" >
+          <property name="maximum">
            <double>9.900000000000000</double>
           </property>
-          <property name="singleStep" >
+          <property name="singleStep">
            <double>0.500000000000000</double>
           </property>
          </widget>
         </item>
-        <item row="2" column="1" >
-         <widget class="QDoubleSpinBox" name="bottomMarginSpinBox" >
-          <property name="toolTip" >
+        <item row="2" column="1">
+         <widget class="QDoubleSpinBox" name="bottomMarginSpinBox">
+          <property name="toolTip">
            <string>Enter the bottom margin in cm.</string>
           </property>
-          <property name="suffix" >
+          <property name="suffix">
            <string> cm</string>
           </property>
-          <property name="decimals" >
+          <property name="decimals">
            <number>1</number>
           </property>
-          <property name="maximum" >
+          <property name="maximum">
            <double>9.900000000000000</double>
           </property>
-          <property name="singleStep" >
+          <property name="singleStep">
            <double>0.500000000000000</double>
           </property>
          </widget>
@@ -267,10 +325,10 @@
      </item>
      <item>
       <spacer>
-       <property name="orientation" >
+       <property name="orientation">
         <enum>Qt::Horizontal</enum>
        </property>
-       <property name="sizeHint" stdset="0" >
+       <property name="sizeHint" stdset="0">
         <size>
          <width>40</width>
          <height>20</height>
@@ -282,10 +340,10 @@
    </item>
    <item>
     <spacer>
-     <property name="orientation" >
+     <property name="orientation">
       <enum>Qt::Vertical</enum>
      </property>
-     <property name="sizeHint" stdset="0" >
+     <property name="sizeHint" stdset="0">
       <size>
        <width>428</width>
        <height>61</height>
@@ -302,6 +360,7 @@
   <tabstop>printFirstPageFirstButton</tabstop>
   <tabstop>printFirstPageLastButton</tabstop>
   <tabstop>printMagnificationSpinBox</tabstop>
+  <tabstop>resolutionSpinBox</tabstop>
   <tabstop>printheaderFontButton</tabstop>
   <tabstop>topMarginSpinBox</tabstop>
   <tabstop>leftMarginSpinBox</tabstop>
diff -r a1e53a9ffcf3 -r f55fa7eb3cf3 Preferences/__init__.py
--- a/Preferences/__init__.py	Mon Feb 29 19:43:46 2016 +0100
+++ b/Preferences/__init__.py	Wed Mar 02 19:41:59 2016 +0100
@@ -712,6 +712,7 @@
         "RightMargin": 1.0,
         "TopMargin": 1.0,
         "BottomMargin": 1.0,
+        "Resolution": 150,      # printer resolution in DPI
     }
     
     # defaults for the project settings
@@ -2270,7 +2271,7 @@
     if key in ["ColorMode", "FirstPageFirst"]:
         return toBool(prefClass.settings.value(
             "Printer/" + key, prefClass.printerDefaults[key]))
-    elif key in ["Magnification", "Orientation", "PageSize"]:
+    elif key in ["Magnification", "Orientation", "PageSize", "Resolution"]:
         return int(prefClass.settings.value(
             "Printer/" + key, prefClass.printerDefaults[key]))
     elif key in ["LeftMargin", "RightMargin", "TopMargin", "BottomMargin"]:
diff -r a1e53a9ffcf3 -r f55fa7eb3cf3 WebBrowser/WebBrowserTabWidget.py
--- a/WebBrowser/WebBrowserTabWidget.py	Mon Feb 29 19:43:46 2016 +0100
+++ b/WebBrowser/WebBrowserTabWidget.py	Wed Mar 02 19:41:59 2016 +0100
@@ -14,7 +14,7 @@
 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl
 from PyQt5.QtGui import QIcon
 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QMenu, QToolButton, QDialog
-##from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
+from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
 ##from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
 
 from E5Gui.E5TabWidget import E5TabWidget
@@ -22,6 +22,7 @@
 from E5Gui.E5Application import e5App
 
 from .WebBrowserView import WebBrowserView
+from .Tools import WebBrowserTools
 from . import WebInspector
 
 import UI.PixmapCache
@@ -171,17 +172,16 @@
             self.tr("Close Others"), self.__tabContextMenuCloseOthers)
         self.__tabContextMenu.addAction(
             self.tr('Close All'), self.closeAllBrowsers)
-        # TODO: Print
-##        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)
-##        self.__tabContextMenu.addAction(
-##            UI.PixmapCache.getIcon("printPdf.png"),
-##            self.tr('Print as PDF'), self.__tabContextMenuPrintPdf)
+        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)
+        self.__tabContextMenu.addAction(
+            UI.PixmapCache.getIcon("printPdf.png"),
+            self.tr('Print as PDF'), self.__tabContextMenuPrintPdf)
         self.__tabContextMenu.addSeparator()
         self.__tabContextMenu.addAction(
             UI.PixmapCache.getIcon("reload.png"),
@@ -271,28 +271,27 @@
                 list(range(index - 1, -1, -1)):
             self.closeBrowserAt(i)
     
-    # TODO: Print
-##    def __tabContextMenuPrint(self):
-##        """
-##        Private method to print the selected tab.
-##        """
-##        browser = self.widget(self.__tabContextMenuIndex)
-##        self.printBrowser(browser)
-##    
-##    def __tabContextMenuPrintPdf(self):
-##        """
-##        Private method to print the selected tab as PDF.
-##        """
-##        browser = self.widget(self.__tabContextMenuIndex)
-##        self.printBrowserPdf(browser)
-##    
-##    def __tabContextMenuPrintPreview(self):
-##        """
-##        Private method to show a print preview of the selected tab.
-##        """
-##        browser = self.widget(self.__tabContextMenuIndex)
-##        self.printPreviewBrowser(browser)
-##    
+    def __tabContextMenuPrint(self):
+        """
+        Private method to print the selected tab.
+        """
+        browser = self.widget(self.__tabContextMenuIndex)
+        self.printBrowser(browser)
+    
+    def __tabContextMenuPrintPdf(self):
+        """
+        Private method to print the selected tab as PDF.
+        """
+        browser = self.widget(self.__tabContextMenuIndex)
+        self.printBrowserPdf(browser)
+    
+    def __tabContextMenuPrintPreview(self):
+        """
+        Private method to show a print preview of the selected tab.
+        """
+        browser = self.widget(self.__tabContextMenuIndex)
+        self.printPreviewBrowser(browser)
+    
     @pyqtSlot()
     def __newBrowser(self):
         """
@@ -351,7 +350,6 @@
         browser.search.connect(self.newBrowser)
         browser.page().windowCloseRequested.connect(
             self.__windowCloseRequested)
-##        browser.page().printRequested.connect(self.__printRequested)
         browser.zoomValueChanged.connect(self.browserZoomValueChanged)
         
         if position == -1:
@@ -528,159 +526,108 @@
             li.append(self.widget(index))
         return li
     
-    # TODO: Print
-##    @pyqtSlot()
-##    def printBrowser(self, browser=None):
-##        """
-##        Public slot called to print the displayed page.
-##        
-##        @param browser reference to the browser to be printed (WebBrowserView)
-##        """
-##        if browser is None:
-##            browser = self.currentBrowser()
-##        
-##        browser.page().runJavaScript("window.print()")
-##        self.__printRequested(browser.page().mainFrame())
-##    
-##    def __printRequested(self, frame):
-##        """
-##        Private slot to handle a print request.
-##        
-##        @param frame reference to the frame to be printed (QWebFrame)
-##        """
-##        printer = QPrinter(mode=QPrinter.HighResolution)
-##        if Preferences.getPrinter("ColorMode"):
-##            printer.setColorMode(QPrinter.Color)
-##        else:
-##            printer.setColorMode(QPrinter.GrayScale)
-##        if Preferences.getPrinter("FirstPageFirst"):
-##            printer.setPageOrder(QPrinter.FirstPageFirst)
-##        else:
-##            printer.setPageOrder(QPrinter.LastPageFirst)
-##        printer.setPageMargins(
-##            Preferences.getPrinter("LeftMargin") * 10,
-##            Preferences.getPrinter("TopMargin") * 10,
-##            Preferences.getPrinter("RightMargin") * 10,
-##            Preferences.getPrinter("BottomMargin") * 10,
-##            QPrinter.Millimeter
-##        )
-##        printerName = Preferences.getPrinter("PrinterName")
-##        if printerName:
-##            printer.setPrinterName(printerName)
-##        
-##        printDialog = QPrintDialog(printer, self)
-##        if printDialog.exec_() == QDialog.Accepted:
-##            try:
-##                frame.print_(printer)
-##            except AttributeError:
-##                E5MessageBox.critical(
-##                    self,
-##                    self.tr("eric6 Web Browser"),
-##                    self.tr(
-##                        """<p>Printing is not available due to a bug in"""
-##                        """ PyQt5. Please upgrade.</p>"""))
-##                return
-##    
-##    @pyqtSlot()
-##    def printBrowserPdf(self, browser=None):
-##        """
-##        Public slot called to print the displayed page to PDF.
-##        
-##        @param browser reference to the browser to be printed (HelpBrowser)
-##        """
-##        if browser is None:
-##            browser = self.currentBrowser()
-##        
-##        self.__printPdfRequested(browser.page().mainFrame())
-##    
-##    def __printPdfRequested(self, frame):
-##        """
-##        Private slot to handle a print to PDF request.
-##        
-##        @param frame reference to the frame to be printed (QWebFrame)
-##        """
-##        printer = QPrinter(mode=QPrinter.HighResolution)
-##        if Preferences.getPrinter("ColorMode"):
-##            printer.setColorMode(QPrinter.Color)
-##        else:
-##            printer.setColorMode(QPrinter.GrayScale)
-##        printerName = Preferences.getPrinter("PrinterName")
-##        if printerName:
-##            printer.setPrinterName(printerName)
-##        printer.setOutputFormat(QPrinter.PdfFormat)
-##        name = frame.url().path().rsplit('/', 1)[-1]
-##        if name:
-##            name = name.rsplit('.', 1)[0]
-##            name += '.pdf'
-##            printer.setOutputFileName(name)
-##        
-##        printDialog = QPrintDialog(printer, self)
-##        if printDialog.exec_() == QDialog.Accepted:
-##            try:
-##                frame.print_(printer)
-##            except AttributeError:
-##                E5MessageBox.critical(
-##                    self,
-##                    self.tr("eric6 Web Browser"),
-##                    self.tr(
-##                        """<p>Printing is not available due to a bug in"""
-##                        """ PyQt5. Please upgrade.</p>"""))
-##                return
-##    
-##    @pyqtSlot()
-##    def printPreviewBrowser(self, browser=None):
-##        """
-##        Public slot called to show a print preview of the displayed file.
-##        
-##        @param browser reference to the browser to be printed (HelpBrowserWV)
-##        """
-##        from PyQt5.QtPrintSupport import QPrintPreviewDialog
-##        
-##        if browser is None:
-##            browser = self.currentBrowser()
-##        
-##        printer = QPrinter(mode=QPrinter.HighResolution)
-##        if Preferences.getPrinter("ColorMode"):
-##            printer.setColorMode(QPrinter.Color)
-##        else:
-##            printer.setColorMode(QPrinter.GrayScale)
-##        if Preferences.getPrinter("FirstPageFirst"):
-##            printer.setPageOrder(QPrinter.FirstPageFirst)
-##        else:
-##            printer.setPageOrder(QPrinter.LastPageFirst)
-##        printer.setPageMargins(
-##            Preferences.getPrinter("LeftMargin") * 10,
-##            Preferences.getPrinter("TopMargin") * 10,
-##            Preferences.getPrinter("RightMargin") * 10,
-##            Preferences.getPrinter("BottomMargin") * 10,
-##            QPrinter.Millimeter
-##        )
-##        printerName = Preferences.getPrinter("PrinterName")
-##        if printerName:
-##            printer.setPrinterName(printerName)
-##        
-##        self.__printPreviewBrowser = browser
-##        preview = QPrintPreviewDialog(printer, self)
-##        preview.paintRequested.connect(self.__printPreview)
-##        preview.exec_()
-##    
-##    def __printPreview(self, printer):
-##        """
-##        Private slot to generate a print preview.
-##        
-##        @param printer reference to the printer object (QPrinter)
-##        """
-##        try:
-##            self.__printPreviewBrowser.print_(printer)
-##        except AttributeError:
-##            E5MessageBox.critical(
-##                self,
-##                self.tr("eric6 Web Browser"),
-##                self.tr(
-##                    """<p>Printing is not available due to a bug in PyQt5."""
-##                    """Please upgrade.</p>"""))
-##            return
-##    
+    @pyqtSlot()
+    def printBrowser(self, browser=None):
+        """
+        Public slot called to print the displayed page.
+        
+        @param browser reference to the browser to be printed (WebBrowserView)
+        """
+        if browser is None:
+            browser = self.currentBrowser()
+        
+        printer = QPrinter(mode=QPrinter.HighResolution)
+        if Preferences.getPrinter("ColorMode"):
+            printer.setColorMode(QPrinter.Color)
+        else:
+            printer.setColorMode(QPrinter.GrayScale)
+        if Preferences.getPrinter("FirstPageFirst"):
+            printer.setPageOrder(QPrinter.FirstPageFirst)
+        else:
+            printer.setPageOrder(QPrinter.LastPageFirst)
+        printer.setPageMargins(
+            Preferences.getPrinter("LeftMargin") * 10,
+            Preferences.getPrinter("TopMargin") * 10,
+            Preferences.getPrinter("RightMargin") * 10,
+            Preferences.getPrinter("BottomMargin") * 10,
+            QPrinter.Millimeter
+        )
+        printerName = Preferences.getPrinter("PrinterName")
+        if printerName:
+            printer.setPrinterName(printerName)
+        printer.setResolution(Preferences.getPrinter("Resolution"))
+        
+        printDialog = QPrintDialog(printer, self)
+        if printDialog.exec_() == QDialog.Accepted:
+            browser.render(printer)
+    
+    @pyqtSlot()
+    def printBrowserPdf(self, browser=None):
+        """
+        Public slot called to print the displayed page to PDF.
+        
+        @param browser reference to the browser to be printed (HelpBrowser)
+        """
+        if browser is None:
+            browser = self.currentBrowser()
+        
+        printer = QPrinter(mode=QPrinter.HighResolution)
+        if Preferences.getPrinter("ColorMode"):
+            printer.setColorMode(QPrinter.Color)
+        else:
+            printer.setColorMode(QPrinter.GrayScale)
+        printerName = Preferences.getPrinter("PrinterName")
+        if printerName:
+            printer.setPrinterName(printerName)
+        printer.setOutputFormat(QPrinter.PdfFormat)
+        name = WebBrowserTools.getFileNameFromUrl(browser.url())
+        if name:
+            name = name.rsplit('.', 1)[0]
+            name += '.pdf'
+            printer.setOutputFileName(name)
+        printer.setResolution(Preferences.getPrinter("Resolution"))
+        
+        printDialog = QPrintDialog(printer, self)
+        if printDialog.exec_() == QDialog.Accepted:
+            browser.render(printer)
+    
+    @pyqtSlot()
+    def printPreviewBrowser(self, browser=None):
+        """
+        Public slot called to show a print preview of the displayed file.
+        
+        @param browser reference to the browser to be printed (HelpBrowserWV)
+        """
+        from PyQt5.QtPrintSupport import QPrintPreviewDialog
+        
+        if browser is None:
+            browser = self.currentBrowser()
+        
+        printer = QPrinter(mode=QPrinter.HighResolution)
+        if Preferences.getPrinter("ColorMode"):
+            printer.setColorMode(QPrinter.Color)
+        else:
+            printer.setColorMode(QPrinter.GrayScale)
+        if Preferences.getPrinter("FirstPageFirst"):
+            printer.setPageOrder(QPrinter.FirstPageFirst)
+        else:
+            printer.setPageOrder(QPrinter.LastPageFirst)
+        printer.setPageMargins(
+            Preferences.getPrinter("LeftMargin") * 10,
+            Preferences.getPrinter("TopMargin") * 10,
+            Preferences.getPrinter("RightMargin") * 10,
+            Preferences.getPrinter("BottomMargin") * 10,
+            QPrinter.Millimeter
+        )
+        printerName = Preferences.getPrinter("PrinterName")
+        if printerName:
+            printer.setPrinterName(printerName)
+        printer.setResolution(Preferences.getPrinter("Resolution"))
+        
+        preview = QPrintPreviewDialog(printer, self)
+        preview.paintRequested.connect(lambda p: browser.render(p))
+        preview.exec_()
+    
     def __sourceChanged(self, url):
         """
         Private slot to handle a change of a browsers source.
diff -r a1e53a9ffcf3 -r f55fa7eb3cf3 WebBrowser/WebBrowserWindow.py
--- a/WebBrowser/WebBrowserWindow.py	Mon Feb 29 19:43:46 2016 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Wed Mar 02 19:41:59 2016 +0100
@@ -671,53 +671,52 @@
                 bookmarksManager.exportBookmarks)
         self.__actions.append(self.exportBookmarksAct)
         
-        # TODO: Print
-##        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)
-##        
-##        self.printPdfAct = E5Action(
-##            self.tr('Print as PDF'),
-##            UI.PixmapCache.getIcon("printPdf.png"),
-##            self.tr('Print as PDF'),
-##            0, 0, self, 'webbrowser_file_print_pdf')
-##        self.printPdfAct.setStatusTip(self.tr(
-##            'Print the displayed help as PDF'))
-##        self.printPdfAct.setWhatsThis(self.tr(
-##            """<b>Print as PDF</b>"""
-##            """<p>Print the displayed help text as a PDF file.</p>"""
-##        ))
-##        if not self.__initShortcutsOnly:
-##            self.printPdfAct.triggered.connect(
-##                self.__tabWidget.printBrowserPdf)
-##        self.__actions.append(self.printPdfAct)
-##        
-##        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)
+        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)
+        
+        self.printPdfAct = E5Action(
+            self.tr('Print as PDF'),
+            UI.PixmapCache.getIcon("printPdf.png"),
+            self.tr('Print as PDF'),
+            0, 0, self, 'webbrowser_file_print_pdf')
+        self.printPdfAct.setStatusTip(self.tr(
+            'Print the displayed help as PDF'))
+        self.printPdfAct.setWhatsThis(self.tr(
+            """<b>Print as PDF</b>"""
+            """<p>Print the displayed help text as a PDF file.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.printPdfAct.triggered.connect(
+                self.__tabWidget.printBrowserPdf)
+        self.__actions.append(self.printPdfAct)
+        
+        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)
         
         self.closeAct = E5Action(
             self.tr('Close'),
@@ -1759,15 +1758,13 @@
         menu.addAction(self.savePageScreenAct)
         menu.addAction(self.saveVisiblePageScreenAct)
         menu.addSeparator()
-##        menu.addAction(self.printPreviewAct)
-##        menu.addAction(self.printAct)
-##        menu.addAction(self.printPdfAct)
-##        menu.addSeparator()
+        menu.addAction(self.printPreviewAct)
+        menu.addAction(self.printAct)
+        menu.addAction(self.printPdfAct)
+        menu.addSeparator()
         menu.addAction(self.closeAct)
         menu.addAction(self.closeAllAct)
         menu.addSeparator()
-##        menu.addAction(self.privateBrowsingAct)
-##        menu.addSeparator()
         menu.addAction(self.exitAct)
         
         menu = mb.addMenu(self.tr('&Edit'))
@@ -1928,10 +1925,10 @@
 ##        filetb.addAction(self.saveAsAct)
         filetb.addAction(self.savePageScreenAct)
         filetb.addSeparator()
-##        filetb.addAction(self.printPreviewAct)
-##        filetb.addAction(self.printAct)
-##        filetb.addAction(self.printPdfAct)
-##        filetb.addSeparator()
+        filetb.addAction(self.printPreviewAct)
+        filetb.addAction(self.printAct)
+        filetb.addAction(self.printPdfAct)
+        filetb.addSeparator()
         filetb.addAction(self.closeAct)
         filetb.addAction(self.exitAct)
         

eric ide

mercurial