Warning, /office/klevernotes/src/contents/ui/pages/PrintingPage.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2023 Louis Schul <schul9louis@gmail.com>
0003 
0004 // LOOSELY BASED ON : https://invent.kde.org/network/angelfish/-/blob/master/lib/contents/ui/PrintPreview.qml
0005 // SPDX-License-Identifier: GPL-2.0-or-later
0006 // SPDX-FileCopyrightText: 2023 Michael Lang <criticaltemp@protonmail.com>
0007 
0008 import QtQuick 2.15
0009 import QtWebEngine 1.10
0010 import QtQuick.Controls 2.2
0011 import QtQuick.Pdf
0012 import Qt.labs.platform 1.1
0013 
0014 import org.kde.kirigami 2.19 as Kirigami
0015 
0016 import org.kde.Klever 1.0
0017 
0018 import "qrc:/contents/ui/dialogs"
0019 
0020 Kirigami.Page {
0021     id: printPreview
0022 
0023     readonly property QtObject textDisplay: applicationWindow().pageStack.get(0).editorView.display
0024     readonly property string pdfPath: StandardPaths.writableLocation(StandardPaths.TempLocation)+"/pdf-preview.pdf"
0025     readonly property string emptyPdf: StandardPaths.writableLocation(StandardPaths.TempLocation)+"/empty.pdf"
0026 
0027     property var colors
0028 
0029     title: i18nc("@title:page", "Print")
0030 
0031     actions: [
0032         Kirigami.Action {
0033             displayComponent: ComboBox {
0034                 id: colorTheme
0035 
0036                 model: ColorSchemer.model
0037                 textRole: "display"
0038                 valueRole: "display"
0039                 displayText: i18nc("@label:ComboBox", "Color theme")
0040 
0041                 onCurrentValueChanged: {
0042                     if (currentIndex === 0) printPreview.colors = {}
0043                     else {
0044                         printPreview.colors = ColorSchemer.getUsefullColors(currentIndex)
0045                     }
0046                     requestPdf(toogleBackground.checked)
0047                 }
0048             }
0049         },
0050         Kirigami.Action {
0051             id: toogleBackground
0052 
0053             text: i18nc("@label:button, as in 'the background of an image'", "Background")
0054             checkable: true
0055             icon.name: "backgroundtool"
0056 
0057             onTriggered: {
0058                 requestPdf(checked)
0059                 if (!Config.pdfWarningHidden && checked) backgroundWarning.open()
0060             }
0061         },
0062         Kirigami.Action {
0063             enabled: false
0064             separator: true
0065         },
0066         Kirigami.Action {
0067             id: saveAction
0068 
0069             property string path
0070 
0071             text: i18nc("@label:button", "Save")
0072             icon.name: "document-save-symbolic"
0073 
0074             onTriggered: {
0075                 pdfSaver.open()
0076             }
0077             onPathChanged: {
0078                 printingUtility.copy(pdfPath.substring(7), path.substring(7))
0079             }
0080         }
0081     ]
0082 
0083     onBackRequested: (event) => {
0084         event.accepted = true;
0085         closePage()
0086     }
0087     Component.onCompleted: {
0088         requestPdf(false)
0089     }
0090 
0091     WarningDialog {
0092         id: backgroundWarning
0093 
0094         onClosed: {
0095             Config.pdfWarningHidden = dontShow
0096         }
0097     }
0098 
0099     FileSaverDialog {
0100         id: pdfSaver
0101 
0102         caller: saveAction
0103         noteName: applicationWindow().pageStack.get(0).title
0104     }
0105 
0106     PdfMultiPageView {
0107         id: viewer
0108         width: Kirigami.Units.gridUnit * 15
0109         anchors.fill: parent
0110         document: PdfDocument { 
0111             id: pdfDoc
0112 
0113             onStatusChanged: function (status) {
0114                 if (status === PdfDocument.Ready && source.toString().endsWith("preview.pdf")) {
0115                     busyIndicator.visible = false
0116                 }
0117             }
0118         }
0119     }
0120 
0121     BusyIndicator {
0122         id: busyIndicator
0123         
0124         width: Kirigami.Units.gridUnit * 5
0125         height: width
0126         anchors.centerIn: parent
0127     }
0128 
0129     Timer {
0130         id: applyingCssTimer
0131 
0132         repeat: false
0133         interval: Kirigami.Units.longDuration
0134 
0135         onTriggered: {
0136             textDisplay.makePdf()
0137         }
0138     }
0139 
0140     Timer {
0141         id: changingDocTimer
0142 
0143         repeat: false
0144         interval: Kirigami.Units.longDuration
0145 
0146         onTriggered: {
0147             pdfDoc.source = pdfPath
0148         }
0149     }
0150 
0151     PrintingUtility {
0152         id: printingUtility
0153 
0154         onPdfCopyDone: function (succes, errorMessage) {
0155             if (!succes) {
0156                 showPassiveNotification(errorMessage)
0157             } else {
0158                 closePage()
0159             } 
0160         }
0161     }
0162 
0163     function displayPdf() {
0164         busyIndicator.visible = true
0165         printingUtility.writePdf(emptyPdf.substring(7)) 
0166         pdfDoc.source = emptyPdf
0167         changingDocTimer.start()
0168     }
0169 
0170     function requestPdf(changeBackground) {
0171         textDisplay.printBackground = changeBackground
0172         textDisplay.changeStyle(colors)
0173         applyingCssTimer.start()
0174     }
0175 
0176     function closePage() {
0177         applicationWindow().currentPageName = "Main"
0178         textDisplay.printBackground = true
0179         textDisplay.changeStyle(textDisplay.defaultCSS)
0180     }
0181 }