Qt 4.8
qdeclarativelistaccessor.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativelistaccessor_p.h"
43 
44 #include <qdeclarativemetatype_p.h>
45 
46 #include <QtCore/qstringlist.h>
47 #include <QtCore/qdebug.h>
48 
49 // ### Remove me
50 #include <qdeclarativeengine_p.h>
51 
53 
55 : m_type(Invalid)
56 {
57 }
58 
60 {
61 }
62 
64 {
65  return d;
66 }
67 
69 {
70  d = v;
71 
72  QDeclarativeEnginePrivate *enginePrivate = engine?QDeclarativeEnginePrivate::get(engine):0;
73 
74  if (!d.isValid()) {
75  m_type = Invalid;
76  } else if (d.userType() == QVariant::StringList) {
78  } else if (d.userType() == QMetaType::QVariantList) {
80  } else if (d.canConvert(QVariant::Int)) {
81  m_type = Integer;
82  } else if ((!enginePrivate && QDeclarativeMetaType::isQObject(d.userType())) ||
83  (enginePrivate && enginePrivate->isQObject(d.userType()))) {
84  QObject *data = enginePrivate?enginePrivate->toQObject(v):QDeclarativeMetaType::toQObject(v);
85  d = QVariant::fromValue(data);
86  m_type = Instance;
87  } else if (d.userType() == qMetaTypeId<QDeclarativeListReference>()) {
89  } else {
90  m_type = Instance;
91  }
92 }
93 
95 {
96  switch(m_type) {
97  case StringList:
98  return qvariant_cast<QStringList>(d).count();
99  case VariantList:
100  return qvariant_cast<QVariantList>(d).count();
101  case ListProperty:
102  return ((QDeclarativeListReference *)d.constData())->count();
103  case Instance:
104  return 1;
105  case Integer:
106  return d.toInt();
107  default:
108  case Invalid:
109  return 0;
110  }
111 }
112 
114 {
115  Q_ASSERT(idx >= 0 && idx < count());
116  switch(m_type) {
117  case StringList:
118  return QVariant::fromValue(qvariant_cast<QStringList>(d).at(idx));
119  case VariantList:
120  return qvariant_cast<QVariantList>(d).at(idx);
121  case ListProperty:
123  case Instance:
124  return d;
125  case Integer:
126  return QVariant(idx);
127  default:
128  case Invalid:
129  return QVariant();
130  }
131 }
132 
134 {
135  return m_type != Invalid;
136 }
137 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QObject * toQObject(const QVariant &, bool *ok=0)
void setList(const QVariant &, QDeclarativeEngine *=0)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QDeclarativeListReference class allows the manipulation of QDeclarativeListProperty properties...
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static QDeclarativeEnginePrivate * get(QDeclarativeEngine *e)
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static const char * data(const QByteArray &arr)
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
The QDeclarativeEngine class provides an environment for instantiating QML components.
const void * constData() const
Definition: qvariant.cpp:3065
T qvariant_cast(const QVariant &)
Definition: qvariant.h:571
QObject * toQObject(const QVariant &, bool *ok=0) const
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485