src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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  *****************************************************************************
       
    59  XXX Experimental
       
    60  *****************************************************************************
       
    61 *****************************************************************************/
       
    62 
       
    63 class QDeclarativeItem;
       
    64 class QDeclarativeComponent;
       
    65 class QDeclarativePackage;
       
    66 class QDeclarativeVisualDataModelPrivate;
       
    67 
       
    68 class Q_DECLARATIVE_EXPORT QDeclarativeVisualModel : public QObject
       
    69 {
       
    70     Q_OBJECT
       
    71 
       
    72     Q_PROPERTY(int count READ count NOTIFY countChanged)
       
    73 
       
    74 public:
       
    75     QDeclarativeVisualModel(QObject *parent=0) : QObject(parent) {}
       
    76     virtual ~QDeclarativeVisualModel() {}
       
    77 
       
    78     enum ReleaseFlag { Referenced = 0x01, Destroyed = 0x02 };
       
    79     Q_DECLARE_FLAGS(ReleaseFlags, ReleaseFlag)
       
    80 
       
    81     virtual int count() const = 0;
       
    82     virtual bool isValid() const = 0;
       
    83     virtual QDeclarativeItem *item(int index, bool complete=true) = 0;
       
    84     virtual ReleaseFlags release(QDeclarativeItem *item) = 0;
       
    85     virtual bool completePending() const = 0;
       
    86     virtual void completeItem() = 0;
       
    87     virtual QVariant evaluate(int index, const QString &expression, QObject *objectContext) = 0;
       
    88     virtual QString stringValue(int, const QString &) { return QString(); }
       
    89 
       
    90     virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const = 0;
       
    91 
       
    92 Q_SIGNALS:
       
    93     void countChanged();
       
    94     void itemsInserted(int index, int count);
       
    95     void itemsRemoved(int index, int count);
       
    96     void itemsMoved(int from, int to, int count);
       
    97     void modelReset();
       
    98     void createdItem(int index, QDeclarativeItem *item);
       
    99     void destroyingItem(QDeclarativeItem *item);
       
   100 
       
   101 protected:
       
   102     QDeclarativeVisualModel(QObjectPrivate &dd, QObject *parent = 0)
       
   103         : QObject(dd, parent) {}
       
   104 
       
   105 private:
       
   106     Q_DISABLE_COPY(QDeclarativeVisualModel)
       
   107 };
       
   108 
       
   109 class QDeclarativeVisualItemModelAttached;
       
   110 class QDeclarativeVisualItemModelPrivate;
       
   111 class Q_DECLARATIVE_EXPORT QDeclarativeVisualItemModel : public QDeclarativeVisualModel
       
   112 {
       
   113     Q_OBJECT
       
   114     Q_DECLARE_PRIVATE(QDeclarativeVisualItemModel)
       
   115 
       
   116     Q_PROPERTY(QDeclarativeListProperty<QDeclarativeItem> children READ children NOTIFY childrenChanged DESIGNABLE false)
       
   117     Q_CLASSINFO("DefaultProperty", "children")
       
   118 
       
   119 public:
       
   120     QDeclarativeVisualItemModel(QObject *parent=0);
       
   121     virtual ~QDeclarativeVisualItemModel() {}
       
   122 
       
   123     virtual int count() const;
       
   124     virtual bool isValid() const;
       
   125     virtual QDeclarativeItem *item(int index, bool complete=true);
       
   126     virtual ReleaseFlags release(QDeclarativeItem *item);
       
   127     virtual bool completePending() const;
       
   128     virtual void completeItem();
       
   129     virtual QString stringValue(int index, const QString &role);
       
   130     virtual QVariant evaluate(int index, const QString &expression, QObject *objectContext);
       
   131 
       
   132     virtual int indexOf(QDeclarativeItem *item, QObject *objectContext) const;
       
   133 
       
   134     QDeclarativeListProperty<QDeclarativeItem> children();
       
   135 
       
   136     static QDeclarativeVisualItemModelAttached *qmlAttachedProperties(QObject *obj);
       
   137 
       
   138 Q_SIGNALS:
       
   139     void childrenChanged();
       
   140 
       
   141 private:
       
   142     Q_DISABLE_COPY(QDeclarativeVisualItemModel)
       
   143 };
       
   144 
       
   145 
       
   146 class Q_DECLARATIVE_EXPORT QDeclarativeVisualDataModel : public QDeclarativeVisualModel
       
   147 {
       
   148     Q_OBJECT
       
   149     Q_DECLARE_PRIVATE(QDeclarativeVisualDataModel)
       
   150 
       
   151     Q_PROPERTY(QVariant model READ model WRITE setModel)
       
   152     Q_PROPERTY(QDeclarativeComponent *delegate READ delegate WRITE setDelegate)
       
   153     Q_PROPERTY(QString part READ part WRITE setPart)
       
   154     Q_PROPERTY(QObject *parts READ parts CONSTANT)
       
   155     Q_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged)
       
   156     Q_CLASSINFO("DefaultProperty", "delegate")
       
   157 public:
       
   158     QDeclarativeVisualDataModel();
       
   159     QDeclarativeVisualDataModel(QDeclarativeContext *, QObject *parent=0);
       
   160     virtual ~QDeclarativeVisualDataModel();
       
   161 
       
   162     QVariant model() const;
       
   163     void setModel(const QVariant &);
       
   164 
       
   165     QDeclarativeComponent *delegate() const;
       
   166     void setDelegate(QDeclarativeComponent *);
       
   167 
       
   168     QVariant rootIndex() const;
       
   169     void setRootIndex(const QVariant &root);
       
   170 
       
   171     Q_INVOKABLE QVariant modelIndex(int idx) const;
       
   172     Q_INVOKABLE QVariant parentModelIndex() const;
       
   173 
       
   174     QString part() const;
       
   175     void setPart(const QString &);
       
   176 
       
   177     int count() const;
       
   178     bool isValid() const { return delegate() != 0; }
       
   179     QDeclarativeItem *item(int index, bool complete=true);
       
   180     QDeclarativeItem *item(int index, const QByteArray &, bool complete=true);
       
   181     ReleaseFlags release(QDeclarativeItem *item);
       
   182     bool completePending() const;
       
   183     void completeItem();
       
   184     virtual QString stringValue(int index, const QString &role);
       
   185     QVariant evaluate(int index, const QString &expression, QObject *objectContext);
       
   186 
       
   187     int indexOf(QDeclarativeItem *item, QObject *objectContext) const;
       
   188 
       
   189     QObject *parts();
       
   190 
       
   191 Q_SIGNALS:
       
   192     void createdPackage(int index, QDeclarativePackage *package);
       
   193     void destroyingPackage(QDeclarativePackage *package);
       
   194     void rootIndexChanged();
       
   195 
       
   196 private Q_SLOTS:
       
   197     void _q_itemsChanged(int, int, const QList<int> &);
       
   198     void _q_itemsInserted(int index, int count);
       
   199     void _q_itemsRemoved(int index, int count);
       
   200     void _q_itemsMoved(int from, int to, int count);
       
   201     void _q_rowsInserted(const QModelIndex &,int,int);
       
   202     void _q_rowsRemoved(const QModelIndex &,int,int);
       
   203     void _q_rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int);
       
   204     void _q_dataChanged(const QModelIndex&,const QModelIndex&);
       
   205     void _q_modelReset();
       
   206     void _q_createdPackage(int index, QDeclarativePackage *package);
       
   207     void _q_destroyingPackage(QDeclarativePackage *package);
       
   208 
       
   209 private:
       
   210     Q_DISABLE_COPY(QDeclarativeVisualDataModel)
       
   211 };
       
   212 
       
   213 class QDeclarativeVisualItemModelAttached : public QObject
       
   214 {
       
   215     Q_OBJECT
       
   216 
       
   217 public:
       
   218     QDeclarativeVisualItemModelAttached(QObject *parent)
       
   219         : QObject(parent), m_index(0) {}
       
   220     ~QDeclarativeVisualItemModelAttached() {
       
   221         attachedProperties.remove(parent());
       
   222     }
       
   223 
       
   224     Q_PROPERTY(int index READ index NOTIFY indexChanged)
       
   225     int index() const { return m_index; }
       
   226     void setIndex(int idx) {
       
   227         if (m_index != idx) {
       
   228             m_index = idx;
       
   229             emit indexChanged();
       
   230         }
       
   231     }
       
   232 
       
   233     static QDeclarativeVisualItemModelAttached *properties(QObject *obj) {
       
   234         QDeclarativeVisualItemModelAttached *rv = attachedProperties.value(obj);
       
   235         if (!rv) {
       
   236             rv = new QDeclarativeVisualItemModelAttached(obj);
       
   237             attachedProperties.insert(obj, rv);
       
   238         }
       
   239         return rv;
       
   240     }
       
   241 
       
   242 Q_SIGNALS:
       
   243     void indexChanged();
       
   244 
       
   245 public:
       
   246     int m_index;
       
   247 
       
   248     static QHash<QObject*, QDeclarativeVisualItemModelAttached*> attachedProperties;
       
   249 };
       
   250 
       
   251 
       
   252 QT_END_NAMESPACE
       
   253 
       
   254 QML_DECLARE_TYPE(QDeclarativeVisualModel)
       
   255 QML_DECLARE_TYPE(QDeclarativeVisualItemModel)
       
   256 QML_DECLARE_TYPEINFO(QDeclarativeVisualItemModel, QML_HAS_ATTACHED_PROPERTIES)
       
   257 QML_DECLARE_TYPE(QDeclarativeVisualDataModel)
       
   258 
       
   259 QT_END_HEADER
       
   260 
       
   261 #endif // QDECLARATIVEVISUALDATAMODEL_H