Quellcode durchsuchen

first test of print to pdf, aouch :(

Bachir Soussi Chiadmi vor 7 Jahren
Ursprung
Commit
4db47a9259
2 geänderte Dateien mit 45 neuen und 9 gelöschten Zeilen
  1. 35 9
      classes/design.py
  2. 10 0
      classes/mainwindow.py

+ 35 - 9
classes/design.py

@@ -9,6 +9,7 @@ from PyQt5.QtGui import QKeySequence, QFont
 from PyQt5.QtWidgets import QWidget, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit, QShortcut, QPushButton, QCheckBox
 from PyQt5.QtWidgets import QWidget, QTabWidget, QVBoxLayout, QHBoxLayout, QSplitter, QPlainTextEdit, QShortcut, QPushButton, QCheckBox
 from PyQt5.QtWebKit import QWebSettings
 from PyQt5.QtWebKit import QWebSettings
 from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
 from PyQt5.QtWebKitWidgets import QWebView, QWebInspector
+from PyQt5.QtPrintSupport import QPrintPreviewDialog, QPrinter
 
 
 
 
 class WebkitView(QWebView):
 class WebkitView(QWebView):
@@ -19,6 +20,19 @@ class WebkitView(QWebView):
       self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
       self.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
       # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
       # self.settings().setAttribute(QWebSettings.PluginsEnabled, True)
 
 
+      self.printer = QPrinter()
+      self.printer.setPageSize(QPrinter.A4)
+      self.printer.setOrientation(QPrinter.Portrait)
+      self.printer.setPageMargins(10,10,10,10,QPrinter.Millimeter)
+      self.setFixedWidth(1000)
+
+
+   def onPrint(self):
+      dialog = QPrintPreviewDialog(self.printer)
+      # dialog.setWindowState(Qt.WindowMaximized)
+      dialog.paintRequested.connect(self.print_)
+      # dialog.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint | Qt.WindowMinMaxButtonsHint | Qt.WindowCloseButtonHint | Qt.WindowContextHelpButtonHint)
+      dialog.exec()
 
 
 class WebkitInspector(QWebInspector):
 class WebkitInspector(QWebInspector):
    def __init__(self, parent, webkitview):
    def __init__(self, parent, webkitview):
@@ -28,9 +42,9 @@ class WebkitInspector(QWebInspector):
       self.showMaximized()
       self.showMaximized()
       # TODO: webkitinspector is disappearing when chaging tabs
       # TODO: webkitinspector is disappearing when chaging tabs
 
 
-class WebViewOptions(QWidget):
+class WebViewToolBar(QWidget):
    def __init__(self, parent):
    def __init__(self, parent):
-      super(WebViewOptions, self).__init__(parent)
+      super(WebViewToolBar, self).__init__(parent)
       self.parent = parent
       self.parent = parent
       self.hbox = QHBoxLayout()
       self.hbox = QHBoxLayout()
       self.hbox.setContentsMargins(0,0,0,0)
       self.hbox.setContentsMargins(0,0,0,0)
@@ -72,11 +86,14 @@ class WebViewOptions(QWidget):
       self.reload.clicked.connect(self.onReload)
       self.reload.clicked.connect(self.onReload)
       self.hbox.addWidget(self.reload)
       self.hbox.addWidget(self.reload)
 
 
-      self.setLayout(self.hbox)
+      self.imprim = QPushButton("&Print", self)
+      # self.imprim.setShortcut('Ctrl+Shift+r')
+      # TODO: how to define same shortcut in different places
+      # self.imprim.setIcon(Icon(ico)))
+      self.imprim.clicked.connect(self.onPrint)
+      self.hbox.addWidget(self.imprim)
 
 
-   def onReload(self):
-      print("onReload")
-      # self.parent.webkitview.
+      self.setLayout(self.hbox)
 
 
    def onPreview(self):
    def onPreview(self):
       print('onPreview')
       print('onPreview')
@@ -90,6 +107,16 @@ class WebViewOptions(QWidget):
    def onSpread(self):
    def onSpread(self):
       print('onSpread')
       print('onSpread')
 
 
+
+   def onReload(self):
+      print("onReload")
+      # self.parent.webkitview.
+
+   def onPrint(self):
+      print("onReload")
+      self.parent.webkitview.onPrint()
+
+
 class CodeEditor(QPlainTextEdit):
 class CodeEditor(QPlainTextEdit):
    def __init__(self, core, tabs, file=None):
    def __init__(self, core, tabs, file=None):
       super(CodeEditor, self).__init__()
       super(CodeEditor, self).__init__()
@@ -181,7 +208,6 @@ class DesignStack(QWidget):
       self.webview.setLayout(self.webview.vbox)
       self.webview.setLayout(self.webview.vbox)
       self.webview.vbox.setContentsMargins(0,0,0,0)
       self.webview.vbox.setContentsMargins(0,0,0,0)
 
 
-
       self.webkitview = WebkitView(self, core.server.port)
       self.webkitview = WebkitView(self, core.server.port)
 
 
       self.webkitinspector = WebkitInspector(self, self.webkitview)
       self.webkitinspector = WebkitInspector(self, self.webkitview)
@@ -191,8 +217,8 @@ class DesignStack(QWidget):
       shortcut.activated.connect(self.toggleInspector)
       shortcut.activated.connect(self.toggleInspector)
       self.webkitinspector.setVisible(False)
       self.webkitinspector.setVisible(False)
 
 
-      self.webviewoptions = WebViewOptions(self)
-      self.webview.vbox.addWidget(self.webviewoptions)
+      self.webviewtoolbar = WebViewToolBar(self)
+      self.webview.vbox.addWidget(self.webviewtoolbar)
 
 
       self.vsplitter = QSplitter(QtCore.Qt.Vertical)
       self.vsplitter = QSplitter(QtCore.Qt.Vertical)
       self.vsplitter.addWidget(self.webkitview)
       self.vsplitter.addWidget(self.webkitview)

+ 10 - 0
classes/mainwindow.py

@@ -54,6 +54,10 @@ class MainWindow(QMainWindow):
       self.save_action.setShortcut("Ctrl+Shift+s")
       self.save_action.setShortcut("Ctrl+Shift+s")
       file.addAction(self.save_action)
       file.addAction(self.save_action)
 
 
+      prnt = QAction("&Print",self)
+      prnt.setShortcut("Ctrl+p")
+      file.addAction(prnt)
+
       quit = QAction("&Quit",self)
       quit = QAction("&Quit",self)
       quit.setShortcut("Ctrl+q")
       quit.setShortcut("Ctrl+q")
       file.addAction(quit)
       file.addAction(quit)
@@ -105,6 +109,8 @@ class MainWindow(QMainWindow):
          self.openprojectdialogue()
          self.openprojectdialogue()
       elif q.text() == "&Save Project as":
       elif q.text() == "&Save Project as":
          self.saveprojectdialogue()
          self.saveprojectdialogue()
+      elif q.text() == "&Print":
+         self.imprim()
       elif q.text() == "&Quit":
       elif q.text() == "&Quit":
          self.quit()
          self.quit()
 
 
@@ -179,6 +185,10 @@ class MainWindow(QMainWindow):
          print('Exception', e)
          print('Exception', e)
          pass
          pass
 
 
+   def imprim(self):
+      print('imprim')
+      self.designstack.webkitview.onPrint()
+
    def quit(self):
    def quit(self):
       print("Quit")
       print("Quit")
       if self.core.tempcwd:
       if self.core.tempcwd: