Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "printprovider.h"
23 :
24 : #include <QPrinter>
25 : #include <QPrintDialog>
26 : #include <QWindow>
27 :
28 : #include "draw/painter.h"
29 :
30 : #include "notation/inotationpainting.h"
31 :
32 : #include "log.h"
33 :
34 : using namespace muse;
35 : using namespace muse::draw;
36 : using namespace mu::notation;
37 :
38 : namespace mu::print {
39 0 : PrintProvider::PrintProvider(const kors::modularity::ContextPtr& iocCtx)
40 0 : : Contextable(iocCtx)
41 : {
42 0 : }
43 :
44 0 : Ret PrintProvider::printNotation(INotationPtr notation)
45 : {
46 0 : IF_ASSERT_FAILED(notation) {
47 0 : return make_ret(Ret::Code::InternalError);
48 : }
49 :
50 0 : INotationPaintingPtr painting = notation->painting();
51 :
52 0 : SizeF pageSizeInch = painting->pageSizeInch();
53 0 : QPrinter printerDev(QPrinter::HighResolution);
54 0 : QPageSize ps(QPageSize::id(pageSizeInch.toQSizeF(), QPageSize::Inch));
55 0 : printerDev.setPageSize(ps);
56 0 : printerDev.setPageOrientation(pageSizeInch.width() > pageSizeInch.height() ? QPageLayout::Landscape : QPageLayout::Portrait);
57 :
58 : //printerDev.setCreator("MuseScore Studio Version: " VERSION);
59 0 : printerDev.setFullPage(true);
60 0 : if (!printerDev.setPageMargins(QMarginsF())) {
61 0 : LOGW() << "unable to clear printer margins";
62 : }
63 :
64 0 : printerDev.setDocName(notation->projectWorkTitleAndPartName());
65 0 : printerDev.setOutputFormat(QPrinter::NativeFormat);
66 0 : printerDev.setFromTo(1, painting->pageCount());
67 :
68 0 : QPrintDialog pd(&printerDev);
69 0 : pd.setMinMax(1, painting->pageCount());
70 :
71 : // HACK: ensure we have a valid windowHandle to which we can set a transient parent to
72 0 : pd.winId();
73 : // the print dialog needs a valid parent window to show the modern print dialog on Windows 11
74 0 : pd.windowHandle()->setTransientParent(mainWindow()->qWindow());
75 0 : if (!pd.exec()) {
76 0 : return muse::make_ret(Ret::Code::Cancel);
77 : }
78 :
79 0 : Painter painter(&printerDev, "print");
80 :
81 0 : INotationPainting::Options opt;
82 0 : opt.fromPage = printerDev.fromPage() - 1;
83 0 : opt.toPage = printerDev.toPage() - 1;
84 : // See https://doc.qt.io/qt-5/qprinter.html#supportsMultipleCopies
85 0 : opt.copyCount = printerDev.supportsMultipleCopies() ? 1 : printerDev.copyCount();
86 0 : opt.deviceDpi = printerDev.logicalDpiX();
87 0 : opt.onNewPage = [&printerDev]() { printerDev.newPage(); };
88 :
89 0 : painting->paintPrint(&painter, opt);
90 :
91 0 : painter.endDraw();
92 :
93 0 : return muse::make_ok();
94 0 : }
95 : }
|