File: gridlayoutmanager.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (559 lines) | stat: -rw-r--r-- 19,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
    SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "gridlayoutmanager.h"
#include "appletslayout.h"
#include "containmentlayoutmanager_debug.h"

#include <cmath>

GridLayoutManager::GridLayoutManager(AppletsLayout *layout)
    : AbstractLayoutManager(layout)
{
}

GridLayoutManager::~GridLayoutManager()
{
}

QString GridLayoutManager::serializeLayout() const
{
    QString result;

    for (auto *item : layout()->childItems()) {
        ItemContainer *itemCont = qobject_cast<ItemContainer *>(item);
        if (itemCont && itemCont != layout()->placeHolder()) {
            result += itemCont->key() + QLatin1Char(':') + QString::number(itemCont->x()) + QLatin1Char(',') + QString::number(itemCont->y()) + QLatin1Char(',')
                + QString::number(itemCont->width()) + QLatin1Char(',') + QString::number(itemCont->height()) + QLatin1Char(',')
                + QString::number(itemCont->rotation()) + QLatin1Char(';');
        }
    }

    return result;
}

void GridLayoutManager::parseLayout(const QString &savedLayout)
{
    m_parsedConfig.clear();
    const QStringList itemsConfigs = savedLayout.split(QLatin1Char(';'));

    for (const auto &itemString : itemsConfigs) {
        QStringList itemConfig = itemString.split(QLatin1Char(':'));
        if (itemConfig.count() != 2) {
            continue;
        }

        QString id = itemConfig[0];
        QStringList itemGeom = itemConfig[1].split(QLatin1Char(','));
        if (itemGeom.count() != 5) {
            continue;
        }

        m_parsedConfig[id] = {itemGeom[0].toDouble(), itemGeom[1].toDouble(), itemGeom[2].toDouble(), itemGeom[3].toDouble(), itemGeom[4].toDouble()};
    }
}

bool GridLayoutManager::itemIsManaged(ItemContainer *item)
{
    return m_pointsForItem.contains(item);
}

inline void maintainItemEdgeAlignment(ItemContainer *item, const QRectF &newRect, const QRectF &oldRect)
{
    const qreal leftDist = item->x() - oldRect.x();
    const qreal hCenterDist = item->x() + item->width() / 2 - oldRect.center().x();
    const qreal rightDist = oldRect.right() - item->x() - item->width();

    qreal hMin = qMin(qMin(qAbs(leftDist), qAbs(hCenterDist)), qAbs(rightDist));
    if (qFuzzyCompare(hMin, qAbs(leftDist))) {
        // Right alignment, do nothing
    } else if (qFuzzyCompare(hMin, qAbs(hCenterDist))) {
        item->setX(newRect.center().x() - item->width() / 2 + hCenterDist);
    } else if (qFuzzyCompare(hMin, qAbs(rightDist))) {
        item->setX(newRect.right() - item->width() - rightDist);
    }

    const qreal topDist = item->y() - oldRect.y();
    const qreal vCenterDist = item->y() + item->height() / 2 - oldRect.center().y();
    const qreal bottomDist = oldRect.bottom() - item->y() - item->height();

    qreal vMin = qMin(qMin(qAbs(topDist), qAbs(vCenterDist)), qAbs(bottomDist));

    if (qFuzzyCompare(vMin, qAbs(topDist))) {
        // Top alignment, do nothing
    } else if (qFuzzyCompare(vMin, qAbs(vCenterDist))) {
        item->setY(newRect.center().y() - item->height() / 2 + vCenterDist);
    } else if (qFuzzyCompare(vMin, qAbs(bottomDist))) {
        item->setY(newRect.bottom() - item->height() - bottomDist);
    }
}

void GridLayoutManager::layoutGeometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
    Q_UNUSED(newGeometry);
    Q_UNUSED(oldGeometry);

    m_grid.clear();
    m_pointsForItem.clear();
    for (auto *item : layout()->childItems()) {
        // Stash the old config
        // m_parsedConfig[item->key()] = {item->x(), item->y(), item->width(), item->height(), item->rotation()};
        // Move the item to maintain the distance with the anchors point
        auto *itemCont = qobject_cast<ItemContainer *>(item);
        if (itemCont && itemCont != layout()->placeHolder()) {
            // NOTE: do not use positionItemAndAssign here, because we do not want to Q_EMIT layoutNeedsSaving, to not save after resize
            positionItem(itemCont);
            assignSpaceImpl(itemCont);
        }
    }
}

void GridLayoutManager::resetLayout()
{
    m_grid.clear();
    m_pointsForItem.clear();
    for (auto *item : layout()->childItems()) {
        ItemContainer *itemCont = qobject_cast<ItemContainer *>(item);
        if (itemCont && itemCont != layout()->placeHolder()) {
            // NOTE: do not use positionItemAndAssign here, because we do not want to Q_EMIT layoutNeedsSaving, to not save after resize
            positionItem(itemCont);
            assignSpaceImpl(itemCont);
        }
    }
}

void GridLayoutManager::resetLayoutFromConfig(const QRectF &newGeom, const QRectF &oldGeom)
{
    m_grid.clear();
    m_pointsForItem.clear();
    QList<ItemContainer *> missingItems;

    for (auto *item : layout()->childItems()) {
        ItemContainer *itemCont = qobject_cast<ItemContainer *>(item);
        if (itemCont && itemCont != layout()->placeHolder()) {
            if (!restoreItem(itemCont)) {
                missingItems << itemCont;
            }
        }
    }

    for (auto *item : qAsConst(missingItems)) {
        // NOTE: do not use positionItemAndAssign here, because we do not want to Q_EMIT layoutNeedsSaving, to not save after resize
        maintainItemEdgeAlignment(item, newGeom, oldGeom);
        positionItem(item);
        assignSpaceImpl(item);
    }

    if (!missingItems.isEmpty()) {
        layout()->save();
    }
}

bool GridLayoutManager::restoreItem(ItemContainer *item)
{
    auto it = m_parsedConfig.find(item->key());

    if (it != m_parsedConfig.end()) {
        // Actual restore
        item->setPosition(QPointF(it.value().x, it.value().y));
        item->setSize(QSizeF(it.value().width, it.value().height));
        item->setRotation(it.value().rotation);

        // NOTE: do not use positionItemAndAssign here, because we do not want to Q_EMIT layoutNeedsSaving, to not save after resize
        // If size is empty the layout is not in a valid state and probably startup is not completed yet
        if (!layout()->size().isEmpty()) {
            releaseSpaceImpl(item);
            positionItem(item);
            assignSpaceImpl(item);
        }

        return true;
    }

    return false;
}

bool GridLayoutManager::isRectAvailable(const QRectF &rect)
{
    // TODO: define directions in which it can grow
    if (rect.x() < 0 || rect.y() < 0 || rect.x() + rect.width() > layout()->width() || rect.y() + rect.height() > layout()->height()) {
        return false;
    }

    const QRect cellItemGeom = cellBasedGeometry(rect);

    for (int row = cellItemGeom.top(); row <= cellItemGeom.bottom(); ++row) {
        for (int column = cellItemGeom.left(); column <= cellItemGeom.right(); ++column) {
            if (!isCellAvailable(QPair<int, int>(row, column))) {
                return false;
            }
        }
    }
    return true;
}

bool GridLayoutManager::assignSpaceImpl(ItemContainer *item)
{
    // Don't Q_EMIT extra layoutneedssaving signals
    releaseSpaceImpl(item);
    if (!isRectAvailable(itemGeometry(item))) {
        qCWarning(CONTAINMENTLAYOUTMANAGER_DEBUG) << "Trying to take space not available" << item;
        return false;
    }

    const QRect cellItemGeom = cellBasedGeometry(itemGeometry(item));

    for (int row = cellItemGeom.top(); row <= cellItemGeom.bottom(); ++row) {
        for (int column = cellItemGeom.left(); column <= cellItemGeom.right(); ++column) {
            QPair<int, int> cell(row, column);
            m_grid.insert(cell, item);
            m_pointsForItem[item].insert(cell);
        }
    }

    // Reorder items tab order
    for (auto *i2 : layout()->childItems()) {
        ItemContainer *item2 = qobject_cast<ItemContainer *>(i2);
        if (item2 && item2->parentItem() == item->parentItem() && item != item2 && item2 != layout()->placeHolder() && item->y() < item2->y() + item2->height()
            && item->x() <= item2->x()) {
            item->stackBefore(item2);
            break;
        }
    }

    if (item->layoutAttached()) {
        connect(item, &ItemContainer::sizeHintsChanged, this, [this, item]() {
            adjustToItemSizeHints(item);
        });
    }

    return true;
}

void GridLayoutManager::releaseSpaceImpl(ItemContainer *item)
{
    auto it = m_pointsForItem.find(item);

    if (it == m_pointsForItem.end()) {
        return;
    }

    for (const auto &point : it.value()) {
        m_grid.remove(point);
    }

    m_pointsForItem.erase(it);

    disconnect(item, &ItemContainer::sizeHintsChanged, this, nullptr);
}

int GridLayoutManager::rows() const
{
    return layout()->height() / cellSize().height();
}

int GridLayoutManager::columns() const
{
    return layout()->width() / cellSize().width();
}

void GridLayoutManager::adjustToItemSizeHints(ItemContainer *item)
{
    if (!item->layoutAttached() || item->editMode()) {
        return;
    }

    bool changed = false;

    // Minimum
    const qreal newMinimumHeight = item->layoutAttached()->property("minimumHeight").toReal();
    const qreal newMinimumWidth = item->layoutAttached()->property("minimumWidth").toReal();

    if (newMinimumHeight > item->height()) {
        item->setHeight(newMinimumHeight);
        changed = true;
    }
    if (newMinimumWidth > item->width()) {
        item->setWidth(newMinimumWidth);
        changed = true;
    }

    // Preferred
    const qreal newPreferredHeight = item->layoutAttached()->property("preferredHeight").toReal();
    const qreal newPreferredWidth = item->layoutAttached()->property("preferredWidth").toReal();

    if (newPreferredHeight > item->height()) {
        item->setHeight(layout()->cellHeight() * ceil(newPreferredHeight / layout()->cellHeight()));
        changed = true;
    }
    if (newPreferredWidth > item->width()) {
        item->setWidth(layout()->cellWidth() * ceil(newPreferredWidth / layout()->cellWidth()));
        changed = true;
    }

    /*// Maximum : IGNORE?
    const qreal newMaximumHeight = item->layoutAttached()->property("preferredHeight").toReal();
    const qreal newMaximumWidth = item->layoutAttached()->property("preferredWidth").toReal();

    if (newMaximumHeight > 0 && newMaximumHeight < height()) {
        item->setHeight(newMaximumHeight);
        changed = true;
    }
    if (newMaximumHeight > 0 && newMaximumWidth < width()) {
        item->setWidth(newMaximumWidth);
        changed = true;
    }*/

    // Relayout if anything changed
    if (changed && itemIsManaged(item)) {
        releaseSpace(item);
        positionItem(item);
        assignSpace(item);
    }
}

QRect GridLayoutManager::cellBasedGeometry(const QRectF &geom) const
{
    return QRect(round(qBound(0.0, geom.x(), layout()->width() - geom.width()) / cellSize().width()),
                 round(qBound(0.0, geom.y(), layout()->height() - geom.height()) / cellSize().height()),
                 round((qreal)geom.width() / cellSize().width()),
                 round((qreal)geom.height() / cellSize().height()));
}

QRect GridLayoutManager::cellBasedBoundingGeometry(const QRectF &geom) const
{
    return QRect(floor(qBound(0.0, geom.x(), layout()->width() - geom.width()) / cellSize().width()),
                 floor(qBound(0.0, geom.y(), layout()->height() - geom.height()) / cellSize().height()),
                 ceil((qreal)geom.width() / cellSize().width()),
                 ceil((qreal)geom.height() / cellSize().height()));
}

bool GridLayoutManager::isOutOfBounds(const QPair<int, int> &cell) const
{
    return cell.first < 0 || cell.second < 0 || cell.first >= rows() || cell.second >= columns();
}

bool GridLayoutManager::isCellAvailable(const QPair<int, int> &cell) const
{
    return !isOutOfBounds(cell) && !m_grid.contains(cell);
}

QRectF GridLayoutManager::itemGeometry(QQuickItem *item) const
{
    return QRectF(item->x(), item->y(), item->width(), item->height());
}

QPair<int, int> GridLayoutManager::nextCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const
{
    QPair<int, int> nCell = cell;

    switch (direction) {
    case AppletsLayout::AppletsLayout::BottomToTop:
        --nCell.first;
        break;
    case AppletsLayout::AppletsLayout::TopToBottom:
        ++nCell.first;
        break;
    case AppletsLayout::AppletsLayout::RightToLeft:
        --nCell.second;
        break;
    case AppletsLayout::AppletsLayout::LeftToRight:
    default:
        ++nCell.second;
        break;
    }

    return nCell;
}

QPair<int, int> GridLayoutManager::nextAvailableCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const
{
    QPair<int, int> nCell = cell;
    while (!isOutOfBounds(nCell)) {
        nCell = nextCell(nCell, direction);

        if (isOutOfBounds(nCell)) {
            switch (direction) {
            case AppletsLayout::AppletsLayout::BottomToTop:
                nCell.first = rows() - 1;
                --nCell.second;
                break;
            case AppletsLayout::AppletsLayout::TopToBottom:
                nCell.first = 0;
                ++nCell.second;
                break;
            case AppletsLayout::AppletsLayout::RightToLeft:
                --nCell.first;
                nCell.second = columns() - 1;
                break;
            case AppletsLayout::AppletsLayout::LeftToRight:
            default:
                ++nCell.first;
                nCell.second = 0;
                break;
            }
        }

        if (isCellAvailable(nCell)) {
            return nCell;
        }
    }

    return QPair<int, int>(-1, -1);
}

QPair<int, int> GridLayoutManager::nextTakenCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const
{
    QPair<int, int> nCell = cell;
    while (!isOutOfBounds(nCell)) {
        nCell = nextCell(nCell, direction);

        if (isOutOfBounds(nCell)) {
            switch (direction) {
            case AppletsLayout::AppletsLayout::BottomToTop:
                nCell.first = rows() - 1;
                --nCell.second;
                break;
            case AppletsLayout::AppletsLayout::TopToBottom:
                nCell.first = 0;
                ++nCell.second;
                break;
            case AppletsLayout::AppletsLayout::RightToLeft:
                --nCell.first;
                nCell.second = columns() - 1;
                break;
            case AppletsLayout::AppletsLayout::LeftToRight:
            default:
                ++nCell.first;
                nCell.second = 0;
                break;
            }
        }

        if (!isCellAvailable(nCell)) {
            return nCell;
        }
    }

    return QPair<int, int>(-1, -1);
}

int GridLayoutManager::freeSpaceInDirection(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const
{
    QPair<int, int> nCell = cell;

    int avail = 0;

    while (isCellAvailable(nCell)) {
        ++avail;
        nCell = nextCell(nCell, direction);
    }

    return avail;
}

QRectF GridLayoutManager::nextAvailableSpace(ItemContainer *item, const QSizeF &minimumSize, AppletsLayout::PreferredLayoutDirection direction) const
{
    // The mionimum size in grid units
    const QSize minimumGridSize(ceil((qreal)minimumSize.width() / cellSize().width()), ceil((qreal)minimumSize.height() / cellSize().height()));

    QRect itemCellGeom = cellBasedGeometry(itemGeometry(item));
    itemCellGeom.setWidth(qMax(itemCellGeom.width(), minimumGridSize.width()));
    itemCellGeom.setHeight(qMax(itemCellGeom.height(), minimumGridSize.height()));

    QSize partialSize;

    QPair<int, int> cell(itemCellGeom.y(), itemCellGeom.x());
    if (direction == AppletsLayout::AppletsLayout::RightToLeft) {
        cell.second += itemCellGeom.width();
    } else if (direction == AppletsLayout::AppletsLayout::BottomToTop) {
        cell.first += itemCellGeom.height();
    }

    if (!isCellAvailable(cell)) {
        cell = nextAvailableCell(cell, direction);
    }

    while (!isOutOfBounds(cell)) {
        if (direction == AppletsLayout::LeftToRight || direction == AppletsLayout::RightToLeft) {
            partialSize = QSize(INT_MAX, 0);

            int currentRow = cell.first;
            for (; currentRow < cell.first + itemCellGeom.height(); ++currentRow) {
                const int freeRow = freeSpaceInDirection(QPair<int, int>(currentRow, cell.second), direction);

                partialSize.setWidth(qMin(partialSize.width(), freeRow));

                if (freeRow > 0) {
                    partialSize.setHeight(partialSize.height() + 1);
                } else if (partialSize.height() < minimumGridSize.height()) {
                    break;
                }

                if (partialSize.width() >= itemCellGeom.width() && partialSize.height() >= itemCellGeom.height()) {
                    break;
                } else if (partialSize.width() < minimumGridSize.width()) {
                    break;
                }
            }

            if (partialSize.width() >= minimumGridSize.width() && partialSize.height() >= minimumGridSize.height()) {
                const int width = qMin(itemCellGeom.width(), partialSize.width()) * cellSize().width();
                const int height = qMin(itemCellGeom.height(), partialSize.height()) * cellSize().height();

                if (direction == AppletsLayout::RightToLeft) {
                    return QRectF((cell.second + 1) * cellSize().width() - width, cell.first * cellSize().height(), width, height);
                    // AppletsLayout::LeftToRight
                } else {
                    return QRectF(cell.second * cellSize().width(), cell.first * cellSize().height(), width, height);
                }
            } else {
                cell = nextAvailableCell(nextTakenCell(cell, direction), direction);
            }

        } else if (direction == AppletsLayout::TopToBottom || direction == AppletsLayout::BottomToTop) {
            partialSize = QSize(0, INT_MAX);

            int currentColumn = cell.second;
            for (; currentColumn < cell.second + itemCellGeom.width(); ++currentColumn) {
                const int freeColumn = freeSpaceInDirection(QPair<int, int>(cell.first, currentColumn), direction);

                partialSize.setHeight(qMin(partialSize.height(), freeColumn));

                if (freeColumn > 0) {
                    partialSize.setWidth(partialSize.width() + 1);
                } else if (partialSize.width() < minimumGridSize.width()) {
                    break;
                }

                if (partialSize.width() >= itemCellGeom.width() && partialSize.height() >= itemCellGeom.height()) {
                    break;
                } else if (partialSize.height() < minimumGridSize.height()) {
                    break;
                }
            }

            if (partialSize.width() >= minimumGridSize.width() && partialSize.height() >= minimumGridSize.height()) {
                const int width = qMin(itemCellGeom.width(), partialSize.width()) * cellSize().width();
                const int height = qMin(itemCellGeom.height(), partialSize.height()) * cellSize().height();

                if (direction == AppletsLayout::BottomToTop) {
                    return QRectF(cell.second * cellSize().width(), (cell.first + 1) * cellSize().height() - height, width, height);
                    // AppletsLayout::TopToBottom:
                } else {
                    return QRectF(cell.second * cellSize().width(), cell.first * cellSize().height(), width, height);
                }
            } else {
                cell = nextAvailableCell(nextTakenCell(cell, direction), direction);
            }
        }
    }

    // We didn't manage to find layout space, return invalid geometry
    return QRectF();
}

#include "moc_gridlayoutmanager.cpp"