added context menus (Qt)

Wed, 21 Jan 2015 16:02:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 21 Jan 2015 16:02:30 +0100
changeset 73
473acef47ddd
parent 72
a00b46d92c54
child 74
55718d4ed227

added context menus (Qt)

application/main.c 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/menu.cpp file | annotate | diff | comparison | revisions
ui/qt/menu.h file | annotate | diff | comparison | revisions
ui/ui/menu.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Jan 12 12:03:05 2015 +0100
+++ b/application/main.c	Wed Jan 21 16:02:30 2015 +0100
@@ -134,6 +134,14 @@
     ui_list_append(list, p4);
     
     ui_listview(window, list, (ui_model_getvalue_f)person_getvalue, action_activate, NULL);
+    ui_contextmenu(window);
+    ui_widget_menuitem(window, "OK", action_test, NULL);
+    ui_widget_menuitem(window, "1", action_test, NULL);
+    ui_widget_menuitem(window, "2", action_test, NULL);
+    ui_widget_menuitem(window, "3", action_test, NULL);
+    ui_widget_menuitem(window, "4", action_test, NULL);
+    ui_widget_menuitem_st(window, UI_STOCK_CLOSE, action_test, NULL);
+    
     
     ui_show(window);
     ui_main();
--- a/ui/qt/container.cpp	Mon Jan 12 12:03:05 2015 +0100
+++ b/ui/qt/container.cpp	Wed Jan 21 16:02:30 2015 +0100
@@ -35,6 +35,8 @@
 /* -------------------- UiBoxContainer -------------------- */
 
 UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
+    this->current = NULL;
+    this->menu = NULL;
     this->box = box;
     box->setContentsMargins(QMargins(0,0,0,0));
     box->setSpacing(0);
@@ -65,6 +67,7 @@
         hasStretchedWidget = true;
     }
     ui_reset_layout(layout);
+    current = widget;
 }
 
 UIWIDGET ui_box(UiObject *obj, QBoxLayout::Direction dir) {
@@ -94,6 +97,8 @@
 /* -------------------- UiGridContainer -------------------- */
 
 UiGridContainer::UiGridContainer(QGridLayout* grid) {
+    this->current = NULL;
+    this->menu = NULL;
     this->grid = grid;
     grid->setContentsMargins(QMargins(0,0,0,0));
     grid->setSpacing(0);
@@ -120,12 +125,15 @@
     x++;
     
     ui_reset_layout(layout);
+    current = widget;
 }
 
 
 /* -------------------- UiTabViewContainer -------------------- */
 
 UiTabViewContainer::UiTabViewContainer(QTabWidget* tabwidget) {
+    this->current = NULL;
+    this->menu = NULL;
     this->tabwidget = tabwidget;
 }
 
@@ -140,6 +148,7 @@
 
 void UiStackContainer::add(QWidget* widget, bool fill) {
     stack->addWidget(widget);
+    current = widget;
 }
 
 UIWIDGET ui_tabview(UiObject *obj) {
--- a/ui/qt/container.h	Mon Jan 12 12:03:05 2015 +0100
+++ b/ui/qt/container.h	Wed Jan 21 16:02:30 2015 +0100
@@ -58,8 +58,10 @@
 };
 
 struct UiContainer {
-    UiLayout layout;
-    
+    UiLayout layout; 
+    UIWIDGET current;
+    QMenu    *menu;
+
     virtual void add(QWidget *widget, bool fill) = 0;
 };
 
--- a/ui/qt/menu.cpp	Mon Jan 12 12:03:05 2015 +0100
+++ b/ui/qt/menu.cpp	Wed Jan 21 16:02:30 2015 +0100
@@ -38,6 +38,7 @@
 #include "../ui/properties.h"
 #include "../ui/window.h"
 #include "stock.h"
+#include "container.h"
 
 static UcxList *menus;
 static UcxList *current;
@@ -324,3 +325,96 @@
     i->value = value;
     action->setChecked(value);
 }
+
+
+/*
+ * widget menu functions
+ */
+
+UiContextMenuHandler::UiContextMenuHandler(QWidget *widget, QMenu* menu) {
+    this->widget = widget;
+    this->menu = menu;
+}
+
+void UiContextMenuHandler::contextMenuEvent(const QPoint & pos) {
+    menu->popup(widget->mapToGlobal(pos));
+}
+
+void ui_contextmenu(UiObject *obj) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ui_contextmenu_w(obj, ct->current);
+}
+
+void ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    QMenu *menu = new QMenu(widget);
+    widget->setContextMenuPolicy(Qt::CustomContextMenu);
+    
+    UiContextMenuHandler *handler = new UiContextMenuHandler(widget, menu);
+    QObject::connect(
+            widget,
+            SIGNAL(customContextMenuRequested(QPoint)),
+            handler,
+            SLOT(contextMenuEvent(QPoint)));
+    
+    ct->menu = menu;
+}
+
+void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
+    ui_widget_menuitem_gr(obj, label, f, userdata, -1);
+}
+
+void ui_widget_menuitem_gr(UiObject *obj, char *label, ui_callback f, void *userdata, ...) {
+    UiContainer *ct = uic_get_current_container(obj);
+    if(!ct->menu) {
+        return;
+    }
+    
+    // add groups
+    UcxList *groups = NULL;
+    va_list ap;
+    va_start(ap, userdata);
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        ucx_list_append(groups, (void*)(intptr_t)group);
+    }
+    va_end(ap);
+    
+    // create menuitem
+    QString str = QString::fromUtf8(label);
+    UiAction *action = new UiAction(obj, str, f, userdata);
+    ct->menu->addAction(action);
+    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
+}
+
+void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata) {
+    ui_widget_menuitem_stgr(obj, stockid, f, userdata, -1);
+}
+
+void ui_widget_menuitem_stgr(UiObject *obj, char *stockid, ui_callback f, void *userdata, ...) {
+    UiContainer *ct = uic_get_current_container(obj);
+    if(!ct->menu) {
+        return;
+    }
+    
+    // add groups
+    UcxList *groups = NULL;
+    va_list ap;
+    va_start(ap, userdata);
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        ucx_list_append(groups, (void*)(intptr_t)group);
+    }
+    va_end(ap);
+    
+    // create menuitem
+    UiStockItem *stockItem = ui_get_stock_item(stockid);
+    
+    QString str = QString::fromUtf8(stockItem->label);
+    UiAction *action = new UiAction(obj, str, f, userdata);
+    action->setIcon(QIcon::fromTheme(stockItem->icon_name));
+    action->setIconVisibleInMenu(true);
+    ct->menu->addAction(action);
+    QObject::connect(action, SIGNAL(triggered()), action, SLOT(trigger()));
+}
--- a/ui/qt/menu.h	Mon Jan 12 12:03:05 2015 +0100
+++ b/ui/qt/menu.h	Wed Jan 21 16:02:30 2015 +0100
@@ -35,6 +35,7 @@
 #include <QMainWindow>
 #include <QMenu>
 #include <QMenuBar>
+#include <QContextMenuEvent>
 
 class UiMenuItemI {
 public:
@@ -116,5 +117,18 @@
 extern "C" int ui_checkitem_get(UiInteger *i);
 extern "C" void ui_checkitem_set(UiInteger *i, int value);
 
+class UiContextMenuHandler : public QObject {
+    Q_OBJECT
+    
+    QWidget *widget;
+    QMenu *menu;
+    
+public:
+    UiContextMenuHandler(QWidget *widget, QMenu *menu);
+    
+public slots:
+    void contextMenuEvent(const QPoint & pos);
+};
+
 #endif	/* MENU_H */
 
--- a/ui/ui/menu.h	Mon Jan 12 12:03:05 2015 +0100
+++ b/ui/ui/menu.h	Wed Jan 21 16:02:30 2015 +0100
@@ -35,6 +35,9 @@
 extern "C" {
 #endif
 
+/*
+ * application menu functions
+ */
 void ui_menu(char *label);
 void ui_submenu(char *label);
 void ui_submenu_end();
@@ -51,6 +54,17 @@
 
 void ui_menuitem_list(UiList *items, ui_callback f, void *userdata);
 
+/*
+ * widget menu functions
+ */
+void ui_contextmenu(UiObject *obj);
+void ui_contextmenu_w(UiObject *obj, UIWIDGET widget);
+
+void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata);
+void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata);
+void ui_widget_menuitem_gr(UiObject *obj, char *label, ui_callback f, void *userdata, ...);
+void ui_widget_menuitem_stgr(UiObject *obj, char *stockid, ui_callback f, void *userdata, ...);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial