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

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_INDEXMETADATA_P_H
0019 #define KQUICKITEMVIEWS_INDEXMETADATA_P_H
0020 
0021 // Qt
0022 #include <QtCore/QRectF>
0023 #include <QtCore/QModelIndex>
0024 #include <QtCore/QItemSelectionModel>
0025 
0026 namespace StateTracker {
0027     class ViewItem;
0028     class Geometry;
0029     class ModelItem;
0030     class Proximity;
0031     class Index;
0032     class Selection;
0033 }
0034 
0035 class ContextAdapter;
0036 class Viewport;
0037 class SelectionAdapter;
0038 
0039 class IndexMetadataPrivate;
0040 
0041 /**
0042  * The shared view of a QModelIndex between the various adapters. The
0043  * StateTracker::Content has `StateTracker::ModelItem` and the AbstractItemAdapter
0044  * has the `StateTracker::ViewItem`. Both are the QModelIndex from their point of view
0045  * (model for the former and view for the later).
0046  *
0047  * This class is the glue between those vision of the QModelIndex and holds
0048  * the little they can share.
0049  *
0050  * Also note that the StateTracker::ModelItem and AbstractItemAdapter have very
0051  * different life cycle. It is why a more neutral entity is needed to bridge
0052  * them across the adapters.
0053  *
0054  * Note that some methods of this class are implemented in other "cpp" files
0055  * to give them the visibility of some otherwise private structures. This was
0056  * done to limit the proliferation of under-used _p.h classes and make them
0057  * easier to modify as time goes by.
0058  */
0059 class IndexMetadata final
0060 {
0061 
0062 public:
0063     explicit IndexMetadata(StateTracker::Index *idxT, Viewport *p);
0064     ~IndexMetadata();
0065 
0066     /**
0067      * The actions to perform on the model change tracking state machine.
0068      */
0069     enum class LoadAction {
0070         SHOW     = 0, /*!< Make visible on screen (or buffer) */
0071         HIDE     = 1, /*!< Remove from the screen (or buffer) */
0072         ATTACH   = 2, /*!< Track, but do not show             */
0073         DETACH   = 3, /*!< Stop tracking for changes          */
0074         UPDATE   = 4, /*!< Update the element                 */
0075         MOVE     = 5, /*!< Update the depth and lookup        */
0076         RESET    = 6, /*!< Flush the visual item              */
0077         REPARENT = 7, /*!< Move in the item tree                     */
0078     };
0079 
0080     /**
0081      * The actions to perform on the geometry tracking state machine.
0082      */
0083     enum class GeometryAction {
0084         MOVE    , /*!< When moved                              */
0085         RESIZE  , /*!< When the content size changes           */
0086         PLACE   , /*!< When setting the position               */
0087         RESET   , /*!< The delegate, layout changes, or pooled */
0088         MODIFY  , /*!< When the QModelIndex role changes       */
0089         DECORATE, /*!< When the decoration size changes        */
0090         VIEW    , /*!< When the geometry is accessed           */
0091     };
0092 
0093     /**
0094      * Actions to perform on the view (widget) state tracker.
0095      */
0096     enum class ViewAction {
0097         ATTACH      , /*!< Activate the element (do not sync it) */
0098         ENTER_BUFFER, /*!< Sync all roles                        */
0099         ENTER_VIEW  , /*!< NOP (todo)                            */
0100         UPDATE      , /*!< Reload the roles                      */
0101         MOVE        , /*!< Move to a new position                */
0102         LEAVE_BUFFER, /*!< Stop keeping track of data changes    */
0103         DETACH      , /*!< Delete                                */
0104     };
0105 
0106     /**
0107      * The actions associated with the state of the (cartesian) siblings status
0108      * if this index.
0109      */
0110     enum class ProximityAction {
0111         QUERY  , /*!<  */
0112         DISCARD, /*!<  */
0113         MOVE   , /*!<  */
0114     };
0115 
0116     /**
0117      *
0118      */
0119     enum class EdgeType {
0120         FREE    , /*!<  */
0121         VISIBLE , /*!<  */
0122         BUFFERED, /*!<  */
0123         NONE    , /*!<  */
0124     };
0125 
0126     // Getters
0127     QRectF decoratedGeometry() const;
0128     QModelIndex index() const;
0129 
0130     StateTracker::ViewItem  *viewTracker     () const;
0131     StateTracker::Index     *indexTracker    () const;
0132     StateTracker::ModelItem *modelTracker    () const;
0133     StateTracker::Proximity *proximityTracker() const;
0134     StateTracker::Geometry  *geometryTracker () const;
0135     StateTracker::Selection *selectionTracker() const;
0136     ContextAdapter          *contextAdapter  () const;
0137 
0138     // Mutator
0139     bool performAction(ViewAction      a);
0140     bool performAction(LoadAction      a);
0141     bool performAction(GeometryAction  a);
0142     bool performAction(ProximityAction a, Qt::Edge e);
0143     bool performAction(QItemSelectionModel::SelectionFlag f, SelectionAdapter *ad);
0144 
0145     // Navigation
0146     IndexMetadata *up   () const; //DEPRECATED remove, use `next(Qt::Edge)`
0147     IndexMetadata *down () const; //DEPRECATED remove, use `next(Qt::Edge)`
0148     IndexMetadata *left () const; //DEPRECATED remove, use `next(Qt::Edge)`
0149     IndexMetadata *right() const; //DEPRECATED remove, use `next(Qt::Edge)`
0150 
0151     IndexMetadata *next(Qt::Edge e) const;
0152 
0153     bool isTopItem() const;
0154 
0155     /**
0156      * Check if the expected geometry and current geometry match.
0157      *
0158      * @return True if the view delegate (widget) is in the expected position.
0159      */
0160     bool isInSync() const;
0161 
0162     void setViewTracker(StateTracker::ViewItem *i);
0163 
0164     /**
0165      * Return true when the metadata is complete enough to be displayed.
0166      *
0167      * This means the following thing:
0168      *
0169      *  * The QModelIndex is valid
0170      *  * The position is known
0171      *  * The size is known
0172      */
0173     bool isValid() const;
0174 
0175     /**
0176      * If the object is considered visible by the viewport.
0177      *
0178      * Note that it doesn't always means it is directly in on screen. It can
0179      * be shadowed by something else, 99% out of view or the view decided not
0180      * to actually display it.
0181      *
0182      * It only means the engine fully tracks it and assumes the tracking
0183      * overhead isn't wasted.
0184      */
0185     bool isVisible() const;
0186 
0187     QSizeF sizeHint();
0188 
0189     qreal borderDecoration(Qt::Edge e) const;
0190     void setBorderDecoration(Qt::Edge e, qreal r);
0191 
0192     void setSize(const QSizeF& s);
0193     void setPosition(const QPointF& p);
0194 
0195     bool isCollapsed() const;
0196     void setCollapsed(bool c);
0197 
0198     Viewport *viewport() const;
0199 
0200 private:
0201     IndexMetadataPrivate *d_ptr;
0202 };
0203 
0204 /**
0205  * Allow daisy-chaining when the return value isn't used.
0206  */
0207 template<typename A> inline IndexMetadata *operator<<(IndexMetadata* md, A a)
0208 { md->performAction(a); return md; }
0209 
0210 #endif