svgpart.cpp 2.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
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.

*/
#include "svgpart.moc"

// Qt
22
23
24
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QGraphicsView>
25
26

// KDE
27
28
#include <kactioncollection.h>
#include <kstandardaction.h>
29
#include <ksvgrenderer.h>
30
31
32
33
34
35
36
37
38
39
40
41
#include <kparts/genericfactory.h>

// Local

//Factory Code
typedef KParts::GenericFactory<SvgPart> SvgPartFactory;
K_EXPORT_COMPONENT_FACTORY( svgpart /*library name*/, SvgPartFactory )


SvgPart::SvgPart(QWidget* parentWidget, QObject* parent, const QStringList&)
: KParts::ReadOnlyPart(parent)
{
42
	mRenderer = new KSvgRenderer(this);
43
44
45
46
47
	mScene = new QGraphicsScene(this);
	mView = new QGraphicsView(mScene, parentWidget);
	mView->setDragMode(QGraphicsView::ScrollHandDrag);
	mItem = 0;
	setWidget(mView);
48

49
	KStandardAction::actualSize(this, SLOT(zoomActualSize()), actionCollection());
50
51
	KStandardAction::zoomIn(this, SLOT(zoomIn()), actionCollection());
	KStandardAction::zoomOut(this, SLOT(zoomOut()), actionCollection());
52
53
54
55
56
	setXMLFile("svgpart/svgpart.rc");
}


bool SvgPart::openFile() {
57
	delete mItem;
58
59
60
61
62
	if (!mRenderer->load(localFilePath())) {
		return false;
	}
	mItem = new QGraphicsSvgItem();
	mItem->setSharedRenderer(mRenderer);
63
	mScene->addItem(mItem);
64
65
66
67
68
69
70
71
72
73
74
	return true;
}


KAboutData* SvgPart::createAboutData() {
	KAboutData* aboutData = new KAboutData( "svgpart", 0, ki18n("SVG Part"),
		"1.0", ki18n("A KPart to display SVG images"),
		KAboutData::License_GPL,
		ki18n("Copyright 2007, Aurélien Gâteau <aurelien.gateau@free.fr>"));
	return aboutData;
}
75
76
77
78
79
80
81
82
83
84
85
86


void SvgPart::zoomIn() {
	setZoom(zoom() * 2);
}


void SvgPart::zoomOut() {
	setZoom(zoom() / 2);
}


87
88
89
90
91
void SvgPart::zoomActualSize() {
	setZoom(1.0);
}


92
93
94
95
96
97
98
99
100
101
qreal SvgPart::zoom() const {
	return mView->matrix().m11();
}


void SvgPart::setZoom(qreal value) {
	QMatrix matrix;
	matrix.scale(value, value);
	mView->setMatrix(matrix);
}