File indexing completed on 2024-04-14 04:36:56

0001 /***************************************************************************
0002  *   Copyright (C) 2017 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 VIEWBASE_H
0019 #define VIEWBASE_H
0020 
0021 // KQuickItemViews
0022 #include <KQuickItemViews/views/flickable.h>
0023 
0024 // Qt
0025 #include <QtCore/QAbstractItemModel>
0026 #include <QtCore/QItemSelectionModel>
0027 class QQmlComponent;
0028 
0029 // KQuickItemViews
0030 class ViewBasePrivate;
0031 class AbstractItemAdapter;
0032 class ModelAdapter;
0033 class Viewport;
0034 
0035 /**
0036  * Second generation of QtQuick treeview.
0037  *
0038  * The first one was designed for the chat view. It had a limited number of
0039  * requirement when it came to QtModel. However it required total control of
0040  * the layout.
0041  *
0042  * This is the opposite use case. The layout is classic, but the model support
0043  * has to be complete. Performance and lazy loading is also more important.
0044  *
0045  * It require less work to write a new treeview than refector the first one to
0046  * support the additional requirements. In the long run, the first generation
0047  * could be folded into this widget (if it ever makes sense, otherwise they will
0048  * keep diverging).
0049  */
0050 class Q_DECL_EXPORT ViewBase : public Flickable
0051 {
0052     Q_OBJECT
0053 public:
0054     struct ItemFactoryBase {
0055         virtual AbstractItemAdapter* create(Viewport* r) const = 0;
0056         virtual ~ItemFactoryBase() {}
0057     };
0058 
0059     template<typename T> struct ItemFactory final : public ItemFactoryBase {
0060         virtual AbstractItemAdapter* create(Viewport* r) const override {
0061             return new T(r);
0062         }
0063     };
0064 
0065     Q_PROPERTY(bool empty READ isEmpty NOTIFY contentChanged)
0066     Q_PROPERTY(Qt::Corner gravity READ gravity WRITE setGravity)
0067 
0068     Qt::Corner gravity() const;
0069     void setGravity(Qt::Corner g);
0070 
0071     explicit ViewBase(QQuickItem* parent = nullptr);
0072 
0073     virtual ~ViewBase();
0074 
0075     QVector<ModelAdapter*> modelAdapters() const;
0076 
0077     bool isEmpty() const;
0078 
0079 protected:
0080     virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
0081 
0082 
0083     /// To be used with moderation. Necessary when the delegate is replaced.
0084     void reload();
0085 
0086     /**
0087      * Extend this function when tasks need to be taken care of when an
0088      * exlicit refresh is needed. Remember to call the parent class
0089      * ::refresh() from the extended one.
0090      *
0091      * It is called, for example, when the model change.
0092      */
0093     virtual void refresh() final;
0094 
0095     void addModelAdapter(ModelAdapter* a);
0096     void removeModelAdapter(ModelAdapter* a);
0097 
0098 Q_SIGNALS:
0099     void contentChanged();
0100 
0101 private:
0102     ViewBasePrivate* d_ptr;
0103     Q_DECLARE_PRIVATE(ViewBase)
0104 };
0105 
0106 Q_DECLARE_METATYPE(QSharedPointer<QItemSelectionModel>)
0107 
0108 #endif