PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/backend/qt/ftk_display_qt.cpp

http://ftk.googlecode.com/
C++ | 242 lines | 186 code | 46 blank | 10 comment | 14 complexity | f63c9c082228a1d0a3bdbe2588e0f694 MD5 | raw file
  1
  2extern "C" {
  3#include "ftk_typedef.h"
  4}
  5#include "ftk.h"
  6#include "ftk_event.h"
  7#include "ftk_globals.h"
  8#include "ftk_display_rotate.h"
  9#include "ftk_display_qt.h"
 10#include "ftk_qt_widget.h"
 11#include <QtGui/QApplication>
 12#include <QtGui/QCloseEvent>
 13#include <QtGui/QDesktopWidget>
 14#include <QtGui/QImage>
 15#include <QtGui/QPainter>
 16
 17typedef struct _PrivInfo
 18{
 19	QImage* img;
 20	unsigned char* bits;
 21	int width;
 22	int height;
 23}PrivInfo;
 24
 25static QtWidget* widget = NULL;
 26
 27QtWidget::QtWidget(QWidget* parent)
 28	: QWidget(parent)
 29{
 30	setAttribute(Qt::WA_PaintOutsidePaintEvent);
 31	setAttribute(Qt::WA_InputMethodEnabled);
 32	//setAttribute(Qt::WA_PaintOnScreen);
 33
 34	setUpdatesEnabled(false);
 35
 36	//showMaximized();
 37
 38	//setWindowFlags(Qt::Dialog);
 39	//showFullScreen();
 40
 41	//setWindowFlags(Qt::subwindow);
 42	//showNormal();
 43
 44	memset(&ev, 0, sizeof(FtkEvent));
 45}
 46
 47QtWidget::~QtWidget()
 48{
 49}
 50
 51void QtWidget::paintEvent(QPaintEvent* event)
 52{
 53	FtkDisplay* display;
 54
 55	display = ftk_default_display();
 56	if(display == NULL)
 57	{
 58		return;
 59	}
 60
 61	display = ftk_display_get_real_display(display);
 62	DECL_PRIV(display, priv);
 63
 64	QPainter painter(this);
 65	QRect rect(0, 0, priv->width, priv->height);
 66	painter.drawImage(rect, *priv->img, rect/*event->rect()*/);
 67}
 68
 69void QtWidget::keyPressEvent(QKeyEvent* event)
 70{
 71	ftk_logd("keyPressEvent\n");
 72}
 73
 74void QtWidget::keyReleaseEvent(QKeyEvent* event)
 75{
 76	ftk_logd("keyReleaseEvent\n");
 77}
 78
 79void QtWidget::mouseDoubleClickEvent(QMouseEvent* event)
 80{
 81	ftk_logd("mouseDoubleClickEvent\n");
 82}
 83
 84void QtWidget::mouseMoveEvent(QMouseEvent* event)
 85{
 86	ev.type = FTK_EVT_MOUSE_MOVE;
 87	ev.u.mouse.x = event->x();
 88	ev.u.mouse.y = event->y();
 89	ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
 90
 91	//ftk_logd("mouseMoveEvent\n");
 92}
 93
 94void QtWidget::mousePressEvent(QMouseEvent* event)
 95{
 96	ev.type = FTK_EVT_MOUSE_DOWN;
 97	ev.u.mouse.x = event->x();
 98	ev.u.mouse.y = event->y();
 99	ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
100
101	//ftk_logd("mousePressEvent\n");
102}
103
104void QtWidget::mouseReleaseEvent(QMouseEvent* event)
105{
106	ev.type = FTK_EVT_MOUSE_UP;
107	ev.u.mouse.x = event->x();
108	ev.u.mouse.y = event->y();
109	ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
110
111	//ftk_logd("mouseReleaseEvent\n");
112}
113
114void QtWidget::inputMethodEvent(QInputMethodEvent* event)
115{
116	ftk_logd("inputMethodEvent\n");
117}
118
119void QtWidget::closeEvent(QCloseEvent* event)
120{
121	ftk_logd("closeEvent\n");
122	FTK_QUIT();
123	event->accept();
124}
125
126static Ret ftk_display_qt_update(FtkDisplay* thiz, FtkBitmap* bitmap, FtkRect* rect, int xoffset, int yoffset)
127{
128	int i = 0;
129	int j = 0;
130	Ret ret = RET_OK;
131	DECL_PRIV(thiz, priv);
132	int width = rect->width;
133	int height = rect->height;
134	int src_width = ftk_bitmap_width(bitmap);
135	int src_height = ftk_bitmap_height(bitmap);
136	int dst_width = priv->width;
137	int dst_height = priv->height;
138	FtkColor* src = ftk_bitmap_lock(bitmap);
139	FtkColor* dst = (FtkColor*)priv->bits;
140	return_val_if_fail(rect->x < src_width && rect->y < src_height
141		&& xoffset < dst_width && yoffset < dst_height, RET_FAIL);
142
143	width = (xoffset + width) >= dst_width ? dst_width - xoffset : width;
144	height = (yoffset + height) >= dst_height ? dst_height - yoffset : height;
145
146	src += rect->y * src_width + rect->x;
147	dst += yoffset * dst_width + xoffset;
148
149	for(i = 0; i < height; i++)
150	{
151		memcpy(dst, src, sizeof(FtkColor) * width);
152		dst += dst_width;
153		src += src_width;
154	}
155
156	QApplication::postEvent(widget, new QPaintEvent(QRect(xoffset, yoffset, width, height)));
157
158	return ret;
159}
160
161static Ret ftk_display_qt_snap(FtkDisplay* thiz, FtkRect* r, FtkBitmap* bitmap)
162{
163#if 0
164	int i = 0;
165	int xoffset = r->x;
166	int yoffset = r->y;
167	int width = r->width;
168	int height = r->height;
169	DECL_PRIV(thiz, priv);
170	FtkColor* dst = ftk_bitmap_lock(bitmap);
171	FtkColor* src = ftk_bitmap_lock(priv->bitmap);
172
173	/*TODO*/
174#endif
175
176	return RET_OK;
177}
178
179static int ftk_display_qt_width(FtkDisplay* thiz)
180{
181	DECL_PRIV(thiz, priv);
182	return_val_if_fail(priv != NULL, 0);
183	return priv->width;
184}
185
186static int ftk_display_qt_height(FtkDisplay* thiz)
187{
188	DECL_PRIV(thiz, priv);
189	return_val_if_fail(priv != NULL, 0);
190	return priv->height;
191}
192
193static void ftk_display_qt_destroy(FtkDisplay* thiz)
194{
195	if(thiz != NULL)
196	{
197		DECL_PRIV(thiz, priv);
198		FTK_FREE(priv->bits);
199		FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
200	}
201}
202
203FtkDisplay* ftk_display_qt_create(void)
204{
205	FtkDisplay* thiz = NULL;
206
207	thiz = (FtkDisplay*)FTK_ZALLOC(sizeof(FtkDisplay) + sizeof(PrivInfo));
208	if(thiz != NULL)
209	{
210		DECL_PRIV(thiz, priv);
211		thiz->update = ftk_display_qt_update;
212		thiz->width = ftk_display_qt_width;
213		thiz->height = ftk_display_qt_height;
214		thiz->snap = ftk_display_qt_snap;
215		thiz->destroy = ftk_display_qt_destroy;
216
217		ftk_logd("%d-%d\n", QApplication::desktop()->width(), QApplication::desktop()->height());
218
219		priv->width = /*widget->width();*/480;
220		priv->height = /*widget->height();*/320;
221
222		widget->setFixedSize(priv->width, priv->height);
223
224		ftk_logd("%d-%d\n", priv->width, priv->height);
225		priv->bits = (unsigned char*)FTK_ZALLOC(priv->width * priv->height * 4);
226
227		priv->img = new QImage(priv->bits, priv->width, priv->height, priv->width * 4, QImage::Format_ARGB32);
228		if(priv->img == NULL)
229		{
230			FTK_FREE(priv->bits);
231			FTK_ZFREE(thiz, sizeof(FtkDisplay) + sizeof(PrivInfo));
232		}
233	}
234
235	return thiz;
236}
237
238void* ftk_display_qt_create_win(void)
239{
240	widget = new QtWidget();
241	return widget;
242}