| 30 |
30 |
| 31 #include "toolbar.h" |
31 #include "toolbar.h" |
| 32 #include "menu.h" |
32 #include "menu.h" |
| 33 #include "stock.h" |
33 #include "stock.h" |
| 34 |
34 |
| |
35 static void add_items(UiObject *obj, QToolBar *toolbar, CxList *defaults, CxMap *items); |
| |
36 static void create_item(UiObject *obj, QToolBar *toolbar, UiToolbarItemI *i); |
| |
37 |
| |
38 QToolBar* ui_create_toolbar(UiObject *obj) { |
| |
39 CxMap *items = uic_get_toolbar_items(); |
| |
40 CxList *left_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_LEFT); |
| |
41 CxList *center_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_CENTER); |
| |
42 CxList *right_defaults = uic_get_toolbar_defaults(UI_TOOLBAR_RIGHT); |
| |
43 |
| |
44 if(!items || cxMapSize(items) == 0) { |
| |
45 return nullptr; |
| |
46 } |
| |
47 |
| |
48 QToolBar *toolbar = new QToolBar(); |
| |
49 add_items(obj, toolbar, left_defaults, items); |
| |
50 add_items(obj, toolbar, center_defaults, items); |
| |
51 add_items(obj, toolbar, right_defaults, items); |
| |
52 |
| |
53 |
| |
54 return toolbar; |
| |
55 } |
| |
56 |
| |
57 static void add_items(UiObject *obj, QToolBar *toolbar, CxList *defaults, CxMap *items) { |
| |
58 CxIterator i = cxListIterator(defaults); |
| |
59 cx_foreach(char *, name, i) { |
| |
60 UiToolbarItemI *item = (UiToolbarItemI*)cxMapGet(items, name); |
| |
61 if(item) { |
| |
62 create_item(obj, toolbar, item); |
| |
63 } else { |
| |
64 fprintf(stderr, "UI Error: unknown toolbar item '%s'\n", name); |
| |
65 } |
| |
66 } |
| |
67 } |
| |
68 |
| |
69 static void create_item(UiObject *obj, QToolBar *toolbar, UiToolbarItemI *i) { |
| |
70 switch(i->type) { |
| |
71 case UI_TOOLBAR_ITEM: { |
| |
72 ui_toolbar_add_item(obj, toolbar, (UiToolbarItem*)i); |
| |
73 break; |
| |
74 } |
| |
75 case UI_TOOLBAR_TOGGLEITEM: { |
| |
76 ui_toolbar_add_toggleitem(obj, toolbar, (UiToolbarToggleItem*)i); |
| |
77 break; |
| |
78 } |
| |
79 case UI_TOOLBAR_MENU: { |
| |
80 ui_toolbar_add_menu(obj, toolbar, (UiToolbarMenuItem*)i); |
| |
81 break; |
| |
82 } |
| |
83 default: fprintf(stderr, "toolbar item type unimplemented: %d\n", (int)i->type); |
| |
84 } |
| |
85 } |
| |
86 |
| |
87 void ui_toolbar_add_item(UiObject *obj, QToolBar *toolbar, UiToolbarItem *item) { |
| |
88 QAction *action = new QAction(); |
| |
89 if(item->args.label) { |
| |
90 action->setText(item->args.label); |
| |
91 } |
| |
92 if(item->args.icon) { |
| |
93 action->setIcon(QIcon::fromTheme(item->args.icon)); |
| |
94 } |
| |
95 toolbar->addAction(action); |
| |
96 |
| |
97 UiEventWrapper *event = new UiEventWrapper(obj, item->args.onclick, item->args.onclickdata); |
| |
98 action->connect(action, SIGNAL(triggered()), event, SLOT(slot())); |
| |
99 action->connect(action, SIGNAL(destroyed()), event, SLOT(destroy())); |
| |
100 } |
| |
101 |
| |
102 static void toolbar_togglebutton_event(UiEvent *event, UiEventWrapper *wrapper) { |
| |
103 QAction *action = (QAction*)wrapper->customdata1; |
| |
104 event->intval = action->isChecked(); |
| |
105 if(wrapper->var) { |
| |
106 event->eventdata = wrapper->var->value; |
| |
107 event->eventdatatype = UI_EVENT_DATA_INTEGER_VALUE; |
| |
108 } |
| |
109 } |
| |
110 |
| |
111 void ui_toolbar_add_toggleitem(UiObject *obj, QToolBar *toolbar, UiToolbarToggleItem *item) { |
| |
112 QAction *action = new QAction(); |
| |
113 action->setCheckable(true); |
| |
114 if(item->args.label) { |
| |
115 action->setText(item->args.label); |
| |
116 } |
| |
117 if(item->args.icon) { |
| |
118 action->setIcon(QIcon::fromTheme(item->args.icon)); |
| |
119 } |
| |
120 toolbar->addAction(action); |
| |
121 |
| |
122 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, nullptr, item->args.varname, UI_VAR_INTEGER); |
| |
123 UiEventWrapper *event = new UiEventWrapper(obj, item->args.onchange, item->args.onchangedata); |
| |
124 event->var = var; |
| |
125 event->customdata1 = action; |
| |
126 event->prepare_event = toolbar_togglebutton_event; |
| |
127 action->connect(action, SIGNAL(triggered()), event, SLOT(slot())); |
| |
128 action->connect(action, SIGNAL(destroyed()), event, SLOT(destroy())); |
| |
129 } |
| |
130 |
| |
131 void ui_toolbar_add_menu(UiObject *obj, QToolBar *toolbar, UiToolbarMenuItem *item) { |
| |
132 |
| |
133 } |