ui/qt/container.cpp

changeset 112
c3f2f16fa4b8
parent 109
c3dfcb8f0be7
equal deleted inserted replaced
111:81c4f73236a4 112:c3f2f16fa4b8
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" 31 #include "../common/object.h"
32 #include "../common/container.h"
32 33
33 #include <cx/mempool.h> 34 #include <cx/mempool.h>
34 35
35 #include <QSpacerItem> 36 #include <QSpacerItem>
36 #include <QStackedWidget> 37 #include <QStackedWidget>
39 40
40 static void delete_container(UiContainerPrivate *ct) { 41 static void delete_container(UiContainerPrivate *ct) {
41 delete ct; 42 delete ct;
42 } 43 }
43 44
44 void ui_container_add(UiObject *obj, UiContainerPrivate *ct) { 45 void ui_obj_add_container(UiObject *obj, UiContainerPrivate *ct) {
45 UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX)); 46 UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX));
46 container->close = 0; 47 container->close = 0;
47 container->container = ct; 48 container->container = ct;
48 container->prev = NULL; 49 container->prev = NULL;
49 container->next = NULL; 50 container->next = NULL;
51 ct->container = container;
50 cxMempoolRegister(obj->ctx->mp, ct, (cx_destructor_func)delete_container); 52 cxMempoolRegister(obj->ctx->mp, ct, (cx_destructor_func)delete_container);
51 uic_object_push_container(obj, container); 53 uic_object_push_container(obj, container);
52 } 54 }
53 55
56 /* ------------------------ margin helper ------------------------ */
57
58 QWidget* ui_widget_set_margin(QWidget *w, int left, int right, int top, int bottom) {
59 if((left | right | top | bottom) == 0) {
60 return w;
61 }
62 QWidget* wrapper = new QWidget;
63 QVBoxLayout* inner = new QVBoxLayout(wrapper);
64 inner->setContentsMargins(left, top, right, bottom);
65 inner->addWidget(w);
66
67 // TODO: handle widget visibility changes
68
69 return wrapper;
70 }
71
54 /* -------------------- UiBoxContainer -------------------- */ 72 /* -------------------- UiBoxContainer -------------------- */
55 73
56 UiBoxContainer::UiBoxContainer(QBoxLayout* box) { 74 UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
57 this->box = box; 75 this->box = box;
76 this->direction = box->direction();
58 box->setContentsMargins(QMargins(0,0,0,0)); 77 box->setContentsMargins(QMargins(0,0,0,0));
59 box->setSpacing(0); 78 box->setSpacing(0);
60 79 }
61 ui_reset_layout(layout); 80
62 } 81 void UiBoxContainer::add(QWidget* widget, UiLayout& layout) {
63
64 void UiBoxContainer::add(QWidget* widget) {
65 bool fill = layout.fill; 82 bool fill = layout.fill;
66 if(hasStretchedWidget && fill) { 83 if(hasStretchedWidget && fill) {
67 fill = false; 84 fill = false;
68 fprintf(stderr, "UiError: container has 2 filled widgets"); 85 fprintf(stderr, "UiError: container has 2 filled widgets");
69 } 86 }
70 87
88 uic_layout_setup_margin(&layout);
89 widget = ui_widget_set_margin(widget, layout.margin_left, layout.margin_right, layout.margin_top, layout.margin_bottom);
71 box->addWidget(widget); 90 box->addWidget(widget);
91 if(direction == Qt::LeftToRight) {
92 box->setAlignment(widget, Qt::AlignTop);
93 }
72 94
73 if(!hasStretchedWidget) { 95 if(!hasStretchedWidget) {
74 QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); 96 QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
75 box->removeItem(space); 97 box->removeItem(space);
76 box->addSpacerItem(newspace); 98 box->addSpacerItem(newspace);
78 } 100 }
79 101
80 if(fill) { 102 if(fill) {
81 hasStretchedWidget = true; 103 hasStretchedWidget = true;
82 } 104 }
83 ui_reset_layout(layout);
84 current = widget; 105 current = widget;
85 } 106 }
86 107
87 UIWIDGET ui_box(UiObject *obj, UiContainerArgs *args, QBoxLayout::Direction dir) { 108 UIWIDGET ui_box(UiObject *obj, UiContainerArgs *args, QBoxLayout::Direction dir) {
88 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj); 109 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
89 UI_APPLY_LAYOUT(ctn->layout, args); 110 UiLayout layout = UI_ARGS2LAYOUT(args);
90 111
91 QWidget *widget = new QWidget(); 112 QWidget *widget = new QWidget();
92 QBoxLayout *box = new QBoxLayout(dir); 113 QBoxLayout *box = new QBoxLayout(dir);
93 widget->setLayout(box); 114 widget->setLayout(box);
94 ctn->add(widget); 115 ctn->add(widget, layout);
95 116
96 ui_container_add(obj, new UiBoxContainer(box)); 117 ui_obj_add_container(obj, new UiBoxContainer(box));
97 118
98 return widget; 119 return widget;
99 } 120 }
100 121
101 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs *args) { 122 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs *args) {
126 this->def_hfill = def_hfill; 147 this->def_hfill = def_hfill;
127 this->def_vfill = def_vfill; 148 this->def_vfill = def_vfill;
128 grid->setContentsMargins(QMargins(margin, margin, margin, margin)); 149 grid->setContentsMargins(QMargins(margin, margin, margin, margin));
129 grid->setHorizontalSpacing(columnspacing); 150 grid->setHorizontalSpacing(columnspacing);
130 grid->setVerticalSpacing(rowspacing); 151 grid->setVerticalSpacing(rowspacing);
131 ui_reset_layout(layout); 152 }
132 } 153
133 154 void UiGridContainer::add(QWidget* widget, UiLayout& layout) {
134 void UiGridContainer::add(QWidget* widget) { 155 if(container->newline) {
135 if(layout.newline) {
136 x = 0; 156 x = 0;
137 y++; 157 y++;
138 } 158 container->newline = false;
139 159 }
140 bool fill = layout.fill; 160
141 bool hexpand = false; 161 uic_layout_setup_expand_fill(&layout, def_hexpand, def_vexpand, def_hfill, def_vfill);
142 bool vexpand = false; 162 uic_layout_setup_margin(&layout);
143 bool hfill = false;
144 bool vfill = false;
145 if(!layout.override_defaults) {
146 if(def_hexpand) {
147 hexpand = true;
148 hfill = true;
149 } else if(def_hfill) {
150 hfill = true;
151 }
152 if(def_vexpand) {
153 vexpand = true;
154 vfill = true;
155 } else if(def_vfill) {
156 vfill = true;
157 }
158 }
159 163
160 if(layout.hexpand) { 164 if(layout.hexpand) {
161 hexpand = true; 165 col_expanding = true;
162 //hfill = true;
163 } else if(layout.hfill) {
164 hfill = true;
165 } 166 }
166 if(layout.vexpand) { 167 if(layout.vexpand) {
167 vexpand = true;
168 //vfill = true;
169 } else if(layout.vfill) {
170 vfill = true;
171 }
172 if(fill) {
173 hfill = true;
174 vfill = true;
175 }
176
177 if(hexpand) {
178 col_expanding = true;
179 }
180 if(vexpand) {
181 row_expanding = true; 168 row_expanding = true;
182 } 169 }
183 170
184 if(hexpand) { 171 if(layout.hexpand) {
185 grid->setColumnStretch(x, 1); 172 grid->setColumnStretch(x, 1);
186 } 173 }
187 if(vexpand) { 174 if(layout.vexpand) {
188 grid->setRowStretch(y, 1); 175 grid->setRowStretch(y, 1);
189 } 176 }
190 177
191 Qt::Alignment alignment = 0; 178 Qt::Alignment alignment = 0;
192 if(!hfill) { 179 if(!layout.hfill) {
193 alignment = Qt::AlignLeft; 180 alignment = Qt::AlignLeft;
194 } 181 }
195 if(!vfill) { 182 if(!layout.vfill) {
196 alignment = Qt::AlignTop; 183 alignment = Qt::AlignTop;
197 } 184 }
198 185
199 int colspan = layout.colspan > 0 ? layout.colspan : 1; 186 int colspan = layout.colspan > 0 ? layout.colspan : 1;
200 int rowspan = layout.rowspan > 0 ? layout.rowspan : 1; 187 int rowspan = layout.rowspan > 0 ? layout.rowspan : 1;
201 188
189 widget = ui_widget_set_margin(widget, layout.margin_left, layout.margin_right, layout.margin_top, layout.margin_bottom);
202 grid->addWidget(widget, y, x, rowspan, colspan, alignment); 190 grid->addWidget(widget, y, x, rowspan, colspan, alignment);
203 191
204 if(x > max_x) { 192 if(x > max_x) {
205 max_x = x; 193 max_x = x;
206 } 194 }
208 max_y = y; 196 max_y = y;
209 } 197 }
210 198
211 x += colspan; 199 x += colspan;
212 200
213 ui_reset_layout(layout);
214 current = widget; 201 current = widget;
215 } 202 }
216 203
217 void UiGridContainer::end() { 204 void UiGridContainer::end() {
218 if(!col_expanding) { 205 if(!col_expanding) {
229 } 216 }
230 } 217 }
231 218
232 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) { 219 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) {
233 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj); 220 UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
234 UI_APPLY_LAYOUT(ctn->layout, args); 221 UiLayout layout = UI_ARGS2LAYOUT(args);
235 222
236 QWidget *widget = new QWidget(); 223 QWidget *widget = new QWidget();
237 QGridLayout *grid = new QGridLayout(); 224 QGridLayout *grid = new QGridLayout();
238 widget->setLayout(grid); 225 widget->setLayout(grid);
239 ctn->add(widget); 226 ctn->add(widget, layout);
240 227
241 ui_container_add(obj, new UiGridContainer( 228 ui_obj_add_container(obj, new UiGridContainer(
242 grid, 229 grid,
243 args->margin, 230 args->margin,
244 args->columnspacing, 231 args->columnspacing,
245 args->rowspacing, 232 args->rowspacing,
246 args->def_hexpand, 233 args->def_hexpand,
265 QWidget *widget = new QWidget(); 252 QWidget *widget = new QWidget();
266 QBoxLayout *box = new QBoxLayout(QBoxLayout::TopToBottom); 253 QBoxLayout *box = new QBoxLayout(QBoxLayout::TopToBottom);
267 widget->setLayout(box); 254 widget->setLayout(box);
268 dock->setWidget(widget); 255 dock->setWidget(widget);
269 256
270 ui_container_add(obj, new UiBoxContainer(box)); 257 ui_obj_add_container(obj, new UiBoxContainer(box));
271 258
272 return dock; 259 return dock;
273 } 260 }
274 261
275 /* -------------------- Container Helper Functions -------------------- */ 262 /* -------------------- Container Helper Functions -------------------- */
286 return 0; 273 return 0;
287 } 274 }
288 return 1; 275 return 1;
289 } 276 }
290 277
291
292 /*
293 * -------------------- Layout Functions --------------------
294 *
295 * functions for setting layout attributes for the current container
296 *
297 */
298
299 void ui_newline(UiObject *obj) {
300 UiContainerPrivate *ct = ui_obj_container(obj);
301 ct->layout.newline = TRUE;
302 }

mercurial