From 185724f8926db7ba66055c66c0c9002db839cae8 Mon Sep 17 00:00:00 2001 From: Mathieu Pellerin Date: Sun, 21 Jan 2024 10:10:20 +0700 Subject: [PATCH] Make QtPrinter support non-mandatory --- CMakeLists.txt | 13 ++++++++++--- cmake_templates/qgsconfig.h.in | 2 ++ python/CMakeLists.txt | 4 ++++ .../auto_generated/layout/qgslayoutexporter.sip.in | 3 +++ python/PyQt6/core/core.sip.in | 1 + .../auto_generated/layout/qgslayoutexporter.sip.in | 3 +++ python/core/core.sip.in | 1 + src/analysis/processing/qgsnativealgorithms.cpp | 4 ---- src/core/CMakeLists.txt | 10 +++++----- src/core/layout/qgslayoutexporter.cpp | 10 +++++----- src/core/layout/qgslayoutexporter.h | 8 ++++++-- src/core/project/qgsprojectfiletransform.cpp | 5 +++-- src/core/qgsmultirenderchecker.cpp | 5 ----- src/core/raster/qgsrasterdrawer.cpp | 10 +++------- src/core/symbology/qgsfillsymbollayer.cpp | 10 ++-------- tests/src/analysis/testqgsprocessingalgspt2.cpp | 4 ---- 16 files changed, 48 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c40151076df..2318388f508 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -496,11 +496,18 @@ if(WITH_CORE) if (BUILD_WITH_QT6) find_package(${QT_VERSION_BASE} COMPONENTS Core5Compat REQUIRED) endif() + if (NOT IOS) - find_package(${QT_VERSION_BASE} COMPONENTS PrintSupport REQUIRED) - else() - add_definitions(-DQT_NO_PRINTER) + set (DEFAULT_WITH_QTPRINTER TRUE) + else () + set (DEFAULT_WITH_QTPRINTER FALSE) endif() + set (WITH_QTPRINTER ${DEFAULT_WITH_QTPRINTER} CACHE BOOL "Enable QtPrinter support") + if (WITH_QTPRINTER) + find_package(${QT_VERSION_BASE} COMPONENTS PrintSupport REQUIRED) + set (HAVE_QTPRINTER TRUE) + endif() + if (WITH_QTWEBKIT) if(BUILD_WITH_QT6) message(FATAL_ERROR "Qt WebKit support cannot be enabled on Qt 6 builds") diff --git a/cmake_templates/qgsconfig.h.in b/cmake_templates/qgsconfig.h.in index 0a8742f2b61..0ed93b22edc 100644 --- a/cmake_templates/qgsconfig.h.in +++ b/cmake_templates/qgsconfig.h.in @@ -98,6 +98,8 @@ #cmakedefine HAVE_QTGAMEPAD +#cmakedefine HAVE_QTPRINTER + #cmakedefine HAVE_STATIC_PROVIDERS #cmakedefine HAVE_EPT diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 978b37e8812..0b267ce78ca 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -170,6 +170,10 @@ if(NOT WITH_QTSERIALPORT) set(SIP_DISABLE_FEATURES ${SIP_DISABLE_FEATURES} HAVE_QTSERIALPORT) endif() +if(NOT WITH_QTPRINTER) + set(SIP_DISABLE_FEATURES ${SIP_DISABLE_FEATURES} HAVE_QTPRINTER) +endif() + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/core/project.py.in ${CMAKE_CURRENT_BINARY_DIR}/core/project.py @ONLY) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/core/pyproject.toml.in ${CMAKE_CURRENT_BINARY_DIR}/core/pyproject.toml @ONLY) GENERATE_SIP_PYTHON_MODULE_CODE(qgis._core core/core.sip "${sip_files_core}" cpp_files) diff --git a/python/PyQt6/core/auto_generated/layout/qgslayoutexporter.sip.in b/python/PyQt6/core/auto_generated/layout/qgslayoutexporter.sip.in index 29243edd3ec..9611931e437 100644 --- a/python/PyQt6/core/auto_generated/layout/qgslayoutexporter.sip.in +++ b/python/PyQt6/core/auto_generated/layout/qgslayoutexporter.sip.in @@ -272,6 +272,7 @@ Constructor for PrintExportSettings }; +%If (HAVE_QTPRINTER) ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings ); %Docstring @@ -290,6 +291,8 @@ Exports a layout ``iterator`` to a ``printer``, with the specified export ``sett Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then ``error`` will be set to the error description. +%End + %End struct SvgExportSettings diff --git a/python/PyQt6/core/core.sip.in b/python/PyQt6/core/core.sip.in index 95b43bb5332..ff86c820c26 100644 --- a/python/PyQt6/core/core.sip.in +++ b/python/PyQt6/core/core.sip.in @@ -101,6 +101,7 @@ done: %Feature HAVE_GUI %Feature HAVE_QTSERIALPORT +%Feature HAVE_QTPRINTER %Feature ANDROID %Feature VECTOR_MAPPED_TYPE %Feature PYQT6 diff --git a/python/core/auto_generated/layout/qgslayoutexporter.sip.in b/python/core/auto_generated/layout/qgslayoutexporter.sip.in index 29243edd3ec..9611931e437 100644 --- a/python/core/auto_generated/layout/qgslayoutexporter.sip.in +++ b/python/core/auto_generated/layout/qgslayoutexporter.sip.in @@ -272,6 +272,7 @@ Constructor for PrintExportSettings }; +%If (HAVE_QTPRINTER) ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings ); %Docstring @@ -290,6 +291,8 @@ Exports a layout ``iterator`` to a ``printer``, with the specified export ``sett Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then ``error`` will be set to the error description. +%End + %End struct SvgExportSettings diff --git a/python/core/core.sip.in b/python/core/core.sip.in index a860638bf93..f387a125b90 100644 --- a/python/core/core.sip.in +++ b/python/core/core.sip.in @@ -101,6 +101,7 @@ done: %Feature HAVE_GUI %Feature HAVE_QTSERIALPORT +%Feature HAVE_QTPRINTER %Feature ANDROID %Feature VECTOR_MAPPED_TYPE diff --git a/src/analysis/processing/qgsnativealgorithms.cpp b/src/analysis/processing/qgsnativealgorithms.cpp index 179b15a3ba8..f6b23a735d0 100644 --- a/src/analysis/processing/qgsnativealgorithms.cpp +++ b/src/analysis/processing/qgsnativealgorithms.cpp @@ -115,12 +115,10 @@ #include "qgsalgorithmintersection.h" #include "qgsalgorithmkeepnbiggestparts.h" #include "qgsalgorithmkmeansclustering.h" -#ifndef QT_NO_PRINTER #include "qgsalgorithmlayoutatlastoimage.h" #include "qgsalgorithmlayoutatlastopdf.h" #include "qgsalgorithmlayouttoimage.h" #include "qgsalgorithmlayouttopdf.h" -#endif #include "qgsalgorithmlinedensity.h" #include "qgsalgorithmlineintersection.h" #include "qgsalgorithmlinesubstring.h" @@ -398,13 +396,11 @@ void QgsNativeAlgorithms::loadAlgorithms() addAlgorithm( new QgsKMeansClusteringAlgorithm() ); addAlgorithm( new QgsLayerToBookmarksAlgorithm() ); addAlgorithm( new QgsLayoutMapExtentToLayerAlgorithm() ); -#ifndef QT_NO_PRINTER addAlgorithm( new QgsLayoutAtlasToImageAlgorithm() ); addAlgorithm( new QgsLayoutAtlasToPdfAlgorithm() ); addAlgorithm( new QgsLayoutAtlasToMultiplePdfAlgorithm() ); addAlgorithm( new QgsLayoutToImageAlgorithm() ); addAlgorithm( new QgsLayoutToPdfAlgorithm() ); -#endif addAlgorithm( new QgsLineDensityAlgorithm() ); addAlgorithm( new QgsLineIntersectionAlgorithm() ); addAlgorithm( new QgsLineSubstringAlgorithm() ); diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index aefefdf5a28..ede90783537 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -2242,7 +2242,6 @@ target_include_directories(qgis_core SYSTEM PUBLIC ${${QT_VERSION_BASE}Xml_INCLUDE_DIRS} ${${QT_VERSION_BASE}Widgets_INCLUDE_DIRS} ${${QT_VERSION_BASE}Svg_INCLUDE_DIRS} - ${${QT_VERSION_BASE}PrintSupport_INCLUDE_DIRS} ${${QT_VERSION_BASE}Network_INCLUDE_DIRS} ${${QT_VERSION_BASE}Sql_INCLUDE_DIRS} ${${QT_VERSION_BASE}Concurrent_INCLUDE_DIRS} @@ -2425,12 +2424,13 @@ target_link_libraries(qgis_core PROJ::proj ) -if (WITH_DRACO) - target_link_libraries(qgis_core ${DRACO_LIBRARY}) +if (WITH_QTPRINTER) + target_link_libraries(qgis_core ${QT_VERSION_BASE}::PrintSupport) + target_include_directories(qgis_core SYSTEM PUBLIC ${${QT_VERSION_BASE}PrintSupport_INCLUDE_DIRS}) endif() -if (NOT IOS) - target_link_libraries(qgis_core ${QT_VERSION_BASE}::PrintSupport) +if (WITH_DRACO) + target_link_libraries(qgis_core ${DRACO_LIBRARY}) endif() if (WITH_SPATIALITE) diff --git a/src/core/layout/qgslayoutexporter.cpp b/src/core/layout/qgslayoutexporter.cpp index 9b02d557a6a..84e700d693b 100644 --- a/src/core/layout/qgslayoutexporter.cpp +++ b/src/core/layout/qgslayoutexporter.cpp @@ -868,7 +868,7 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdfs( QgsAbstractLayo return Success; } -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) QgsLayoutExporter::ExportResult QgsLayoutExporter::print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &s ) { if ( !mLayout ) @@ -988,7 +988,7 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::print( QgsAbstractLayoutItera iterator->endRender(); return Success; } -#endif // QT_NO_PRINTER +#endif // HAVE_QTPRINTER QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &s ) { @@ -1255,7 +1255,7 @@ void QgsLayoutExporter::preparePrint( QgsLayout *layout, QPagedPaintDevice *devi { pdf->setResolution( static_cast< int>( std::round( layout->renderContext().dpi() ) ) ); } -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) else if ( QPrinter *printer = dynamic_cast( device ) ) { printer->setFullPage( true ); @@ -1295,7 +1295,7 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::printPrivate( QPagedPaintDevi int fromPage = 0; int toPage = mLayout->pageCollection()->pageCount() - 1; -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) if ( QPrinter *printer = dynamic_cast( device ) ) { if ( printer->fromPage() >= 1 ) @@ -1368,7 +1368,7 @@ void QgsLayoutExporter::updatePrinterPageSize( QgsLayout *layout, QPagedPaintDev device->setPageLayout( pageLayout ); device->setPageMargins( QMarginsF( 0, 0, 0, 0 ) ); -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) if ( QPrinter *printer = dynamic_cast( device ) ) { printer->setFullPage( true ); diff --git a/src/core/layout/qgslayoutexporter.h b/src/core/layout/qgslayoutexporter.h index b0f1608b946..05808e50618 100644 --- a/src/core/layout/qgslayoutexporter.h +++ b/src/core/layout/qgslayoutexporter.h @@ -17,6 +17,7 @@ #define QGSLAYOUTEXPORTER_H #include "qgis_core.h" +#include "qgsconfig.h" #include "qgsmargins.h" #include "qgslayoutrendercontext.h" #include "qgslayoutreportcontext.h" @@ -29,7 +30,7 @@ #include #include -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) #include #endif @@ -471,7 +472,8 @@ class CORE_EXPORT QgsLayoutExporter }; -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) + SIP_IF_FEATURE( HAVE_QTPRINTER ) /** * Prints the layout to a \a printer, using the specified export \a settings. @@ -491,6 +493,8 @@ class CORE_EXPORT QgsLayoutExporter static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr ); + + SIP_END #endif //! Contains settings relating to exporting layouts to SVG diff --git a/src/core/project/qgsprojectfiletransform.cpp b/src/core/project/qgsprojectfiletransform.cpp index c2fd0b961ea..5a21596b7ec 100644 --- a/src/core/project/qgsprojectfiletransform.cpp +++ b/src/core/project/qgsprojectfiletransform.cpp @@ -33,11 +33,12 @@ #include "qgsxmlutils.h" #include "qgssymbollayerreference.h" #include "qgssymbollayerutils.h" +#include "qgsconfig.h" #include #include #include -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) #include //to find out screen resolution #endif #include @@ -304,7 +305,7 @@ void transform0100to0110( QgsProjectFileTransform *pft ) { if ( ! pft->dom().isNull() ) { -#ifndef QT_NO_PRINTER +#if defined( HAVE_QTPRINTER ) //Change 'outlinewidth' in QgsSymbol const QPrinter myPrinter( QPrinter::ScreenResolution ); const int screenDpi = myPrinter.resolution(); diff --git a/src/core/qgsmultirenderchecker.cpp b/src/core/qgsmultirenderchecker.cpp index 5d74fd53a4b..7dbab9950cf 100644 --- a/src/core/qgsmultirenderchecker.cpp +++ b/src/core/qgsmultirenderchecker.cpp @@ -266,9 +266,6 @@ QgsLayoutChecker::QgsLayoutChecker( const QString &testName, QgsLayout *layout ) bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDiff, bool createReferenceImage ) { -#ifdef QT_NO_PRINTER - return false; -#else if ( !mLayout ) { return false; @@ -322,9 +319,7 @@ bool QgsLayoutChecker::testLayout( QString &checkedReport, int page, int pixelDi checkedReport += report(); return testResult; -#endif // QT_NO_PRINTER } - ///@endcond diff --git a/src/core/raster/qgsrasterdrawer.cpp b/src/core/raster/qgsrasterdrawer.cpp index 0ac05287a2b..123603b16a9 100644 --- a/src/core/raster/qgsrasterdrawer.cpp +++ b/src/core/raster/qgsrasterdrawer.cpp @@ -25,9 +25,7 @@ #include "qgsrendercontext.h" #include #include -#ifndef QT_NO_PRINTER -#include -#endif +#include QgsRasterDrawer::QgsRasterDrawer( QgsRasterIterator *iterator, double dpiTarget ) : mIterator( iterator ) @@ -94,11 +92,10 @@ void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsM QImage img = block->image(); -#ifndef QT_NO_PRINTER // Because of bug in Acrobat Reader we must use "white" transparent color instead // of "black" for PDF. See #9101. - QPrinter *printer = dynamic_cast( p->device() ); - if ( printer && printer->outputFormat() == QPrinter::PdfFormat ) + QPdfWriter *pdfWriter = dynamic_cast( p->device() ); + if ( pdfWriter ) { QgsDebugMsgLevel( QStringLiteral( "PdfFormat" ), 4 ); @@ -116,7 +113,6 @@ void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsM } } } -#endif if ( feedback && feedback->renderPartialOutput() ) { diff --git a/src/core/symbology/qgsfillsymbollayer.cpp b/src/core/symbology/qgsfillsymbollayer.cpp index e1f082286fa..830bc37d02b 100644 --- a/src/core/symbology/qgsfillsymbollayer.cpp +++ b/src/core/symbology/qgsfillsymbollayer.cpp @@ -41,6 +41,7 @@ #include "qgscolorutils.h" #include +#include #include #include #include @@ -48,9 +49,6 @@ #include #include -#ifndef QT_NO_PRINTER -#include -#endif QgsSimpleFillSymbolLayer::QgsSimpleFillSymbolLayer( const QColor &color, Qt::BrushStyle style, const QColor &strokeColor, Qt::PenStyle strokeStyle, double strokeWidth, Qt::PenJoinStyle penJoinStyle ) @@ -328,15 +326,12 @@ void QgsSimpleFillSymbolLayer::renderPolygon( const QPolygonF &points, const QVe const bool useSelectedColor = shouldRenderUsingSelectionColor( context ); -#ifndef QT_NO_PRINTER - if ( mBrush.style() == Qt::SolidPattern || mBrush.style() == Qt::NoBrush || !dynamic_cast( p->device() ) ) -#endif + if ( mBrush.style() == Qt::SolidPattern || mBrush.style() == Qt::NoBrush || !dynamic_cast( p->device() ) ) { p->setPen( useSelectedColor ? mSelPen : mPen ); p->setBrush( useSelectedColor ? mSelBrush : mBrush ); _renderPolygon( p, points, rings, context ); } -#ifndef QT_NO_PRINTER else { // workaround upstream issue https://github.com/qgis/QGIS/issues/36580 @@ -350,7 +345,6 @@ void QgsSimpleFillSymbolLayer::renderPolygon( const QPolygonF &points, const QVe p->setBrush( Qt::NoBrush ); _renderPolygon( p, points, rings, context ); } -#endif if ( !offset.isNull() ) { diff --git a/tests/src/analysis/testqgsprocessingalgspt2.cpp b/tests/src/analysis/testqgsprocessingalgspt2.cpp index 0cdf0577ae9..d61c7a45876 100644 --- a/tests/src/analysis/testqgsprocessingalgspt2.cpp +++ b/tests/src/analysis/testqgsprocessingalgspt2.cpp @@ -63,13 +63,11 @@ class TestQgsProcessingAlgsPt2: public QgsTest void init() {} // will be called before each testfunction is executed. void cleanup() {} // will be called after every testfunction. -#ifndef QT_NO_PRINTER void exportLayoutPdf(); void exportLayoutPng(); void exportAtlasLayoutPdf(); void exportAtlasLayoutPdfMultiple(); void exportAtlasLayoutPng(); -#endif void tinMeshCreation(); void exportMeshVertices(); @@ -220,7 +218,6 @@ QVariantMap pkgAlg( const QStringList &layers, const QString &outputGpkg, bool o return package->run( parameters, *context, &feedback, ok ); } -#ifndef QT_NO_PRINTER void TestQgsProcessingAlgsPt2::exportLayoutPdf() { QgsProject p; @@ -464,7 +461,6 @@ void TestQgsProcessingAlgsPt2::exportAtlasLayoutPng() QVERIFY( QFile::exists( QDir::tempPath() + "/my_atlas/custom_10.png" ) ); QGSVERIFYIMAGECHECK( "export_atlas_custom_layers", "export_atlas_custom_layers", QDir::tempPath() + "/my_atlas/custom_1.png", QString(), 500, QSize( 3, 3 ) ); } -#endif void TestQgsProcessingAlgsPt2::tinMeshCreation() {