added check item for menus (Qt)

Sun, 11 Jan 2015 16:33:28 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Jan 2015 16:33:28 +0100
changeset 71
3e021c5f18a0
parent 70
3d801e8dda3a
child 72
a00b46d92c54

added check item for menus (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/menu.cpp file | annotate | diff | comparison | revisions
ui/qt/menu.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 11 15:29:38 2015 +0100
+++ b/application/main.c	Sun Jan 11 16:33:28 2015 +0100
@@ -84,6 +84,10 @@
     //ui_select_tab(tabview, 1);
 }
 
+void action_check(UiEvent *event, void *data) {
+    printf("check: %d\n", event->intval);
+}
+
 void action_test(UiEvent *event, void *data) {
     //ui_select_tab(tabview, 0);
 }
@@ -98,6 +102,7 @@
     
     ui_menu("File");
     ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL);
+    ui_checkitem("Test", action_check, NULL);
     //ui_checkitem("Check", action_button, NULL);
     
     ui_toolitem_st("button", UI_STOCK_GO_BACK, action_button, NULL);
@@ -106,17 +111,6 @@
     printf("create window\n");
     UiObject *window = ui_window("Mod0", NULL);
     
-    ui_grid(window);
-    
-    ui_label(window, "Name");
-    ui_textfield(window, &name);
-    ui_newline(window);
-    
-    ui_label(window, "Email");
-    ui_textfield(window, &mail);
-    
-    
-    ui_end(window);
     
     
     ui_show(window);
--- a/ui/qt/menu.cpp	Sun Jan 11 15:29:38 2015 +0100
+++ b/ui/qt/menu.cpp	Sun Jan 11 16:33:28 2015 +0100
@@ -81,11 +81,18 @@
     groups = ucx_list_append(groups, (void*)(intptr_t)group);
 }
 
+void UiMenuItem::setCheckable(bool c) {
+    checkable = c;
+}
+
 void UiMenuItem::addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu) {
     QString str = QString::fromUtf8(label);
     UiAction *action = new UiAction(obj, str, callback, userdata);
+    action->setCheckable(checkable);
+    if(checkable) {
+        action->setChecked(false);
+    }
     menu->addAction(action);
-    //UiEventWrapper *ev = new UiEventWrapper(callback, userdata);
     QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
 }
 
@@ -116,6 +123,41 @@
 }
 
 
+/* -------------------------- UiMenuSeparator -------------------------- */
+
+void UiMenuSeparator::addTo(UiObject* obj, QMenuBar* menubar, QMenu* menu) {
+    menu->addSeparator();
+}
+
+
+/* -------------------------- UiCheckItemNV -------------------------- */
+
+UiCheckItemNV::UiCheckItemNV(char* label, char* varname) {
+    this->label = label;
+    this->varname = varname;
+}
+
+void UiCheckItemNV::addTo(UiObject* obj, QMenuBar* menubar, QMenu* menu) {
+    QString str = QString::fromUtf8(label);
+    UiAction *action = new UiAction(obj, str, NULL, NULL);
+    action->setCheckable(true);
+    menu->addAction(action);
+    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
+    
+    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_INTEGER);
+    if(var) {
+        UiInteger *value = (UiInteger*)var->value;
+        action->setChecked(value->value);
+        value->obj = action;
+        value->get = ui_checkitem_get;
+        value->set = ui_checkitem_set;
+        value = 0;
+    } else {
+        // TODO: error
+    }
+}
+
+
 /* -------------------------- UiAction -------------------------- */
 
 UiAction::UiAction(UiObject *obj, QString &label, ui_callback f, void* userdata) : QAction(label, NULL) {
@@ -126,12 +168,22 @@
 }
 
 void UiAction::trigger() {
+    if(!callback) {
+        return;
+    }
+    
     UiEvent e;
     e.obj = obj;
     e.window = obj->window;
     e.document = obj->ctx->document;
     e.eventdata = NULL;
-    e.intval = 0;
+    
+    if(isCheckable()) {
+        e.intval = isChecked();
+    } else {
+        e.intval = 0;
+    }
+    
     callback(&e, userdata);
 }
 
@@ -215,8 +267,42 @@
     cm->addMenuItem(item);
 }
 
+void ui_menuseparator() {
+    if(!current) {
+        return;
+    }
+    
+    UiMenuSeparator *item = new UiMenuSeparator();
+    UiMenu *cm = (UiMenu*)current->data;
+    cm->addMenuItem(item);
+}
 
+void ui_checkitem(char *label, ui_callback f, void *userdata) {
+    if(!current) {
+        return;
+    }
+    
+    UiMenuItem *item = new UiMenuItem(label, f, userdata);
+    item->setCheckable(true);
+    
+    UiMenu *cm = (UiMenu*)current->data;
+    cm->addMenuItem(item);
+}
 
+void ui_checkitem_nv(char *label, char *vname) {
+    if(!current) {
+        return;
+    }
+    
+    UiCheckItemNV *item = new UiCheckItemNV(label, vname);
+    
+    UiMenu *cm = (UiMenu*)current->data;
+    cm->addMenuItem(item);
+}
+
+void ui_menuitem_list(UiList *items, ui_callback f, void *userdata) {
+    
+}
 
 void ui_add_menus(UiObject *obj, QMainWindow *window) {
     QMenuBar *mb = window->menuBar();
@@ -226,3 +312,15 @@
         menu->addTo(obj, mb, NULL);        
     }
 }
+
+int ui_checkitem_get(UiInteger *i) {
+    QAction *action = (QAction*)i->obj;
+    i->value = action->isChecked();
+    return i->value;
+}
+
+void ui_checkitem_set(UiInteger *i, int value) {
+    QAction *action = (QAction*)i->obj;
+    i->value = value;
+    action->setChecked(value);
+}
--- a/ui/qt/menu.h	Sun Jan 11 15:29:38 2015 +0100
+++ b/ui/qt/menu.h	Sun Jan 11 16:33:28 2015 +0100
@@ -59,10 +59,12 @@
     ui_callback callback;
     void *userdata;
     UcxList *groups;
+    bool checkable = false;
     
 public:
     UiMenuItem(char *label, ui_callback f, void *userdata);
     void addGroup(int group);
+    void setCheckable(bool c);
     
     virtual void addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu);
 };
@@ -80,6 +82,20 @@
     virtual void addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu);
 };
 
+class UiMenuSeparator : public UiMenuItemI {
+public:
+    virtual void addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu);
+};
+
+class UiCheckItemNV : public UiMenuItemI {
+    char *label;
+    char *varname;
+    
+public:
+    UiCheckItemNV(char *label, char *varname);
+    virtual void addTo(UiObject *obj, QMenuBar *menubar, QMenu *menu);
+};
+
 
 class UiAction : public QAction {
     Q_OBJECT
@@ -95,8 +111,10 @@
     void trigger();
 };
 
+void ui_add_menus(UiObject *obj, QMainWindow *window);
 
-void ui_add_menus(UiObject *obj, QMainWindow *window);
+extern "C" int ui_checkitem_get(UiInteger *i);
+extern "C" void ui_checkitem_set(UiInteger *i, int value);
 
 #endif	/* MENU_H */
 

mercurial