added drawing area (Qt)

Wed, 20 Jan 2016 17:37:52 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 20 Jan 2016 17:37:52 +0100
changeset 103
6a6718269c22
parent 102
2988f00ed9d6
child 104
3efe0210e27e

added drawing area (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/motif/graphics.c file | annotate | diff | comparison | revisions
ui/qt/graphics.cpp file | annotate | diff | comparison | revisions
ui/qt/graphics.h file | annotate | diff | comparison | revisions
ui/qt/qt4.pro file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Jan 20 16:00:05 2016 +0100
+++ b/application/main.c	Wed Jan 20 17:37:52 2016 +0100
@@ -43,11 +43,13 @@
     printf("button clicked\n");
     fflush(stdout);
 }
-/*
+
 void draw(UiEvent *event, UiGraphics *g, void *data) {
+
     int width = g->width;
     int height = g->height;
-    //printf("rec[%d,%d]\n", width, height);
+    printf("rec[%d,%d]\n", width, height);
+    
     ui_graphics_color(g, 64, 64, 64);
     ui_draw_rect(g, 0, 0, width, height, TRUE);
     
@@ -63,7 +65,6 @@
     
     ui_text_free(text);
 }
-*/
 
 void click(UiEvent *event, void *data) {
     UiMouseEvent *me = event->eventdata;
@@ -89,9 +90,10 @@
     ui_toolbar_add_default("button2");
     
     UiObject *obj = ui_window("Test", NULL);
-    //UIWIDGET w = ui_drawingarea(obj, draw, NULL);
+    UIWIDGET w = ui_drawingarea(obj, draw, NULL);
     //ui_mouse_handler(obj, w, click, NULL);
     
+/*
     ui_grid_sp(obj, 8, 4, 4);
     
     ui_button(obj, "OK", NULL, NULL);
@@ -113,6 +115,7 @@
     ui_button(obj, "BTN1", NULL, NULL);
     
     ui_end(obj);
+*/
     
     ui_show(obj);
     ui_main();
--- a/ui/motif/graphics.c	Wed Jan 20 16:00:05 2016 +0100
+++ b/ui/motif/graphics.c	Wed Jan 20 17:37:52 2016 +0100
@@ -43,6 +43,8 @@
     ev.obj = drawevent->obj;
     ev.window = drawevent->obj->window;
     ev.document = drawevent->obj->ctx->document;
+    ev.eventdata = NULL;
+    ev.intval = 0;
     
     XtVaGetValues(
             widget,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/graphics.cpp	Wed Jan 20 17:37:52 2016 +0100
@@ -0,0 +1,133 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2015 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "graphics.h"
+#include "container.h"
+
+
+DrawingArea::DrawingArea(UiObject *obj, ui_drawfunc cb, void *data) {
+    object = obj;
+    drawCallback = cb;
+    userdata = data;
+}
+
+DrawingArea::~DrawingArea() {
+    
+}
+
+void DrawingArea::paintEvent(QPaintEvent *event) {
+    QPainter painter(this);
+    
+    UiQtGraphics g;
+    g.g.width = this->width();
+    g.g.height = this->height();
+    g.painter = &painter;
+    
+    UiEvent ev;
+    ev.obj = object;
+    ev.window = object->window;
+    ev.document = object->ctx->document;
+    ev.eventdata = NULL;
+    ev.intval = 0;
+    
+    drawCallback(&ev, &g.g, userdata);
+}
+
+
+
+UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
+    DrawingArea *widget = new DrawingArea(obj, f, userdata);
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(widget, true);
+    
+    return widget;
+}
+
+
+/* -------------------- text layout functions -------------------- */
+
+UiTextLayout* ui_text(UiGraphics *g) {
+    UiTextLayout *textlayout = new UiTextLayout();
+    return textlayout;
+}
+
+void ui_text_free(UiTextLayout *text) {
+    delete text;
+}
+
+void ui_text_setstring(UiTextLayout *layout, char *str) {
+    layout->text.setText(QString::fromUtf8(str));
+}
+
+void ui_text_setstringl(UiTextLayout *layout, char *str, int len) {
+    layout->text.setText(QString::fromUtf8(str, len));
+}
+
+void ui_text_setfont(UiTextLayout *layout, char *font, int size) {
+    layout->font = QFont(QString::fromUtf8(font), size);
+}
+
+void ui_text_getsize(UiTextLayout *layout, int *width, int *height) {
+    QSizeF size = layout->text.size();
+    *width = (int)size.width();
+    *height = (int)size.height();
+}
+
+void ui_text_setwidth(UiTextLayout *layout, int width) {
+    layout->text.setTextWidth((qreal)width);
+}
+
+
+/* -------------------- drawing functions -------------------- */
+
+void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
+    UiQtGraphics *gr = (UiQtGraphics*)g;
+    gr->color = QColor(red, green, blue);
+    gr->painter->setPen(gr->color);
+}
+
+void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
+    UiQtGraphics *gr = (UiQtGraphics*)g;
+    
+    QRect rect(x, y, w, h);
+    if(fill) {
+        gr->painter->fillRect(rect, gr->color);
+        
+    } else {
+        gr->painter->drawRect(rect);
+    }
+}
+
+void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
+    UiQtGraphics *gr = (UiQtGraphics*)g;
+    
+    gr->painter->setFont(text->font);
+    gr->painter->drawStaticText(x, y, text->text);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/graphics.h	Wed Jan 20 17:37:52 2016 +0100
@@ -0,0 +1,67 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2015 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef GRAPHICS_H
+#define GRAPHICS_H
+
+#include "toolkit.h"
+#include "../ui/graphics.h"
+
+#include <QWidget>
+#include <QPainter>
+#include <QColor>
+#include <QStaticText>
+
+typedef struct UiQtGraphics {
+    UiGraphics g;
+    QPainter   *painter;
+    QColor     color;
+} UiXlibGraphics;
+
+struct UiTextLayout {
+    QStaticText text;
+    QFont font;
+};
+
+
+class DrawingArea : public QWidget {
+    Q_OBJECT
+    
+    UiObject    *object;
+    ui_drawfunc drawCallback;
+    void        *userdata;
+    
+public:
+    DrawingArea(UiObject *obj, ui_drawfunc cb, void *data);
+    ~DrawingArea();
+    
+    virtual void paintEvent(QPaintEvent * event);
+};
+
+#endif /* GRAPHICS_H */
+
--- a/ui/qt/qt4.pro	Wed Jan 20 16:00:05 2016 +0100
+++ b/ui/qt/qt4.pro	Wed Jan 20 17:37:52 2016 +0100
@@ -46,6 +46,7 @@
 SOURCES += tree.cpp
 SOURCES += button.cpp
 SOURCES += label.cpp
+SOURCES += graphics.cpp
 
 HEADERS += toolkit.h
 HEADERS += window.h
@@ -58,4 +59,5 @@
 HEADERS += tree.h
 HEADERS += button.h
 HEADERS += label.h
+HEADERS += graphics.h
 

mercurial