summaryrefslogtreecommitdiff
path: root/navit/graphics/qt_qpainter/RenderArea.cpp
blob: 4a20b1d54163e3d9097ae498e9ca28d777846e6e (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * 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 "graphics_qt_qpainter.h"
#include "RenderArea.h"

#include "RenderArea.moc"

#ifdef QT_QPAINTER_USE_EMBEDDING
EmbeddedWidget::EmbeddedWidget(struct graphics_priv *priv, QWidget* child, QWidget *parent) 
: QX11EmbedWidget(parent) {
    this->gra=priv;
		this->setWindowTitle(priv->window_title);
        QStackedLayout* _outerLayout = new QStackedLayout(this);
        this->setLayout(_outerLayout);
        _outerLayout->addWidget(child);
        _outerLayout->setCurrentWidget(child);
}

void EmbeddedWidget::closeEvent(QCloseEvent* event) 
{
	gra->widget->processClose();
}
#endif /* QT_QPAINTER_USE_EMBEDDING */

//##############################################################################################################
//# Description: Constructor
//# Comment: Using a QPixmap for rendering the graphics
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
RenderArea::RenderArea(struct graphics_priv *priv, QT_QPAINTER_RENDERAREA_PARENT *parent, int w, int h, int overlay)
	: QT_QPAINTER_RENDERAREA_PARENT(parent)
{
	pixmap = new QPixmap(w, h);
#ifndef QT_QPAINTER_NO_WIDGET
	if (!overlay) {
#if QT_VERSION >= 0x040700                                                 
		grabGesture(Qt::PinchGesture);
		grabGesture(Qt::SwipeGesture);
		grabGesture(Qt::PanGesture);
#endif
		setWindowTitle(priv->window_title);
	}
#endif
	is_overlay=overlay;
	gra=priv;
#ifdef QT_QPAINTER_USE_EVENT_QT
	timer_type=g_hash_table_new(NULL, NULL);
	timer_callback=g_hash_table_new(NULL, NULL);
	watches=g_hash_table_new(NULL, NULL);
#ifndef QT_QPAINTER_NO_WIDGET
	setAttribute(Qt::WA_OpaquePaintEvent, true);
#endif
#endif
}

//##############################################################################################################
//# Description: QWidget:closeEvent
//# Comment: Deletes navit object and stops event loop on graphics shutdown
//##############################################################################################################
void RenderArea::processClose() 
{
	callback_list_call_attr_0(this->cbl, attr_window_closed);
}
void RenderArea::closeEvent(QCloseEvent* event) 
{
	this->processClose();
}

bool RenderArea::event(QEvent *event)
{
#if QT_VERSION >= 0x040700                                                 
	if (event->type() == QEvent::Gesture) {
		dbg(lvl_debug,"gesture");
		return true;
	}
#endif
	return QWidget::event(event);
}
//##############################################################################################################
//# Description: QWidget:sizeHint
//# Comment: This property holds the recommended size for the widget
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
QSize RenderArea::sizeHint() const
{
	return QSize(gra->w, gra->h);
}

//##############################################################################################################
//# Description: QWidget:paintEvent
//# Comment: A paint event is a request to repaint all or part of the widget.
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
void RenderArea::paintEvent(QPaintEvent * event)
{
	qt_qpainter_draw(gra, &event->rect(), 1);
}

void RenderArea::do_resize(QSize size)
{
    if (pixmap->paintingActive()) {
        pixmap->paintEngine()->painter()->end();
    }
	delete pixmap;
	pixmap=new QPixmap(size);
	pixmap->fill();
    QPainter painter(pixmap);
    QBrush brush;
	painter.fillRect(0, 0, size.width(), size.height(), brush);
	dbg(lvl_debug,"size %dx%d", size.width(), size.height());
	dbg(lvl_debug,"pixmap %p %dx%d", pixmap, pixmap->width(), pixmap->height());
	callback_list_call_attr_2(this->cbl, attr_resize, GINT_TO_POINTER(size.width()), GINT_TO_POINTER(size.height()));
}

//##############################################################################################################
//# Description: QWidget::resizeEvent()
//# Comment: When resizeEvent() is called, the widget already has its new geometry.
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
void RenderArea::resizeEvent(QResizeEvent * event)
{
	if (!this->is_overlay) {
		RenderArea::do_resize(event->size());
	}
}

//##############################################################################################################
//# Description: Method to handle mouse clicks
//# Comment: Delegate of QWidget::mousePressEvent and QWidget::mouseReleaseEvent (see below)
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
void RenderArea::mouseEvent(int pressed, QMouseEvent *event)
{
	struct point p;
	p.x=event->x();
	p.y=event->y();
	switch (event->button()) {
	case Qt::LeftButton:
		callback_list_call_attr_3(this->cbl, attr_button, GINT_TO_POINTER(pressed), GINT_TO_POINTER(1), GINT_TO_POINTER(&p));
		break;
	case Qt::MidButton:
		callback_list_call_attr_3(this->cbl, attr_button, GINT_TO_POINTER(pressed), GINT_TO_POINTER(2), GINT_TO_POINTER(&p));
		break;
	case Qt::RightButton:
		callback_list_call_attr_3(this->cbl, attr_button, GINT_TO_POINTER(pressed), GINT_TO_POINTER(3), GINT_TO_POINTER(&p));
		break;
	default:
		break;
	}
}

void RenderArea::mousePressEvent(QMouseEvent *event)
{
	mouseEvent(1, event);
}

void RenderArea::mouseReleaseEvent(QMouseEvent *event)
{
	mouseEvent(0, event);
}

//##############################################################################################################
//# Description: QWidget::mouseMoveEvent
//# Comment: If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.
//# Authors: Martin Schaller (04/2008)
//##############################################################################################################
void RenderArea::mouseMoveEvent(QMouseEvent *event)
{
	struct point p;
	p.x=event->x();
	p.y=event->y();
	callback_list_call_attr_1(this->cbl, attr_motion, (void *)&p);
}


//##############################################################################################################
//# Description: Qt Event :: Zoom in/out with the mouse's scrollwheel
//# Comment:
//# Authors: Stefan Klumpp (04/2008)
//##############################################################################################################
void RenderArea::wheelEvent(QWheelEvent *event)
{
	struct point p;
	int button;
	
	p.x=event->x();	// xy-coordinates of the mouse pointer
	p.y=event->y();
	
	if (event->delta() > 0)	// wheel movement away from the person
		button=4;
	else if (event->delta() < 0) // wheel movement towards the person
		button=5;
	else
		button=-1;
	
	if (button != -1) {
		callback_list_call_attr_3(this->cbl, attr_button, GINT_TO_POINTER(1), GINT_TO_POINTER(button), GINT_TO_POINTER(&p));
		callback_list_call_attr_3(this->cbl, attr_button, GINT_TO_POINTER(0), GINT_TO_POINTER(button), GINT_TO_POINTER(&p));
	}
	
	event->accept();
}

#define CASE(x) case x:

void RenderArea::keyPressEvent(QKeyEvent *event)
{
	QString str=event->text();
	const char *text=str.toUtf8().constData();
	dbg(lvl_debug,"enter text='%s' 0x%x (%zu) key=%d", text, text[0], strlen(text), event->key());
	if (!text || !text[0] || text[0] == 0x7f) {
		dbg(lvl_debug,"special key");
		switch (event->key()) {
		case 4099:
			{
				char text_backspace[] = {NAVIT_KEY_BACKSPACE,'\0'};
				text=text_backspace;
			}
			break;
		case 4101:
#ifdef QT_QPAINTER_CUSTOM_RETURN
		QT_QPAINTER_CUSTOM_RETURN
#endif
			{
				char text_return[] = {NAVIT_KEY_RETURN,'\0'};
				text=text_return;
			}
			break;
		case 4114:
#ifdef QT_QPAINTER_CUSTOM_LEFT
		QT_QPAINTER_CUSTOM_LEFT
#endif
			{
				char text_left[] = {NAVIT_KEY_LEFT,'\0'};
				text=text_left;
			}
			break;
		case 4115:
#ifdef QT_QPAINTER_CUSTOM_UP
		QT_QPAINTER_CUSTOM_UP
#endif
			{
				char text_up[] = {NAVIT_KEY_UP,'\0'};
				text=text_up;
			}
			break;
		case 4116:
#ifdef QT_QPAINTER_CUSTOM_RIGHT
		QT_QPAINTER_CUSTOM_RIGHT
#endif
			{
				char text_right[] = {NAVIT_KEY_RIGHT,'\0'};
				text=text_right;
			}
			break;
		case 4117:
#ifdef QT_QPAINTER_CUSTOM_DOWN
		QT_QPAINTER_CUSTOM_DOWN
#endif
			{
				char text_down[] = {NAVIT_KEY_DOWN,'\0'};
				text=text_down;
			}
			break;
		}
	}
	callback_list_call_attr_1(this->cbl, attr_keypress, (void *)text);
	event->accept();
}

void RenderArea::watchEvent(int fd)
{
#ifdef QT_QPAINTER_USE_EVENT_QT
	struct event_watch *ev=(struct event_watch *)g_hash_table_lookup(watches, (void *)fd);
	dbg(lvl_debug,"fd=%d ev=%p cb=%p", fd, ev, ev->cb);
	callback_call_0(ev->cb);
#endif
}

#ifdef QT_QPAINTER_USE_EVENT_QT
void RenderArea::timerEvent(QTimerEvent *event)
{
	int id=event->timerId();
	struct callback *cb=(struct callback *)g_hash_table_lookup(timer_callback, (void *)id);
	if (cb) 
		callback_call_0(cb);
	if (!g_hash_table_lookup(timer_type, (void *)id))
		event_qt_remove_timeout((struct event_timeout *)id);
}
#endif /* QT_QPAINTER_USE_EVENT_QT */