Thu, 09 Oct 2025 12:49:53 +0200
implement widget margin (Qt)
| application/main.c | file | annotate | diff | comparison | revisions | |
| ui/common/container.c | file | annotate | diff | comparison | revisions | |
| ui/common/container.h | 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/window.cpp | file | annotate | diff | comparison | revisions |
--- a/application/main.c Thu Oct 09 11:44:10 2025 +0200 +++ b/application/main.c Thu Oct 09 12:49:53 2025 +0200 @@ -1076,6 +1076,17 @@ ui_radiobutton(obj, .label = "Radio 3", .varname = "radio"); ui_radiobutton(obj, .label = "Radio 4", .varname = "radio"); } + ui_newline(obj); + + ui_hbox(obj, .colspan = 3) { + ui_button(obj, .label = "Margin Test 1", .margin_top = 10); + ui_button(obj, .label = "Margin Test 2", .margin_top = 20); + UIWIDGET w = ui_button(obj, .label = "Margin Test 3", .margin = 30); + //ui_set_visible(w, FALSE); + ui_button(obj, .label = "Margin Test 4", .margin_top = 40); + ui_button(obj, .label = "Margin Test 5", .margin_top = 50); + ui_button(obj, .label = "Margin Test 6", .margin_top = 60); + } } ui_show(obj); }
--- a/ui/common/container.c Thu Oct 09 11:44:10 2025 +0200 +++ b/ui/common/container.c Thu Oct 09 12:49:53 2025 +0200 @@ -75,3 +75,13 @@ } } } + +void uic_layout_setup_margin(UiLayout *layout) { + int margin = layout->margin; + if(margin > 0) { + layout->margin_left = margin; + layout->margin_right = margin; + layout->margin_top = margin; + layout->margin_bottom = margin; + } +}
--- a/ui/common/container.h Thu Oct 09 11:44:10 2025 +0200 +++ b/ui/common/container.h Thu Oct 09 12:49:53 2025 +0200 @@ -46,6 +46,11 @@ UiBool def_hfill, UiBool def_vfill); +/* + * adjusts margin_* if margin > 0 + */ +void uic_layout_setup_margin(UiLayout *layout); + #ifdef __cplusplus } #endif
--- a/ui/qt/container.cpp Thu Oct 09 11:44:10 2025 +0200 +++ b/ui/qt/container.cpp Thu Oct 09 12:49:53 2025 +0200 @@ -42,7 +42,7 @@ delete ct; } -void ui_container_add(UiObject *obj, UiContainerPrivate *ct) { +void ui_obj_add_container(UiObject *obj, UiContainerPrivate *ct) { UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX)); container->close = 0; container->container = ct; @@ -53,10 +53,27 @@ uic_object_push_container(obj, container); } +/* ------------------------ margin helper ------------------------ */ + +QWidget* ui_widget_set_margin(QWidget *w, int left, int right, int top, int bottom) { + if((left | right | top | bottom) == 0) { + return w; + } + QWidget* wrapper = new QWidget; + QVBoxLayout* inner = new QVBoxLayout(wrapper); + inner->setContentsMargins(left, top, right, bottom); + inner->addWidget(w); + + // TODO: handle widget visibility changes + + return wrapper; +} + /* -------------------- UiBoxContainer -------------------- */ UiBoxContainer::UiBoxContainer(QBoxLayout* box) { this->box = box; + this->direction = box->direction(); box->setContentsMargins(QMargins(0,0,0,0)); box->setSpacing(0); } @@ -68,7 +85,12 @@ fprintf(stderr, "UiError: container has 2 filled widgets"); } + uic_layout_setup_margin(&layout); + widget = ui_widget_set_margin(widget, layout.margin_left, layout.margin_right, layout.margin_top, layout.margin_bottom); box->addWidget(widget); + if(direction == Qt::LeftToRight) { + box->setAlignment(widget, Qt::AlignTop); + } if(!hasStretchedWidget) { QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); @@ -92,7 +114,7 @@ widget->setLayout(box); ctn->add(widget, layout); - ui_container_add(obj, new UiBoxContainer(box)); + ui_obj_add_container(obj, new UiBoxContainer(box)); return widget; } @@ -137,6 +159,7 @@ } uic_layout_setup_expand_fill(&layout, def_hexpand, def_vexpand, def_hfill, def_vfill); + uic_layout_setup_margin(&layout); if(layout.hexpand) { col_expanding = true; @@ -163,6 +186,7 @@ int colspan = layout.colspan > 0 ? layout.colspan : 1; int rowspan = layout.rowspan > 0 ? layout.rowspan : 1; + widget = ui_widget_set_margin(widget, layout.margin_left, layout.margin_right, layout.margin_top, layout.margin_bottom); grid->addWidget(widget, y, x, rowspan, colspan, alignment); if(x > max_x) { @@ -201,7 +225,7 @@ widget->setLayout(grid); ctn->add(widget, layout); - ui_container_add(obj, new UiGridContainer( + ui_obj_add_container(obj, new UiGridContainer( grid, args->margin, args->columnspacing, @@ -230,7 +254,7 @@ widget->setLayout(box); dock->setWidget(widget); - ui_container_add(obj, new UiBoxContainer(box)); + ui_obj_add_container(obj, new UiBoxContainer(box)); return dock; }
--- a/ui/qt/container.h Thu Oct 09 11:44:10 2025 +0200 +++ b/ui/qt/container.h Thu Oct 09 12:49:53 2025 +0200 @@ -52,9 +52,10 @@ class UiBoxContainer : public UiContainerPrivate { public: - QBoxLayout *box; - bool hasStretchedWidget = false; - QSpacerItem *space; + QBoxLayout *box; + bool hasStretchedWidget = false; + QSpacerItem *space; + QBoxLayout::Direction direction; UiBoxContainer(QBoxLayout *box); @@ -89,7 +90,9 @@ virtual void end(); }; -void ui_container_add(UiObject *obj, UiContainerPrivate *ct); +void ui_obj_add_container(UiObject *obj, UiContainerPrivate *ct); + +QWidget* ui_widget_set_margin(QWidget *w, int left, int right, int top, int bottom); #endif /* CONTAINER_H */
--- a/ui/qt/window.cpp Thu Oct 09 11:44:10 2025 +0200 +++ b/ui/qt/window.cpp Thu Oct 09 12:49:53 2025 +0200 @@ -62,7 +62,7 @@ QWidget *boxWidget = new QWidget(); boxWidget->setLayout(box); window->setCentralWidget(boxWidget); - ui_container_add(obj, new UiBoxContainer(box)); + ui_obj_add_container(obj, new UiBoxContainer(box)); if(sidebar) { QDockWidget *dock = new QDockWidget(); window->addDockWidget(Qt::LeftDockWidgetArea, dock);