added button and box container (Qt)

Wed, 07 Jan 2015 17:25:33 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 07 Jan 2015 17:25:33 +0100
changeset 66
8d490d97aab8
parent 65
4697592e24ba
child 67
f72c4f01bf4a

added button and box container (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/container.h file | annotate | diff | comparison | revisions
ui/qt/button.cpp file | annotate | diff | comparison | revisions
ui/qt/button.h file | annotate | diff | comparison | revisions
ui/qt/container.cpp file | annotate | diff | comparison | revisions
ui/qt/container.h file | annotate | diff | comparison | revisions
ui/qt/qt4.pro file | annotate | diff | comparison | revisions
ui/qt/text.cpp file | annotate | diff | comparison | revisions
ui/qt/toolkit.cpp file | annotate | diff | comparison | revisions
ui/qt/toolkit.h file | annotate | diff | comparison | revisions
ui/qt/tree.cpp file | annotate | diff | comparison | revisions
ui/qt/window.cpp file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Jan 05 18:47:07 2015 +0100
+++ b/application/main.c	Wed Jan 07 17:25:33 2015 +0100
@@ -78,13 +78,13 @@
     printf("button: %d\n", event->intval);
     char *s = ui_gettext(event->obj, "text");
     printf("{%s}\n", s);
-    printf("name: {%s}\n", ui_getval(name));
-    printf("mail: {%s}\n", ui_getval(mail));
-    ui_select_tab(tabview, 0);
+    //printf("name: {%s}\n", ui_getval(name));
+    //printf("mail: {%s}\n", ui_getval(mail));
+    //ui_select_tab(tabview, 0);
 }
 
 void action_test(UiEvent *event, void *data) {
-    ui_select_tab(tabview, 1);
+    //ui_select_tab(tabview, 1);
 }
 
 int main(int argc, char** argv) { 
@@ -97,7 +97,7 @@
     
     ui_menu("File");
     ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL);
-    ui_checkitem("Check", action_button, NULL);
+    //ui_checkitem("Check", action_button, NULL);
     
     ui_toolitem_st("button", UI_STOCK_GO_BACK, action_button, NULL);
     ui_toolbar_add_default("button");
@@ -105,92 +105,22 @@
     printf("create window\n");
     UiObject *window = ui_window("Mod0", NULL);
     
-    UiModelInfo *model = ui_model_info(window->ctx, UI_STRING, "Name", UI_STRING, "Email", -1);
-    model->getvalue = (ui_model_getvalue_f)person_getvalue;
-    model->activate = action_activate;
-    model->selection = action_select;
-    UiList *list = ui_list_new();
-    Person *p1 = ui_malloc(window->ctx, sizeof(Person));
-    Person *p2 = ui_malloc(window->ctx, sizeof(Person));
-    Person *p3 = ui_malloc(window->ctx, sizeof(Person));
-    Person *p4 = ui_malloc(window->ctx, sizeof(Person));
-    p1->name = "Some Näme";
-    p1->mail = "mail@host.com";
-    p2->name = "押井守";
-    p2->mail = "other.person@provider.com";
-    p3->name = "My Self";
-    p3->mail = "my@self.org";
-    p4->name = "Gregory House";
-    p4->mail = "greg@pp";
-    ui_list_append(list, p1);
-    ui_list_append(list, p2);
-    ui_list_append(list, p3);
-    ui_list_append(list, p4);
+    ui_button(window, "Test1", action_button, NULL);
+    ui_button(window, "Test2", action_button, NULL);
     
-    /*
-    ui_sidebar(window);
+    ui_hbox(window);
+    
     ui_layout_fill(window, FALSE);
     ui_vbox(window);
-    ui_button(window, "Test1", NULL, NULL);
-    ui_button(window, "Test2", NULL, NULL);
-    ui_button(window, "Test3", NULL, NULL);
-    ui_button(window, "Test4", NULL, NULL);
-    ui_button(window, "Test5", NULL, NULL);
-    ui_button(window, "Test6", NULL, NULL);
-    ui_button(window, "Test7", NULL, NULL);
-    ui_button(window, "Test8", NULL, NULL);
-    ui_end(window);
-    ui_end(window);
-    ui_table(window, list, model);
-    */
-    ui_layout_fill(window, FALSE);
-    ui_grid(window);
-    ui_label(window, "Name");
-    ui_textfield(window, &name);
-    ui_newline(window);
-    ui_label(window, "Email");
-    ui_textfield(window, &mail);
-    ui_newline(window);
-    ui_end(window);
-    
-    tabview = ui_tabview(window);
-    
-    ui_tab(window, "Editor");
-    ui_textarea_nv(window, "text");
-    ui_button(window, "Submit", action_test, NULL);
+    ui_button(window, "A", NULL, NULL);
+    ui_button(window, "B", NULL, NULL);
+    ui_button(window, "C", NULL, NULL);
     ui_end(window);
     
-    ui_tab(window, "Button");
-    ui_button(window, "TestButton", NULL, NULL);
+    ui_textarea_nv(window, "text");
     ui_end(window);
     
-    ui_end(window);
-    ui_button(window, "End", NULL, NULL);
-    /*
-    UiTabbedPane *view = ui_tabbed_document_view(window);
-    
-    UiObject *tab1 = ui_document_tab(view);
-    Document *doc1 = ui_document_new(1024);
-    UiContext *ctx1 = ui_document_context(doc1);
-    ui_document_regtext(doc1, "text", &doc1->text);
-    ui_textarea_nv(tab1, "text");
-    ui_set_document(tab1, doc1);
-    
-    
-    UiObject *tab2 = ui_document_tab(view);
-    Document *doc2 = ui_document_new(1024);
-    UiContext *ctx2 = ui_document_context(doc2);
-    ui_document_regtext(doc2, "text", &doc2->text);
-    ui_textarea_nv(tab2, "text");
-    ui_set_document(tab2, doc2);
-    
-    UiObject *tab3 = ui_document_tab(view);
-    Document *doc3 = ui_document_new(1024);
-    UiContext *ctx3 = ui_document_context(doc3);
-    ui_document_regtext(doc3, "text", &doc3->text);
-    ui_textarea_nv(tab3, "text");
-    ui_set_document(tab3, doc3);
-    */
+    ui_button(window, "Test4", action_button, NULL);
     
     ui_show(window);
     ui_main();
--- a/ui/gtk/container.c	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/gtk/container.c	Wed Jan 07 17:25:33 2015 +0100
@@ -33,14 +33,6 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
-static UiBool ui_lb2bool(UiLayoutBool b) {
-    return b == UI_LAYOUT_TRUE ? TRUE : FALSE;
-}
-
-static UiLayoutBool ui_bool2lb(UiBool b) {
-    return b ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE;
-}
-
 GtkWidget* ui_gtk_vbox_new() {
 #ifdef UI_GTK3
     return gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
--- a/ui/gtk/container.h	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/gtk/container.h	Wed Jan 07 17:25:33 2015 +0100
@@ -37,6 +37,8 @@
 #endif
 
 #define ui_reset_layout(layout) memset(&(layout), 0, sizeof(UiLayout))
+#define ui_lb2bool(b) ((b) == UI_LAYOUT_TRUE ? TRUE : FALSE)
+#define ui_bool2lb(b) ((b) ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE)
     
 typedef void (*ui_container_add_f)(UiContainer*, GtkWidget*, UiBool);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/button.cpp	Wed Jan 07 17:25:33 2015 +0100
@@ -0,0 +1,46 @@
+/*
+ * 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 "button.h"
+#include "container.h"
+#include "toolkit.h"
+
+UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data) {
+    QString str = QString::fromUtf8(label);
+    QPushButton *button = new QPushButton(label);
+    
+    if(f) {
+        UiEventWrapper *event = new UiEventWrapper(obj, f, data);
+        button->connect(button, SIGNAL(clicked()), event, SLOT(slot()));
+    }
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(button, false);
+    
+    return button;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/button.h	Wed Jan 07 17:25:33 2015 +0100
@@ -0,0 +1,37 @@
+/*
+ * 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 BUTTON_H
+#define	BUTTON_H
+
+#include "toolkit.h"
+#include "../ui/button.h"
+#include <QPushButton>
+
+#endif	/* BUTTON_H */
+
--- a/ui/qt/container.cpp	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/container.cpp	Wed Jan 07 17:25:33 2015 +0100
@@ -26,14 +26,75 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <stdio.h>
 #include "container.h"
 
+#include <QSpacerItem>
+
+/* -------------------- UiBoxContainer -------------------- */
+
+UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
+    this->box = box;
+    box->setContentsMargins(QMargins(0,0,0,0));
+    box->setSpacing(0);
+    
+    ui_reset_layout(layout);
+}
+
+void UiBoxContainer::add(QWidget* widget, bool fill) {
+    if(layout.fill != UI_LAYOUT_UNDEFINED) {
+        fill = ui_lb2bool(layout.fill);
+    }
+    
+    if(hasStretchedWidget && fill) {
+        fill = false;
+        fprintf(stderr, "UiError: container has 2 filled widgets");
+    }
+    
+    box->addWidget(widget, fill);
+    
+    if(!hasStretchedWidget) {
+        QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
+        box->removeItem(space);
+        box->addSpacerItem(newspace);
+        space = newspace;
+        
+    }
+    
+    if(fill) {
+        hasStretchedWidget = true;
+    }
+    ui_reset_layout(layout);
+}
+
+UIWIDGET ui_box(UiObject *obj, QBoxLayout::Direction dir) {
+    UiContainer *ct = uic_get_current_container(obj);
+    QWidget *widget = new QWidget();
+    QBoxLayout *box = new QBoxLayout(dir);
+    widget->setLayout(box);
+    ct->add(widget, true);
+    
+    UiObject *newobj = uic_object_new(obj, widget);
+    newobj->container = new UiBoxContainer(box);
+    uic_obj_add(obj, newobj);
+    
+    return widget;
+}
+
+UIWIDGET ui_vbox(UiObject *obj) {
+    return ui_box(obj, QBoxLayout::TopToBottom);
+}
+
+UIWIDGET ui_hbox(UiObject *obj) {
+    return ui_box(obj, QBoxLayout::LeftToRight);
+}
+
 
-UiWindowContainer::UiWindowContainer(QMainWindow* window) {
-    this->window = window;
-}
+
+
+/* -------------------- layout functions -------------------- */
 
-void UiWindowContainer::add(QWidget* widget) {
-    window->setCentralWidget(widget);
+void ui_layout_fill(UiObject *obj, UiBool fill) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.fill = ui_bool2lb(fill);
 }
-
--- a/ui/qt/container.h	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/container.h	Wed Jan 07 17:25:33 2015 +0100
@@ -32,18 +32,41 @@
 #include "toolkit.h"
 #include "window.h"
 
+#include <string.h>
+#include <QBoxLayout>
 
-struct UiContainer {
-    virtual void add(QWidget *widget) = 0;
+#define ui_lb2bool(b) ((b) == UI_LAYOUT_TRUE ? TRUE : FALSE)
+#define ui_bool2lb(b) ((b) ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE)
+#define ui_reset_layout(layout) memset(&(layout), 0, sizeof(UiLayout))
+
+typedef struct UiLayout UiLayout;
+
+enum UiLayoutBool {
+    UI_LAYOUT_UNDEFINED = 0,
+    UI_LAYOUT_TRUE,
+    UI_LAYOUT_FALSE,
+};
+typedef enum UiLayoutBool UiLayoutBool;
+
+struct UiLayout {
+    UiLayoutBool fill;
 };
 
-class UiWindowContainer : public UiContainer {
-public:  
-    QMainWindow *window;
+struct UiContainer {
+    UiLayout layout;
     
-    UiWindowContainer(QMainWindow *window);
+    virtual void add(QWidget *widget, bool fill) = 0;
+};
+
+class UiBoxContainer : public UiContainer {
+public:
+    QBoxLayout  *box;
+    bool        hasStretchedWidget = false;
+    QSpacerItem *space;
     
-    virtual void add(QWidget *widget);
+    UiBoxContainer(QBoxLayout *box);
+    
+    virtual void add(QWidget *widget, bool fill);
 };
 
 #endif	/* CONTAINER_H */
--- a/ui/qt/qt4.pro	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/qt4.pro	Wed Jan 07 17:25:33 2015 +0100
@@ -44,6 +44,7 @@
 SOURCES += text.cpp
 SOURCES += model.cpp
 SOURCES += tree.cpp
+SOURCES += button.cpp
 
 HEADERS += toolkit.h
 HEADERS += window.h
@@ -53,5 +54,6 @@
 HEADERS += container.h
 HEADERS += text.h
 HEADERS += model.h
-HEADESR += tree.h
+HEADERS += tree.h
+HEADERS += button.h
 
--- a/ui/qt/text.cpp	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/text.cpp	Wed Jan 07 17:25:33 2015 +0100
@@ -55,7 +55,7 @@
     UiContainer *ct = uic_get_current_container(obj); 
     QTextEdit *textedit = new QTextEdit();
     textedit->setDocument(txtdoc);
-    ct->add((textedit));
+    ct->add(textedit, true);
     
     return textedit;
 }
--- a/ui/qt/toolkit.cpp	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/toolkit.cpp	Wed Jan 07 17:25:33 2015 +0100
@@ -100,12 +100,18 @@
 
 
 
-UiEventWrapper::UiEventWrapper(ui_callback f, void* userdata) {
+UiEventWrapper::UiEventWrapper(UiObject *obj, ui_callback f, void* userdata) {
+    this->obj = obj;
     this->callback = f;
     this->userdata = userdata;
 }
 
 void UiEventWrapper::slot() {
-    printf("event\n");
-    
+    UiEvent e;
+    e.obj = obj;
+    e.window = obj->window;
+    e.document = obj->ctx->document;
+    e.eventdata = NULL;
+    e.intval = 0;
+    callback(&e, userdata);
 }
--- a/ui/qt/toolkit.h	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/toolkit.h	Wed Jan 07 17:25:33 2015 +0100
@@ -38,11 +38,12 @@
 class UiEventWrapper : public QObject {
     Q_OBJECT
     
+    UiObject *obj;
     ui_callback callback;
     void *userdata;
 
 public:
-    UiEventWrapper(ui_callback f, void *userdata);
+    UiEventWrapper(UiObject *obj, ui_callback f, void *userdata);
     
 public slots:
     void slot();
--- a/ui/qt/tree.cpp	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/tree.cpp	Wed Jan 07 17:25:33 2015 +0100
@@ -35,7 +35,7 @@
     view->setModel(model);
     
     UiContainer *ct = uic_get_current_container(obj); 
-    ct->add(view);
+    ct->add(view, true);
     return view;
 }
 
--- a/ui/qt/window.cpp	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/qt/window.cpp	Wed Jan 07 17:25:33 2015 +0100
@@ -34,6 +34,8 @@
 #include "toolbar.h"
 #include "container.h"
 
+#include <QVBoxLayout>
+
 UiObject* ui_window(char *title, void *window_data) {
     UcxMempool *mp = ucx_mempool_new(256);
     UiObject *obj = (UiObject*)ucx_mempool_calloc(mp, 1, sizeof(UiObject));
@@ -49,7 +51,12 @@
     QToolBar *toolbar = ui_create_toolbar(obj);
     window->addToolBar(Qt::TopToolBarArea, toolbar);
     
-    obj->container = new UiWindowContainer(window);
+    //QBoxLayout *box = new QBoxLayout(QBoxLayout::TopToBottom);
+    QBoxLayout *box = new QVBoxLayout();
+    QWidget *boxWidget = new QWidget();
+    boxWidget->setLayout(box);
+    window->setCentralWidget(boxWidget);
+    obj->container = new UiBoxContainer(box);
     
     obj->widget = window;
     return obj;
--- a/ui/ui/toolkit.h	Mon Jan 05 18:47:07 2015 +0100
+++ b/ui/ui/toolkit.h	Wed Jan 07 17:25:33 2015 +0100
@@ -60,6 +60,13 @@
 
 #endif
 
+#ifndef TRUE
+#define TRUE 1
+#endif
+#ifndef FALSE
+#define FALSE 0
+#endif
+
 #ifdef	__cplusplus
 extern "C" {
 #endif

mercurial