OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / model / variantparser.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
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, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "variantparser.h"
35 #include <private/qdeclarativevaluetype_p.h>
36
37 #include <QPoint>
38 #include <QPointF>
39 #include <QFont>
40 #include <QRect>
41 #include <QRectF>
42 #include <QSize>
43 #include <QSizeF>
44 #include <QVector3D>
45 #include <QEasingCurve>
46 #include <QMetaProperty>
47
48 namespace QmlDesigner {
49 namespace Internal {
50
51 VariantParser::VariantParser(const QVariant &value) : m_valueType(QDeclarativeValueTypeFactory::valueType(value.type()))
52 {
53     if (m_valueType) {
54         m_valueType->setValue(value);
55         if (!m_valueType->value().isValid())
56             qWarning("VariantParser::VariantParser: value not valid");
57     }
58 }
59
60 VariantParser::~VariantParser()
61 {
62     if (m_valueType)
63         delete m_valueType;
64 }
65
66 QVariant VariantParser::value() const
67 {
68     return m_valueType->value();
69 }
70
71 QVariant VariantParser::property(QString name) const
72 {
73     if (!m_valueType)
74         return QVariant();
75     return m_valueType->property(name.toLatin1());
76 }
77
78 bool VariantParser::setProperty(const QString &name, const QVariant &value)
79 {
80     if (!m_valueType)
81         return false;
82     if (name == "pixelSize" && value.toInt() < 1)
83         return false; //this check we have to harcode
84     return m_valueType->setProperty(name.toLatin1(), value);
85 }
86
87 bool VariantParser::isValueType(const QString &type)
88 {
89     return VariantParser::create(type).isValid();
90 }
91
92 QStringList VariantParser::properties()
93 {
94     if (!m_valueType)
95         return QStringList();
96     QStringList propertyList;
97     for (int i=1; i < m_valueType->metaObject()->propertyCount(); i++) {
98         QMetaProperty metaProperty = m_valueType->metaObject()->property(i);
99         propertyList.append(metaProperty.name());
100     }
101     return propertyList;
102 }
103
104 VariantParser VariantParser::create(const QString &type)
105 {
106     if (type == "QFont")
107         return VariantParser(QVariant(QFont()));
108     if (type == "QPoint")
109         return VariantParser(QVariant(QPoint()));
110     if (type == "QPointF")
111         return VariantParser(QVariant(QPointF()));
112     if (type == "QSize")
113         return VariantParser(QVariant(QSize()));
114     if (type == "QSizeF")
115         return VariantParser(QVariant(QSizeF()));
116     if (type == "QRect")
117         return VariantParser(QVariant(QRect()));
118     if (type == "QRectF")
119         return VariantParser(QVariant(QRectF()));
120     if (type == "QVector3D")
121         return VariantParser(QVariant(QVector3D()));
122     if (type == "QEasingCurve")
123         return VariantParser(QVariant(QEasingCurve()));
124
125     return VariantParser(QVariant());
126 }
127
128 void VariantParser::init(const QString &type)
129 {
130     if (type == "QFont")
131         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::Font);
132     if (type == "QPoint")
133         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::Point);
134     if (type == "QPointF")
135         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::PointF);
136     if (type == "QSize")
137         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::Size);
138     if (type == "QSizeF")
139         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::SizeF);
140     if (type == "QRect")
141         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::Rect);
142     if (type == "QRectF")
143         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::RectF);
144     if (type == "QVector3D")
145         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::Vector3D);    
146     if (type == "QEasingCurve")
147         m_valueType  = QDeclarativeValueTypeFactory::valueType(QVariant::EasingCurve);
148 }
149
150 bool VariantParser::isValid()
151 {
152     return m_valueType && m_valueType->value().isValid();
153 }
154
155 } // namespace Internal
156 } // namespace QmlDesigner
157
158