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

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_MODEL_P_H
0019 #define KQUICKITEMVIEWS_MODEL_P_H
0020 
0021 
0022 class QAbstractItemModel;
0023 
0024 namespace StateTracker
0025 {
0026 class Content;
0027 
0028 class Model
0029 {
0030 public:
0031     explicit Model(StateTracker::Content* q);
0032 
0033     enum class State {
0034         NO_MODEL , /*!< The model is not set, there is nothing to do                  */
0035         PAUSED   , /*!< The model is set, but the reflector is not listening          */
0036         POPULATED, /*!< The initial insertion has been done, it is ready for tracking */
0037         TRACKING , /*!< The model is set and the reflector is listening to changes    */
0038         RESETING , /*!< The model is undergoing a reset process                       */
0039         MUTATING , /*!< Insertion events are not re-entrant, prevent this             */
0040     };
0041 
0042     enum class Action {
0043         POPULATE, /*!< Fetch the model content and fill the view */
0044         DISABLE , /*!< Disconnect the model tracking             */
0045         ENABLE  , /*!< Connect the pending model                 */
0046         RESET   , /*!< Remove the delegates but keep the trackers*/
0047         FREE    , /*!< Free the whole tracking tree              */
0048         MOVE    , /*!< Try to fix the viewport with content      */
0049         TRIM    , /*!< Remove the elements until the edge is free*/
0050     };
0051 
0052     /**
0053      * Manipulate the tracking state.
0054      */
0055     State performAction(Action);
0056 
0057     State state() const;
0058 
0059     QAbstractItemModel *trackedModel  () const;
0060     QAbstractItemModel *modelCandidate() const;
0061     void setModel(QAbstractItemModel* m);
0062 
0063 private:
0064     typedef void(StateTracker::Model::*StateF)();
0065 
0066     // Attributes
0067     State m_State {State::NO_MODEL};
0068     QAbstractItemModel* m_pModel        {nullptr};
0069     QAbstractItemModel* m_pTrackedModel {nullptr};
0070 
0071     // Actions, do not call directly
0072     void track();
0073     void untrack();
0074     void nothing();
0075     void reset();
0076     void free();
0077     void error();
0078     void populate();
0079     void fill();
0080     void trim();
0081 
0082     static const State  m_fStateMap    [6][7];
0083     static const StateF m_fStateMachine[6][7];
0084 
0085     StateTracker::Content *q_ptr;
0086 };
0087 
0088 /**
0089  * Allow daisy-chaining when the return value isn't used.
0090  */
0091 template<typename A>
0092 inline Model *operator<<(Model* md, A a)
0093 {
0094     md->performAction(a);
0095     return md;
0096 }
0097 
0098 }
0099 
0100 
0101 #endif