Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickvisualadaptormodel_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKVISUALADAPTORMODEL_P_H
43 #define QQUICKVISUALADAPTORMODEL_P_H
44
45 #include <QtCore/qabstractitemmodel.h>
46
47 #include "private/qquicklistaccessor_p.h"
48
49 #include <private/qqmlguard_p.h>
50
51 QT_BEGIN_HEADER
52
53 QT_BEGIN_NAMESPACE
54
55 class QQmlEngine;
56
57 class QQuickVisualDataModel;
58 class QQuickVisualDataModelItem;
59 class QQuickVisualDataModelItemMetaType;
60
61 class QQuickVisualAdaptorModel : public QQmlGuard<QObject>
62 {
63 public:
64     class Accessors
65     {
66     public:
67         inline Accessors() {}
68         virtual int count(const QQuickVisualAdaptorModel &) const { return 0; }
69         virtual void cleanup(QQuickVisualAdaptorModel &, QQuickVisualDataModel * = 0) const {}
70
71         virtual QVariant value(const QQuickVisualAdaptorModel &, int, const QString &) const {
72             return QVariant(); }
73
74         virtual QQuickVisualDataModelItem *createItem(
75                 QQuickVisualAdaptorModel &,
76                 QQuickVisualDataModelItemMetaType *,
77                 QQmlEngine *,
78                 int) const { return 0; }
79
80         virtual bool notify(
81                 const QQuickVisualAdaptorModel &,
82                 const QList<QQuickVisualDataModelItem *> &,
83                 int,
84                 int,
85                 const QVector<int> &) const { return false; }
86         virtual void replaceWatchedRoles(
87                 QQuickVisualAdaptorModel &,
88                 const QList<QByteArray> &,
89                 const QList<QByteArray> &) const {}
90         virtual QVariant parentModelIndex(const QQuickVisualAdaptorModel &) const {
91             return QVariant(); }
92         virtual QVariant modelIndex(const QQuickVisualAdaptorModel &, int) const {
93             return QVariant(); }
94         virtual bool canFetchMore(const QQuickVisualAdaptorModel &) const { return false; }
95         virtual void fetchMore(QQuickVisualAdaptorModel &) const {}
96     };
97
98     const Accessors *accessors;
99     QPersistentModelIndex rootIndex;
100     QQuickListAccessor list;
101
102     QQuickVisualAdaptorModel();
103     ~QQuickVisualAdaptorModel();
104
105     inline QVariant model() const { return list.list(); }
106     void setModel(const QVariant &variant, QQuickVisualDataModel *vdm, QQmlEngine *engine);
107     void invalidateModel(QQuickVisualDataModel *vdm);
108
109     bool isValid() const;
110
111     inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); }
112     inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); }
113
114     inline int count() const { return qMax(0, accessors->count(*this)); }
115     inline QVariant value(int index, const QString &role) const {
116         return accessors->value(*this, index, role); }
117     inline QQuickVisualDataModelItem *createItem(QQuickVisualDataModelItemMetaType *metaType, QQmlEngine *engine, int index) {
118         return accessors->createItem(*this, metaType, engine, index); }
119     inline bool hasProxyObject() const {
120         return list.type() == QQuickListAccessor::Instance || list.type() == QQuickListAccessor::ListProperty; }
121
122     inline bool notify(
123             const QList<QQuickVisualDataModelItem *> &items,
124             int index,
125             int count,
126             const QVector<int> &roles) const {
127         return accessors->notify(*this, items, index, count, roles); }
128     inline void replaceWatchedRoles(
129             const QList<QByteArray> &oldRoles, const QList<QByteArray> &newRoles) {
130         accessors->replaceWatchedRoles(*this, oldRoles, newRoles); }
131
132     inline QVariant modelIndex(int index) const { return accessors->modelIndex(*this, index); }
133     inline QVariant parentModelIndex() const { return accessors->parentModelIndex(*this); }
134     inline bool canFetchMore() const { return accessors->canFetchMore(*this); }
135     inline void fetchMore() { return accessors->fetchMore(*this); }
136
137 protected:
138     void objectDestroyed(QObject *);
139 };
140
141 class QQuickVisualAdaptorModelProxyInterface
142 {
143 public:
144     virtual ~QQuickVisualAdaptorModelProxyInterface() {}
145
146     virtual QObject *proxiedObject() = 0;
147 };
148
149 #define QQuickVisualAdaptorModelProxyInterface_iid "org.qt-project.Qt.QQuickVisualAdaptorModelProxyInterface"
150
151 Q_DECLARE_INTERFACE(QQuickVisualAdaptorModelProxyInterface, QQuickVisualAdaptorModelProxyInterface_iid)
152
153 QT_END_NAMESPACE
154
155 #endif