File indexing completed on 2024-04-28 04:41:47

0001 /***************************************************************************
0002  *   Copyright (C) 2018 by Emmanuel Lepage Vallee                          *
0003  *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@kde.org>             *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 3 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0017  **************************************************************************/
0018 #ifndef KQUICKITEMVIEWS_GEOMETRYADAPTER_H
0019 #define KQUICKITEMVIEWS_GEOMETRYADAPTER_H
0020 
0021 #include <QtCore/QObject>
0022 #include <QtCore/QSizeF>
0023 #include <QtCore/QPointF>
0024 
0025 class GeometryAdapterPrivate;
0026 class AbstractItemAdapter;
0027 class Viewport;
0028 
0029 /**
0030  * Add strategies to get the size (and optionally position) hints.
0031  *
0032  * One size don't fit everything here. QtWidgets had a `sizeHint` method.
0033  * However getting this information in QML is a lot harder for many reasons.
0034  */
0035 class Q_DECL_EXPORT GeometryAdapter : public QObject
0036 {
0037     Q_OBJECT
0038 public:
0039     Q_INVOKABLE explicit GeometryAdapter(Viewport *parent = nullptr);
0040     virtual ~GeometryAdapter();
0041 
0042     /**
0043      * Features this adapter provide and requires.
0044      */
0045     enum Capabilities {
0046         NONE                      = 0x0 << 0,
0047 
0048        /**
0049         * Most GeometryAdapter only provide size hints.
0050         *
0051         * However some views might not have built-in layouts and then the position
0052         * is also necessary.
0053         *
0054         * This property only state that the adapter *can* provide positions.
0055         * `alwaysHasPositionHints` will tell if this is always the case.
0056         */
0057         HAS_POSITION_HINTS        = 0x1 << 0,
0058 
0059        /**
0060         * Allow the view to skip the GeometryAdapter when they only need the height.
0061         *
0062         * It will be called once, then the view will assume the result will always
0063         * be the same.
0064         */
0065         HAS_UNIFORM_HEIGHT        = 0x1 << 1,
0066 
0067        /**
0068         * Allow the view to skip the GeometryAdapter when they only need the width.
0069         *
0070         * It will be called once, then the view will assume the result will always
0071         * be the same.
0072         */
0073         HAS_UNIFORM_WIDTH         = 0x1 << 2,
0074 
0075        /**
0076         * The sizeHint will *always* return a valid size.
0077         *
0078         * Some GeometryAdapter may only provide size hints when some preconditions
0079         * are met. For example something that depends on the current delegate
0080         * will only work once the delegate is created.
0081         */
0082         ALWAYS_HAS_SIZE_HINTS     = 0x1 << 3,
0083 
0084         /** @see alwaysHasSizeHints */
0085         ALWAYS_HAS_POSITION_HINTS = 0x1 << 4,
0086 
0087        /**
0088         * This GeometryAdapter allows to query the size before creating a
0089         * delegate instance.
0090         */
0091         HAS_AHEAD_OF_TIME         = 0x1 << 5,
0092 
0093        /**
0094         * This GeometryAdapter requires the view to track each QQuickItem
0095         * delegate geometry.
0096         */
0097         TRACKS_QQUICKITEM_GEOMETRY= 0x1 << 6,
0098 
0099        /**
0100         * This GeometryAdapter requires a single delegate instance to work.
0101         */
0102         REQUIRES_SINGLE_INSTANCE  = 0x1 << 7,
0103 
0104        /**
0105         * Apply the size hints instead of using the delegate implicit size.
0106         */
0107         FORCE_DELEGATE_SIZE       = 0x1 << 8,
0108 
0109         /**
0110          * Each QModelIndex, including the children QModelIndex or positioned
0111          * one after the other in a Cartesian plane. This capability is only
0112          * relevant when HAS_POSITION_HINTS is set and implies
0113          * HAS_ORDERED_SIBLINGS.
0114          *
0115          * Do not set this hint if the
0116          */
0117         IS_ORDERED                = 0x1 << 9,
0118 
0119         /**
0120          * When the QModelIndex with the same parent are positioned next to
0121          * each other on a Cartesian plane.
0122          *
0123          * Use IS_ORDERED if this is also true among QModelIndex of different
0124          * parent.
0125          */
0126         HAS_ORDERED_SIBLINGS      = 0x1 << 10,
0127     };
0128 //     Q_FLAGS(Capabilities)
0129 
0130     /**
0131      * A feature set provided by this adapter.
0132      */
0133     Q_PROPERTY(int capabilities READ capabilities NOTIFY flagsChanged)
0134 
0135     /**
0136      * If the GeometryAdapter should override the delegate implicit size
0137      * with the size hints.
0138      */
0139     Q_PROPERTY(bool forceSize READ isSizeForced WRITE setSizeForced NOTIFY flagsChanged)
0140 
0141     virtual int capabilities() const;
0142 
0143     virtual bool isSizeForced() const;
0144     virtual void setSizeForced(bool f);
0145 
0146     /**
0147      * Get the size hints for an AbstractItemAdapter.
0148      */
0149     Q_INVOKABLE virtual QSizeF sizeHint(const QModelIndex &index, AbstractItemAdapter *adapter) const;
0150 
0151     /**
0152      * Get the position hints for an AbstractItemAdapter.
0153      */
0154     Q_INVOKABLE virtual QPointF positionHint(const QModelIndex &index, AbstractItemAdapter *adapter) const;
0155 
0156     Viewport *viewport() const;
0157 
0158 protected:
0159     /**
0160      * Set the flags to be used by the internal optimization engine.
0161      */
0162     void setCapabilities(int f);
0163 
0164     void addCapabilities(int f);
0165 
0166     void removeCapabilities(int f);
0167 
0168 Q_SIGNALS:
0169 
0170     /**
0171      * Tell the view to disregard previously returned values and start over.
0172      */
0173     void dismissResult();
0174 
0175     void flagsChanged();
0176 
0177 
0178 private:
0179     GeometryAdapterPrivate *d_ptr;
0180     Q_DECLARE_PRIVATE(GeometryAdapter)
0181 };
0182 
0183 #endif