svgpart.cpp 6.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Copyright 2007 Aurélien Gâteau

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
19
20
#include "svgpart.h"

21
22
#include "svgbrowserextension.h"

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
23
24
25
26
27
28
29
// KF
#include <KAboutData>
#include <KLocalizedString>
#include <KActionCollection>
#include <KStandardAction>
#include <KPluginFactory>

30
// Qt
31
32
33
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QGraphicsView>
Volker Krause's avatar
Volker Krause committed
34
#include <QSvgRenderer>
35
36
37
#include <QMimeDatabase>
#include <QScrollBar>
#include <QTimer>
38
39


Pino Toscano's avatar
Pino Toscano committed
40
41
static KAboutData createAboutData()
{
42
    KAboutData aboutData(QStringLiteral("svgpart"), i18n("SVG Part"),
43
                         QStringLiteral("1.1"),
44
                         i18n("A KPart to display SVG images"),
45
46
47
                         KAboutLicense::KAboutLicense::GPL,
                         i18n("Copyright 2007, Aurélien Gâteau <aurelien.gateau@free.fr>"));
    return aboutData;
Pino Toscano's avatar
Pino Toscano committed
48
49
}

50
//Factory Code
51
52
K_PLUGIN_FACTORY_WITH_JSON(SvgPartFactory, "svgpart.json",
                           registerPlugin<SvgPart>();)
53
54


Pino Toscano's avatar
Pino Toscano committed
55
SvgPart::SvgPart(QWidget* parentWidget, QObject* parent, const QVariantList&)
56
    : KParts::ReadOnlyPart(parent)
57
    , mItem(nullptr)
58
    , m_browserExtension(new SvgBrowserExtension(this))
59
{
60
61
62
63
64
65
66
67
68
    setComponentData(createAboutData());

    mRenderer = new QSvgRenderer(this);
    mScene = new QGraphicsScene(this);
    mView = new QGraphicsView(mScene, parentWidget);
    mView->setFrameStyle(QFrame::NoFrame);
    mView->setDragMode(QGraphicsView::ScrollHandDrag);
    setWidget(mView);

69
70
71
    KStandardAction::actualSize(this, &SvgPart::zoomActualSize, actionCollection());
    KStandardAction::zoomIn(this, &SvgPart::zoomIn, actionCollection());
    KStandardAction::zoomOut(this, &SvgPart::zoomOut, actionCollection());
72
    setXMLFile(QStringLiteral("svgpart.rc"));
73
74
75
}


76
77
78
79
80
81
82
83
84
85
86
87
bool SvgPart::openUrl(const QUrl& url)
{
    mCloseUrlFromOpen = true;

    const auto success = KParts::ReadOnlyPart::openUrl(url);

    mCloseUrlFromOpen = false;

    return success;
}


88
89
90
91
92
bool SvgPart::openFile()
{
    if (!mRenderer->load(localFilePath())) {
        return false;
    }
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

    createViewForDocument();

    return true;
}


bool SvgPart::doOpenStream(const QString& mimeType)
{
    auto mime = QMimeDatabase().mimeTypeForName(mimeType);
    if (!mime.inherits(QStringLiteral("image/svg+xml"))
        && !mime.inherits(QStringLiteral("image/svg+xml-compressed"))) {
            return false;
    }

    mStreamedData.clear();

    return true;
}


bool SvgPart::doWriteStream(const QByteArray& data)
{
    mStreamedData.append(data);
    return true;
}


bool SvgPart::doCloseStream()
{
    // too bad QSvgRenderer supports QXmlStreamReader, but not its incremental parsing
    if (!mRenderer->load(mStreamedData)) {
        mStreamedData.clear();
        return false;
    }

    mStreamedData.clear();

    createViewForDocument();

133
    return true;
134
135
136
}


137
138
bool SvgPart::closeUrl()
{
139
140
141
142
143
144
145
146
147
148
149
    // protect against repeated call if already closed
    const auto currentUrl = url();
    // remember old view state for a possible reload from same url
    if (currentUrl.isValid()) {
        mPreviousUrl = currentUrl;

        mPreviousZoom = zoom();
        mPreviousHorizontalScrollPosition = mView->horizontalScrollBar()->value();
        mPreviousVerticalScrollPosition = mView->verticalScrollBar()->value();
    }

150
151
152
153
154
    mView->resetTransform();
    // cannot reset the rect completely, as a null QRectF is ignored
    // so at least just a 1 pixel square one
    mScene->setSceneRect(QRectF(0,0,1,1));

155
    delete mItem;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
156
    mItem = nullptr;
157

158
159
160
161
162
    // reset arguments
    if (!mCloseUrlFromOpen) {
        mHasExtendedRestoreArguments = false;
    }

163
    return KParts::ReadOnlyPart::closeUrl();
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
void SvgPart::createViewForDocument()
{
    mItem = new QGraphicsSvgItem();
    mItem->setSharedRenderer(mRenderer);
    mScene->addItem(mItem);
    // we reuse the scene, whose scenerect though is not properly resetable, so ensure up-to-date one
    mScene->setSceneRect(mItem->boundingRect());

    // ideally the viewstate would be restored here, but at this point in time
    // the view has not yet been updated to the scene and is a wrong size,
    // so setting the scrollbars etc now will not have any effect
    // TODO: this results in flickering, needs to find a better way to hook into
    // updating of view state to new content before the first rendering is done
    QTimer::singleShot(0, this, &SvgPart::delayedRestoreViewState);
}

void SvgPart::delayedRestoreViewState()
{
    // arguments set by caller or restore method
    KParts::OpenUrlArguments args(arguments());
    qreal zoomValue = mHasExtendedRestoreArguments ? mRestoreZoom : 1.0;

    // reloading same url?
Yuri Chornoivan's avatar
Yuri Chornoivan committed
190
    // we can't tell if caller has explicitly set xOffset/yOffset of OpenUrlArguments
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    // so in case of same url we just assume a reload and ignore the OpenUrlArguments xOffset/yOffset
    if (!mHasExtendedRestoreArguments && (url() == mPreviousUrl)) {
        // restore last view state instead
        zoomValue = mPreviousZoom;
        args.setXOffset(mPreviousHorizontalScrollPosition);
        args.setYOffset(mPreviousVerticalScrollPosition);
    }

    // now restore view state
    setZoom(zoomValue);

    mView->horizontalScrollBar()->setValue(args.xOffset());
    mView->verticalScrollBar()->setValue(args.yOffset());
}

void SvgPart::setExtendedRestoreArguments(qreal zoom)
{
    mHasExtendedRestoreArguments = true;
    mRestoreZoom = zoom;
}

212
213
214
void SvgPart::zoomIn()
{
    setZoom(zoom() * 2);
215
216
217
}


218
219
220
void SvgPart::zoomOut()
{
    setZoom(zoom() / 2);
221
222
223
}


224
225
226
void SvgPart::zoomActualSize()
{
    setZoom(1.0);
227
228
229
}


230
231
232
qreal SvgPart::zoom() const
{
    return mView->matrix().m11();
233
234
235
}


236
237
void SvgPart::setZoom(qreal value)
{
Laurent Montel's avatar
Laurent Montel committed
238
    QTransform matrix;
239
    matrix.scale(value, value);
Laurent Montel's avatar
Laurent Montel committed
240
    mView->setTransform(matrix);
241
}
Hrvoje Senjan's avatar
Hrvoje Senjan committed
242

243
244
245
246
247
248
249
250
251
252
253
254
int SvgPart::horizontalScrollPosition() const
{
    return mView->horizontalScrollBar()->value();
}


int SvgPart::verticalScrollPosition() const
{
    return mView->verticalScrollBar()->value();
}


Pino Toscano's avatar
Pino Toscano committed
255
#include "svgpart.moc"