ui/qt/container.cpp

changeset 103
6606616eca9f
parent 0
2483f517c562
child 108
77254bd6dccb
equal deleted inserted replaced
102:64ded9f6a6c6 103:6606616eca9f
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include "container.h" 30 #include "container.h"
31 #include "../common/object.h"
32
33 #include <cx/mempool.h>
31 34
32 #include <QSpacerItem> 35 #include <QSpacerItem>
33 #include <QStackedWidget> 36 #include <QStackedWidget>
34 37 #include <QLabel>
38
39 static void delete_container(UiContainerPrivate *ct) {
40 delete ct;
41 }
42
43 void ui_container_add(UiObject *obj, UiContainerPrivate *ct) {
44 UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX));
45 container->close = 0;
46 container->container = ct;
47 container->prev = NULL;
48 container->next = NULL;
49 cxMempoolRegister(obj->ctx->mp, ct, (cx_destructor_func)delete_container);
50 uic_object_push_container(obj, container);
51 }
35 52
36 /* -------------------- UiBoxContainer -------------------- */ 53 /* -------------------- UiBoxContainer -------------------- */
37 54
38 UiBoxContainer::UiBoxContainer(QBoxLayout* box) { 55 UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
39 this->current = NULL;
40 this->menu = NULL;
41 this->box = box; 56 this->box = box;
42 box->setContentsMargins(QMargins(0,0,0,0)); 57 box->setContentsMargins(QMargins(0,0,0,0));
43 box->setSpacing(0); 58 box->setSpacing(0);
44 59
45 ui_reset_layout(layout); 60 ui_reset_layout(layout);
69 } 84 }
70 ui_reset_layout(layout); 85 ui_reset_layout(layout);
71 current = widget; 86 current = widget;
72 } 87 }
73 88
74 UIWIDGET ui_box(UiObject *obj, QBoxLayout::Direction dir) { 89 UIWIDGET ui_box(UiObject *obj, UiContainerArgs args, QBoxLayout::Direction dir) {
75 UiContainer *ct = uic_get_current_container(obj); 90 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
91 UI_APPLY_LAYOUT(ctn->layout, args);
92
76 QWidget *widget = new QWidget(); 93 QWidget *widget = new QWidget();
77 QBoxLayout *box = new QBoxLayout(dir); 94 QBoxLayout *box = new QBoxLayout(dir);
78 widget->setLayout(box); 95 widget->setLayout(box);
79 ct->add(widget, true); 96 ctn->add(widget, true);
80 97
81 UiObject *newobj = uic_object_new(obj, widget); 98 ui_container_add(obj, new UiBoxContainer(box));
82 newobj->container = new UiBoxContainer(box);
83 uic_obj_add(obj, newobj);
84 99
85 return widget; 100 return widget;
86 } 101 }
87 102
88 UIWIDGET ui_vbox(UiObject *obj) { 103 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs args) {
89 return ui_box(obj, QBoxLayout::TopToBottom); 104 return ui_box(obj, args, QBoxLayout::TopToBottom);
90 } 105 }
91 106
92 UIWIDGET ui_hbox(UiObject *obj) { 107 UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) {
93 return ui_box(obj, QBoxLayout::LeftToRight); 108 return ui_box(obj, args, QBoxLayout::LeftToRight);
94 } 109 }
95
96 110
97 111
98 /* -------------------- UiGridContainer -------------------- */ 112 /* -------------------- UiGridContainer -------------------- */
99 113
100 UiGridContainer::UiGridContainer(QGridLayout* grid, int margin, int columnspacing, int rowspacing) { 114 UiGridContainer::UiGridContainer(
115 QGridLayout *grid,
116 int margin,
117 int columnspacing,
118 int rowspacing,
119 bool def_hexpand,
120 bool def_vexpand,
121 bool def_hfill,
122 bool def_vfill)
123 {
101 this->current = NULL; 124 this->current = NULL;
102 this->menu = NULL;
103 this->grid = grid; 125 this->grid = grid;
126 this->def_hexpand = def_hexpand;
127 this->def_vexpand = def_vexpand;
128 this->def_hfill = def_hfill;
129 this->def_vfill = def_vfill;
104 grid->setContentsMargins(QMargins(margin, margin, margin, margin)); 130 grid->setContentsMargins(QMargins(margin, margin, margin, margin));
105 grid->setHorizontalSpacing(columnspacing); 131 grid->setHorizontalSpacing(columnspacing);
106 grid->setVerticalSpacing(rowspacing); 132 grid->setVerticalSpacing(rowspacing);
107 ui_reset_layout(layout); 133 ui_reset_layout(layout);
108 } 134 }
111 if(layout.newline) { 137 if(layout.newline) {
112 x = 0; 138 x = 0;
113 y++; 139 y++;
114 } 140 }
115 141
116 Qt::Alignment alignment = Qt::AlignTop; 142 int hexpand = false;
117 grid->setColumnStretch(x, layout.hexpand ? 1 : 0); 143 int vexpand = false;
144 int hfill = false;
145 int vfill = false;
146 if(!layout.override_defaults) {
147 if(def_hexpand) {
148 hexpand = true;
149 hfill = true;
150 } else if(def_hfill) {
151 hfill = true;
152 }
153 if(def_vexpand) {
154 vexpand = true;
155 vfill = true;
156 } else if(def_vfill) {
157 vfill = true;
158 }
159 }
160
161 if(layout.fill != UI_LAYOUT_UNDEFINED) {
162 fill = ui_lb2bool(layout.fill);
163 }
164 if(layout.hexpand) {
165 hexpand = true;
166 //hfill = true;
167 } else if(layout.hfill) {
168 hfill = true;
169 }
118 if(layout.vexpand) { 170 if(layout.vexpand) {
171 vexpand = true;
172 //vfill = true;
173 } else if(layout.vfill) {
174 vfill = true;
175 }
176 if(fill) {
177 hfill = true;
178 vfill = true;
179 }
180
181 if(hexpand) {
182 col_expanding = true;
183 }
184 if(vexpand) {
185 row_expanding = true;
186 }
187
188 if(hexpand) {
189 grid->setColumnStretch(x, 1);
190 }
191 if(vexpand) {
119 grid->setRowStretch(y, 1); 192 grid->setRowStretch(y, 1);
120 alignment = 0; 193 }
121 } else { 194
122 grid->setRowStretch(y, 0); 195 Qt::Alignment alignment = 0;
123 } 196 if(!hfill) {
124 197 alignment = Qt::AlignLeft;
125 int gwidth = layout.gridwidth > 0 ? layout.gridwidth : 1; 198 }
126 199 if(!vfill) {
127 grid->addWidget(widget, y, x, 1, gwidth, alignment); 200 alignment = Qt::AlignTop;
128 x += gwidth; 201 }
202
203 int colspan = layout.colspan > 0 ? layout.colspan : 1;
204 int rowspan = layout.rowspan > 0 ? layout.rowspan : 1;
205
206 grid->addWidget(widget, y, x, rowspan, colspan, alignment);
207
208 if(x > max_x) {
209 max_x = x;
210 }
211 if(y > max_y) {
212 max_y = y;
213 }
214
215 x += colspan;
129 216
130 ui_reset_layout(layout); 217 ui_reset_layout(layout);
131 current = widget; 218 current = widget;
132 } 219 }
133 220
134 UIWIDGET ui_grid(UiObject *obj) { 221 void UiGridContainer::end() {
135 return ui_grid_sp(obj, 0, 0, 0); 222 if(!col_expanding) {
136 } 223 QSpacerItem *filler = new QSpacerItem(0, 0);
137 224 x = max_x + 1;
138 UIWIDGET ui_grid_sp(UiObject *obj, int margin, int columnspacing, int rowspacing) { 225 grid->setColumnStretch(x, 1);
139 UiContainer *ct = uic_get_current_container(obj); 226 grid->addItem(filler, 0, x, 1, 1, 0);
227 }
228 if(!row_expanding) {
229 QSpacerItem *filler = new QSpacerItem(0, 0);
230 y++;
231 grid->setRowStretch(y, 1);
232 grid->addItem(filler, y, 0, 1, 1, 0);
233 }
234 }
235
236 UIEXPORT UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) {
237 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
238 UI_APPLY_LAYOUT(ctn->layout, args);
239
140 QWidget *widget = new QWidget(); 240 QWidget *widget = new QWidget();
141 QGridLayout *grid = new QGridLayout(); 241 QGridLayout *grid = new QGridLayout();
142 widget->setLayout(grid); 242 widget->setLayout(grid);
143 ct->add(widget, true); 243 ctn->add(widget, true);
144 244
145 UiObject *newobj = uic_object_new(obj, widget); 245 ui_container_add(obj, new UiGridContainer(
146 newobj->container = new UiGridContainer(grid, margin, columnspacing, rowspacing); 246 grid,
147 uic_obj_add(obj, newobj); 247 args.margin,
248 args.columnspacing,
249 args.rowspacing,
250 args.def_hexpand,
251 args.def_vexpand,
252 args.def_hfill,
253 args.def_vfill));
148 254
149 return widget; 255 return widget;
150 } 256 }
151 257
152 258
153 /* -------------------- UiTabViewContainer -------------------- */ 259
154 260 /* -------------------- Container Helper Functions -------------------- */
155 UiTabViewContainer::UiTabViewContainer(QTabWidget* tabwidget) { 261
156 this->current = NULL; 262 void ui_container_begin_close(UiObject *obj) {
157 this->menu = NULL; 263 obj->container_end->close = true;
158 this->tabwidget = tabwidget; 264 }
159 } 265
160 266 int ui_container_finish(UiObject *obj) {
161 void UiTabViewContainer::add(QWidget* widget, bool fill) { 267 if(obj->container_end->close) {
162 QString str = QString::fromUtf8(layout.label); 268 UiContainerPrivate *ctn = (UiContainerPrivate*)obj->container_end->container;
163 tabwidget->addTab(widget, str); 269 ctn->end();
164 } 270 ui_end_new(obj);
165 271 return 0;
166 272 }
167 /* -------------------- UiStackContainer -------------------- */ 273 return 1;
168 274 }
169 UiStackContainer::UiStackContainer(QStackedWidget *stack) { 275
170 this->stack = stack; 276
171 } 277 /*
172 278 * -------------------- Layout Functions --------------------
173 void UiStackContainer::add(QWidget* widget, bool fill) { 279 *
174 stack->addWidget(widget); 280 * functions for setting layout attributes for the current container
175 current = widget; 281 *
176 } 282 */
177
178 UIWIDGET ui_tabview(UiObject *obj) {
179 QStackedWidget *tabwidget = new QStackedWidget();
180
181 UiContainer *ct = uic_get_current_container(obj);
182 ct->add(tabwidget, true);
183
184 UiObject *tabviewobj = uic_object_new(obj, tabwidget);
185 tabviewobj->container = new UiStackContainer(tabwidget);
186 uic_obj_add(obj, tabviewobj);
187
188 return tabwidget;
189 }
190
191 void ui_tab(UiObject *obj, char *title) {
192 UiContainer *ct = uic_get_current_container(obj);
193 ct->layout.label = title;
194 ui_vbox(obj);
195 }
196
197 void ui_select_tab(UIWIDGET tabview, int tab) {
198 QStackedWidget *w = (QStackedWidget*)tabview;
199 w->setCurrentIndex(tab);
200 }
201
202
203 /* -------------------- UiSidebarContainer -------------------- */
204
205 UiSidebarContainer::UiSidebarContainer(QSplitter *splitter) {
206 this->splitter = splitter;
207 }
208
209 UIWIDGET ui_sidebar(UiObject *obj) {
210 QSplitter *splitter = new QSplitter(Qt::Horizontal);
211 UiContainer *ct = uic_get_current_container(obj);
212 ct->add(splitter, true);
213
214 UiObject *left = uic_object_new(obj, splitter);
215 left->container = new UiSidebarContainer(splitter);
216
217 UiObject *right = uic_object_new(obj, splitter);
218 right->container = new UiSidebarContainer(splitter);
219
220 uic_obj_add(obj, right);
221 uic_obj_add(obj, left);
222
223 return splitter;
224 }
225
226 void UiSidebarContainer::add(QWidget *widget, bool fill) {
227 splitter->addWidget(widget);
228 }
229
230
231 /* -------------------- layout functions -------------------- */
232
233 void ui_layout_fill(UiObject *obj, UiBool fill) {
234 UiContainer *ct = uic_get_current_container(obj);
235 ct->layout.fill = ui_bool2lb(fill);
236 }
237
238 void ui_layout_hexpand(UiObject *obj, UiBool expand) {
239 UiContainer *ct = uic_get_current_container(obj);
240 ct->layout.hexpand = expand;
241 }
242
243 void ui_layout_vexpand(UiObject *obj, UiBool expand) {
244 UiContainer *ct = uic_get_current_container(obj);
245 ct->layout.vexpand = expand;
246 }
247
248 void ui_layout_gridwidth(UiObject *obj, int width) {
249 UiContainer *ct = uic_get_current_container(obj);
250 ct->layout.gridwidth = width;
251 }
252 283
253 void ui_newline(UiObject *obj) { 284 void ui_newline(UiObject *obj) {
254 UiContainer *ct = uic_get_current_container(obj); 285 UiContainerPrivate *ct = ui_obj_container(obj);
255 ct->layout.newline = TRUE; 286 ct->layout.newline = TRUE;
256 } 287 }

mercurial