added toolbar (Qt)

Mon, 25 Aug 2014 17:27:29 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Aug 2014 17:27:29 +0200
changeset 55
9076eb40454d
parent 54
97bafeca1c7c
child 56
87e3a5dc66dd

added toolbar (Qt)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/menu.cpp file | annotate | diff | comparison | revisions
ui/qt/qt4.pro file | annotate | diff | comparison | revisions
ui/qt/stock.cpp file | annotate | diff | comparison | revisions
ui/qt/toolbar.cpp file | annotate | diff | comparison | revisions
ui/qt/toolbar.h file | annotate | diff | comparison | revisions
ui/qt/window.cpp file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Aug 24 13:31:07 2014 +0200
+++ b/application/main.c	Mon Aug 25 17:27:29 2014 +0200
@@ -43,9 +43,13 @@
     UiText text;
 } Document;
 
+void action_new(UiEvent *event, void *data) {
+    printf("new\n");
+}
+
 void action_close(UiEvent *event, void *data) {
     printf("action_close\n");
-    //ui_close(event->obj);
+    ui_close(event->obj);
 }
 
 char* person_getvalue(Person *p, int column) {
@@ -72,8 +76,8 @@
 
 void action_button(UiEvent *event, void *data) {
     printf("button: %d\n", event->intval);
-    char *s = ui_gettext(event->obj, "text");
-    printf("{%s}\n", s);    
+    //char *s = ui_gettext(event->obj, "text");
+    //printf("{%s}\n", s);    
 }
 
 int main(int argc, char** argv) { 
@@ -86,11 +90,12 @@
     
     ui_menu("File");
     //ui_menuitem("Close", action_close, NULL);
+    ui_menuitem_st(UI_STOCK_NEW, action_new, NULL);
     ui_menuitem_st(UI_STOCK_CLOSE, action_close, NULL);
     //ui_checkitem("Check", action_button, NULL);
     
-    //ui_toolitem_st("button", UI_STOCK_GO_BACK, action_button, NULL);
-    //ui_toolbar_add_default("button");
+    ui_toolitem_st("button", UI_STOCK_GO_BACK, action_button, NULL);
+    ui_toolbar_add_default("button");
     
     printf("create window\n");
     UiObject *window = ui_window("Mod0", NULL);
--- a/ui/qt/menu.cpp	Sun Aug 24 13:31:07 2014 +0200
+++ b/ui/qt/menu.cpp	Mon Aug 25 17:27:29 2014 +0200
@@ -108,6 +108,8 @@
     
     QString str = QString::fromUtf8(stockItem->label);
     UiAction *action = new UiAction(obj, str, callback, userdata);
+    action->setIcon(QIcon::fromTheme(stockItem->icon_name));
+    action->setIconVisibleInMenu(true);
     menu->addAction(action);
     //UiEventWrapper *ev = new UiEventWrapper(callback, userdata);
     QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
--- a/ui/qt/qt4.pro	Sun Aug 24 13:31:07 2014 +0200
+++ b/ui/qt/qt4.pro	Mon Aug 25 17:27:29 2014 +0200
@@ -1,7 +1,7 @@
 #
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 #
-# Copyright 2012 Olaf Wintermann. All rights reserved.
+# Copyright 2014 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:
@@ -38,10 +38,12 @@
 SOURCES += toolkit.cpp
 SOURCES += window.cpp
 SOURCES += menu.cpp
+SOURCES += toolbar.cpp
 SOURCES += stock.cpp
 
 HEADERS += toolkit.h
 HEADERS += window.h
 HEADERS += menu.h
+HEADERS += toolbar.h
 HEADERS += stock.h
 
--- a/ui/qt/stock.cpp	Sun Aug 24 13:31:07 2014 +0200
+++ b/ui/qt/stock.cpp	Mon Aug 25 17:27:29 2014 +0200
@@ -36,20 +36,20 @@
 void ui_stock_init() {
     stock_items = ucx_map_new(64);
     
-    ui_add_stock_item(UI_STOCK_NEW, "New", "");
-    ui_add_stock_item(UI_STOCK_OPEN, "Open", "");
-    ui_add_stock_item(UI_STOCK_SAVE, "Save", "");
-    ui_add_stock_item(UI_STOCK_SAVE_AS, "Save as ...", "");
-    ui_add_stock_item(UI_STOCK_REVERT_TO_SAVED, "Revert to saved", "");
-    ui_add_stock_item(UI_STOCK_CLOSE, "Close", "");
-    ui_add_stock_item(UI_STOCK_UNDO, "Undo", "");
-    ui_add_stock_item(UI_STOCK_REDO, "Redo", "");
-    ui_add_stock_item(UI_STOCK_GO_BACK, "Back", "");
-    ui_add_stock_item(UI_STOCK_GO_FORWARD, "Forward", "");
-    ui_add_stock_item(UI_STOCK_CUT, "Cut", "");
-    ui_add_stock_item(UI_STOCK_COPY, "Copy", "");
-    ui_add_stock_item(UI_STOCK_PASTE, "Paste", "");
-    ui_add_stock_item(UI_STOCK_DELETE, "Delete", "");
+    ui_add_stock_item(UI_STOCK_NEW, "New", "document-new");
+    ui_add_stock_item(UI_STOCK_OPEN, "Open", "document-open");
+    ui_add_stock_item(UI_STOCK_SAVE, "Save", "document-save");
+    ui_add_stock_item(UI_STOCK_SAVE_AS, "Save as ...", "document-save-as");
+    ui_add_stock_item(UI_STOCK_REVERT_TO_SAVED, "Revert to saved", "document-revert");
+    ui_add_stock_item(UI_STOCK_CLOSE, "Close", "window-close");
+    ui_add_stock_item(UI_STOCK_UNDO, "Undo", "edit-undo");
+    ui_add_stock_item(UI_STOCK_REDO, "Redo", "edit-redo");
+    ui_add_stock_item(UI_STOCK_GO_BACK, "Back", "go-previous");
+    ui_add_stock_item(UI_STOCK_GO_FORWARD, "Forward", "go-next");
+    ui_add_stock_item(UI_STOCK_CUT, "Cut", "edit-cut");
+    ui_add_stock_item(UI_STOCK_COPY, "Copy", "edit-copy");
+    ui_add_stock_item(UI_STOCK_PASTE, "Paste", "edit-paste");
+    ui_add_stock_item(UI_STOCK_DELETE, "Delete", "edit-delete");
 }
 
 void ui_add_stock_item(char *id, char *label, char *icon) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/toolbar.cpp	Mon Aug 25 17:27:29 2014 +0200
@@ -0,0 +1,165 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 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 <ucx/map.h>
+#include <inttypes.h>
+
+#include "toolbar.h"
+#include "menu.h"
+#include "stock.h"
+
+static UcxMap *toolbar_items = ucx_map_new(16);
+static UcxList *defaults;
+
+/* ------------------------- UiToolItem ------------------------- */
+
+UiToolItem::UiToolItem(char *label, ui_callback f, void *userdata) {
+    this->label = label;
+    this->image = NULL;
+    this->callback = f;
+    this->userdata = userdata;
+    this->isimportant = false;
+    this->groups = NULL;
+}
+
+void UiToolItem::addGroup(int group) {
+    groups = ucx_list_append(groups, (void*)(intptr_t)group);
+}
+
+void UiToolItem::addTo(UiObject *obj, QToolBar *toolbar) {
+    QString str = QString::fromUtf8(label);
+    UiAction *action = new UiAction(obj, str, callback, userdata);
+    action->setIcon(QIcon::fromTheme(image));
+    toolbar->addAction(action);
+    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
+}
+
+
+/* ------------------------- UiStockToolItem ------------------------- */
+
+UiStockToolItem::UiStockToolItem(char *stockid, ui_callback f, void *userdata) {
+    this->stockid = stockid;
+    this->callback = f;
+    this->userdata = userdata;
+    this->isimportant = false;
+    this->groups = NULL;
+}
+
+void UiStockToolItem::addGroup(int group) {
+    groups = ucx_list_append(groups, (void*)(intptr_t)group);
+}
+
+void UiStockToolItem::addTo(UiObject *obj, QToolBar *toolbar) {
+    UiStockItem *stockItem = ui_get_stock_item(stockid);
+    QString str = QString::fromUtf8(stockItem->label);
+    
+    UiAction *action = new UiAction(obj, str, callback, userdata);
+    action->setIcon(QIcon::fromTheme(stockItem->icon_name));
+    toolbar->addAction(action);
+    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
+}
+
+
+
+void ui_toolitem_vstgr(
+        char *name,
+        char *stockid,
+        int isimportant,
+        ui_callback f,
+        void *userdata,
+        va_list ap)
+{
+    UiStockToolItem *item = new UiStockToolItem(stockid, f, userdata);
+    item->isimportant = isimportant;
+    
+    // add groups
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        item->addGroup(group);
+    }
+    
+    ucx_map_cstr_put(toolbar_items, name, item);
+}
+
+void ui_toolitem_img(char *name, char *label, char *img, ui_callback f, void *udata) {
+    UiToolItem *item = new UiToolItem(label, f, udata);
+    item->image = img;
+    item->isimportant = false;
+    
+    ucx_map_cstr_put(toolbar_items, name, item);
+}
+
+void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
+    ui_toolitem_img(name, label, NULL, f, udata);
+}
+
+void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *userdata) {
+    ui_toolitem_stgr(name, stockid, f, userdata, -1);
+}
+
+void ui_toolitem_sti(char *name, char *stockid, ui_callback f, void *userdata) {
+    ui_toolitem_stgri(name, stockid, f, userdata, -1);
+}
+
+void ui_toolitem_stgr(char *name, char *stockid, ui_callback f, void *userdata, ...) {
+    va_list ap;
+    va_start(ap, userdata);
+    ui_toolitem_vstgr(name, stockid, 0, f, userdata, ap);
+    va_end(ap);
+}
+
+void ui_toolitem_stgri(char *name, char *stockid, ui_callback f, void *userdata, ...) {
+    va_list ap;
+    va_start(ap, userdata);
+    ui_toolitem_vstgr(name, stockid, 1, f, userdata, ap);
+    va_end(ap);
+}
+
+
+void ui_toolbar_add_default(char *name) {
+    char *s = strdup(name);
+    defaults = ucx_list_append(defaults, s);
+}
+
+
+QToolBar* ui_create_toolbar(UiObject *obj) {
+    QToolBar *toolbar = new QToolBar();
+    
+    UCX_FOREACH(elm, defaults) {
+        UiToolItemI *item = (UiToolItemI*)ucx_map_cstr_get(toolbar_items, (char*)elm->data);
+        if(item) {
+            item->addTo(obj, toolbar);
+        } else if(!strcmp((char*)elm->data, "@separator")) {
+            
+        } else {
+            fprintf(stderr, "UI Error: Unknown toolbar item: %s\n", elm->data);
+        }
+    }
+    
+    return toolbar;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/toolbar.h	Mon Aug 25 17:27:29 2014 +0200
@@ -0,0 +1,83 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 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 TOOLBAR_H
+#define	TOOLBAR_H
+
+#include "toolkit.h"
+#include "../ui/toolbar.h"
+#include <ucx/list.h>
+#include <QToolBar>
+
+class UiToolItemI {
+public:
+    virtual void addTo(UiObject *obj, QToolBar *toolbar) = 0;
+};
+
+class UiToolItem {
+public:
+    char *label;
+    char *image;
+    ui_callback callback;
+    void *userdata;
+    UcxList *groups;
+    bool isimportant;
+    
+    UiToolItem(char *label, ui_callback f, void *userdata);
+    void addGroup(int group);
+    virtual void addTo(UiObject *obj, QToolBar *toolbar);
+};
+
+class UiStockToolItem {
+public:
+    char *stockid;
+    ui_callback callback;
+    void *userdata;
+    UcxList *groups;
+    bool isimportant;
+    
+    UiStockToolItem(char *stockid, ui_callback f, void *userdata);
+    void addGroup(int group);
+    virtual void addTo(UiObject *obj, QToolBar *toolbar);
+};
+
+
+void ui_toolitem_vstgr(
+        char *name,
+        char *stockid,
+        int isimportant,
+        ui_callback f,
+        void *userdata,
+        va_list ap);
+
+
+QToolBar* ui_create_toolbar(UiObject *obj);
+
+
+#endif	/* TOOLBAR_H */
+
--- a/ui/qt/window.cpp	Sun Aug 24 13:31:07 2014 +0200
+++ b/ui/qt/window.cpp	Mon Aug 25 17:27:29 2014 +0200
@@ -31,6 +31,7 @@
 
 #include "window.h"
 #include "menu.h"
+#include "toolbar.h"
 
 UiObject* ui_window(char *title, void *window_data) {
     UcxMempool *mp = ucx_mempool_new(256);
@@ -44,6 +45,9 @@
     
     ui_add_menus(obj, window);
     
+    QToolBar *toolbar = ui_create_toolbar(obj);
+    window->addToolBar(Qt::TopToolBarArea, toolbar);
+    
     obj->widget = window;
     return obj;
 }

mercurial