svgpart.cpp 2.78 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
21
22
23
24
25
26
27
#include "svgpart.h"

// KF
#include <KAboutData>
#include <KLocalizedString>
#include <KActionCollection>
#include <KStandardAction>
#include <KPluginFactory>

28
// Qt
29
30
31
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QGraphicsView>
Volker Krause's avatar
Volker Krause committed
32
#include <QSvgRenderer>
33
34


Pino Toscano's avatar
Pino Toscano committed
35
36
static KAboutData createAboutData()
{
37
38
39
40
41
    KAboutData aboutData(QStringLiteral("svgpart"), i18n("SVG Part"),
                         "1.0", i18n("A KPart to display SVG images"),
                         KAboutLicense::KAboutLicense::GPL,
                         i18n("Copyright 2007, Aurélien Gâteau <aurelien.gateau@free.fr>"));
    return aboutData;
Pino Toscano's avatar
Pino Toscano committed
42
43
}

44
//Factory Code
45
K_PLUGIN_FACTORY(SvgPartFactory, registerPlugin<SvgPart>();)
46
47


Pino Toscano's avatar
Pino Toscano committed
48
SvgPart::SvgPart(QWidget* parentWidget, QObject* parent, const QVariantList&)
49
    : KParts::ReadOnlyPart(parent)
50
{
51
52
53
54
55
56
57
    setComponentData(createAboutData());

    mRenderer = new QSvgRenderer(this);
    mScene = new QGraphicsScene(this);
    mView = new QGraphicsView(mScene, parentWidget);
    mView->setFrameStyle(QFrame::NoFrame);
    mView->setDragMode(QGraphicsView::ScrollHandDrag);
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
58
    mItem = nullptr;
59
60
61
62
63
64
    setWidget(mView);

    KStandardAction::actualSize(this, SLOT(zoomActualSize()), actionCollection());
    KStandardAction::zoomIn(this, SLOT(zoomIn()), actionCollection());
    KStandardAction::zoomOut(this, SLOT(zoomOut()), actionCollection());
    setXMLFile("svgpart.rc");
65
66
67
}


68
69
70
71
72
73
74
75
76
bool SvgPart::openFile()
{
    if (!mRenderer->load(localFilePath())) {
        return false;
    }
    mItem = new QGraphicsSvgItem();
    mItem->setSharedRenderer(mRenderer);
    mScene->addItem(mItem);
    return true;
77
78
79
}


80
81
82
bool SvgPart::closeUrl()
{
    delete mItem;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
83
    mItem = nullptr;
84
    return KParts::ReadOnlyPart::closeUrl();
85
86
87
}


88
89
90
void SvgPart::zoomIn()
{
    setZoom(zoom() * 2);
91
92
93
}


94
95
96
void SvgPart::zoomOut()
{
    setZoom(zoom() / 2);
97
98
99
}


100
101
102
void SvgPart::zoomActualSize()
{
    setZoom(1.0);
103
104
105
}


106
107
108
qreal SvgPart::zoom() const
{
    return mView->matrix().m11();
109
110
111
}


112
113
114
115
116
void SvgPart::setZoom(qreal value)
{
    QMatrix matrix;
    matrix.scale(value, value);
    mView->setMatrix(matrix);
117
}
Hrvoje Senjan's avatar
Hrvoje Senjan committed
118

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