From 166e66496114975b9580e489b16963191fd4f600 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 4 Dec 2012 12:34:19 +0100 Subject: Centralize and fixup qtsvg example sources install targets This follows suit with aeb036e in qtbase. Change-Id: I2e64dcce15b3b39f98395df5627cf0839debdf99 Reviewed-by: Oswald Buddenhagen --- examples/svg/richtext/richtext.pro | 3 + examples/svg/richtext/textobject/files/heart.svg | 55 ++++++++++ examples/svg/richtext/textobject/main.cpp | 52 +++++++++ examples/svg/richtext/textobject/resources.qrc | 5 + examples/svg/richtext/textobject/svgtextobject.cpp | 71 +++++++++++++ examples/svg/richtext/textobject/svgtextobject.h | 69 ++++++++++++ .../svg/richtext/textobject/textobject.desktop | 11 ++ examples/svg/richtext/textobject/textobject.pro | 18 ++++ examples/svg/richtext/textobject/window.cpp | 117 +++++++++++++++++++++ examples/svg/richtext/textobject/window.h | 80 ++++++++++++++ 10 files changed, 481 insertions(+) create mode 100644 examples/svg/richtext/richtext.pro create mode 100644 examples/svg/richtext/textobject/files/heart.svg create mode 100644 examples/svg/richtext/textobject/main.cpp create mode 100644 examples/svg/richtext/textobject/resources.qrc create mode 100644 examples/svg/richtext/textobject/svgtextobject.cpp create mode 100644 examples/svg/richtext/textobject/svgtextobject.h create mode 100644 examples/svg/richtext/textobject/textobject.desktop create mode 100644 examples/svg/richtext/textobject/textobject.pro create mode 100644 examples/svg/richtext/textobject/window.cpp create mode 100644 examples/svg/richtext/textobject/window.h (limited to 'examples/svg/richtext') diff --git a/examples/svg/richtext/richtext.pro b/examples/svg/richtext/richtext.pro new file mode 100644 index 0000000..be9c6c3 --- /dev/null +++ b/examples/svg/richtext/richtext.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs +!contains(QT_CONFIG, no-widgets): SUBDIRS += textobject +QT+=widgets diff --git a/examples/svg/richtext/textobject/files/heart.svg b/examples/svg/richtext/textobject/files/heart.svg new file mode 100644 index 0000000..ba5f050 --- /dev/null +++ b/examples/svg/richtext/textobject/files/heart.svg @@ -0,0 +1,55 @@ + + + + + +Heart Left-Highlight +This is a normal valentines day heart. + + +holiday +valentines + +valentine +hash(0x8a091c0) +hash(0x8a0916c) +signs_and_symbols +hash(0x8a091f0) +day + + + + +Jon Phillips + + + + +Jon Phillips + + + + +Jon Phillips + + + +image/svg+xml + + +en + + + + + + + + + + + + + + + diff --git a/examples/svg/richtext/textobject/main.cpp b/examples/svg/richtext/textobject/main.cpp new file mode 100644 index 0000000..4a0a6a0 --- /dev/null +++ b/examples/svg/richtext/textobject/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +#include "window.h" + +int main(int argv, char **args) +{ + QApplication app(argv, args); + + Window window; + window.show(); + return app.exec(); +} diff --git a/examples/svg/richtext/textobject/resources.qrc b/examples/svg/richtext/textobject/resources.qrc new file mode 100644 index 0000000..39b2f5d --- /dev/null +++ b/examples/svg/richtext/textobject/resources.qrc @@ -0,0 +1,5 @@ + + + files/heart.svg + + diff --git a/examples/svg/richtext/textobject/svgtextobject.cpp b/examples/svg/richtext/textobject/svgtextobject.cpp new file mode 100644 index 0000000..8e3e3e9 --- /dev/null +++ b/examples/svg/richtext/textobject/svgtextobject.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "svgtextobject.h" +#include "window.h" + +//![0] +QSizeF SvgTextObject::intrinsicSize(QTextDocument * /*doc*/, int /*posInDocument*/, + const QTextFormat &format) +{ + QImage bufferedImage = qvariant_cast(format.property(Window::SvgData)); + QSize size = bufferedImage.size(); + + if (size.height() > 25) + size *= 25.0 / (double) size.height(); + + return QSizeF(size); +} +//![0] + +//![1] +void SvgTextObject::drawObject(QPainter *painter, const QRectF &rect, + QTextDocument * /*doc*/, int /*posInDocument*/, + const QTextFormat &format) +{ + QImage bufferedImage = qvariant_cast(format.property(Window::SvgData)); + + painter->drawImage(rect, bufferedImage); +} +//![1] + diff --git a/examples/svg/richtext/textobject/svgtextobject.h b/examples/svg/richtext/textobject/svgtextobject.h new file mode 100644 index 0000000..f9ba114 --- /dev/null +++ b/examples/svg/richtext/textobject/svgtextobject.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SVGTEXTOBJECT_H +#define SVGTEXTOBJECT_H + +#include + +QT_BEGIN_NAMESPACE +class QTextDocument; +class QTextFormat; +class QPainter; +class QRectF; +class QSizeF; +QT_END_NAMESPACE + +//![0] //![1] +class SvgTextObject : public QObject, public QTextObjectInterface +{ + Q_OBJECT + Q_INTERFACES(QTextObjectInterface) +//![1] + +public: + QSizeF intrinsicSize(QTextDocument *doc, int posInDocument, + const QTextFormat &format); + void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, + int posInDocument, const QTextFormat &format); +}; +//![0] + +#endif diff --git a/examples/svg/richtext/textobject/textobject.desktop b/examples/svg/richtext/textobject/textobject.desktop new file mode 100644 index 0000000..1b0980f --- /dev/null +++ b/examples/svg/richtext/textobject/textobject.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Encoding=UTF-8 +Version=1.0 +Type=Application +Terminal=false +Name=Text Object +Exec=/opt/usr/bin/textobject +Icon=textobject +X-Window-Icon= +X-HildonDesk-ShowInToolbar=true +X-Osso-Type=application/x-executable diff --git a/examples/svg/richtext/textobject/textobject.pro b/examples/svg/richtext/textobject/textobject.pro new file mode 100644 index 0000000..34e84dc --- /dev/null +++ b/examples/svg/richtext/textobject/textobject.pro @@ -0,0 +1,18 @@ +HEADERS = svgtextobject.h \ + window.h +SOURCES = main.cpp \ + svgtextobject.cpp \ + window.cpp + +QT += svg + +RESOURCES = resources.qrc + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/svg/richtext/textobject +INSTALLS += target + +filesToDeploy.files = files/*.svg +filesToDeploy.path = files +DEPLOYMENT += filesToDeploy +QT+=widgets diff --git a/examples/svg/richtext/textobject/window.cpp b/examples/svg/richtext/textobject/window.cpp new file mode 100644 index 0000000..ba1d16d --- /dev/null +++ b/examples/svg/richtext/textobject/window.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "window.h" +#include "svgtextobject.h" + +Window::Window() +{ + setupGui(); + setupTextObject(); + + setWindowTitle("Text Object Example"); +} + +//![1] +void Window::insertTextObject() +{ + QString fileName = fileNameLineEdit->text(); + QFile file(fileName); + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox::warning(this, tr("Error Opening File"), + tr("Could not open '%1'").arg(fileName)); + } + + QByteArray svgData = file.readAll(); +//![1] + +//![2] + QTextCharFormat svgCharFormat; + svgCharFormat.setObjectType(SvgTextFormat); + QSvgRenderer renderer(svgData); + + QImage svgBufferImage(renderer.defaultSize(), QImage::Format_ARGB32); + QPainter painter(&svgBufferImage); + renderer.render(&painter, svgBufferImage.rect()); + + svgCharFormat.setProperty(SvgData, svgBufferImage); + + QTextCursor cursor = textEdit->textCursor(); + cursor.insertText(QString(QChar::ObjectReplacementCharacter), svgCharFormat); + textEdit->setTextCursor(cursor); +} +//![2] + +//![3] +void Window::setupTextObject() +{ + QObject *svgInterface = new SvgTextObject; + svgInterface->setParent(this); + textEdit->document()->documentLayout()->registerHandler(SvgTextFormat, svgInterface); +} +//![3] + +void Window::setupGui() +{ + fileNameLabel = new QLabel(tr("Svg File Name:")); + fileNameLineEdit = new QLineEdit; + insertTextObjectButton = new QPushButton(tr("Insert Image")); + + fileNameLineEdit->setText(":/files/heart.svg"); + connect(insertTextObjectButton, SIGNAL(clicked()), + this, SLOT(insertTextObject())); + + QHBoxLayout *bottomLayout = new QHBoxLayout; + bottomLayout->addWidget(fileNameLabel); + bottomLayout->addWidget(fileNameLineEdit); + bottomLayout->addWidget(insertTextObjectButton); + + textEdit = new QTextEdit; + + QVBoxLayout *mainLayout = new QVBoxLayout; + mainLayout->addWidget(textEdit); + mainLayout->addLayout(bottomLayout); + + setLayout(mainLayout); +} + diff --git a/examples/svg/richtext/textobject/window.h b/examples/svg/richtext/textobject/window.h new file mode 100644 index 0000000..7551233 --- /dev/null +++ b/examples/svg/richtext/textobject/window.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WINDOW_H +#define WINDOW_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QTextEdit; +class QLineEdit; +class QPushButton; +class QLabel; +QT_END_NAMESPACE + +//![0] +class Window : public QWidget +{ + Q_OBJECT + +public: + enum { SvgTextFormat = QTextFormat::UserObject + 1 }; + enum SvgProperties { SvgData = 1 }; + + Window(); + +private slots: + void insertTextObject(); + +private: + void setupTextObject(); + void setupGui(); + +private: + QTextEdit *textEdit; + QLabel *fileNameLabel; + QLineEdit *fileNameLineEdit; + QPushButton *insertTextObjectButton; +}; +//![0] + +#endif -- cgit v1.2.1