implement all grid layout features (QT)

4 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 27 Mar 2025 21:28:04 +0100 (4 days ago)
changeset 525
878df45e6441
parent 524
ce7a198b54b0
child 526
f6a6b0b08641

implement all grid layout features (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/qt/container.cpp file | annotate | diff | comparison | revisions
ui/qt/container.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Mar 26 22:20:27 2025 +0100
+++ b/application/main.c	Thu Mar 27 21:28:04 2025 +0100
@@ -777,10 +777,14 @@
     UiObject *obj = ui_window("My Window", NULL);
     ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) {
         ui_button(obj, .label = "Button 1");
-        ui_button(obj, .label = "Button 2");
+        ui_button(obj, .label = "Button 2", .hexpand = TRUE, .hfill = TRUE);
+        ui_button(obj, .label = "Button end");
         ui_newline(obj);
         
-        ui_button(obj, .label = "Button 3", .colspan = 2);
+        ui_button(obj, .label = "Button 3", .vexpand = TRUE, .vfill = TRUE);
+        ui_newline(obj);
+        
+        ui_button(obj, .label = "Button Y");
     }
     ui_show(obj);
 }
--- a/ui/qt/container.cpp	Wed Mar 26 22:20:27 2025 +0100
+++ b/ui/qt/container.cpp	Thu Mar 27 21:28:04 2025 +0100
@@ -34,6 +34,7 @@
 
 #include <QSpacerItem>
 #include <QStackedWidget>
+#include <QLabel>
 
 static void delete_container(UiContainerPrivate *ct) {
     delete ct;
@@ -110,9 +111,22 @@
 
 /* -------------------- UiGridContainer -------------------- */
 
-UiGridContainer::UiGridContainer(QGridLayout* grid, int margin, int columnspacing, int rowspacing) {
+UiGridContainer::UiGridContainer(
+        QGridLayout *grid,
+        int margin,
+        int columnspacing,
+        int rowspacing,
+        bool def_hexpand,
+        bool def_vexpand,
+        bool def_hfill,
+        bool def_vfill)
+{
     this->current = NULL;
     this->grid = grid;
+    this->def_hexpand = def_hexpand;
+    this->def_vexpand = def_vexpand;
+    this->def_hfill = def_hfill;
+    this->def_vfill = def_vfill;
     grid->setContentsMargins(QMargins(margin, margin, margin, margin));
     grid->setHorizontalSpacing(columnspacing);
     grid->setVerticalSpacing(rowspacing);
@@ -125,25 +139,100 @@
         y++;
     }
     
-    Qt::Alignment alignment = Qt::AlignTop;
-    grid->setColumnStretch(x, layout.hexpand ? 1 : 0);
+    int hexpand = false;
+    int vexpand = false;
+    int hfill = false;
+    int vfill = false;
+    if(!layout.override_defaults) {
+        if(def_hexpand) {
+            hexpand = true;
+            hfill = true;
+        } else if(def_hfill) {
+            hfill = true;
+        }
+        if(def_vexpand) {
+            vexpand = true;
+            vfill = true;
+        } else if(def_vfill) {
+            vfill = true;
+        }
+    }
+    
+    if(layout.fill != UI_LAYOUT_UNDEFINED) {
+        fill = ui_lb2bool(layout.fill);
+    }
+    if(layout.hexpand) {
+        hexpand = true;
+        //hfill = true;
+    } else if(layout.hfill) {
+        hfill = true;
+    }
     if(layout.vexpand) {
+        vexpand = true;
+        //vfill = true;
+    } else if(layout.vfill) {
+        vfill = true;
+    }
+    if(fill) {
+        hfill = true;
+        vfill = true;
+    }
+    
+    if(hexpand) {
+        col_expanding = true;
+    }
+    if(vexpand) {
+        row_expanding = true;
+    }
+    
+    if(hexpand) {
+        grid->setColumnStretch(x, 1);
+    }
+    if(vexpand) {
         grid->setRowStretch(y, 1);
-        alignment = 0;
-    } else {
-        grid->setRowStretch(y, 0);
+    }
+    
+    Qt::Alignment alignment = 0;
+    if(!hfill) {
+        alignment = Qt::AlignLeft;
+    }
+    if(!vfill) {
+        alignment = Qt::AlignTop;
     }
     
     int colspan = layout.colspan > 0 ? layout.colspan : 1;
     int rowspan = layout.rowspan > 0 ? layout.rowspan : 1;
     
     grid->addWidget(widget, y, x, rowspan, colspan, alignment);
+    
+    if(x > max_x) {
+        max_x = x;
+    }
+    if(y > max_y) {
+        max_y = y;
+    }
+    
     x += colspan;
     
     ui_reset_layout(layout);
     current = widget;
 }
 
+void UiGridContainer::end() {
+    if(!col_expanding) {
+        QLabel *filler = new QLabel("");
+        x = max_x + 1;
+        grid->setColumnStretch(x, 1);
+        grid->addWidget(filler, 0, x, 1, 1, 0);
+    }
+    if(!row_expanding) {
+        QLabel *filler = new QLabel("");
+        y++;
+        grid->setRowStretch(y, 1);
+        grid->addWidget(filler, y, 0, 1, 1, 0);
+    }
+}
+
 UIEXPORT UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) {
     UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
     UI_APPLY_LAYOUT(ctn->layout, args);
@@ -153,7 +242,15 @@
     widget->setLayout(grid);
     ctn->add(widget, true);
     
-    ui_container_add(obj, new UiGridContainer(grid, args.margin, args.columnspacing, args.rowspacing));
+    ui_container_add(obj, new UiGridContainer(
+            grid,
+            args.margin,
+            args.columnspacing,
+            args.rowspacing,
+            args.def_hexpand,
+            args.def_vexpand,
+            args.def_hfill,
+            args.def_vfill));
     
     return widget;
 }
@@ -168,6 +265,8 @@
 
 int ui_container_finish(UiObject *obj) {
     if(obj->container_end->close) {
+        UiContainerPrivate *ctn = (UiContainerPrivate*)obj->container_end->container;
+        ctn->end();
         ui_end_new(obj);
         return 0;
     }
--- a/ui/qt/container.h	Wed Mar 26 22:20:27 2025 +0100
+++ b/ui/qt/container.h	Thu Mar 27 21:28:04 2025 +0100
@@ -82,6 +82,7 @@
     UIWIDGET current;
 
     virtual void add(QWidget *widget, bool fill) = 0;
+    virtual void end() {}
 };
 
 class UiBoxContainer : public UiContainerPrivate {
@@ -100,10 +101,27 @@
     QGridLayout *grid;
     int x = 0;
     int y = 0;
+    bool def_hexpand;
+    bool def_vexpand;
+    bool def_hfill;
+    bool def_vfill;
+    bool col_expanding = false;
+    bool row_expanding = false;
+    int max_x;
+    int max_y;
     
-    UiGridContainer(QGridLayout *grid, int margin, int columnspacing, int rowspacing);
+    UiGridContainer(
+            QGridLayout *grid,
+            int margin,
+            int columnspacing,
+            int rowspacing,
+            bool def_hexpand,
+            bool def_vexpand,
+            bool def_hfill,
+            bool def_vfill);
     
     virtual void add(QWidget *widget, bool fill);
+    virtual void end();
 };
 
 void ui_container_add(UiObject *obj, UiContainerPrivate *ct);

mercurial