summaryrefslogtreecommitdiffstats
path: root/ksvg/test/external/SVGTestWidget.cc
blob: aa968e24230572a8436a2664f083a0372339ace6 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Very small test app to look at ksvg performance without konqueror overhead (Rob)

#include "SVGTestWidget.moc"
#include "DocumentFactory.h"
#include "CanvasFactory.h"
#include "SVGCircleElement.h"
#include "SVGAnimatedLength.h"
#include "SVGLength.h"
#include "SVGSVGElement.h"
#include "SVGEventImpl.h"
#include "KSVGCanvas.h"
#include "CanvasItem.h"
#include <kcursor.h>

using namespace KSVG;

SVGTestWidget::SVGTestWidget(const KURL &url)
{
	setBackgroundColor(Qt::white);
	setMouseTracking(true);	
	setFocusPolicy(WheelFocus);

	m_doc = DocumentFactory::self()->requestDocument(this, TQT_SLOT(slotRenderingFinished()));
	if(!m_doc)
		return;	

	resize(450, 450);
	m_canvas = CanvasFactory::self()->loadCanvas(450, 450);
	if(!m_canvas)
		return;

	m_canvas->setup(this, this);

	if(!DocumentFactory::self()->attachCanvas(m_canvas, m_doc))
		return;

	if(!DocumentFactory::self()->startParsing(m_doc, url))
		return;
}

SVGTestWidget::~SVGTestWidget()
{
	delete m_canvas;
	delete m_doc;
}

void SVGTestWidget::slotRenderingFinished()
{
	TQRect rect(0, 0, width(), height());
	m_canvas->blit(rect, true);

	SVGElement test = m_doc->rootElement().getElementById("test");
	if(test.nodeName() == "circle")
	{
		SVGCircleElement c = dom_cast(SVGCircleElement, test);
		c.r().baseVal().setValue(150);
		c.setAttribute("fill", "blue");
	}
}

void SVGTestWidget::paintEvent(TQPaintEvent *event)
{
	m_canvas->update();
	m_canvas->blit(event->rect(), true);
}

void SVGTestWidget::resizeEvent(TQResizeEvent *event)
{
	int w = event->size().width();
	int h = event->size().height();

	m_canvas->resize(w, h);
	resize(w, h);
	m_canvas->blit();
}
/*
KSVG::SVGMouseEventImpl *newMouseEvent(SVGDocument *doc, KSVG::SVGEventImpl::EventId id, TQMouseEvent *event)
{
	DOM::AbstractView temp;

	int clientX = event->x();
	int clientY = event->y();

	if(doc && doc->rootElement())
	{
		clientX = int(clientX / doc->rootElement()->currentScale());
		clientY = int(clientY / doc->rootElement()->currentScale());
	}

	int button = 0;
	if(event->stateAfter() & Qt::LeftButton)
		button = 1;
	else if(event->stateAfter() & Qt::MidButton)
		button = 2;
	else if(event->stateAfter() & Qt::RightButton)
		button = 3;

	KSVG::SVGMouseEventImpl *mev = new KSVG::SVGMouseEventImpl(id, // type
			true, // canBubbleArg
			true, // cancelableArg
			temp, // view
			0, // detail
			event->globalX(), //screenXArg
			event->globalY(), // screenYArg,
			clientX, // clientXArg
			clientY, // clientYArg
			(event->state() & Qt::ControlButton), // ctrlKeyArg
			(event->state() & Qt::AltButton), // altKeyArg
			(event->state() & Qt::ShiftButton), // shiftKeyArg
			(event->state() & Qt::MetaButton), // metaKeyArg
			button, // buttonArg
			0);

	mev->ref();

	return mev;
}

void SVGTestWidget::mouseMoveEvent(TQMouseEvent *event)
{
	if(event->state() & TQMouseEvent::ControlButton && event->state() & TQMouseEvent::LeftButton)
	{
		if(m_panningPos.isNull())
			m_panningPos = event->pos();
		else
		{
			TQPoint panPoint = m_oldPanningPos - (m_panningPos - event->pos());
			m_doc->rootElement()->setCurrentTranslate(panPoint);
//			m_doc->syncCachedMatrices(); FIXME
			m_canvas->update(panPoint);
		}
		return;
	}
	else if(event->state() & TQMouseEvent::ControlButton)
		return;
}

void SVGTestWidget::mousePressEvent(TQMouseEvent *event)
{
	if(event->state() & TQMouseEvent::ControlButton)
		return;

	KSVG::SVGMouseEventImpl *mev = newMouseEvent(m_doc, KSVG::SVGEventImpl::MOUSEDOWN_EVENT, event);

	if(m_doc && m_doc->rootElement())
		m_doc->rootElement()->prepareMouseEvent(event->pos(), event->pos(), mev);
	mev->deref();
}

void SVGTestWidget::mouseReleaseEvent(TQMouseEvent *event)
{
	if(!m_panningPos.isNull())
	{
		m_oldPanningPos = m_oldPanningPos - (m_panningPos - event->pos());
		m_panningPos.setX(0);
		m_panningPos.setY(0);
	}

	if(event->state() & TQMouseEvent::ControlButton)
		return;

	// only simulate mouse clicks for now
	KSVG::SVGMouseEventImpl *mev = newMouseEvent(m_doc, KSVG::SVGEventImpl::MOUSEUP_EVENT, event);

	if(m_doc && m_doc->rootElement())
		m_doc->rootElement()->prepareMouseEvent(event->pos(), event->pos(), mev);
	mev->deref();
}

void SVGTestWidget::keyPressEvent(TQKeyEvent *event)
{
	if(event->stateAfter() & TQMouseEvent::ControlButton)
	{
		setCursor(KCursor::sizeAllCursor());
		return;
	}
}

void SVGTestWidget::keyReleaseEvent(TQKeyEvent *event)
{
	if(event->state() & TQMouseEvent::ControlButton)
	{
		setCursor(KCursor::arrowCursor());
		return;
	}
	SVGSVGElementImpl *root = m_doc->rootElement();
	if(!root) return;
	if(event->key() == Qt::Key_Minus)
	{
		erase(0, 0, m_canvas->width(), m_canvas->height());
		float zoomFactor = root->currentScale() / 1.2;
		root->setCurrentScale(zoomFactor);
		m_doc->syncCachedMatrices();
		m_canvas->update(zoomFactor);
	}
	else if(event->key() == Qt::Key_Plus)
	{
		float zoomFactor = root->currentScale() * 1.2;
		root->setCurrentScale(zoomFactor);
		m_doc->syncCachedMatrices();
		m_canvas->update(zoomFactor);
	}
}
*/

// vim:ts=4:noet