1/*
2Copyright 2007 Aurélien Gâteau
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18*/
19#include "svgpart.moc"
20
21// Qt
22#include <QGraphicsScene>
23#include <QGraphicsSvgItem>
24#include <QGraphicsView>
25#include <QSvgRenderer>
26
27// KDE
28#include <kaboutdata.h>
29#include <kactioncollection.h>
30#include <kgenericfactory.h>
31#include <kstandardaction.h>
32
33// Local
34
35static KAboutData createAboutData()
36{
37 KAboutData aboutData( "svgpart", 0, ki18n("SVG Part"),
38 "1.0", ki18n("A KPart to display SVG images"),
39 KAboutData::License_GPL,
40 ki18n("Copyright 2007, Aurélien Gâteau <aurelien.gateau@free.fr>"));
41 return aboutData;
42}
43
44//Factory Code
45K_PLUGIN_FACTORY( SvgPartFactory, registerPlugin< SvgPart >(); )
46K_EXPORT_PLUGIN( SvgPartFactory( createAboutData() ) )
47
48
49SvgPart::SvgPart(QWidget* parentWidget, QObject* parent, const QVariantList&)
50: KParts::ReadOnlyPart(parent)
51{
52 mRenderer = new QSvgRenderer(this);
53 mScene = new QGraphicsScene(this);
54 mView = new QGraphicsView(mScene, parentWidget);
55 mView->setFrameStyle(QFrame::NoFrame);
56 mView->setDragMode(QGraphicsView::ScrollHandDrag);
57 mItem = 0;
58 setWidget(mView);
59
60 KStandardAction::actualSize(this, SLOT(zoomActualSize()), actionCollection());
61 KStandardAction::zoomIn(this, SLOT(zoomIn()), actionCollection());
62 KStandardAction::zoomOut(this, SLOT(zoomOut()), actionCollection());
63 setXMLFile("svgpart/svgpart.rc");
64}
65
66
67bool SvgPart::openFile() {
68 if (!mRenderer->load(localFilePath())) {
69 return false;
70 }
71 mItem = new QGraphicsSvgItem();
72 mItem->setSharedRenderer(mRenderer);
73 mScene->addItem(mItem);
74 return true;
75}
76
77
78bool SvgPart::closeUrl() {
79 delete mItem;
80 mItem = 0;
81 return KParts::ReadOnlyPart::closeUrl();
82}
83
84
85void SvgPart::zoomIn() {
86 setZoom(zoom() * 2);
87}
88
89
90void SvgPart::zoomOut() {
91 setZoom(zoom() / 2);
92}
93
94
95void SvgPart::zoomActualSize() {
96 setZoom(1.0);
97}
98
99
100qreal SvgPart::zoom() const {
101 return mView->matrix().m11();
102}
103
104
105void SvgPart::setZoom(qreal value) {
106 QMatrix matrix;
107 matrix.scale(value, value);
108 mView->setMatrix(matrix);
109}
110