File indexing completed on 2024-05-12 17:08:55

0001 /*
0002     SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "abstractlayoutmanager.h"
0010 #include "appletcontainer.h"
0011 
0012 class AppletsLayout;
0013 class ItemContainer;
0014 
0015 struct Geom {
0016     qreal x;
0017     qreal y;
0018     qreal width;
0019     qreal height;
0020     qreal rotation;
0021 };
0022 
0023 class GridLayoutManager : public AbstractLayoutManager
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     GridLayoutManager(AppletsLayout *layout);
0029     ~GridLayoutManager();
0030 
0031     void layoutGeometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0032 
0033     QString serializeLayout() const override;
0034     void parseLayout(const QString &savedLayout) override;
0035 
0036     bool itemIsManaged(ItemContainer *item) override;
0037 
0038     void resetLayout() override;
0039     void resetLayoutFromConfig(const QRectF &newGeom, const QRectF &oldGeom) override;
0040 
0041     bool restoreItem(ItemContainer *item) override;
0042 
0043     bool isRectAvailable(const QRectF &rect) override;
0044 
0045 protected:
0046     // The rectangle as near as possible to the current item geometry which can fit it
0047     QRectF nextAvailableSpace(ItemContainer *item, const QSizeF &minimumSize, AppletsLayout::PreferredLayoutDirection direction) const override;
0048 
0049     bool assignSpaceImpl(ItemContainer *item) override;
0050     void releaseSpaceImpl(ItemContainer *item) override;
0051 
0052 private:
0053     // Total cell rows
0054     inline int rows() const;
0055 
0056     // Total cell columns
0057     inline int columns() const;
0058 
0059     // Converts the item pixel-based geometry to a cellsize-based geometry
0060     inline QRect cellBasedGeometry(const QRectF &geom) const;
0061 
0062     // Converts the item pixel-based geometry to a cellsize-based geometry
0063     // This is the bounding geometry, usually larger than cellBasedGeometry
0064     inline QRect cellBasedBoundingGeometry(const QRectF &geom) const;
0065 
0066     // true if the cell is out of the bounds of the containment
0067     inline bool isOutOfBounds(const QPair<int, int> &cell) const;
0068 
0069     // True if the space for the given cell is available
0070     inline bool isCellAvailable(const QPair<int, int> &cell) const;
0071 
0072     // Returns the qrect geometry for an item
0073     inline QRectF itemGeometry(QQuickItem *item) const;
0074 
0075     // The next cell given the direction
0076     QPair<int, int> nextCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;
0077 
0078     // The next cell that is available given the direction
0079     QPair<int, int> nextAvailableCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;
0080 
0081     // The next cell that is has something in it given the direction
0082     QPair<int, int> nextTakenCell(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;
0083 
0084     // How many cells are available in the row starting from the given cell and direction
0085     int freeSpaceInDirection(const QPair<int, int> &cell, AppletsLayout::PreferredLayoutDirection direction) const;
0086 
0087     /**
0088      * This reacts to changes in size hints by an item
0089      */
0090     void adjustToItemSizeHints(ItemContainer *item);
0091 
0092     // What is the item that occupies the point. The point is expressed in cells rather than pixels. a qpair rather a QPointF as QHash doesn't support
0093     // identification by QPointF
0094     QHash<QPair<int, int>, ItemContainer *> m_grid;
0095     QHash<ItemContainer *, QSet<QPair<int, int>>> m_pointsForItem;
0096 
0097     QHash<QString, Geom> m_parsedConfig;
0098 };