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

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 "appletslayout.h"
0010 #include <QObject>
0011 
0012 class ItemContainer;
0013 
0014 class AbstractLayoutManager : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 public:
0019     AbstractLayoutManager(AppletsLayout *layout);
0020     ~AbstractLayoutManager();
0021 
0022     AppletsLayout *layout() const;
0023 
0024     void setCellSize(const QSizeF &size);
0025     QSizeF cellSize() const;
0026 
0027     /**
0028      * A size aligned to the gid that fully contains the given size
0029      */
0030     QSizeF cellAlignedContainingSize(const QSizeF &size) const;
0031 
0032     /**
0033      * Positions the item, does *not* assign the space as taken
0034      */
0035     void positionItem(ItemContainer *item);
0036 
0037     /**
0038      * Positions the item and assigns the space as taken by this item
0039      */
0040     void positionItemAndAssign(ItemContainer *item);
0041 
0042     /**
0043      * Set the space of item's rect as occupied by item.
0044      * The operation may fail if some space of the item's geometry is already occupied.
0045      * @returns true if the operation succeeded
0046      */
0047     bool assignSpace(ItemContainer *item);
0048 
0049     /**
0050      * If item is occupying space, set it as available
0051      */
0052     void releaseSpace(ItemContainer *item);
0053 
0054     // VIRTUALS
0055     virtual QString serializeLayout() const = 0;
0056 
0057     virtual void parseLayout(const QString &savedLayout) = 0;
0058 
0059     virtual void layoutGeometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
0060 
0061     /**
0062      * true if the item is managed by the grid
0063      */
0064     virtual bool itemIsManaged(ItemContainer *item) = 0;
0065 
0066     /**
0067      * Forget about layout information and relayout all items based solely on their current geometry
0068      */
0069     virtual void resetLayout() = 0;
0070 
0071     /**
0072      * Forget about layout information and relayout all items based on their stored geometry first,
0073      * and if that fails from their current geometry.
0074      * If this function has been called from a resize, oldGeomety and newGeometry
0075      * represent the geometries that this layout had before and after the resize, for placing strategies
0076      * of items that weren't in the stored config.
0077      * If one of those rects are empty, neither should be considered in the layouting strategy.
0078      */
0079     virtual void resetLayoutFromConfig(const QRectF &newGeom, const QRectF &oldGeom) = 0;
0080 
0081     /**
0082      * Restores an item geometry from the serialized config
0083      * parseLayout needs to be called before this
0084      * @returns true if the item was stored in the config
0085      * and the restore has been performed.
0086      * Otherwise, the item is not touched and returns false
0087      */
0088     virtual bool restoreItem(ItemContainer *item) = 0;
0089 
0090     /**
0091      * @returns true if the given rectangle is all free space
0092      */
0093     virtual bool isRectAvailable(const QRectF &rect) = 0;
0094 
0095 Q_SIGNALS:
0096     /**
0097      * Emitted when the layout has been changed and now needs saving
0098      */
0099     void layoutNeedsSaving();
0100 
0101 protected:
0102     /**
0103      * Subclasses implement their assignSpace logic here
0104      */
0105     virtual bool assignSpaceImpl(ItemContainer *item) = 0;
0106 
0107     /**
0108      * Subclasses implement their releasespace logic here
0109      */
0110     virtual void releaseSpaceImpl(ItemContainer *item) = 0;
0111 
0112     /**
0113      * @returns a rectangle big at least as minimumSize, trying to be as near as possible to the current item's geometry, displaced in the direction we asked,
0114      * forwards or backwards
0115      * @param item the item container we want to place an item in
0116      * @param minimumSize the minimum size we need to make sure is available
0117      * @param direction the preferred item layout direction, can be Closest, LeftToRight, RightToLeft, TopToBottom, and BottomToTop
0118      */
0119     virtual QRectF nextAvailableSpace(ItemContainer *item, const QSizeF &minimumSize, AppletsLayout::PreferredLayoutDirection direction) const = 0;
0120 
0121 private:
0122     QRectF candidateGeometry(ItemContainer *item) const;
0123 
0124     AppletsLayout *m_layout;
0125 
0126     // size in pixels of a crid cell
0127     QSizeF m_cellSize;
0128 };