From 2e4383f2aa9d69a6e23c19c8ef6fbe3e9b3895d8 Mon Sep 17 00:00:00 2001 From: martin-s Date: Fri, 4 Apr 2008 11:54:06 +0000 Subject: [PATCH] Add:graphics_qt_qpainter:New graphics driver git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@980 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/configure.in | 12 + navit/src/graphics/Makefile.am | 3 + navit/src/graphics/qt_qpainter/Makefile.am | 5 + .../graphics/qt_qpainter/graphics_qt_qpainter.cpp | 441 +++++++++++++++++++++ 4 files changed, 461 insertions(+) create mode 100644 navit/src/graphics/qt_qpainter/Makefile.am create mode 100644 navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp diff --git a/navit/configure.in b/navit/configure.in index f6cf712..c3afc61 100644 --- a/navit/configure.in +++ b/navit/configure.in @@ -109,6 +109,17 @@ AM_CONDITIONAL(GUI_GTK, [test "x$gtk2_pkgconfig" = "xyes"]) AM_CONDITIONAL(GRAPHICS_GTK_DRAWING_AREA, [test "x$gtk2_pkgconfig" = "xyes"]) AM_CONDITIONAL(PLUGINS, [test "x$plugins" = "xyes"]) +AC_ARG_ENABLE(graphics-qt-qpainter, [ --disable-graphics-qt-qpainter don't create graphics qt-qpainter], enable_graphics_qt_qpainter=$enableval, enable_graphics_qt_qpainter=yes) +if test "x${enable_graphics_qt_qpainter}" = "xyes" ; then + PKG_CHECK_MODULES(QT_GUI, [QtGui QtCore], ,enable_graphics_qt_qpainter=no); +fi +if test "x${enable_graphics_qt_qpainter}" = "xyes" ; then + AC_DEFINE(USE_GRAPICS_QT_QPAINTER, 1, [Build with graphics qt_qpainter]) + AC_SUBST(QT_GUI_CFLAGS) + AC_SUBST(QT_GUI_LIBS) +fi +AM_CONDITIONAL(USE_GRAPHICS_QT_QPAINTER, test "x${enable_graphics_qt_qpainter}" = "xyes") + PKG_CHECK_MODULES(FREETYPE2, [freetype2], [freetype2_pkgconfig=yes], [freetype2_pkgconfig=no]) if test "x$freetype2_pkgconfig" = "xyes"; then AC_DEFINE(HAVE_FREETYPE2, 1, [Define to 1 if you have freetype2]) @@ -470,6 +481,7 @@ src/graphics/Makefile src/graphics/gtk_drawing_area/Makefile src/graphics/opengl/Makefile src/graphics/null/Makefile +src/graphics/qt_qpainter/Makefile src/gui/Makefile src/gui/gtk/Makefile src/gui/internal/Makefile diff --git a/navit/src/graphics/Makefile.am b/navit/src/graphics/Makefile.am index 87d68de..22bbc6e 100644 --- a/navit/src/graphics/Makefile.am +++ b/navit/src/graphics/Makefile.am @@ -5,3 +5,6 @@ endif if GRAPHICS_OPENGL SUBDIRS+=opengl endif +if USE_GRAPHICS_QT_QPAINTER + SUBDIRS+=qt_qpainter +endif diff --git a/navit/src/graphics/qt_qpainter/Makefile.am b/navit/src/graphics/qt_qpainter/Makefile.am new file mode 100644 index 0000000..228236d --- /dev/null +++ b/navit/src/graphics/qt_qpainter/Makefile.am @@ -0,0 +1,5 @@ +include $(top_srcdir)/Makefile.inc +AM_CPPFLAGS = @QT_GUI_CFLAGS@ @NAVIT_CFLAGS@ -I$(top_srcdir)/src -DMODULE=graphics_qt_qpainter +modulegraphics_LTLIBRARIES = libgraphics_qt_qpainter.la +libgraphics_qt_qpainter_la_SOURCES = graphics_qt_qpainter.cpp +libgraphics_qt_qpainter_la_LDFLAGS = @QT_GUI_LIBS@ diff --git a/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp b/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp new file mode 100644 index 0000000..446fdd4 --- /dev/null +++ b/navit/src/graphics/qt_qpainter/graphics_qt_qpainter.cpp @@ -0,0 +1,441 @@ +#include +#include "config.h" +#include "point.h" +#include "graphics.h" +#include "color.h" +#include "debug.h" +#include "plugin.h" + +#if 0 +#define QWS +#define NO_DEBUG +#endif + +#include + +#if QT_VERSION < 0x040000 +#include +#include +#include +#include +#include +#include +#include +#include +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + + +class RenderArea : public QWidget + { + public: + RenderArea(QWidget *parent = 0); + QPixmap *pixmap; + void (*resize_callback)(void *data, int w, int h); + void *resize_callback_data; + void (*motion_callback)(void *data, struct point *p); + void *motion_callback_data; + void (*button_callback)(void *data, int press, int button, struct point *p); + void *button_callback_data; + + + protected: + QSize sizeHint() const; + void paintEvent(QPaintEvent *event); + void resizeEvent(QResizeEvent *event); + void mouseEvent(int pressed, QMouseEvent *event); + void mousePressEvent(QMouseEvent *event); + void mouseReleaseEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + + }; + +RenderArea::RenderArea(QWidget *parent) + : QWidget(parent) + { + pixmap = new QPixmap(800, 600); + } + + QSize RenderArea::sizeHint() const + { + return QSize(800, 600); + } + +void RenderArea::paintEvent(QPaintEvent * event) +{ + QPainter painter(this); + painter.drawPixmap(0, 0, *pixmap); +} + +void RenderArea::resizeEvent(QResizeEvent * event) +{ + QSize size=event->size(); + delete pixmap; + pixmap = new QPixmap(size); + if (this->resize_callback) + (this->resize_callback)(this->resize_callback_data, size.width(), size.height()); +} + +void RenderArea::mouseEvent(int pressed, QMouseEvent *event) +{ + struct point p; + if (!this->button_callback) + return; + p.x=event->x(); + p.y=event->y(); + switch (event->button()) { + case Qt::LeftButton: + (this->button_callback)(this->button_callback_data, pressed, 1, &p); + break; + case Qt::MidButton: + (this->button_callback)(this->button_callback_data, pressed, 2, &p); + break; + case Qt::RightButton: + (this->button_callback)(this->button_callback_data, pressed, 3, &p); + break; + default: + break; + } +} + +void RenderArea::mousePressEvent(QMouseEvent *event) +{ + mouseEvent(1, event); +} + +void RenderArea::mouseReleaseEvent(QMouseEvent *event) +{ + mouseEvent(0, event); +} + +void RenderArea::mouseMoveEvent(QMouseEvent *event) +{ + struct point p; + if (!this->motion_callback) + return; + p.x=event->x(); + p.y=event->y(); + (this->motion_callback)(this->motion_callback_data, &p); +} + + + +static int dummy; + +struct graphics_priv { + QApplication *app; + RenderArea *widget; + QPainter *painter; + struct graphics_gc_priv *background_gc; + enum draw_mode_num mode; +}; + +static struct graphics_font_priv { + int dummy; +} graphics_font_priv; + +static struct graphics_gc_priv { + QPen *pen; + QBrush *brush; +} graphics_gc_priv; + +static struct graphics_image_priv { + QImage *image; +} graphics_image_priv; + +static void +graphics_destroy(struct graphics_priv *gr) +{ +} + +static void font_destroy(struct graphics_font_priv *font) +{ + +} + +static struct graphics_font_methods font_methods = { + font_destroy +}; + +static struct graphics_font_priv *font_new(struct graphics_priv *gr, struct graphics_font_methods *meth, int size, int flags) +{ + *meth=font_methods; + return &graphics_font_priv; +} + +static void +gc_destroy(struct graphics_gc_priv *gc) +{ +} + +static void +gc_set_linewidth(struct graphics_gc_priv *gc, int w) +{ + gc->pen->setWidth(w); +} + +static void +gc_set_dashes(struct graphics_gc_priv *gc, int w, int offset, unsigned char *dash_list, int n) +{ +} + +static void +gc_set_foreground(struct graphics_gc_priv *gc, struct color *c) +{ +#if QT_VERSION >= 0x040000 + QColor col(c->r >> 8, c->g >> 8, c->b >> 8, c->a >> 8); +#else + QColor col(c->r >> 8, c->g >> 8, c->b >> 8); +#endif + gc->pen->setColor(col); + gc->brush->setColor(col); +} + +static void +gc_set_background(struct graphics_gc_priv *gc, struct color *c) +{ +} + +static struct graphics_gc_methods gc_methods = { + gc_destroy, + gc_set_linewidth, + gc_set_dashes, + gc_set_foreground, + gc_set_background +}; + +static struct graphics_gc_priv *gc_new(struct graphics_priv *gr, struct graphics_gc_methods *meth) +{ + *meth=gc_methods; + struct graphics_gc_priv *ret=g_new0(struct graphics_gc_priv, 1); + ret->pen=new QPen(); + ret->brush=new QBrush(Qt::SolidPattern); + return ret; +} + + +static struct graphics_image_priv * +image_new(struct graphics_priv *gr, struct graphics_image_methods *meth, char *path, int *w, int *h, struct point *hot) +{ + struct graphics_image_priv *ret=g_new0(struct graphics_image_priv, 1); + + ret->image=new QImage(path); + *w=ret->image->width(); + *h=ret->image->height(); + if (hot) { + hot->x=*w/2; + hot->y=*h/2; + } + return ret; +} + +static void +draw_lines(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int count) +{ + int i; +#if QT_VERSION >= 0x040000 + QPolygon polygon; +#else + QPointArray polygon; +#endif + + for (i = 0 ; i < count ; i++) + polygon.putPoints(i, 1, p[i].x, p[i].y); + gr->painter->setPen(*gc->pen); + gr->painter->drawPolyline(polygon); +} + +static void +draw_polygon(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int count) +{ + int i; +#if QT_VERSION >= 0x040000 + QPolygon polygon; +#else + QPointArray polygon; +#endif + + for (i = 0 ; i < count ; i++) + polygon.putPoints(i, 1, p[i].x, p[i].y); + gr->painter->setPen(*gc->pen); + gr->painter->setBrush(*gc->brush); + gr->painter->drawPolygon(polygon); +} + +static void +draw_rectangle(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int w, int h) +{ + gr->painter->fillRect(p->x,p->y, w, h, *gc->brush); +} + +static void +draw_circle(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int r) +{ + gr->painter->setPen(*gc->pen); + gr->painter->drawArc(p->x-r, p->y-r, r*2, r*2, 0, 360*16); + +} + + +static void +draw_text(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct graphics_gc_priv *bg, struct graphics_font_priv *font, char *text, struct point *p, int dx, int dy) +{ + gr->painter->drawText(p->x, p->y, text); +} + +static void +draw_image(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct point *p, struct graphics_image_priv *img) +{ + gr->painter->drawImage(p->x, p->y, *img->image); +} + +static void +draw_image_warp(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct point *p, int count, char *data) +{ +} + +static void +draw_restore(struct graphics_priv *gr, struct point *p, int w, int h) +{ +} + +static void +background_gc(struct graphics_priv *gr, struct graphics_gc_priv *gc) +{ + gr->background_gc=gc; +} + +static void +draw_mode(struct graphics_priv *gr, enum draw_mode_num mode) +{ + dbg(0,"mode=%d\n", mode); + if (mode == draw_mode_begin) { + gr->painter->begin(gr->widget->pixmap); +#if 0 + gr->painter->fillRect(QRect(QPoint(0,0), gr->widget->size()), *gr->background_gc->brush); +#endif + } +#if QT_VERSION < 0x040000 + if (mode == draw_mode_cursor) { + gr->painter->begin(gr->widget); + } +#endif + if (mode == draw_mode_end) { + if (gr->mode == draw_mode_begin) { + gr->painter->end(); + gr->widget->update(); + } else { +#if QT_VERSION < 0x040000 + gr->painter->end(); +#endif + } + } + gr->mode=mode; +} + +static struct graphics_priv * overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h); + +static int argc=1; +static char *argv[]={"navit",NULL}; +static void * +get_data(struct graphics_priv *this_, char *type) +{ + if (strcmp(type, "window")) + return NULL; + this_->painter=new QPainter; + this_->widget->show(); + return &dummy; +} + + + +static void +register_resize_callback(struct graphics_priv *this_, void (*callback)(void *data, int w, int h), void *data) +{ + this_->widget->resize_callback=callback; + this_->widget->resize_callback_data=data; +} + +static void +register_motion_callback(struct graphics_priv *this_, void (*callback)(void *data, struct point *p), void *data) +{ + this_->widget->motion_callback=callback; + this_->widget->motion_callback_data=data; +} + +static void +register_button_callback(struct graphics_priv *this_, void (*callback)(void *data, int press, int button, struct point *p), void *data) +{ + this_->widget->button_callback=callback; + this_->widget->button_callback_data=data; +} + +static struct graphics_methods graphics_methods = { + graphics_destroy, + draw_mode, + draw_lines, + draw_polygon, + draw_rectangle, + draw_circle, + draw_text, + draw_image, + draw_image_warp, + draw_restore, + font_new, + gc_new, + background_gc, + overlay_new, + image_new, + get_data, + register_resize_callback, + register_button_callback, + register_motion_callback, +}; + +static struct graphics_priv * +overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h) +{ + *meth=graphics_methods; + return NULL; +} + +#if QT_VERSION < 0x040000 +static gboolean +graphics_qt_qpainter_idle(void *data) +{ + struct graphics_priv *gr=(struct graphics_priv *)data; + gr->app->processOneEvent(); + return TRUE; +} +#endif + + +static struct graphics_priv * +graphics_qt_qpainter_new(struct graphics_methods *meth, struct attr **attrs) +{ + struct graphics_priv *ret=g_new0(struct graphics_priv, 1); + *meth=graphics_methods; + ret->app = new QApplication(argc, argv); + ret->widget= new RenderArea(); +#if QT_VERSION < 0x040000 + g_idle_add(graphics_qt_qpainter_idle, ret); +#endif + + return ret; +} + +void +plugin_init(void) +{ + plugin_register_graphics_type("qt_qpainter", graphics_qt_qpainter_new); +} -- 2.7.4