File: WebShape.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (204 lines) | stat: -rw-r--r-- 5,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 *  Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * either version 2, or (at your option) any later version of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "WebShape.h"

#include <QBuffer>
// #include <QFile>
#include <QPainter>
#include <QSvgGenerator>
#include <QSvgRenderer>
#include <QWebPage>
#include <QWebFrame>

#include <KoShapeSavingContext.h>
#include <KoViewConverter.h>
#include <KoXmlWriter.h>
#include <KoXmlReader.h>
#include <kdebug.h>

#include <../../src/Xml.h>

WebShape::WebShape() : m_webPage(new QWebPage), m_cached(false), m_cacheLocked(false), m_loaded(false), m_firstLoad(false), m_zoom(1.0), m_scrollPosition(0, 0)
{
    m_webPage->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
    m_webPage->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
    connect(m_webPage, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
}

WebShape::~WebShape()
{
}

void WebShape::paint(QPainter &painter,
                     const KoViewConverter &converter, KoShapePaintingContext &)
{
    QRectF target = converter.documentToView(QRectF(QPointF(0, 0), size()));
    m_webPage->setViewportSize(target.size().toSize());
    qreal cz = target.width() / size().width();
    m_webPage->mainFrame()->setZoomFactor(m_zoom * cz);
    m_webPage->mainFrame()->setScrollPosition(m_scrollPosition.toPoint());
    m_webPage->mainFrame()->render(&painter);
}

void WebShape::saveOdf(KoShapeSavingContext & context) const
{
    KoXmlWriter &writer = context.xmlWriter();

    writer.startElement("braindump:web");
    Xml::writeBraindumpNS(writer);
    writer.addAttribute("url", m_url.url());
    writer.addAttribute("scroll_x", m_scrollPosition.x());
    writer.addAttribute("scroll_y", m_scrollPosition.y());
    writer.addAttribute("zoom", m_zoom);
    saveOdfAttributes(context, OdfAllAttributes);
    saveOdfCommonChildElements(context);
    if(m_cached) {
        writer.addAttribute("cached", "true");
    }
    writer.startElement("cache");
    writer.addTextNode(m_cache); // Save after the attribute otherwise it is seen as
    writer.endElement(); // Cache
    writer.endElement(); // braindump:web
}

bool WebShape::loadOdf(const KoXmlElement & element, KoShapeLoadingContext &context)
{
    loadOdfAttributes(element, context, OdfAllAttributes);
    m_url = element.attribute("url");
    m_scrollPosition.setX(element.attribute("scroll_x", "0").toDouble());
    m_scrollPosition.setY(element.attribute("scroll_y", "0").toDouble());
    m_zoom = element.attribute("zoom", "1.0").toDouble();
    if(element.attribute("cached") == "true") {
        m_cached = true;
        m_cacheLocked = true;
    } else {
        m_cached = false;
        m_cacheLocked = false;
    }
    KoXmlElement childElement;
    forEachElement(childElement, element) {
        if(childElement.tagName() == "cache") {
            m_cache = childElement.text();
            m_firstLoad = true;
            m_webPage->mainFrame()->setContent(m_cache.toUtf8());
        }
    }
    if(!m_cached) {
        setUrl(m_url);
    }
    return true;
}

const KUrl& WebShape::url()
{
    return m_url;
}

void WebShape::setUrl(const KUrl& _url)
{
    m_url = _url;
    m_webPage->mainFrame()->load(_url);

    notifyChanged();
    update();
    m_loaded = false;
    m_cacheLocked = false;
}

void WebShape::loadFinished(bool)
{
    update();
    m_loaded = true;
    if(!m_cacheLocked) {
        updateCache();
    }
    m_firstLoad = false;
}

void WebShape::updateCache()
{
    m_cache = m_webPage->mainFrame()->toHtml();
    m_cacheLocked = true;
}

bool WebShape::isCached() const
{
    return m_cached;
}

void WebShape::setCached(bool _cache)
{
    m_cached = _cache;
    if(m_cached) {
        m_cacheLocked = false;
        if(m_loaded) {
            updateCache();
        }
    } else {
        m_webPage->mainFrame()->load(m_url);
    }
    update();
}

void WebShape::setCache(const QString& _cache)
{
    m_cache = _cache;
    m_cacheLocked = true;
    m_webPage->mainFrame()->setContent(_cache.toUtf8());
    update();
}

const QString& WebShape::cache() const
{
    return m_cache;
}

void WebShape::scrollOf(const QPointF& _scroll)
{
    m_scrollPosition += _scroll / m_zoom;
}

void WebShape::zoomOf(qreal z)
{
    m_zoom *= z;
    if(m_zoom <= 0.01) m_zoom = 0.01;
}

QPointF WebShape::scroll() const
{
    return m_scrollPosition;
}

void WebShape::setScroll(const QPointF& point)
{
    m_scrollPosition = point;
}

qreal WebShape::zoom() const
{
    return m_zoom;
}

void WebShape::setZoom(qreal _zoom)
{
    m_zoom = _zoom;
}

#include "WebShape.moc"