ui/qt/container.cpp

changeset 525
878df45e6441
parent 522
bb0d05f45046
child 526
f6a6b0b08641
--- 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;
     }

mercurial