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 SINGLEMODELVIEWBASE_H
0019 #define SINGLEMODELVIEWBASE_H
0020 
0021 #include <KQuickItemViews/viewbase.h>
0022 
0023 class SingleModelViewBasePrivate;
0024 
0025 /**
0026  * Random code to get close to drop-in compatibility with both QML and QtWidgets
0027  * views.
0028  *
0029  * This library
0030  * tries to enforce smaller components with well defined scope. But that makes
0031  * its API larger and more complex. This class helps to collapse all the
0032  * concepts into a single thing. This removes some flexibility, but allows
0033  * the widgets to be (almost) drop-in replacements for the ones provided
0034  * by QtQuick2.
0035  */
0036 class Q_DECL_EXPORT SingleModelViewBase : public ViewBase
0037 {
0038     Q_OBJECT
0039 public:
0040     Q_PROPERTY(QQmlComponent* highlight READ highlight WRITE setHighlight)
0041     Q_PROPERTY(QSharedPointer<QItemSelectionModel> selectionModel READ selectionModel WRITE setSelectionModel NOTIFY selectionModelChanged)
0042     Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
0043     Q_PROPERTY(QModelIndex currentIndex READ currentIndex WRITE setCurrentIndex)
0044     Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged)
0045     Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
0046 
0047     // Visible content
0048     Q_PROPERTY(QModelIndex topLeft     READ topLeft     NOTIFY cornerChanged)
0049     Q_PROPERTY(QModelIndex topRight    READ topRight    NOTIFY cornerChanged)
0050     Q_PROPERTY(QModelIndex bottomLeft  READ bottomLeft  NOTIFY cornerChanged)
0051     Q_PROPERTY(QModelIndex bottomRight READ bottomRight NOTIFY cornerChanged)
0052 
0053     /**
0054      * Whether to use the delegate implicit size or always explicitly apply the
0055      * size hints explicitly.
0056      */
0057     Q_PROPERTY(bool forceDelegateSize READ isDelegateSizeForced WRITE setDelegateSizeForced)
0058 
0059     /// Assume each hierarchy level have the same height (for performance)
0060     Q_PROPERTY(bool uniformRowHeight READ hasUniformRowHeight   WRITE setUniformRowHeight)
0061     /// Assume each column has the same width (for performance)
0062     Q_PROPERTY(bool uniformColumnWidth READ hasUniformColumnWidth WRITE setUniformColumnColumnWidth)
0063 
0064     /**
0065      * It is usually recommanded to use the template constructor unless there is
0066      * extra logic to be executed when an item is created.
0067      */
0068     explicit SingleModelViewBase(ItemFactoryBase *factory, QQuickItem* parent = nullptr);
0069 
0070     template<typename T>
0071     SingleModelViewBase(ItemFactory<T> *b, QQuickItem* parent = nullptr) :
0072         SingleModelViewBase((ItemFactoryBase*) b, parent) {}
0073 
0074     virtual ~SingleModelViewBase();
0075 
0076     QQmlComponent* highlight() const;
0077     void setHighlight(QQmlComponent* h);
0078 
0079     QVariant model() const;
0080     void setModel(const QVariant& m);
0081 
0082     void setDelegate(QQmlComponent* delegate);
0083     QQmlComponent* delegate() const;
0084 
0085     QSharedPointer<QItemSelectionModel> selectionModel() const;
0086     void setSelectionModel(QSharedPointer<QItemSelectionModel> m);
0087 
0088     bool isDelegateSizeForced() const;
0089     void setDelegateSizeForced(bool f);
0090 
0091     QModelIndex currentIndex() const;
0092     Q_INVOKABLE void setCurrentIndex(const QModelIndex& index,
0093         QItemSelectionModel::SelectionFlags f = QItemSelectionModel::ClearAndSelect
0094     );
0095 
0096     bool isSortingEnabled() const;
0097     void setSortingEnabled(bool val);
0098 
0099     bool hasUniformRowHeight() const;
0100     void setUniformRowHeight(bool value);
0101 
0102     bool hasUniformColumnWidth() const;
0103     void setUniformColumnColumnWidth(bool value);
0104 
0105     QModelIndex topLeft    () const;
0106     QModelIndex topRight   () const;
0107     QModelIndex bottomLeft () const;
0108     QModelIndex bottomRight() const;
0109 
0110     Q_INVOKABLE void moveTo(Qt::Edge e);
0111     Q_INVOKABLE QModelIndex indexAt(const QPoint & point) const;
0112     Q_INVOKABLE QRectF itemRect(const QModelIndex& i) const;
0113 
0114 protected Q_SLOTS:
0115 
0116     /**
0117      * Calling `setModel` doesn't do anything that takes effect immediately.
0118      *
0119      * Rather, it will either wait to the next event loop iteration to make
0120      * sure other properties have been applied. If no delegate is set, it will
0121      * also avoid loading the model internal representation because that would
0122      * be useless anyway.
0123      *
0124      * Override this method if extra steps are to be taken when replacing the
0125      * model. Do not forget to call the superclass method.
0126      */
0127     virtual void applyModelChanges(QAbstractItemModel* m);
0128 
0129 protected:
0130     QAbstractItemModel *rawModel() const;
0131 
0132 Q_SIGNALS:
0133     void currentIndexChanged(const QModelIndex& index);
0134     void selectionModelChanged() const;
0135     void modelChanged();
0136     void delegateChanged(QQmlComponent* delegate);
0137     void cornerChanged();
0138 //     virtual void countChanged() override final;
0139 
0140 private:
0141     SingleModelViewBasePrivate* d_ptr;
0142 };
0143 
0144 #endif