added label and textfield (Qt)

Sun, 11 Jan 2015 15:29:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Jan 2015 15:29:38 +0100
changeset 70
3d801e8dda3a
parent 69
419c8c3209e8
child 71
3e021c5f18a0

added label and textfield (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/label.cpp file | annotate | diff | comparison | revisions
ui/qt/label.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/text.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sat Jan 10 10:14:28 2015 +0100
+++ b/application/main.c	Sun Jan 11 15:29:38 2015 +0100
@@ -76,7 +76,8 @@
 
 void action_button(UiEvent *event, void *data) {
     printf("button: %d\n", event->intval);
-    char *s = ui_gettext(event->obj, "text");
+    //char *s = ui_gettext(event->obj, "text");
+    char *s = ui_getval(name);
     printf("{%s}\n", s);
     //printf("name: {%s}\n", ui_getval(name));
     //printf("mail: {%s}\n", ui_getval(mail));
@@ -105,29 +106,17 @@
     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_grid(window);
+    
+    ui_label(window, "Name");
+    ui_textfield(window, &name);
+    ui_newline(window);
     
-    ui_table(window, list, model);
+    ui_label(window, "Email");
+    ui_textfield(window, &mail);
+    
+    
+    ui_end(window);
     
     
     ui_show(window);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/label.cpp	Sun Jan 11 15:29:38 2015 +0100
@@ -0,0 +1,51 @@
+/*
+ * 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 "label.h"
+#include "container.h"
+#include "toolkit.h"
+
+UIWIDGET ui_label(UiObject *obj, char *label) {
+    QString str = QString::fromUtf8(label);
+    QLabel *widget = new QLabel(str);
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(widget, false);
+    
+    return widget;
+}
+
+UIWIDGET ui_space(UiObject *obj) {
+    // TODO: maybe there is a better widget for this purpose
+    QLabel *widget = new QLabel();
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(widget, true);
+    
+    return widget;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/label.h	Sun Jan 11 15:29:38 2015 +0100
@@ -0,0 +1,36 @@
+/*
+ * 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 LABEL_H
+#define	LABEL_H
+
+#include "toolkit.h"
+#include <QLabel>
+
+#endif	/* LABEL_H */
+
--- a/ui/qt/qt4.pro	Sat Jan 10 10:14:28 2015 +0100
+++ b/ui/qt/qt4.pro	Sun Jan 11 15:29:38 2015 +0100
@@ -45,6 +45,7 @@
 SOURCES += model.cpp
 SOURCES += tree.cpp
 SOURCES += button.cpp
+SOURCES += label.cpp
 
 HEADERS += toolkit.h
 HEADERS += window.h
@@ -56,4 +57,5 @@
 HEADERS += model.h
 HEADERS += tree.h
 HEADERS += button.h
+HEADERS += label.h
 
--- a/ui/qt/text.cpp	Sat Jan 10 10:14:28 2015 +0100
+++ b/ui/qt/text.cpp	Sun Jan 11 15:29:38 2015 +0100
@@ -37,7 +37,8 @@
     
     if(value) {
         if(value->value && value->obj) {
-            ui_textarea_set(value, value->value);
+            QString str = QString::fromUtf8(value->value);
+            txtdoc->setPlainText(str);
         }
         
         value->get = ui_textarea_get;
@@ -81,6 +82,10 @@
     QString str = doc->toPlainText();
     QByteArray array = str.toLocal8Bit();
     const char *cstr = array.constData();
+    
+    if(text->value) {
+        free(text->value);
+    }
     text->value = strdup(cstr);
     return text->value;
 }
@@ -123,3 +128,63 @@
 void ui_textarea_remove(UiText *text, int begin, int end) {
     QTextDocument *doc = (QTextDocument*)text->obj;
 }
+
+
+/* ------------------- TextField ------------------- */
+
+UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
+    QLineEdit *textfield = new QLineEdit();
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(textfield, false);
+    
+    if(value) {
+        if(value->value) {
+            QString str = QString::fromUtf8(value->value);
+            textfield->setText(str);
+            free(value->value);
+            value->value = NULL;
+        }
+        value->set = ui_textfield_set;
+        value->get = ui_textfield_get;
+        value->obj = textfield;
+    }
+    
+    return textfield;
+}
+
+UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) {
+    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_STRING);
+    if(var) {
+        UiString *value = (UiString*)var->value;
+        return ui_textfield(obj, value);
+    } else {
+        // TODO: error
+    }
+    return NULL;
+}
+
+char* ui_textfield_get(UiString *str) {
+    QLineEdit *textfield = (QLineEdit*)str->obj;
+    QString qstr = textfield->text();
+    
+    if(str->value) {
+        free(str->value);
+    }
+    QByteArray array = qstr.toLocal8Bit();
+    const char *cstr = array.constData();
+    str->value = strdup(cstr);
+    
+    return str->value;
+}
+
+void ui_textfield_set(UiString *str, char *value) {
+    QLineEdit *textfield = (QLineEdit*)str->obj;
+    QString qstr = QString::fromUtf8(value);
+    textfield->setText(qstr);
+    
+    if(str->value) {
+        free(str->value);
+    }
+    str->value = NULL;
+}
--- a/ui/qt/text.h	Sat Jan 10 10:14:28 2015 +0100
+++ b/ui/qt/text.h	Sun Jan 11 15:29:38 2015 +0100
@@ -32,6 +32,7 @@
 #include "toolkit.h"
 #include "../ui/text.h"
 #include <QTextEdit>
+#include <QLineEdit>
 
 // value implementations
 extern "C" {    
@@ -43,6 +44,9 @@
     void ui_textarea_selection(UiText *text, int *begin, int *end);
     int ui_textarea_length(UiText *text);
     void ui_textarea_remove(UiText *text, int begin, int end);
+    
+    char* ui_textfield_get(UiString *str);
+    void ui_textfield_set(UiString *str, char *value);
 }
 
 

mercurial