Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / graphicsitems / qdeclarativevisualitemmodel_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEVISUALDATAMODEL_H
43 #define QDECLARATIVEVISUALDATAMODEL_H
44
45 #include <qdeclarative.h>
46
47 #include <QtCore/qobject.h>
48 #include <QtCore/qabstractitemmodel.h>
49
50 QT_BEGIN_HEADER
51
52 Q_DECLARE_METATYPE(QModelIndex)
53
54 QT_BEGIN_NAMESPACE
55
56 QT_MODULE(Declarative)
57
58 class QDeclarativeItem;
59 class QDeclarativeComponent;
60 class QDeclarativePackage;
61 class QDeclarativeVisualDataModelPrivate;
62
63 class Q_AUTOTEST_EXPORT QDeclarativeVisualModel : public QObject
64 {
65     Q_OBJECT
66
67     Q_PROPERTY(int count READ count NOTIFY countChanged)
68
69 public:
70     virtual ~QDeclarativeVisualModel() {}
71
72     enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02 };
73     Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag)
74
75     virtual int count() const = 0;
76     virtual bool isValid() const = 0;
77     virtual QDeclarativeItem *item(int index, bool complete=true) = 0;
78     virtual ReleaseFlags release(QDeclarativeItem *item) = 0;
79     virtual bool completePending() const = 0;
80     virtual void completeItem() = 0;
81     virtual QString stringValue(int, const QString &) = 0;
82     virtual void setWatchedRoles(QList<QByteArray> roles) = 0;
83
84     virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const = 0;
85
86 Q_SIGNALS:
87     void countChanged();
88     void itemsInserted(int index, int count);
89     void itemsRemoved(int index, int count);
90     void itemsMoved(int from, int to, int count);
91     void itemsChanged(int index, int count);
92     void modelReset();
93     void createdItem(int index, QDeclarativeItem *item);
94     void destroyingItem(QDeclarativeItem *item);
95
96 protected:
97     QDeclarativeVisualModel(QObjectPrivate &dd, QObject *parent = 0)
98         : QObject(dd, parent) {}
99
100 private:
101     Q_DISABLE_COPY(QDeclarativeVisualModel)
102 };
103
104 class QDeclarativeVisualItemModelAttached;
105 class QDeclarativeVisualItemModelPrivate;
106 class Q_AUTOTEST_EXPORT QDeclarativeVisualItemModel : public QDeclarativeVisualModel
107 {
108     Q_OBJECT
109     Q_DECLARE_PRIVATE(QDeclarativeVisualItemModel)
110
111     Q_PROPERTY(QDeclarativeListProperty<QDeclarativeItem> children READ children NOTIFY childrenChanged DESIGNABLE false)
112     Q_CLASSINFO("DefaultProperty", "children")
113
114 public:
115     QDeclarativeVisualItemModel(QObject *parent=0);
116     virtual ~QDeclarativeVisualItemModel() {}
117
118     virtual int count() const;
119     virtual bool isValid() const;
120     virtual QDeclarativeItem *item(int index, bool complete=true);
121     virtual ReleaseFlags release(QDeclarativeItem *item);
122     virtual bool completePending() const;
123     virtual void completeItem();
124     virtual QString stringValue(int index, const QString &role);
125     virtual void setWatchedRoles(QList<QByteArray>) {}
126
127     virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const;
128
129     QDeclarativeListProperty<QDeclarativeItem> children();
130
131     static QDeclarativeVisualItemModelAttached *qmlAttachedProperties(QObject *obj);
132
133 Q_SIGNALS:
134     void childrenChanged();
135
136 private:
137     Q_DISABLE_COPY(QDeclarativeVisualItemModel)
138 };
139
140
141 class Q_AUTOTEST_EXPORT QDeclarativeVisualDataModel : public QDeclarativeVisualModel
142 {
143     Q_OBJECT
144     Q_DECLARE_PRIVATE(QDeclarativeVisualDataModel)
145
146     Q_PROPERTY(QVariant model READ model WRITE setModel)
147     Q_PROPERTY(QDeclarativeComponent *delegate READ delegate WRITE setDelegate)
148     Q_PROPERTY(QString part READ part WRITE setPart)
149     Q_PROPERTY(QObject *parts READ parts CONSTANT)
150     Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
151     Q_CLASSINFO("DefaultProperty", "delegate")
152 public:
153     QDeclarativeVisualDataModel();
154     QDeclarativeVisualDataModel(QDeclarativeContext *, QObject *parent=0);
155     virtual ~QDeclarativeVisualDataModel();
156
157     QVariant model() const;
158     void setModel(const QVariant &);
159
160     QDeclarativeComponent *delegate() const;
161     void setDelegate(QDeclarativeComponent *);
162
163     QVariant rootIndex() const;
164     void setRootIndex(const QVariant &root);
165
166     Q_INVOKABLE QVariant modelIndex(int idx) const;
167     Q_INVOKABLE QVariant parentModelIndex() const;
168
169     QString part() const;
170     void setPart(const QString &);
171
172     int count() const;
173     bool isValid() const { return delegate() != 0; }
174     QDeclarativeItem *item(int index, bool complete=true);
175     QDeclarativeItem *item(int index, const QByteArray &, bool complete=true);
176     ReleaseFlags release(QDeclarativeItem *item);
177     bool completePending() const;
178     void completeItem();
179     virtual QString stringValue(int index, const QString &role);
180     virtual void setWatchedRoles(QList<QByteArray> roles);
181
182     int indexOf(QDeclarativeItem *item, QObject *objectContext) const;
183
184     QObject *parts();
185
186 Q_SIGNALS:
187     void createdPackage(int index, QDeclarativePackage *package);
188     void destroyingPackage(QDeclarativePackage *package);
189     void rootIndexChanged();
190
191 private Q_SLOTS:
192     void _q_itemsChanged(int, int, const QList<int> &);
193     void _q_itemsInserted(int index, int count);
194     void _q_itemsRemoved(int index, int count);
195     void _q_itemsMoved(int from, int to, int count);
196     void _q_rowsInserted(const QModelIndex &,int,int);
197     void _q_rowsRemoved(const QModelIndex &,int,int);
198     void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
199     void _q_dataChanged(const QModelIndex&,const QModelIndex&);
200     void _q_layoutChanged();
201     void _q_modelReset();
202     void _q_createdPackage(int index, QDeclarativePackage *package);
203     void _q_destroyingPackage(QDeclarativePackage *package);
204
205 private:
206     Q_DISABLE_COPY(QDeclarativeVisualDataModel)
207 };
208
209 class QDeclarativeVisualItemModelAttached : public QObject
210 {
211     Q_OBJECT
212
213 public:
214     QDeclarativeVisualItemModelAttached(QObject *parent)
215         : QObject(parent), m_index(0) {}
216     ~QDeclarativeVisualItemModelAttached() {
217         attachedProperties.remove(parent());
218     }
219
220     Q_PROPERTY(int index READ index NOTIFY indexChanged)
221     int index() const { return m_index; }
222     void setIndex(int idx) {
223         if (m_index != idx) {
224             m_index = idx;
225             emit indexChanged();
226         }
227     }
228
229     static QDeclarativeVisualItemModelAttached *properties(QObject *obj) {
230         QDeclarativeVisualItemModelAttached *rv = attachedProperties.value(obj);
231         if (!rv) {
232             rv = new QDeclarativeVisualItemModelAttached(obj);
233             attachedProperties.insert(obj, rv);
234         }
235         return rv;
236     }
237
238 Q_SIGNALS:
239     void indexChanged();
240
241 public:
242     int m_index;
243
244     static QHash<QObject*, QDeclarativeVisualItemModelAttached*> attachedProperties;
245 };
246
247
248 QT_END_NAMESPACE
249
250 QML_DECLARE_TYPE(QDeclarativeVisualModel)
251 QML_DECLARE_TYPE(QDeclarativeVisualItemModel)
252 QML_DECLARE_TYPEINFO(QDeclarativeVisualItemModel, QML_HAS_ATTACHED_PROPERTIES)
253 QML_DECLARE_TYPE(QDeclarativeVisualDataModel)
254
255 QT_END_HEADER
256
257 #endif // QDECLARATIVEVISUALDATAMODEL_H