/** * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved. * * This file is part of the QCAD project. * * QCAD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * QCAD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with QCAD. */ include("scripts/Edit/DrawingPreferences/PageSettings/PageSettings.js"); include("scripts/File/File.js"); include("scripts/File/NewFile/NewFile.js"); include("scripts/File/Print/Print.js"); include("scripts/sprintf.js"); /** * Switches the current document to print preview mode. * In print preview mode, the paper is shown in the background of * the drawing. */ function PrintPreview(guiAction) { EAction.call(this, guiAction); } PrintPreview.prototype = new EAction(); PrintPreview.includeBasePath = includeBasePath; PrintPreview.instance = undefined; PrintPreview.getInstance = function() { return PrintPreview.instance; }; PrintPreview.isRunning = function() { var di = EAction.getDocumentInterface(); if (isNull(di)) { return false; } var a = di.getDefaultAction(); var ga = a.getGuiAction(); if (isNull(ga)) { return true; } else { return false; } }; /** * Starts the print preview. */ PrintPreview.start = function(initialAction, instance) { var appWin = RMainWindowQt.getMainWindow(); // appWin.enabled = false; var di = EAction.getDocumentInterface(); var doc = di.getDocument(); var a = di.getDefaultAction(); a.finishEvent(); if (isNull(instance)) { PrintPreview.instance = new PrintPreviewImpl(); } else { PrintPreview.instance = instance; } if (!isNull(initialAction)) { PrintPreview.instance.initialAction = initialAction; } // auto setup: var buttons = makeQMessageBoxStandardButtons(QMessageBox.Yes, QMessageBox.No); var paperSizeMM = Print.getPaperSizeMM(doc); var paperSizeName = Print.getPaperSizeNameFromSize(paperSizeMM); var paperSizeAccepted = doc.getVariable("PageSettings/PaperSizeAccepted"); if (paperSizeName==="" && paperSizeAccepted!==true) { // custom paper size: // ask user to adjust to default paper size: var defaultPaperSizeName = Print.getDefaultPaperSizeName(doc); var ret = QMessageBox.warning( appWin, qsTr("Auto Paper Size"), qsTr("The paper size is set to a custom size (%1x%2mm). Do you want to change it to the default paper size of your printer (%3)?").arg(paperSizeMM.width()).arg(paperSizeMM.height()).arg(defaultPaperSizeName), buttons ); if (ret===QMessageBox.Yes) { var paperUnit = Print.getPaperUnit(doc); var defPaperSize = Print.getDefaultPaperSizeMM(); var w = RUnit.convert(defPaperSize.width(), RS.Millimeter, paperUnit); doc.setVariable("PageSettings/PaperWidth", w); var h = RUnit.convert(defPaperSize.height(), RS.Millimeter, paperUnit); doc.setVariable("PageSettings/PaperHeight", h); doc.setVariable("MultiPageSettings/Columns", 1); doc.setVariable("MultiPageSettings/Rows", 1); // 10mm margin: var m = RUnit.convert(10, RS.Millimeter, paperUnit); doc.setVariable("MultiPageSettings/GlueMarginsLeft", m); doc.setVariable("MultiPageSettings/GlueMarginsTop", m); doc.setVariable("MultiPageSettings/GlueMarginsRight", m); doc.setVariable("MultiPageSettings/GlueMarginsBottom", m); } else { // store information that paper size was accepted as is (don't show dialog again): doc.setVariable("PageSettings/PaperSizeAccepted", true); } } var bbPaper = Print.getPaperBox(doc); bbPaper.growXY(bbPaper.getWidth()/100, bbPaper.getHeight()/100); var bbDoc = doc.getBoundingBox(); var positionAccepted = doc.getVariable("PageSettings/PositionAccepted"); // check if paper contains drawing: if (!bbPaper.containsBox(bbDoc) && !positionAccepted) { var initialZoom = appWin.property("PrintPreview/InitialZoom"); if (initialZoom!=="View") { var ret = QMessageBox.warning(appWin, qsTr("Auto fit"), qsTr("Auto fit drawing to paper?"), buttons); if (ret===QMessageBox.Yes) { appWin.setProperty("PrintPreview/InitialZoom", "Auto"); } else { doc.setVariable("PageSettings/PositionAccepted", true); } } } di.setDefaultAction(PrintPreview.instance); var ga = RGuiAction.getByScriptFile("scripts/File/PrintPreview/PrintPreview.js") if (!isNull(ga)) { ga.setChecked(true); } //appWin.enabled = true; }; /** * Exits the print preview. */ PrintPreview.exit = function() { //var appWin = RMainWindowQt.getMainWindow(); //appWin.enabled = false; var di = EAction.getDocumentInterface(); var a = di.getDefaultAction(); a.finishEvent(); NewFile.setupDefaultAction(di); var ga = RGuiAction.getByScriptFile("scripts/File/PrintPreview/PrintPreview.js") if (!isNull(ga)) { ga.setChecked(false); } //appWin.enabled = true; }; PrintPreview.prototype.beginEvent = function() { if (PrintPreview.isRunning()) { // exit print preview: PrintPreview.exit(); } else { // start print preview: PrintPreview.start(); } this.terminate(); }; /** * Implementation of interactive print preview action. * Intended to be used as default action. */ function PrintPreviewImpl(guiAction) { // parent class can be DefaultAction or other configured // default action: var defaultActionClass = NewFile.getDefaultActionClass(); this.parentClass = global[defaultActionClass]; this.parentClass.call(this, guiAction); this.enableSlotPaperSizeChanged = false; this.setUiOptions(PrintPreviewImpl.includeBasePath + "/PrintPreview.ui"); this.panningOffset = false; this.panOffsetOrigin = new RVector(); this.updateDisabled = false; if (RSettings.getBoolValue("GraphicsView/SystemCursors", false)===true) { this.cursor = new QCursor(Qt.OpenHandCursor); } else { var bitmap = new QBitmap(PrintPreviewImpl.includeBasePath + "/PrintPreviewOffsetCursor.png", "PNG"); var mask = new QBitmap(PrintPreviewImpl.includeBasePath + "/PrintPreviewOffsetCursorMask.png", "PNG"); this.cursor = new QCursor(bitmap, mask, 15, 13); } this.view = undefined; this.saveView = false; this.savedScale = undefined; this.savedOffset = undefined; this.savedColumns = undefined; this.savedRows = undefined; this.optOutRelativeZeroResume = true; } PrintPreviewImpl.prototype = NewFile.getDefaultAction(false); PrintPreviewImpl.includeBasePath = includeBasePath; PrintPreviewImpl.State = { SettingOffset : 100 }; PrintPreviewImpl.prototype.beginEvent = function() { var di = this.getDocumentInterface(); var appWin = RMainWindowQt.getMainWindow(); var initialZoom = appWin.property("PrintPreview/InitialZoom"); if (initialZoom==="View") { this.savedScale = Print.getScale(this.getDocument()); this.savedOffset = Print.getOffset(this.getDocument()); this.savedColumns = Print.getColumns(this.getDocument()); this.savedRows = Print.getRows(this.getDocument()); this.saveView = true; } // if (!isNull(this.guiAction)) { // this.guiAction.setChecked(true); // } // globals: //PrintPreview.setRunning(true); //PrintPreview.setInstance(this); var mdiChild = EAction.getMdiChild(); this.view = mdiChild.getLastKnownViewWithFocus(); di.disableUpdates(); this.parentClass.prototype.beginEvent.call(this); di.enableUpdates(); if (!isNull(this.view)) { var document = this.getDocument(); this.bgColor = this.view.getBackgroundColor(); this.updateBackgroundDecoration(); if (!isNull(this.view.getScene())) { if (RSettings.getBoolValue("GraphicsView/AutoSwitchLinetypes", false)===true) { this.view.getScene().setScreenBasedLinetypes(false); var ltModeAction = RGuiAction.getByScriptFile("scripts/View/LinetypeMode/LinetypeMode.js"); ltModeAction.setChecked(false); } } // needed to update linetype pattern scaling according to drawing scale: di.regenerateScenes(); } var action = RGuiAction.getByScriptFile("scripts/Edit/DrawingPreferences/DrawingPreferences.js"); action.triggered.connect(this, this.updateBackgroundDecoration); if (initialZoom==="Auto") { // auto fit drawing and auto set orientation: this.slotAutoFitDrawing(true); appWin.setProperty("PrintPreview/InitialZoom", "Stored"); } else if (initialZoom==="View") { Print.setColumns(di, 1); Print.setRows(di, 1); this.slotAutoFitBox(this.view.getBox()); appWin.setProperty("PrintPreview/InitialZoom", "Stored"); } if (this.initialAction==="Print") { this.slotPrint(); } else if (this.initialAction==="PdfExport") { this.slotPdfExport(); } // create listener to update preview when preferences changed: this.pAdapter = new RPreferencesListenerAdapter(); appWin.addPreferencesListener(this.pAdapter); this.pAdapter.preferencesUpdated.connect(this, this.updateFromPreferences); this.bAdapter = new RBlockListenerAdapter(); appWin.addBlockListener(this.bAdapter); this.bAdapter.blocksUpdated.connect(this, this.updateBackgroundTransform); this.bAdapter.currentBlockSet.connect(this, this.updateBackgroundTransform); this.bAdapter.blocksUpdated.connect(this, this.updateBackgroundDecoration); this.bAdapter.currentBlockSet.connect(this, this.updateBackgroundDecoration); }; /** * Handles additional state changes for offset moving state. */ PrintPreviewImpl.prototype.setState = function(state) { this.parentClass.prototype.setState.call(this, state); if (this.state === PrintPreviewImpl.State.SettingOffset) { this.getDocumentInterface().setClickMode(RAction.PickingDisabled); this.setCursor(this.cursor, "PrintPreviewOffsetCursor"); EAction.showMainTools(); EAction.getMainWindow().setLeftMouseTip(qsTr("Drag to move paper")); EAction.getMainWindow().setRightMouseTip(qsTr("Done")); } var optionsToolBar = EAction.getOptionsToolBar(); var offsetButton = optionsToolBar.findChild("Offset"); if (offsetButton) { offsetButton.checked = (this.state == PrintPreviewImpl.State.SettingOffset); } }; PrintPreviewImpl.prototype.suspendEvent = function() { EAction.prototype.suspendEvent.call(this); }; PrintPreviewImpl.prototype.finishEvent = function() { this.parentClass.prototype.finishEvent.call(this); var di = this.getDocumentInterface(); var appWin = RMainWindowQt.getMainWindow(); var initialZoom = appWin.property("PrintPreview/InitialZoom"); if (!isNull(this.view)) { this.view.setPrintPreview(false); this.view.setBackgroundColor(this.bgColor); this.view.setColorMode(RGraphicsView.FullColor); this.view.clearBackground(); if (!isNull(this.view.getScene())) { if (RSettings.getBoolValue("GraphicsView/AutoSwitchLinetypes", false)===true) { this.view.getScene().setScreenBasedLinetypes(true); var ltModeAction = RGuiAction.getByScriptFile("scripts/View/LinetypeMode/LinetypeMode.js"); ltModeAction.setChecked(true); } } // needed to update pattern scaling without using drawing scale: if (!isNull(di)) { di.regenerateScenes(); } var action = RGuiAction.getByScriptFile("scripts/Edit/DrawingPreferences/DrawingPreferences.js"); action.triggered.disconnect(this, this.updateBackgroundDecoration); } //PrintPreviewImpl.setRunning(false); //PrintPreviewImpl.setInstance(undefined); if (this.saveView===true) { if (!isNull(this.savedScale)) { Print.setScale(di, this.savedScale); } if (!isNull(this.savedOffset)) { Print.setOffset(di, this.savedOffset); } if (!isNull(this.savedColumns)) { Print.setColumns(di, this.savedColumns); } if (!isNull(this.savedRows)) { Print.setRows(di, this.savedRows); } } if (!isNull(this.pAdapter)) { appWin.removePreferencesListener(this.pAdapter); destr(this.pAdapter); } if (!isNull(this.bAdapter)) { appWin.removeBlockListener(this.bAdapter); destr(this.bAdapter); } }; /** * Implements moving of paper (offset). */ PrintPreviewImpl.prototype.mousePressEvent = function(event) { if (this.state === PrintPreviewImpl.State.SettingOffset) { if (event.button() == Qt.LeftButton && event.modifiers().valueOf() != Qt.ControlModifier.valueOf()) { this.panOffsetOrigin = event.getScreenPosition(); this.panningOffset = true; } } else { this.parentClass.prototype.mousePressEvent.call(this, event); } }; /** * Reimplemented from EAction to prevent that a right-click closes the * print preview. */ //PrintPreviewImpl.prototype.mouseReleaseEvent = function(event) { // if (event.button() == Qt.RightButton) { // if (this.state!=DefaultAction.State.Neutral) { // EAction.getDocumentInterface().clearPreview(); // EAction.getDocumentInterface().repaintViews(); // this.setState(DefaultAction.State.Neutral); // } // } // else { // this.parentClass.prototype.mouseReleaseEvent.call(this, event); // } //}; /** * Implements moving of paper (offset). */ PrintPreviewImpl.prototype.mouseMoveEvent = function(event) { if (this.state !== PrintPreviewImpl.State.SettingOffset) { this.parentClass.prototype.mouseMoveEvent.call(this, event); return; } if (event.buttons().valueOf() & Qt.LeftButton.valueOf() && event.modifiers().valueOf() != Qt.ControlModifier.valueOf()) { if (!this.panningOffset) { return; } var panTarget = event.getScreenPosition(); var panDelta = panTarget.operator_subtract(this.panOffsetOrigin); if (Math.abs(panDelta.x) < 1 && Math.abs(panDelta.y) < 1) { return; } this.panOffsetOrigin = panTarget; var di = this.getDocumentInterface(); var document = di.getDocument(); var offset = Print.getOffset(document); offset.x += this.view.mapDistanceFromView(panDelta.x); offset.y -= this.view.mapDistanceFromView(panDelta.y); // update document variables Print.setOffset(di, offset); this.updateBackgroundTransform(); this.view.regenerate(); } }; /** * Reimplementation from EAction. Implements initialization of options * toolbar from member variables. */ PrintPreviewImpl.prototype.showUiOptions = function(resume) { this.updateDisabled = true; EAction.prototype.showUiOptions.call(this, resume); this.updateDisabled = false; var optionsToolBar = EAction.getOptionsToolBar(); var widgets = getWidgets(optionsToolBar); var document = this.getDocument(); var di = this.getDocumentInterface(); var scaleString = Print.getScaleString(document); var mod = document.isModified(); // note: this modifies the document: this.initScaleCombo(); // update option toolbar widgets widgets["ShowPaperBorders"].blockSignals(true); widgets["ShowPaperBorders"].checked = Print.getShowPaperBorders(document); widgets["ShowPaperBorders"].blockSignals(false); widgets["PrintCropMarks"].blockSignals(true); widgets["PrintCropMarks"].checked = Print.getPrintCropMarks(document); widgets["PrintCropMarks"].blockSignals(false); var scaleCombo = widgets["Scale"]; scaleCombo.blockSignals(true); scaleCombo.setEditText(scaleString); di.disableUpdates(); this.slotScaleChanged(scaleString); di.enableUpdates(); scaleCombo.blockSignals(false); var action = RGuiAction.getByScriptFile("scripts/Edit/DrawingPreferences/DrawingPreferences.js"); widgets["ShowMoreOptions"].setDefaultAction(action); switch(Print.getColorMode(document)) { case RGraphicsView.FullColor: widgets["FullColor"].blockSignals(true); widgets["FullColor"].checked=true; widgets["FullColor"].blockSignals(false); break; case RGraphicsView.GrayScale: widgets["Grayscale"].blockSignals(true); widgets["Grayscale"].checked=true; widgets["Grayscale"].blockSignals(false); break; case RGraphicsView.BlackWhite: widgets["BlackWhite"].blockSignals(true); widgets["BlackWhite"].checked=true; widgets["BlackWhite"].blockSignals(false); break; } if (Print.getHairlineMode(document)) { if (!isNull(widgets["Hairline"])) { widgets["Hairline"].blockSignals(true); widgets["Hairline"].checked=true; widgets["Hairline"].blockSignals(false); } } widgets["Portrait"].blockSignals(true); widgets["Landscape"].blockSignals(true); if (Print.getPageOrientationEnum(document) === RS.Portrait) { widgets["Portrait"].checked=true; } else { widgets["Landscape"].checked=true; } widgets["Landscape"].blockSignals(false); widgets["Portrait"].blockSignals(false); // workaround for FS#2356 - File > Close: Closing dialog stays open after clicking Save document.setModified(mod); }; /** * Initializes the scale combo box in the options toolbar with defaults. */ PrintPreviewImpl.prototype.initUiOptions = function(resume, optionsToolBar) { this.initScaleCombo(); }; PrintPreviewImpl.prototype.initScaleCombo = function() { var document = this.getDocument(); if (isNull(document)) { return; } var optionsToolBar = EAction.getOptionsToolBar(); var scaleCombo = optionsToolBar.findChild("Scale"); scaleCombo.blockSignals(true); var prev = scaleCombo.currentText; scaleCombo.clear(); scaleCombo.setScale(true, document.getUnit()) // var scales = this.getScales(); // for (var i=0; i