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

0001 /***************************************************************************
0002  *   Copyright (C) 2017-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_CONTENT_P_H
0019 #define KQUICKITEMVIEWS_CONTENT_P_H
0020 
0021 // Qt
0022 #include <QtCore/QObject>
0023 #include <QtCore/QAbstractItemModel>
0024 
0025 // KQuickItemViews
0026 class ContentPrivate;
0027 class Viewport;
0028 class ModelRect;
0029 #include <private/indexmetadata_p.h>
0030 #include "modelitem_p.h"
0031 
0032 namespace StateTracker {
0033 
0034 struct ModelItem;
0035 struct Model;
0036 
0037 /**
0038  * Listen to the model changes and forward those changes to the other relevant
0039  * state trackers.
0040  */
0041 class Content final : public QObject
0042 {
0043     Q_OBJECT
0044 public:
0045     enum class Event {
0046         ENTER_STATE,
0047         LEAVE_STATE,
0048     };
0049 
0050     explicit Content(Viewport* parent = nullptr);
0051     virtual ~Content();
0052 
0053     // Edges management
0054     void setAvailableEdges(Qt::Edges edges, IndexMetadata::EdgeType type);
0055     Qt::Edges availableEdges(IndexMetadata::EdgeType type) const;
0056     IndexMetadata *getEdge(IndexMetadata::EdgeType t, Qt::Edge e) const;
0057     void setEdge(IndexMetadata::EdgeType et, StateTracker::Index* tti, Qt::Edge e);
0058     ModelRect* edges(IndexMetadata::EdgeType e) const;
0059 
0060     // Getter
0061     StateTracker::Index *root        () const;
0062     StateTracker::Index *lastItem    () const;
0063     StateTracker::Index *firstItem   () const;
0064     StateTracker::Model *modelTracker() const;
0065 
0066     // Mutator
0067     void connectModel(QAbstractItemModel *m);
0068     void disconnectModel(QAbstractItemModel *m);
0069     void resetRoot();
0070     void resetEdges();
0071     void perfromStateChange(Event e, IndexMetadata *md, StateTracker::ModelItem::State s);
0072     void forceInsert(const QModelIndex& idx);
0073     void forceInsert(const QModelIndex& parent, int first, int last);
0074 
0075     // Helpers
0076     IndexMetadata *metadataForIndex(const QModelIndex& idx) const;
0077     bool isActive(const QModelIndex& parent, int first, int last);
0078     Index *find(Index *i, Qt::Edge direction, std::function<bool(Index *i)>) const;
0079 
0080 Q_SIGNALS:
0081     void contentChanged();
0082 
0083 private:
0084     ContentPrivate* d_ptr;
0085 };
0086 
0087 }
0088 
0089 // Inject some extra validation when executed in debug mode.
0090 #ifdef ENABLE_EXTRA_VALIDATION
0091  #define _DO_TEST(f, ...) f(__VA_ARGS__);
0092  #include <private/runtimetests_p.h>
0093 #else
0094  #define _DO_TEST(f, ...) /*NOP*/;
0095 #endif
0096 
0097 #endif