From 778c95d7a181e702766bf0cdf63c50dd1e7d9d9b Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 28 May 2020 17:12:49 +0200 Subject: Move QSvgWidget and QGraphicsSvgItem into separate module Long live QtSvgWidgets. This makes QtSvg independent of QtWidgets. Fixes: QTBUG-41884 Change-Id: I421d27d35b651ee8cdf1a5280d5594b82359ddab Reviewed-by: Lars Knoll --- .../delayedencoding/delayedencoding.pro | 2 +- .../draganddrop/delayedencoding/sourcewidget.cpp | 1 + examples/svg/embedded/weatherinfo/weatherinfo.cpp | 2 +- examples/svg/embedded/weatherinfo/weatherinfo.pro | 2 +- examples/svg/svgviewer/svgviewer.pro | 2 +- src/CMakeLists.txt | 4 + src/src.pro | 1 + src/svg/.prev_CMakeLists.txt | 11 - src/svg/CMakeLists.txt | 2 - src/svg/qgraphicssvgitem.cpp | 388 --------------------- src/svg/qgraphicssvgitem.h | 99 ------ src/svg/qsvgwidget.cpp | 176 ---------- src/svg/qsvgwidget.h | 83 ----- src/svg/qtsvgglobal.h | 2 +- src/svg/svg.pro | 4 - src/svgwidgets/.prev_CMakeLists.txt | 25 ++ src/svgwidgets/CMakeLists.txt | 25 ++ src/svgwidgets/qgraphicssvgitem.cpp | 388 +++++++++++++++++++++ src/svgwidgets/qgraphicssvgitem.h | 97 ++++++ src/svgwidgets/qsvgwidget.cpp | 172 +++++++++ src/svgwidgets/qsvgwidget.h | 77 ++++ src/svgwidgets/qtsvgwidgetsglobal.h | 61 ++++ src/svgwidgets/svgwidgets.pro | 16 + sync.profile | 1 + 24 files changed, 873 insertions(+), 768 deletions(-) delete mode 100644 src/svg/qgraphicssvgitem.cpp delete mode 100644 src/svg/qgraphicssvgitem.h delete mode 100644 src/svg/qsvgwidget.cpp delete mode 100644 src/svg/qsvgwidget.h create mode 100644 src/svgwidgets/.prev_CMakeLists.txt create mode 100644 src/svgwidgets/CMakeLists.txt create mode 100644 src/svgwidgets/qgraphicssvgitem.cpp create mode 100644 src/svgwidgets/qgraphicssvgitem.h create mode 100644 src/svgwidgets/qsvgwidget.cpp create mode 100644 src/svgwidgets/qsvgwidget.h create mode 100644 src/svgwidgets/qtsvgwidgetsglobal.h create mode 100644 src/svgwidgets/svgwidgets.pro diff --git a/examples/svg/draganddrop/delayedencoding/delayedencoding.pro b/examples/svg/draganddrop/delayedencoding/delayedencoding.pro index a0a8371..7e8c14d 100644 --- a/examples/svg/draganddrop/delayedencoding/delayedencoding.pro +++ b/examples/svg/draganddrop/delayedencoding/delayedencoding.pro @@ -1,4 +1,4 @@ -QT += widgets svg +QT += widgets svg svgwidgets HEADERS = mimedata.h \ sourcewidget.h diff --git a/examples/svg/draganddrop/delayedencoding/sourcewidget.cpp b/examples/svg/draganddrop/delayedencoding/sourcewidget.cpp index 9db5748..943c5be 100644 --- a/examples/svg/draganddrop/delayedencoding/sourcewidget.cpp +++ b/examples/svg/draganddrop/delayedencoding/sourcewidget.cpp @@ -50,6 +50,7 @@ #include #include +#include #include "mimedata.h" #include "sourcewidget.h" diff --git a/examples/svg/embedded/weatherinfo/weatherinfo.cpp b/examples/svg/embedded/weatherinfo/weatherinfo.cpp index 21aa986..53caf9a 100644 --- a/examples/svg/embedded/weatherinfo/weatherinfo.cpp +++ b/examples/svg/embedded/weatherinfo/weatherinfo.cpp @@ -51,7 +51,7 @@ #include #include #include -#include +#include #define GET_DATA_ATTR(val) xml.attributes().value(val).toString() #define GET_DATETIME(val) QDateTime::fromString(val, "yyyy-MM-ddThh:mm:ss") diff --git a/examples/svg/embedded/weatherinfo/weatherinfo.pro b/examples/svg/embedded/weatherinfo/weatherinfo.pro index b2c94cf..e69bbe4 100644 --- a/examples/svg/embedded/weatherinfo/weatherinfo.pro +++ b/examples/svg/embedded/weatherinfo/weatherinfo.pro @@ -2,7 +2,7 @@ TEMPLATE = app TARGET = weatherinfo SOURCES = weatherinfo.cpp RESOURCES = weatherinfo.qrc -QT += network widgets svg +QT += network widgets svg svgwidgets target.path = $$[QT_INSTALL_EXAMPLES]/svg/embedded/weatherinfo INSTALLS += target diff --git a/examples/svg/svgviewer/svgviewer.pro b/examples/svg/svgviewer/svgviewer.pro index 427d13a..1a54025 100644 --- a/examples/svg/svgviewer/svgviewer.pro +++ b/examples/svg/svgviewer/svgviewer.pro @@ -6,7 +6,7 @@ SOURCES = main.cpp \ mainwindow.cpp \ svgview.cpp \ exportdialog.cpp -QT += widgets svg +QT += widgets svg svgwidgets qtHaveModule(opengl): QT += opengl openglwidgets diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f1b6e3b..344fbe5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,3 +4,7 @@ if(TARGET Qt::Gui) add_subdirectory(svg) add_subdirectory(plugins) endif() + +if(TARGET Qt::Widgets) + add_subdirectory(svgwidgets) +endif() diff --git a/src/src.pro b/src/src.pro index a1b5d69..1a1d4d4 100644 --- a/src/src.pro +++ b/src/src.pro @@ -1,3 +1,4 @@ TEMPLATE = subdirs CONFIG += ordered qtHaveModule(gui): SUBDIRS += svg plugins +qtHaveModule(widgets): SUBDIRS += svgwidgets diff --git a/src/svg/.prev_CMakeLists.txt b/src/svg/.prev_CMakeLists.txt index bf9e056..31374ee 100644 --- a/src/svg/.prev_CMakeLists.txt +++ b/src/svg/.prev_CMakeLists.txt @@ -6,7 +6,6 @@ qt_add_module(Svg SOURCES - qgraphicssvgitem.cpp qgraphicssvgitem.h qsvgfont.cpp qsvgfont_p.h qsvggenerator.cpp qsvggenerator.h qsvggraphics.cpp qsvggraphics_p.h @@ -16,7 +15,6 @@ qt_add_module(Svg qsvgstructure.cpp qsvgstructure_p.h qsvgstyle.cpp qsvgstyle_p.h qsvgtinydocument.cpp qsvgtinydocument_p.h - qsvgwidget.cpp qsvgwidget.h qtsvgglobal.h DEFINES QT_NO_USING_NAMESPACE @@ -34,15 +32,6 @@ qt_add_module(Svg ## Scopes: ##################################################################### -qt_extend_target(Svg CONDITION TARGET Qt::Widgets - LIBRARIES - Qt::WidgetsPrivate - PUBLIC_LIBRARIES - Qt::Widgets - PRIVATE_MODULE_INTERFACE - Qt::WidgetsPrivate -) - qt_extend_target(Svg CONDITION MSVC AND (TEST_architecture_arch STREQUAL "i386") LINK_OPTIONS "/BASE:0x66000000" diff --git a/src/svg/CMakeLists.txt b/src/svg/CMakeLists.txt index 241b5fc..ca09b70 100644 --- a/src/svg/CMakeLists.txt +++ b/src/svg/CMakeLists.txt @@ -15,7 +15,6 @@ endif() qt_add_module(Svg SOURCES - qgraphicssvgitem.cpp qgraphicssvgitem.h qsvgfont.cpp qsvgfont_p.h qsvggenerator.cpp qsvggenerator.h qsvggraphics.cpp qsvggraphics_p.h @@ -25,7 +24,6 @@ qt_add_module(Svg qsvgstructure.cpp qsvgstructure_p.h qsvgstyle.cpp qsvgstyle_p.h qsvgtinydocument.cpp qsvgtinydocument_p.h - qsvgwidget.cpp qsvgwidget.h qtsvgglobal.h DEFINES QT_NO_USING_NAMESPACE diff --git a/src/svg/qgraphicssvgitem.cpp b/src/svg/qgraphicssvgitem.cpp deleted file mode 100644 index fe6e92e..0000000 --- a/src/svg/qgraphicssvgitem.cpp +++ /dev/null @@ -1,388 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt SVG module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include "qgraphicssvgitem.h" - -#if !defined(QT_NO_GRAPHICSVIEW) && !defined(QT_NO_WIDGETS) - -#include "qpainter.h" -#include "qstyleoption.h" -#include "qsvgrenderer.h" -#include "qdebug.h" - -#include "private/qobject_p.h" -#include "private/qgraphicsitem_p.h" - -QT_BEGIN_NAMESPACE - -class QGraphicsSvgItemPrivate : public QGraphicsItemPrivate -{ -public: - Q_DECLARE_PUBLIC(QGraphicsSvgItem) - - QGraphicsSvgItemPrivate() - : renderer(0), shared(false) - { - } - - void init(QGraphicsItem *parent) - { - Q_Q(QGraphicsSvgItem); - q->setParentItem(parent); - renderer = new QSvgRenderer(q); - QObject::connect(renderer, SIGNAL(repaintNeeded()), - q, SLOT(_q_repaintItem())); - q->setCacheMode(QGraphicsItem::DeviceCoordinateCache); - q->setMaximumCacheSize(QSize(1024, 768)); - } - - void _q_repaintItem() - { - q_func()->update(); - } - - inline void updateDefaultSize() - { - QRectF bounds; - if (elemId.isEmpty()) { - bounds = QRectF(QPointF(0, 0), renderer->defaultSize()); - } else { - bounds = renderer->boundsOnElement(elemId); - } - if (boundingRect.size() != bounds.size()) { - q_func()->prepareGeometryChange(); - boundingRect.setSize(bounds.size()); - } - } - - QSvgRenderer *renderer; - QRectF boundingRect; - bool shared; - QString elemId; -}; - -/*! - \class QGraphicsSvgItem - \inmodule QtSvg - \ingroup graphicsview-api - \brief The QGraphicsSvgItem class is a QGraphicsItem that can be used to render - the contents of SVG files. - - \since 4.2 - - QGraphicsSvgItem provides a way of rendering SVG files onto QGraphicsView. - QGraphicsSvgItem can be created by passing the SVG file to be rendered to - its constructor or by explicit setting a shared QSvgRenderer on it. - - Note that setting QSvgRenderer on a QGraphicsSvgItem doesn't make the item take - ownership of the renderer, therefore if using setSharedRenderer() method one has - to make sure that the lifetime of the QSvgRenderer object will be at least as long - as that of the QGraphicsSvgItem. - - QGraphicsSvgItem provides a way of rendering only parts of the SVG files via - the setElementId. If setElementId() method is called, only the SVG element - (and its children) with the passed id will be renderer. This provides a convenient - way of selectively rendering large SVG files that contain a number of discrete - elements. For example the following code renders only jokers from a SVG file - containing a whole card deck: - - \snippet src_svg_qgraphicssvgitem.cpp 0 - - Size of the item can be set via direct manipulation of the items - transformation matrix. - - By default the SVG rendering is cached using QGraphicsItem::DeviceCoordinateCache - mode to speedup the display of items. Caching can be disabled by passing - QGraphicsItem::NoCache to the QGraphicsItem::setCacheMode() method. - - \sa QSvgWidget, {Qt SVG C++ Classes}, QGraphicsItem, QGraphicsView -*/ - -/*! - Constructs a new SVG item with the given \a parent. -*/ -QGraphicsSvgItem::QGraphicsSvgItem(QGraphicsItem *parent) - : QGraphicsObject(*new QGraphicsSvgItemPrivate(), 0) -{ - Q_D(QGraphicsSvgItem); - d->init(parent); -} - -/*! - Constructs a new item with the given \a parent and loads the contents of the - SVG file with the specified \a fileName. -*/ -QGraphicsSvgItem::QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parent) - : QGraphicsObject(*new QGraphicsSvgItemPrivate(), 0) -{ - Q_D(QGraphicsSvgItem); - d->init(parent); - d->renderer->load(fileName); - d->updateDefaultSize(); -} - -/*! - Returns the currently use QSvgRenderer. -*/ -QSvgRenderer *QGraphicsSvgItem::renderer() const -{ - return d_func()->renderer; -} - - -/*! - Returns the bounding rectangle of this item. -*/ -QRectF QGraphicsSvgItem::boundingRect() const -{ - Q_D(const QGraphicsSvgItem); - return d->boundingRect; -} - -/*! - \internal - - Highlights \a item as selected. - - NOTE: This function is a duplicate of qt_graphicsItem_highlightSelected() in qgraphicsitem.cpp! -*/ -static void qt_graphicsItem_highlightSelected( - QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option) -{ - const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1)); - if (qFuzzyIsNull(qMax(murect.width(), murect.height()))) - return; - - const QRectF mbrect = painter->transform().mapRect(item->boundingRect()); - if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0)) - return; - - qreal itemPenWidth; - switch (item->type()) { - case QGraphicsEllipseItem::Type: - itemPenWidth = static_cast(item)->pen().widthF(); - break; - case QGraphicsPathItem::Type: - itemPenWidth = static_cast(item)->pen().widthF(); - break; - case QGraphicsPolygonItem::Type: - itemPenWidth = static_cast(item)->pen().widthF(); - break; - case QGraphicsRectItem::Type: - itemPenWidth = static_cast(item)->pen().widthF(); - break; - case QGraphicsSimpleTextItem::Type: - itemPenWidth = static_cast(item)->pen().widthF(); - break; - case QGraphicsLineItem::Type: - itemPenWidth = static_cast(item)->pen().widthF(); - break; - default: - itemPenWidth = 1.0; - } - const qreal pad = itemPenWidth / 2; - - const qreal penWidth = 0; // cosmetic pen - - const QColor fgcolor = option->palette.windowText().color(); - const QColor bgcolor( // ensure good contrast against fgcolor - fgcolor.red() > 127 ? 0 : 255, - fgcolor.green() > 127 ? 0 : 255, - fgcolor.blue() > 127 ? 0 : 255); - - painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine)); - painter->setBrush(Qt::NoBrush); - painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); - - painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine)); - painter->setBrush(Qt::NoBrush); - painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); -} - -/*! - \reimp -*/ -void QGraphicsSvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, - QWidget *widget) -{ -// Q_UNUSED(option); - Q_UNUSED(widget); - - Q_D(QGraphicsSvgItem); - if (!d->renderer->isValid()) - return; - - if (d->elemId.isEmpty()) - d->renderer->render(painter, d->boundingRect); - else - d->renderer->render(painter, d->elemId, d->boundingRect); - - if (option->state & QStyle::State_Selected) - qt_graphicsItem_highlightSelected(this, painter, option); -} - -/*! - \reimp -*/ -int QGraphicsSvgItem::type() const -{ - return Type; -} - -/*! - \property QGraphicsSvgItem::maximumCacheSize - \since 4.6 - - This property holds the maximum size of the device coordinate cache - for this item. - */ - -/*! - Sets the maximum device coordinate cache size of the item to \a size. - If the item is cached using QGraphicsItem::DeviceCoordinateCache mode, - caching is bypassed if the extension of the item in device coordinates - is larger than \a size. - - The cache corresponds to the QPixmap which is used to cache the - results of the rendering. - Use QPixmapCache::setCacheLimit() to set limitations on the whole cache - and use setMaximumCacheSize() when setting cache size for individual - items. - - \sa QGraphicsItem::cacheMode() -*/ -void QGraphicsSvgItem::setMaximumCacheSize(const QSize &size) -{ - QGraphicsItem::d_ptr->setExtra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize, size); - update(); -} - -/*! - Returns the current maximum size of the device coordinate cache for this item. - If the item is cached using QGraphicsItem::DeviceCoordinateCache mode, - caching is bypassed if the extension of the item in device coordinates - is larger than the maximum size. - - The default maximum cache size is 1024x768. - QPixmapCache::cacheLimit() gives the - cumulative bounds of the whole cache, whereas maximumCacheSize() refers - to a maximum cache size for this particular item. - - \sa QGraphicsItem::cacheMode() -*/ -QSize QGraphicsSvgItem::maximumCacheSize() const -{ - return QGraphicsItem::d_ptr->extra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize).toSize(); -} - -/*! - \property QGraphicsSvgItem::elementId - \since 4.6 - - This property holds the element's XML ID. - */ - -/*! - Sets the XML ID of the element to \a id. -*/ -void QGraphicsSvgItem::setElementId(const QString &id) -{ - Q_D(QGraphicsSvgItem); - d->elemId = id; - d->updateDefaultSize(); - update(); -} - -/*! - Returns the XML ID the element that is currently - being rendered. Returns an empty string if the whole - file is being rendered. -*/ -QString QGraphicsSvgItem::elementId() const -{ - Q_D(const QGraphicsSvgItem); - return d->elemId; -} - -/*! - Sets \a renderer to be a shared QSvgRenderer on the item. By - using this method one can share the same QSvgRenderer on a number - of items. This means that the SVG file will be parsed only once. - QSvgRenderer passed to this method has to exist for as long as - this item is used. -*/ -void QGraphicsSvgItem::setSharedRenderer(QSvgRenderer *renderer) -{ - Q_D(QGraphicsSvgItem); - if (!d->shared) - delete d->renderer; - - d->renderer = renderer; - d->shared = true; - - d->updateDefaultSize(); - - update(); -} - -/*! - \obsolete - - Use QGraphicsItem::setCacheMode() instead. Passing true to this function is equivalent - to QGraphicsItem::setCacheMode(QGraphicsItem::DeviceCoordinateCache). -*/ -void QGraphicsSvgItem::setCachingEnabled(bool caching) -{ - setCacheMode(caching ? QGraphicsItem::DeviceCoordinateCache : QGraphicsItem::NoCache); -} - -/*! - \obsolete - - Use QGraphicsItem::cacheMode() instead. -*/ -bool QGraphicsSvgItem::isCachingEnabled() const -{ - return cacheMode() != QGraphicsItem::NoCache; -} - -QT_END_NAMESPACE - -#include "moc_qgraphicssvgitem.cpp" - -#endif // QT_NO_WIDGETS diff --git a/src/svg/qgraphicssvgitem.h b/src/svg/qgraphicssvgitem.h deleted file mode 100644 index 5102b91..0000000 --- a/src/svg/qgraphicssvgitem.h +++ /dev/null @@ -1,99 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt SVG module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef QGRAPHICSSVGITEM_H -#define QGRAPHICSSVGITEM_H - -#include - -#if !defined(QT_NO_GRAPHICSVIEW) && !defined(QT_NO_WIDGETS) - -#include - -#include - -QT_BEGIN_NAMESPACE - - -class QSvgRenderer; -class QGraphicsSvgItemPrivate; - -class Q_SVG_EXPORT QGraphicsSvgItem : public QGraphicsObject -{ - Q_OBJECT - Q_INTERFACES(QGraphicsItem) - Q_PROPERTY(QString elementId READ elementId WRITE setElementId) - Q_PROPERTY(QSize maximumCacheSize READ maximumCacheSize WRITE setMaximumCacheSize) - -public: - QGraphicsSvgItem(QGraphicsItem *parentItem = nullptr); - QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parentItem = nullptr); - - void setSharedRenderer(QSvgRenderer *renderer); - QSvgRenderer *renderer() const; - - void setElementId(const QString &id); - QString elementId() const; - - void setCachingEnabled(bool); - bool isCachingEnabled() const; - - void setMaximumCacheSize(const QSize &size); - QSize maximumCacheSize() const; - - QRectF boundingRect() const override; - - void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, - QWidget *widget = nullptr) override; - - enum { Type = 13 }; - int type() const override; - -private: - Q_DISABLE_COPY(QGraphicsSvgItem) - Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QGraphicsSvgItem) - - Q_PRIVATE_SLOT(d_func(), void _q_repaintItem()) -}; - -QT_END_NAMESPACE - -#endif // QT_NO_GRAPHICSVIEW or QT_NO_WIDGETS - -#endif // QGRAPHICSSVGITEM_H diff --git a/src/svg/qsvgwidget.cpp b/src/svg/qsvgwidget.cpp deleted file mode 100644 index acc8cca..0000000 --- a/src/svg/qsvgwidget.cpp +++ /dev/null @@ -1,176 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt SVG module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qsvgwidget.h" - -#ifndef QT_NO_WIDGETS - -#include "qsvgrenderer.h" - -#include "qstyleoption.h" -#include "qpainter.h" -#include "private/qwidget_p.h" - -QT_BEGIN_NAMESPACE - -/*! - \class QSvgWidget - \inmodule QtSvg - \ingroup painting - - \brief The QSvgWidget class provides a widget that is used to display the contents of - Scalable Vector Graphics (SVG) files. - \since 4.1 - - This class enables developers to display SVG drawings alongside standard widgets, and - is used in much the same way as QLabel is used for displaying text and bitmap images. - - Since QSvgWidget is a subclass of QWidget, SVG drawings are rendered using the properties - of the display. More control can be exercised over the rendering process with the - QSvgRenderer class, as this can be used to paint onto other paint devices, such as QImage - and QGLWidget. The renderer used by the widget can be obtained with the renderer() - function. - - Each QSvgWidget can be constructed with the file name of a SVG file, or they can be - constructed without a specific file to render and one can be supplied later. The load() - functions provide two different ways to load an SVG file: they accept either the file name - of an SVG file or a QByteArray containing the serialized XML representation of an SVG file. - - By default, the widget provides a size hint to reflect the size of the drawing that it - displays. If no data has been loaded, the widget provides the default QWidget size hint. - Subclass this class and reimplement sizeHint() if you need to customize this behavior. - - \sa QSvgRenderer, {Qt SVG C++ Classes}, QPicture -*/ - -class QSvgWidgetPrivate : public QWidgetPrivate -{ - Q_DECLARE_PUBLIC(QSvgWidget) -public: - QSvgRenderer *renderer; -}; - -/*! - Constructs a new SVG display widget with the given \a parent. -*/ -QSvgWidget::QSvgWidget(QWidget *parent) - : QWidget(*new QSvgWidgetPrivate, parent, {}) -{ - d_func()->renderer = new QSvgRenderer(this); - QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()), - this, SLOT(update())); -} - -/*! - Constructs a new SVG display widget with the given \a parent and loads the contents - of the specified \a file. -*/ -QSvgWidget::QSvgWidget(const QString &file, QWidget *parent) - : QWidget(*new QSvgWidgetPrivate, parent, {}) -{ - d_func()->renderer = new QSvgRenderer(file, this); - QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()), - this, SLOT(update())); -} - -/*! - Destroys the widget. -*/ -QSvgWidget::~QSvgWidget() -{ - -} - -/*! - Returns the renderer used to display the contents of the widget. -*/ -QSvgRenderer * QSvgWidget::renderer() const -{ - Q_D(const QSvgWidget); - return d->renderer; -} - - -/*! - \reimp -*/ -QSize QSvgWidget::sizeHint() const -{ - Q_D(const QSvgWidget); - if (d->renderer->isValid()) - return d->renderer->defaultSize(); - else - return QSize(128, 64); -} - - -/*! - \reimp -*/ -void QSvgWidget::paintEvent(QPaintEvent *) -{ - Q_D(QSvgWidget); - QStyleOption opt; - opt.init(this); - QPainter p(this); - style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); - d->renderer->render(&p); -} - -/*! - Loads the contents of the specified SVG \a file and updates the widget. -*/ -void QSvgWidget::load(const QString &file) -{ - Q_D(const QSvgWidget); - d->renderer->load(file); -} - -/*! - Loads the specified SVG format \a contents and updates the widget. -*/ -void QSvgWidget::load(const QByteArray &contents) -{ - Q_D(const QSvgWidget); - d->renderer->load(contents); -} - -QT_END_NAMESPACE - -#endif // QT_NO_WIDGETS diff --git a/src/svg/qsvgwidget.h b/src/svg/qsvgwidget.h deleted file mode 100644 index 8f6dd44..0000000 --- a/src/svg/qsvgwidget.h +++ /dev/null @@ -1,83 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt SVG module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSVGWIDGET_H -#define QSVGWIDGET_H - -#include - -#ifndef QT_NO_WIDGETS - -#include - -#include - -QT_BEGIN_NAMESPACE - - -class QSvgWidgetPrivate; -class QPaintEvent; -class QSvgRenderer; - -class Q_SVG_EXPORT QSvgWidget : public QWidget -{ - Q_OBJECT -public: - QSvgWidget(QWidget *parent = nullptr); - QSvgWidget(const QString &file, QWidget *parent = nullptr); - ~QSvgWidget(); - - QSvgRenderer *renderer() const; - - QSize sizeHint() const override; -public Q_SLOTS: - void load(const QString &file); - void load(const QByteArray &contents); -protected: - void paintEvent(QPaintEvent *event) override; -private: - Q_DISABLE_COPY(QSvgWidget) - Q_DECLARE_PRIVATE(QSvgWidget) -}; - -QT_END_NAMESPACE - -#endif // QT_NO_WIDGETS - -#endif // QSVGWIDGET_H diff --git a/src/svg/qtsvgglobal.h b/src/svg/qtsvgglobal.h index 5c79e2c..92075c9 100644 --- a/src/svg/qtsvgglobal.h +++ b/src/svg/qtsvgglobal.h @@ -3,7 +3,7 @@ ** Copyright (C) 2016 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** -** This file is part of the QtCore module of the Qt Toolkit. +** This file is part of the Qt SVG module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage diff --git a/src/svg/svg.pro b/src/svg/svg.pro index b6f17a7..9d70df8 100644 --- a/src/svg/svg.pro +++ b/src/svg/svg.pro @@ -17,8 +17,6 @@ HEADERS += \ qsvgfont_p.h \ qsvgtinydocument_p.h \ qsvgrenderer.h \ - qsvgwidget.h \ - qgraphicssvgitem.h \ qsvggenerator.h \ qtsvgglobal.h @@ -32,8 +30,6 @@ SOURCES += \ qsvgfont.cpp \ qsvgtinydocument.cpp \ qsvgrenderer.cpp \ - qsvgwidget.cpp \ - qgraphicssvgitem.cpp \ qsvggenerator.cpp qtConfig(system-zlib): \ diff --git a/src/svgwidgets/.prev_CMakeLists.txt b/src/svgwidgets/.prev_CMakeLists.txt new file mode 100644 index 0000000..afbe83e --- /dev/null +++ b/src/svgwidgets/.prev_CMakeLists.txt @@ -0,0 +1,25 @@ +##################################################################### +## SvgWidgets Module: +##################################################################### + +qt_add_module(SvgWidgets + SOURCES + qtsvgwidgetsglobal.h + qgraphicssvgitem.cpp qgraphicssvgitem.h + qsvgwidget.cpp qsvgwidget.h + DEFINES + QT_NO_USING_NAMESPACE + LIBRARIES + Qt::CorePrivate + Qt::GuiPrivate + Qt::WidgetsPrivate + PUBLIC_LIBRARIES + Qt::Core + Qt::Gui + Qt::Widgets + Qt::Svg + PRIVATE_MODULE_INTERFACE + Qt::CorePrivate + Qt::GuiPrivate + Qt::WidgetsPrivate +) diff --git a/src/svgwidgets/CMakeLists.txt b/src/svgwidgets/CMakeLists.txt new file mode 100644 index 0000000..afbe83e --- /dev/null +++ b/src/svgwidgets/CMakeLists.txt @@ -0,0 +1,25 @@ +##################################################################### +## SvgWidgets Module: +##################################################################### + +qt_add_module(SvgWidgets + SOURCES + qtsvgwidgetsglobal.h + qgraphicssvgitem.cpp qgraphicssvgitem.h + qsvgwidget.cpp qsvgwidget.h + DEFINES + QT_NO_USING_NAMESPACE + LIBRARIES + Qt::CorePrivate + Qt::GuiPrivate + Qt::WidgetsPrivate + PUBLIC_LIBRARIES + Qt::Core + Qt::Gui + Qt::Widgets + Qt::Svg + PRIVATE_MODULE_INTERFACE + Qt::CorePrivate + Qt::GuiPrivate + Qt::WidgetsPrivate +) diff --git a/src/svgwidgets/qgraphicssvgitem.cpp b/src/svgwidgets/qgraphicssvgitem.cpp new file mode 100644 index 0000000..f1dd252 --- /dev/null +++ b/src/svgwidgets/qgraphicssvgitem.cpp @@ -0,0 +1,388 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt SVG module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "qgraphicssvgitem.h" + +#if !defined(QT_NO_GRAPHICSVIEW) + +#include "qpainter.h" +#include "qstyleoption.h" +#include "qsvgrenderer.h" +#include "qdebug.h" + +#include +#include + +QT_BEGIN_NAMESPACE + +class QGraphicsSvgItemPrivate : public QGraphicsItemPrivate +{ +public: + Q_DECLARE_PUBLIC(QGraphicsSvgItem) + + QGraphicsSvgItemPrivate() + : renderer(0), shared(false) + { + } + + void init(QGraphicsItem *parent) + { + Q_Q(QGraphicsSvgItem); + q->setParentItem(parent); + renderer = new QSvgRenderer(q); + QObject::connect(renderer, SIGNAL(repaintNeeded()), + q, SLOT(_q_repaintItem())); + q->setCacheMode(QGraphicsItem::DeviceCoordinateCache); + q->setMaximumCacheSize(QSize(1024, 768)); + } + + void _q_repaintItem() + { + q_func()->update(); + } + + inline void updateDefaultSize() + { + QRectF bounds; + if (elemId.isEmpty()) { + bounds = QRectF(QPointF(0, 0), renderer->defaultSize()); + } else { + bounds = renderer->boundsOnElement(elemId); + } + if (boundingRect.size() != bounds.size()) { + q_func()->prepareGeometryChange(); + boundingRect.setSize(bounds.size()); + } + } + + QSvgRenderer *renderer; + QRectF boundingRect; + bool shared; + QString elemId; +}; + +/*! + \class QGraphicsSvgItem + \inmodule QtSvg + \ingroup graphicsview-api + \brief The QGraphicsSvgItem class is a QGraphicsItem that can be used to render + the contents of SVG files. + + \since 4.2 + + QGraphicsSvgItem provides a way of rendering SVG files onto QGraphicsView. + QGraphicsSvgItem can be created by passing the SVG file to be rendered to + its constructor or by explicit setting a shared QSvgRenderer on it. + + Note that setting QSvgRenderer on a QGraphicsSvgItem doesn't make the item take + ownership of the renderer, therefore if using setSharedRenderer() method one has + to make sure that the lifetime of the QSvgRenderer object will be at least as long + as that of the QGraphicsSvgItem. + + QGraphicsSvgItem provides a way of rendering only parts of the SVG files via + the setElementId. If setElementId() method is called, only the SVG element + (and its children) with the passed id will be renderer. This provides a convenient + way of selectively rendering large SVG files that contain a number of discrete + elements. For example the following code renders only jokers from a SVG file + containing a whole card deck: + + \snippet src_svg_qgraphicssvgitem.cpp 0 + + Size of the item can be set via direct manipulation of the items + transformation matrix. + + By default the SVG rendering is cached using QGraphicsItem::DeviceCoordinateCache + mode to speedup the display of items. Caching can be disabled by passing + QGraphicsItem::NoCache to the QGraphicsItem::setCacheMode() method. + + \sa QSvgWidget, {Qt SVG C++ Classes}, QGraphicsItem, QGraphicsView +*/ + +/*! + Constructs a new SVG item with the given \a parent. +*/ +QGraphicsSvgItem::QGraphicsSvgItem(QGraphicsItem *parent) + : QGraphicsObject(*new QGraphicsSvgItemPrivate(), 0) +{ + Q_D(QGraphicsSvgItem); + d->init(parent); +} + +/*! + Constructs a new item with the given \a parent and loads the contents of the + SVG file with the specified \a fileName. +*/ +QGraphicsSvgItem::QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parent) + : QGraphicsObject(*new QGraphicsSvgItemPrivate(), 0) +{ + Q_D(QGraphicsSvgItem); + d->init(parent); + d->renderer->load(fileName); + d->updateDefaultSize(); +} + +/*! + Returns the currently use QSvgRenderer. +*/ +QSvgRenderer *QGraphicsSvgItem::renderer() const +{ + return d_func()->renderer; +} + + +/*! + Returns the bounding rectangle of this item. +*/ +QRectF QGraphicsSvgItem::boundingRect() const +{ + Q_D(const QGraphicsSvgItem); + return d->boundingRect; +} + +/*! + \internal + + Highlights \a item as selected. + + NOTE: This function is a duplicate of qt_graphicsItem_highlightSelected() in qgraphicsitem.cpp! +*/ +static void qt_graphicsItem_highlightSelected( + QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option) +{ + const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1)); + if (qFuzzyIsNull(qMax(murect.width(), murect.height()))) + return; + + const QRectF mbrect = painter->transform().mapRect(item->boundingRect()); + if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0)) + return; + + qreal itemPenWidth; + switch (item->type()) { + case QGraphicsEllipseItem::Type: + itemPenWidth = static_cast(item)->pen().widthF(); + break; + case QGraphicsPathItem::Type: + itemPenWidth = static_cast(item)->pen().widthF(); + break; + case QGraphicsPolygonItem::Type: + itemPenWidth = static_cast(item)->pen().widthF(); + break; + case QGraphicsRectItem::Type: + itemPenWidth = static_cast(item)->pen().widthF(); + break; + case QGraphicsSimpleTextItem::Type: + itemPenWidth = static_cast(item)->pen().widthF(); + break; + case QGraphicsLineItem::Type: + itemPenWidth = static_cast(item)->pen().widthF(); + break; + default: + itemPenWidth = 1.0; + } + const qreal pad = itemPenWidth / 2; + + const qreal penWidth = 0; // cosmetic pen + + const QColor fgcolor = option->palette.windowText().color(); + const QColor bgcolor( // ensure good contrast against fgcolor + fgcolor.red() > 127 ? 0 : 255, + fgcolor.green() > 127 ? 0 : 255, + fgcolor.blue() > 127 ? 0 : 255); + + painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); + + painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(item->boundingRect().adjusted(pad, pad, -pad, -pad)); +} + +/*! + \reimp +*/ +void QGraphicsSvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ +// Q_UNUSED(option); + Q_UNUSED(widget); + + Q_D(QGraphicsSvgItem); + if (!d->renderer->isValid()) + return; + + if (d->elemId.isEmpty()) + d->renderer->render(painter, d->boundingRect); + else + d->renderer->render(painter, d->elemId, d->boundingRect); + + if (option->state & QStyle::State_Selected) + qt_graphicsItem_highlightSelected(this, painter, option); +} + +/*! + \reimp +*/ +int QGraphicsSvgItem::type() const +{ + return Type; +} + +/*! + \property QGraphicsSvgItem::maximumCacheSize + \since 4.6 + + This property holds the maximum size of the device coordinate cache + for this item. + */ + +/*! + Sets the maximum device coordinate cache size of the item to \a size. + If the item is cached using QGraphicsItem::DeviceCoordinateCache mode, + caching is bypassed if the extension of the item in device coordinates + is larger than \a size. + + The cache corresponds to the QPixmap which is used to cache the + results of the rendering. + Use QPixmapCache::setCacheLimit() to set limitations on the whole cache + and use setMaximumCacheSize() when setting cache size for individual + items. + + \sa QGraphicsItem::cacheMode() +*/ +void QGraphicsSvgItem::setMaximumCacheSize(const QSize &size) +{ + QGraphicsItem::d_ptr->setExtra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize, size); + update(); +} + +/*! + Returns the current maximum size of the device coordinate cache for this item. + If the item is cached using QGraphicsItem::DeviceCoordinateCache mode, + caching is bypassed if the extension of the item in device coordinates + is larger than the maximum size. + + The default maximum cache size is 1024x768. + QPixmapCache::cacheLimit() gives the + cumulative bounds of the whole cache, whereas maximumCacheSize() refers + to a maximum cache size for this particular item. + + \sa QGraphicsItem::cacheMode() +*/ +QSize QGraphicsSvgItem::maximumCacheSize() const +{ + return QGraphicsItem::d_ptr->extra(QGraphicsItemPrivate::ExtraMaxDeviceCoordCacheSize).toSize(); +} + +/*! + \property QGraphicsSvgItem::elementId + \since 4.6 + + This property holds the element's XML ID. + */ + +/*! + Sets the XML ID of the element to \a id. +*/ +void QGraphicsSvgItem::setElementId(const QString &id) +{ + Q_D(QGraphicsSvgItem); + d->elemId = id; + d->updateDefaultSize(); + update(); +} + +/*! + Returns the XML ID the element that is currently + being rendered. Returns an empty string if the whole + file is being rendered. +*/ +QString QGraphicsSvgItem::elementId() const +{ + Q_D(const QGraphicsSvgItem); + return d->elemId; +} + +/*! + Sets \a renderer to be a shared QSvgRenderer on the item. By + using this method one can share the same QSvgRenderer on a number + of items. This means that the SVG file will be parsed only once. + QSvgRenderer passed to this method has to exist for as long as + this item is used. +*/ +void QGraphicsSvgItem::setSharedRenderer(QSvgRenderer *renderer) +{ + Q_D(QGraphicsSvgItem); + if (!d->shared) + delete d->renderer; + + d->renderer = renderer; + d->shared = true; + + d->updateDefaultSize(); + + update(); +} + +/*! + \obsolete + + Use QGraphicsItem::setCacheMode() instead. Passing true to this function is equivalent + to QGraphicsItem::setCacheMode(QGraphicsItem::DeviceCoordinateCache). +*/ +void QGraphicsSvgItem::setCachingEnabled(bool caching) +{ + setCacheMode(caching ? QGraphicsItem::DeviceCoordinateCache : QGraphicsItem::NoCache); +} + +/*! + \obsolete + + Use QGraphicsItem::cacheMode() instead. +*/ +bool QGraphicsSvgItem::isCachingEnabled() const +{ + return cacheMode() != QGraphicsItem::NoCache; +} + +QT_END_NAMESPACE + +#include "moc_qgraphicssvgitem.cpp" + +#endif // QT_NO_GRAPHICSVIEW diff --git a/src/svgwidgets/qgraphicssvgitem.h b/src/svgwidgets/qgraphicssvgitem.h new file mode 100644 index 0000000..fa2db61 --- /dev/null +++ b/src/svgwidgets/qgraphicssvgitem.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt SVG module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QGRAPHICSSVGITEM_H +#define QGRAPHICSSVGITEM_H + +#include + +#if !defined(QT_NO_GRAPHICSVIEW) + +#include + +QT_BEGIN_NAMESPACE + + +class QSvgRenderer; +class QGraphicsSvgItemPrivate; + +class Q_SVGWIDGETS_EXPORT QGraphicsSvgItem : public QGraphicsObject +{ + Q_OBJECT + Q_INTERFACES(QGraphicsItem) + Q_PROPERTY(QString elementId READ elementId WRITE setElementId) + Q_PROPERTY(QSize maximumCacheSize READ maximumCacheSize WRITE setMaximumCacheSize) + +public: + QGraphicsSvgItem(QGraphicsItem *parentItem = nullptr); + QGraphicsSvgItem(const QString &fileName, QGraphicsItem *parentItem = nullptr); + + void setSharedRenderer(QSvgRenderer *renderer); + QSvgRenderer *renderer() const; + + void setElementId(const QString &id); + QString elementId() const; + + void setCachingEnabled(bool); + bool isCachingEnabled() const; + + void setMaximumCacheSize(const QSize &size); + QSize maximumCacheSize() const; + + QRectF boundingRect() const override; + + void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *widget = nullptr) override; + + enum { Type = 13 }; + int type() const override; + +private: + Q_DISABLE_COPY(QGraphicsSvgItem) + Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QGraphicsSvgItem) + + Q_PRIVATE_SLOT(d_func(), void _q_repaintItem()) +}; + +QT_END_NAMESPACE + +#endif // QT_NO_GRAPHICSVIEW + +#endif // QGRAPHICSSVGITEM_H diff --git a/src/svgwidgets/qsvgwidget.cpp b/src/svgwidgets/qsvgwidget.cpp new file mode 100644 index 0000000..6c80be1 --- /dev/null +++ b/src/svgwidgets/qsvgwidget.cpp @@ -0,0 +1,172 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt SVG module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsvgwidget.h" + +#include + +#include "qstyleoption.h" +#include "qpainter.h" +#include + +QT_BEGIN_NAMESPACE + +/*! + \class QSvgWidget + \inmodule QtSvg + \ingroup painting + + \brief The QSvgWidget class provides a widget that is used to display the contents of + Scalable Vector Graphics (SVG) files. + \since 4.1 + + This class enables developers to display SVG drawings alongside standard widgets, and + is used in much the same way as QLabel is used for displaying text and bitmap images. + + Since QSvgWidget is a subclass of QWidget, SVG drawings are rendered using the properties + of the display. More control can be exercised over the rendering process with the + QSvgRenderer class, as this can be used to paint onto other paint devices, such as QImage + and QGLWidget. The renderer used by the widget can be obtained with the renderer() + function. + + Each QSvgWidget can be constructed with the file name of a SVG file, or they can be + constructed without a specific file to render and one can be supplied later. The load() + functions provide two different ways to load an SVG file: they accept either the file name + of an SVG file or a QByteArray containing the serialized XML representation of an SVG file. + + By default, the widget provides a size hint to reflect the size of the drawing that it + displays. If no data has been loaded, the widget provides the default QWidget size hint. + Subclass this class and reimplement sizeHint() if you need to customize this behavior. + + \sa QSvgRenderer, {Qt SVG C++ Classes}, QPicture +*/ + +class QSvgWidgetPrivate : public QWidgetPrivate +{ + Q_DECLARE_PUBLIC(QSvgWidget) +public: + QSvgRenderer *renderer; +}; + +/*! + Constructs a new SVG display widget with the given \a parent. +*/ +QSvgWidget::QSvgWidget(QWidget *parent) + : QWidget(*new QSvgWidgetPrivate, parent, {}) +{ + d_func()->renderer = new QSvgRenderer(this); + QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()), + this, SLOT(update())); +} + +/*! + Constructs a new SVG display widget with the given \a parent and loads the contents + of the specified \a file. +*/ +QSvgWidget::QSvgWidget(const QString &file, QWidget *parent) + : QWidget(*new QSvgWidgetPrivate, parent, {}) +{ + d_func()->renderer = new QSvgRenderer(file, this); + QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()), + this, SLOT(update())); +} + +/*! + Destroys the widget. +*/ +QSvgWidget::~QSvgWidget() +{ + +} + +/*! + Returns the renderer used to display the contents of the widget. +*/ +QSvgRenderer * QSvgWidget::renderer() const +{ + Q_D(const QSvgWidget); + return d->renderer; +} + + +/*! + \reimp +*/ +QSize QSvgWidget::sizeHint() const +{ + Q_D(const QSvgWidget); + if (d->renderer->isValid()) + return d->renderer->defaultSize(); + else + return QSize(128, 64); +} + + +/*! + \reimp +*/ +void QSvgWidget::paintEvent(QPaintEvent *) +{ + Q_D(QSvgWidget); + QStyleOption opt; + opt.init(this); + QPainter p(this); + style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); + d->renderer->render(&p); +} + +/*! + Loads the contents of the specified SVG \a file and updates the widget. +*/ +void QSvgWidget::load(const QString &file) +{ + Q_D(const QSvgWidget); + d->renderer->load(file); +} + +/*! + Loads the specified SVG format \a contents and updates the widget. +*/ +void QSvgWidget::load(const QByteArray &contents) +{ + Q_D(const QSvgWidget); + d->renderer->load(contents); +} + +QT_END_NAMESPACE diff --git a/src/svgwidgets/qsvgwidget.h b/src/svgwidgets/qsvgwidget.h new file mode 100644 index 0000000..5e4cecd --- /dev/null +++ b/src/svgwidgets/qsvgwidget.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt SVG module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QSVGWIDGET_H +#define QSVGWIDGET_H + +#include +#include + + +QT_BEGIN_NAMESPACE + + +class QSvgWidgetPrivate; +class QPaintEvent; +class QSvgRenderer; + +class Q_SVGWIDGETS_EXPORT QSvgWidget : public QWidget +{ + Q_OBJECT +public: + QSvgWidget(QWidget *parent = nullptr); + QSvgWidget(const QString &file, QWidget *parent = nullptr); + ~QSvgWidget(); + + QSvgRenderer *renderer() const; + + QSize sizeHint() const override; +public Q_SLOTS: + void load(const QString &file); + void load(const QByteArray &contents); +protected: + void paintEvent(QPaintEvent *event) override; +private: + Q_DISABLE_COPY(QSvgWidget) + Q_DECLARE_PRIVATE(QSvgWidget) +}; + +QT_END_NAMESPACE + +#endif // QSVGWIDGET_H diff --git a/src/svgwidgets/qtsvgwidgetsglobal.h b/src/svgwidgets/qtsvgwidgetsglobal.h new file mode 100644 index 0000000..9802247 --- /dev/null +++ b/src/svgwidgets/qtsvgwidgetsglobal.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt SVG module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTSVGWIDGETSGLOBAL_H +#define QTSVGWIDGETSGLOBAL_H + +#include + +QT_BEGIN_NAMESPACE + +#ifndef Q_SVGWIDGETS_EXPORT +# ifndef QT_STATIC +# if defined(QT_BUILD_SVGWIDGETS_LIB) +# define Q_SVGWIDGETS_EXPORT Q_DECL_EXPORT +# else +# define Q_SVGWIDGETS_EXPORT Q_DECL_IMPORT +# endif +# else +# define Q_SVGWIDGETS_EXPORT +# endif +#endif + +QT_END_NAMESPACE + +#endif diff --git a/src/svgwidgets/svgwidgets.pro b/src/svgwidgets/svgwidgets.pro new file mode 100644 index 0000000..2f99b56 --- /dev/null +++ b/src/svgwidgets/svgwidgets.pro @@ -0,0 +1,16 @@ +TARGET = QtSvgWidgets +QT += svg core-private gui-private widgets-private + +DEFINES += QT_NO_USING_NAMESPACE +msvc:equals(QT_ARCH, i386): QMAKE_LFLAGS += /BASE:0x66000000 + +HEADERS += \ + qtsvgwidgetsglobal.h \ + qsvgwidget.h \ + qgraphicssvgitem.h + +SOURCES += \ + qsvgwidget.cpp \ + qgraphicssvgitem.cpp + +load(qt_module) diff --git a/sync.profile b/sync.profile index cd6c4bf..f3f19b8 100644 --- a/sync.profile +++ b/sync.profile @@ -1,5 +1,6 @@ %modules = ( # path to module name map "QtSvg" => "$basedir/src/svg", + "QtSvgWidgets" => "$basedir/src/svgwidgets", ); %moduleheaders = ( # restrict the module headers to those found in relative path ); -- cgit v1.2.1