add grid container (QT)

5 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 26 Mar 2025 22:09:05 +0100 (5 days ago)
changeset 522
bb0d05f45046
parent 521
6e3e1fa6dfcc
child 523
0797c87a2c02

add grid container (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/container.cpp file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Mar 26 22:00:31 2025 +0100
+++ b/application/main.c	Wed Mar 26 22:09:05 2025 +0100
@@ -775,9 +775,13 @@
 
 void application_startup(UiEvent *event, void *data) {
     UiObject *obj = ui_window("My Window", NULL);
-    ui_button(obj, .label = "Button 1");
-    ui_button(obj, .label = "Button 2");
-    ui_button(obj, .label = "Button 3");
+    ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) {
+        ui_button(obj, .label = "Button 1");
+        ui_button(obj, .label = "Button 2");
+        ui_newline(obj);
+        
+        ui_button(obj, .label = "Button 3", .colspan = 2);
+    }
     ui_show(obj);
 }
 
--- a/ui/qt/container.cpp	Wed Mar 26 22:00:31 2025 +0100
+++ b/ui/qt/container.cpp	Wed Mar 26 22:09:05 2025 +0100
@@ -94,7 +94,7 @@
     widget->setLayout(box);
     ctn->add(widget, true);
     
-    
+    ui_container_add(obj, new UiBoxContainer(box));
     
     return widget;
 }
@@ -106,3 +106,83 @@
 UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) {
     return ui_box(obj, args, QBoxLayout::LeftToRight);
 }
+
+
+/* -------------------- UiGridContainer -------------------- */
+
+UiGridContainer::UiGridContainer(QGridLayout* grid, int margin, int columnspacing, int rowspacing) {
+    this->current = NULL;
+    this->grid = grid;
+    grid->setContentsMargins(QMargins(margin, margin, margin, margin));
+    grid->setHorizontalSpacing(columnspacing);
+    grid->setVerticalSpacing(rowspacing);
+    ui_reset_layout(layout);
+}
+
+void UiGridContainer::add(QWidget* widget, bool fill) {
+    if(layout.newline) {
+        x = 0;
+        y++;
+    }
+    
+    Qt::Alignment alignment = Qt::AlignTop;
+    grid->setColumnStretch(x, layout.hexpand ? 1 : 0);
+    if(layout.vexpand) {
+        grid->setRowStretch(y, 1);
+        alignment = 0;
+    } else {
+        grid->setRowStretch(y, 0);
+    }
+    
+    int colspan = layout.colspan > 0 ? layout.colspan : 1;
+    int rowspan = layout.rowspan > 0 ? layout.rowspan : 1;
+    
+    grid->addWidget(widget, y, x, rowspan, colspan, alignment);
+    x += colspan;
+    
+    ui_reset_layout(layout);
+    current = widget;
+}
+
+UIEXPORT UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) {
+    UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
+    UI_APPLY_LAYOUT(ctn->layout, args);
+    
+    QWidget *widget = new QWidget();
+    QGridLayout *grid = new QGridLayout();
+    widget->setLayout(grid);
+    ctn->add(widget, true);
+    
+    ui_container_add(obj, new UiGridContainer(grid, args.margin, args.columnspacing, args.rowspacing));
+    
+    return widget;
+}
+
+
+
+/* -------------------- Container Helper Functions -------------------- */
+
+void ui_container_begin_close(UiObject *obj) {
+    obj->container_end->close = true;
+}
+
+int ui_container_finish(UiObject *obj) {
+    if(obj->container_end->close) {
+        ui_end_new(obj);
+        return 0;
+    }
+    return 1;
+}
+
+
+/*
+ * -------------------- Layout Functions --------------------
+ * 
+ * functions for setting layout attributes for the current container
+ *
+ */
+
+void ui_newline(UiObject *obj) {
+    UiContainerPrivate *ct = ui_obj_container(obj);
+    ct->layout.newline = TRUE;
+}

mercurial