File indexing completed on 2024-04-28 12:30:04

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_QMODELINDEXBINDER_H
0019 #define KQUICKITEMVIEWS_QMODELINDEXBINDER_H
0020 
0021 #include <QQuickItem>
0022 
0023 class QModelIndexBinderPrivate;
0024 
0025 /**
0026  *
0027  * Bind a children component property with a QModelIndex role.
0028  *
0029  * It is intended to be used within an IndexView.
0030  *
0031  * @todo auto-create as attached properties in IndexView
0032  */
0033 class Q_DECL_EXPORT QModelIndexBinder : public QQuickItem
0034 {
0035     Q_OBJECT
0036 public:
0037 
0038     /**
0039      * Only call setData after `delay` milliseconds.
0040      */
0041     Q_PROPERTY(int delay READ delay WRITE setDelay NOTIFY changed)
0042 
0043     /**
0044      * The name of the role to sychronize.
0045      */
0046     Q_PROPERTY(QString modelRole READ modelRole WRITE setModelRole NOTIFY changed)
0047 
0048     /**
0049      * The (child) object property to bind with the role.
0050      */
0051     Q_PROPERTY(QString objectProperty READ objectProperty WRITE setObjectProperty NOTIFY changed)
0052 
0053     /**
0054      * Automatically call setData when the data is modified.
0055      */
0056     Q_PROPERTY(bool autoSave READ autoSave WRITE setAutoSave NOTIFY changed)
0057 
0058     /**
0059      * If the model data match the local one.
0060      */
0061     Q_PROPERTY(bool synchronized READ isSynchronized NOTIFY changed)
0062 
0063     // The content of the container
0064     Q_PROPERTY(QObject* _object READ _object WRITE _setObject NOTIFY changed)
0065     Q_CLASSINFO("DefaultProperty", "_object")
0066 
0067     explicit QModelIndexBinder(QQuickItem *parent = nullptr);
0068     virtual ~QModelIndexBinder();
0069 
0070     QString modelRole() const;
0071     void setModelRole(const QString& role);
0072 
0073     QString objectProperty() const;
0074     void setObjectProperty(const QString& op);
0075 
0076     QObject *_object() const;
0077     void _setObject(QObject *o);
0078 
0079     int delay() const;
0080     void setDelay(int d);
0081 
0082     bool autoSave() const;
0083     void setAutoSave(bool v);
0084 
0085     bool isSynchronized() const;
0086 
0087     /**
0088      * Reset the local property to match the model data.
0089      */
0090     Q_INVOKABLE void reset() const;
0091 
0092     /**
0093      * Call `setData` now.
0094      */
0095     Q_INVOKABLE bool applyNow() const;
0096 
0097     static QModelIndexBinder *qmlAttachedProperties(QObject *object);
0098 
0099 Q_SIGNALS:
0100     void changed();
0101 
0102 protected:
0103     // This is the attached contructor
0104     QModelIndexBinder(QObject *o);
0105 
0106 private:
0107     QModelIndexBinderPrivate *d_ptr;
0108     Q_DECLARE_PRIVATE(QModelIndexBinder)
0109 };
0110 
0111 QML_DECLARE_TYPEINFO(QModelIndexBinder, QML_HAS_ATTACHED_PROPERTIES)
0112 
0113 #endif