1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QtSvg>
|
---|
43 |
|
---|
44 | #include "window.h"
|
---|
45 | #include "svgtextobject.h"
|
---|
46 |
|
---|
47 | Window::Window()
|
---|
48 | {
|
---|
49 | setupGui();
|
---|
50 | setupTextObject();
|
---|
51 |
|
---|
52 | setWindowTitle("Text Object Example");
|
---|
53 | }
|
---|
54 |
|
---|
55 | //![1]
|
---|
56 | void Window::insertTextObject()
|
---|
57 | {
|
---|
58 | QString fileName = fileNameLineEdit->text();
|
---|
59 | QFile file(fileName);
|
---|
60 | if (!file.open(QIODevice::ReadOnly)) {
|
---|
61 | QMessageBox::warning(this, tr("Error Opening File"),
|
---|
62 | tr("Could not open '%1'").arg(fileName));
|
---|
63 | }
|
---|
64 |
|
---|
65 | QByteArray svgData = file.readAll();
|
---|
66 | //![1]
|
---|
67 |
|
---|
68 | //![2]
|
---|
69 | QTextCharFormat svgCharFormat;
|
---|
70 | svgCharFormat.setObjectType(SvgTextFormat);
|
---|
71 | QSvgRenderer renderer(svgData);
|
---|
72 |
|
---|
73 | QImage svgBufferImage(renderer.defaultSize(), QImage::Format_ARGB32);
|
---|
74 | QPainter painter(&svgBufferImage);
|
---|
75 | renderer.render(&painter, svgBufferImage.rect());
|
---|
76 |
|
---|
77 | svgCharFormat.setProperty(SvgData, svgBufferImage);
|
---|
78 |
|
---|
79 | QTextCursor cursor = textEdit->textCursor();
|
---|
80 | cursor.insertText(QString(QChar::ObjectReplacementCharacter), svgCharFormat);
|
---|
81 | textEdit->setTextCursor(cursor);
|
---|
82 | }
|
---|
83 | //![2]
|
---|
84 |
|
---|
85 | //![3]
|
---|
86 | void Window::setupTextObject()
|
---|
87 | {
|
---|
88 | QObject *svgInterface = new SvgTextObject;
|
---|
89 | textEdit->document()->documentLayout()->registerHandler(SvgTextFormat, svgInterface);
|
---|
90 | }
|
---|
91 | //![3]
|
---|
92 |
|
---|
93 | void Window::setupGui()
|
---|
94 | {
|
---|
95 | fileNameLabel = new QLabel(tr("Svg File Name:"));
|
---|
96 | fileNameLineEdit = new QLineEdit;
|
---|
97 | insertTextObjectButton = new QPushButton(tr("Insert Image"));
|
---|
98 |
|
---|
99 | fileNameLineEdit->setText("./files/heart.svg");
|
---|
100 | connect(insertTextObjectButton, SIGNAL(clicked()),
|
---|
101 | this, SLOT(insertTextObject()));
|
---|
102 |
|
---|
103 | QHBoxLayout *bottomLayout = new QHBoxLayout;
|
---|
104 | bottomLayout->addWidget(fileNameLabel);
|
---|
105 | bottomLayout->addWidget(fileNameLineEdit);
|
---|
106 | bottomLayout->addWidget(insertTextObjectButton);
|
---|
107 |
|
---|
108 | textEdit = new QTextEdit;
|
---|
109 |
|
---|
110 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
---|
111 | mainLayout->addWidget(textEdit);
|
---|
112 | mainLayout->addLayout(bottomLayout);
|
---|
113 |
|
---|
114 | setLayout(mainLayout);
|
---|
115 | }
|
---|
116 |
|
---|