|
20 | 20 | #include <QtGui/QScreen> |
21 | 21 | #include <QtWidgets/QComboBox> |
22 | 22 | #include <QtWidgets/QDialog> |
| 23 | +#include <QtWidgets/QGridLayout> |
23 | 24 | #include <QtWidgets/QHeaderView> |
24 | 25 | #include <QtWidgets/QInputDialog> |
25 | 26 | #include <QtWidgets/QLabel> |
@@ -137,6 +138,104 @@ void QtUtils::SetColumnWidthsForTreeView(QTreeView* view, const std::initializer |
137 | 138 | SetColumnWidthForView(view, view->header(), widths); |
138 | 139 | } |
139 | 140 |
|
| 141 | +void QtUtils::RemoveEmptyRowsAndColumns(QGridLayout* const layout) |
| 142 | +{ |
| 143 | + int column = 0; |
| 144 | + while (column < layout->columnCount()) |
| 145 | + { |
| 146 | + const int row_count = layout->rowCount(); |
| 147 | + const int column_count = layout->columnCount(); |
| 148 | + |
| 149 | + // Lambda to check if a column has any items with origin in that column |
| 150 | + static constexpr auto column_has_items = [](QGridLayout* layout, int rowCount, int c) { |
| 151 | + for (int row = 0; row < rowCount; ++row) |
| 152 | + { |
| 153 | + QLayoutItem* item = layout->itemAtPosition(row, c); |
| 154 | + if (item) |
| 155 | + { |
| 156 | + const int idx = layout->indexOf(item); |
| 157 | + int itemRow, itemCol, rowSpan, colSpan; |
| 158 | + layout->getItemPosition(idx, &itemRow, &itemCol, &rowSpan, &colSpan); |
| 159 | + if (itemCol == c) |
| 160 | + return true; |
| 161 | + } |
| 162 | + } |
| 163 | + return false; |
| 164 | + }; |
| 165 | + |
| 166 | + if (!column_has_items(layout, row_count, column)) |
| 167 | + { |
| 168 | + // Count consecutive empty columns starting from col |
| 169 | + int empty_count = 1; |
| 170 | + while ((column + empty_count) < column_count && !column_has_items(layout, row_count, column + empty_count)) |
| 171 | + ++empty_count; |
| 172 | + |
| 173 | + // Shift all items and stretches from columns after the gap |
| 174 | + for (int c = column + empty_count; c < column_count; ++c) |
| 175 | + { |
| 176 | + const int dest_column = c - empty_count; |
| 177 | + |
| 178 | + // Preserve column stretch |
| 179 | + layout->setColumnStretch(dest_column, layout->columnStretch(c)); |
| 180 | + |
| 181 | + // Shift items |
| 182 | + for (int row = 0; row < row_count; ++row) |
| 183 | + { |
| 184 | + QLayoutItem* const item = layout->itemAtPosition(row, c); |
| 185 | + if (!item) |
| 186 | + continue; |
| 187 | + |
| 188 | + const int idx = layout->indexOf(item); |
| 189 | + int item_row, item_column, row_span, column_span; |
| 190 | + layout->getItemPosition(idx, &item_row, &item_column, &row_span, &column_span); |
| 191 | + |
| 192 | + // Only process at item's origin |
| 193 | + if (item_row != row || item_column != c) |
| 194 | + continue; |
| 195 | + |
| 196 | + layout->takeAt(idx); |
| 197 | + layout->addItem(item, item_row, dest_column, row_span, column_span); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Clear stretch on the now-empty trailing columns |
| 202 | + for (int c = column_count - empty_count; c < column_count; ++c) |
| 203 | + layout->setColumnStretch(c, 0); |
| 204 | + |
| 205 | + // Don't increment col - check this position again (new content shifted in) |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + // Column has items - compact rows upward within this column |
| 210 | + int target_row = 0; |
| 211 | + for (int row = 0; row < row_count; ++row) |
| 212 | + { |
| 213 | + QLayoutItem* const item = layout->itemAtPosition(row, column); |
| 214 | + if (!item) |
| 215 | + continue; |
| 216 | + |
| 217 | + const int idx = layout->indexOf(item); |
| 218 | + int item_row, item_column, row_span, column_span; |
| 219 | + layout->getItemPosition(idx, &item_row, &item_column, &row_span, &column_span); |
| 220 | + |
| 221 | + // Only process items whose origin is at (row, col) |
| 222 | + if (item_row != row || item_column != column) |
| 223 | + continue; |
| 224 | + |
| 225 | + if (row != target_row) |
| 226 | + { |
| 227 | + layout->takeAt(idx); |
| 228 | + layout->addItem(item, target_row, column, row_span, column_span); |
| 229 | + } |
| 230 | + |
| 231 | + target_row += row_span; |
| 232 | + } |
| 233 | + |
| 234 | + ++column; |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
140 | 239 | void QtUtils::OpenURL(QWidget* parent, const QUrl& qurl) |
141 | 240 | { |
142 | 241 | if (!QDesktopServices::openUrl(qurl)) |
|
0 commit comments