File: svgpart.cpp

package info (click to toggle)
svgpart 4:4.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88 kB
  • ctags: 15
  • sloc: cpp: 92; sh: 2; makefile: 1
file content (109 lines) | stat: -rw-r--r-- 2,639 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
/*
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
#include <QGraphicsScene>
#include <QGraphicsSvgItem>
#include <QGraphicsView>
#include <QSvgRenderer>

// KDE
#include <kaboutdata.h>
#include <kactioncollection.h>
#include <kgenericfactory.h>
#include <kstandardaction.h>

// Local

static KAboutData createAboutData()
{
	KAboutData aboutData( "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;
}

//Factory Code
K_PLUGIN_FACTORY( SvgPartFactory, registerPlugin< SvgPart >(); )
K_EXPORT_PLUGIN( SvgPartFactory( createAboutData() ) )


SvgPart::SvgPart(QWidget* parentWidget, QObject* parent, const QVariantList&)
: KParts::ReadOnlyPart(parent)
{
	mRenderer = new QSvgRenderer(this);
	mScene = new QGraphicsScene(this);
	mView = new QGraphicsView(mScene, parentWidget);
	mView->setFrameStyle(QFrame::NoFrame);
	mView->setDragMode(QGraphicsView::ScrollHandDrag);
	mItem = 0;
	setWidget(mView);

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


bool SvgPart::openFile() {
	if (!mRenderer->load(localFilePath())) {
		return false;
	}
	mItem = new QGraphicsSvgItem();
	mItem->setSharedRenderer(mRenderer);
	mScene->addItem(mItem);
	return true;
}


bool SvgPart::closeUrl() {
	delete mItem;
	mItem = 0;
	return KParts::ReadOnlyPart::closeUrl();
}


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


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


void SvgPart::zoomActualSize() {
	setZoom(1.0);
}


qreal SvgPart::zoom() const {
	return mView->matrix().m11();
}


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