ui/qt/toolbar.cpp

changeset 0
804d8803eade
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/qt/toolbar.cpp	Wed Dec 09 11:32:01 2020 +0100
@@ -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;
+}

mercurial