propertyeditorvalue.cpp 11 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "propertyeditorvalue.h"
#include <abstractview.h>
#include <nodeabstractproperty.h>
#include <nodeproperty.h>
#include <model.h>
#include <nodemetainfo.h>
#include <metainfo.h>
#include <propertymetainfo.h>
#include <nodeproperty.h>
#include <qmlobjectnode.h>

//using namespace QmlDesigner;

PropertyEditorValue::PropertyEditorValue(QObject *parent)
   : QObject(parent),
   m_isInSubState(false),
   m_isInModel(false),
   m_isBound(false),
   m_isValid(false),
   m_complexNode(new PropertyEditorNodeWrapper(this))
{
}

QVariant PropertyEditorValue::value() const
{
    QVariant returnValue = m_value;
Marco Bubke's avatar
Marco Bubke committed
56 57
    if (modelNode().isValid() && modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name()))
        if (modelNode().metaInfo().propertyType(name()) == QLatin1String("QUrl")) {
58 59 60 61 62 63 64 65
        returnValue = returnValue.toUrl().toString();
    }
    return returnValue;
}

static bool cleverDoubleCompare(QVariant value1, QVariant value2)
{ //we ignore slight changes on doubles
    if ((value1.type() == QVariant::Double) && (value2.type() == QVariant::Double)) {
66 67
        int a = value1.toDouble() * 100;
        int b = value2.toDouble() * 100;
68

69 70
        if (qFuzzyCompare((qreal(a) / 100), (qreal(b) / 100))) {
            return true;
71
        }
72
    }
73 74 75
    return false;
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static bool cleverColorCompare(QVariant value1, QVariant value2)
{
    if ((value1.type() == QVariant::Color) && (value2.type() == QVariant::Color)) {
        QColor c1 = value1.value<QColor>();
        QColor c2 = value2.value<QColor>();
        QString a = c1.name();
        QString b = c2.name();
        if (a != b)
            return false;
        return (c1.alpha() == c2.alpha());
    }
    if ((value1.type() == QVariant::String) && (value2.type() == QVariant::Color)) {
        return cleverColorCompare(QVariant(QColor(value1.toString())), value2);
    }
    if ((value1.type() == QVariant::Color) && (value2.type() == QVariant::String)) {
        return cleverColorCompare(value1, QVariant(QColor(value2.toString())));
    }
    return false;
}

96 97
void PropertyEditorValue::setValueWithEmit(const QVariant &value)
{
98
    if (m_value != value) {
99
        QVariant newValue = value;
Marco Bubke's avatar
Marco Bubke committed
100 101
        if (modelNode().isValid() && modelNode().metaInfo().isValid() && modelNode().metaInfo().hasProperty(name()))
            if (modelNode().metaInfo().propertyType(name()) == QLatin1String("QUrl")) {
102 103 104 105 106
            newValue = QUrl(newValue.toString());
        }

        if (cleverDoubleCompare(newValue, m_value))
            return;
107 108
        if (cleverColorCompare(newValue, m_value))
            return;
109 110
        setValue(newValue);
        m_isBound = false;
111
        emit valueChanged(name(), value);
112
        emit valueChangedQml();
113 114 115 116 117 118
        emit isBoundChanged();
    }
}

void PropertyEditorValue::setValue(const QVariant &value)
{
119 120 121 122 123 124
    if ((m_value != value) &&
        !cleverDoubleCompare(value, m_value) &&
        !cleverColorCompare(value, m_value))

        m_value = value;

125 126
    if (m_value.isValid())
        emit valueChangedQml();
127
    emit isBoundChanged();
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

QString PropertyEditorValue::expression() const
{

    return m_expression;
}

void PropertyEditorValue::setExpressionWithEmit(const QString &expression)
{
    if ( m_expression != expression) {
        setExpression(expression);
        emit expressionChanged(name());
    }
}

void PropertyEditorValue::setExpression(const QString &expression)
{
    if ( m_expression != expression) {
        m_expression = expression;
149
        emit expressionChanged(QString());
150 151 152 153 154
    }
}

bool PropertyEditorValue::isInSubState() const
{
155 156
    const QmlDesigner::QmlObjectNode objectNode(modelNode());
    return objectNode.isValid() && objectNode.propertyAffectedByCurrentState(name());
157 158 159 160
}

bool PropertyEditorValue::isBound() const
{
161 162
    const QmlDesigner::QmlObjectNode objectNode(modelNode());
    return objectNode.isValid() && objectNode.hasBindingProperty(name());
163 164 165 166
}

bool PropertyEditorValue::isInModel() const
{
167
    return modelNode().isValid() && modelNode().hasProperty(name());
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

QString PropertyEditorValue::name() const
{
    return m_name;
}

void PropertyEditorValue::setName(const QString &name)
{
    m_name = name;
}


bool PropertyEditorValue::isValid() const
{
183
    return m_isValid;
184 185 186 187 188 189 190
}

void PropertyEditorValue::setIsValid(bool valid)
{
    m_isValid = valid;
}

191
QmlDesigner::ModelNode PropertyEditorValue::modelNode() const
192 193 194 195
{
    return m_modelNode;
}

196
void PropertyEditorValue::setModelNode(const QmlDesigner::ModelNode &modelNode)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
    if (modelNode != m_modelNode) {
        m_modelNode = modelNode;
        m_complexNode->update();
        emit modelNodeChanged();
    }
}

PropertyEditorNodeWrapper* PropertyEditorValue::complexNode()
{
    return m_complexNode;
}

void PropertyEditorValue::resetValue()
{
212 213
    if (m_value.isValid() || isBound()) {
        m_value = QVariant();
214
        m_isBound = false;
215
        emit valueChanged(name(), QVariant());
216 217 218
    }
}

219 220
void PropertyEditorValue::registerDeclarativeTypes()
{
Roberto Raggi's avatar
Roberto Raggi committed
221 222 223
    qmlRegisterType<PropertyEditorValue>("Bauhaus",1,0,"PropertyEditorValue");
    qmlRegisterType<PropertyEditorNodeWrapper>("Bauhaus",1,0,"PropertyEditorNodeWrapper");
    qmlRegisterType<QDeclarativePropertyMap>("Bauhaus",1,0,"QDeclarativePropertyMap");
224 225
}

226
PropertyEditorNodeWrapper::PropertyEditorNodeWrapper(PropertyEditorValue* parent) : QObject(parent), m_valuesPropertyMap(this)
227 228 229 230 231
{
    m_editorValue = parent;
    connect(m_editorValue, SIGNAL(modelNodeChanged()), this, SLOT(update()));
}

232
PropertyEditorNodeWrapper::PropertyEditorNodeWrapper(QObject *parent) : QObject(parent), m_editorValue(NULL)
233 234 235 236 237 238 239 240 241 242 243 244 245
{
}

bool PropertyEditorNodeWrapper::exists()
{
    if (!(m_editorValue && m_editorValue->modelNode().isValid()))
        return false;

    return m_modelNode.isValid();
}

QString PropertyEditorNodeWrapper::type()
{
246
    if (!(m_modelNode.isValid()))
247 248
        return QString("");

249
    return m_modelNode.simplifiedTypeName();
250 251 252

}

253
QmlDesigner::ModelNode PropertyEditorNodeWrapper::parentModelNode() const
254 255 256 257 258 259 260 261 262
{
    return  m_editorValue->modelNode();
}

QString PropertyEditorNodeWrapper::propertyName() const
{
    return m_editorValue->name();
}

263
QDeclarativePropertyMap* PropertyEditorNodeWrapper::properties()
264
{
265
    return &m_valuesPropertyMap;
266 267 268 269 270 271 272 273
}

void PropertyEditorNodeWrapper::add(const QString &type)
{

    QString propertyType = type;

    if ((m_editorValue && m_editorValue->modelNode().isValid())) {
274
        if (propertyType.isEmpty())
Marco Bubke's avatar
Marco Bubke committed
275
            propertyType = m_editorValue->modelNode().metaInfo().propertyType(m_editorValue->name());
276 277
        while (propertyType.contains('*')) //strip star
            propertyType.chop(1);
278
        m_modelNode = m_editorValue->modelNode().view()->createModelNode(propertyType, 4, 7);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        m_editorValue->modelNode().nodeAbstractProperty(m_editorValue->name()).reparentHere(m_modelNode);
        if (!m_modelNode.isValid()) {
            qWarning("PropertyEditorNodeWrapper::add failed");
        }
    } else {
        qWarning("PropertyEditorNodeWrapper::add failed - node invalid");
    }
    setup();
}

void PropertyEditorNodeWrapper::remove()
{
    if ((m_editorValue && m_editorValue->modelNode().isValid())) {
        if (QmlDesigner::QmlObjectNode(m_modelNode).isValid())
            QmlDesigner::QmlObjectNode(m_modelNode).destroy();
        m_editorValue->modelNode().removeProperty(m_editorValue->name());
    } else {
        qWarning("PropertyEditorNodeWrapper::remove failed - node invalid");
    }
    m_modelNode = QmlDesigner::ModelNode();

    foreach (const QString &propertyName, m_valuesPropertyMap.keys())
        m_valuesPropertyMap.clear(propertyName);
    foreach (QObject *object, m_valuesPropertyMap.children())
        delete object;
    emit propertiesChanged();
    emit existsChanged();
}

void PropertyEditorNodeWrapper::changeValue(const QString &name)
{
310 311
    if (name.isNull())
        return;
312 313 314 315 316 317
    if (m_modelNode.isValid()) {
        QmlDesigner::QmlObjectNode fxObjectNode(m_modelNode);

        PropertyEditorValue *valueObject = qvariant_cast<PropertyEditorValue *>(m_valuesPropertyMap.value(name));

        if (valueObject->value().isValid())
318
            fxObjectNode.setVariantProperty(name, valueObject->value());
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        else
            fxObjectNode.removeVariantProperty(name);
    }
}

void PropertyEditorNodeWrapper::setup()
{
    Q_ASSERT(m_editorValue);
    Q_ASSERT(m_editorValue->modelNode().isValid());
    if ((m_editorValue->modelNode().isValid() && m_modelNode.isValid())) {
        QmlDesigner::QmlObjectNode fxObjectNode(m_modelNode);
        foreach ( const QString &propertyName, m_valuesPropertyMap.keys())
            m_valuesPropertyMap.clear(propertyName);
        foreach (QObject *object, m_valuesPropertyMap.children())
            delete object;

Marco Bubke's avatar
Marco Bubke committed
335
        foreach (const QString &propertyName, m_modelNode.metaInfo().propertyNames()) {
336 337 338 339 340 341 342
            if (fxObjectNode.isValid()) {
                PropertyEditorValue *valueObject = new PropertyEditorValue(&m_valuesPropertyMap);
                valueObject->setName(propertyName);
                valueObject->setValue(fxObjectNode.instanceValue(propertyName));
                connect(valueObject, SIGNAL(valueChanged(QString, const QVariant&)), &m_valuesPropertyMap, SIGNAL(valueChanged(QString, const QVariant&)));
                m_valuesPropertyMap.insert(propertyName, QVariant::fromValue(valueObject));
            }
343 344
        }
    }
345
    connect(&m_valuesPropertyMap, SIGNAL(valueChanged(const QString &, const QVariant&)), this, SLOT(changeValue(const QString&)));
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

    emit propertiesChanged();
    emit existsChanged();
}

void PropertyEditorNodeWrapper::update()
{
    if (m_editorValue && m_editorValue->modelNode().isValid()) {
        if (m_editorValue->modelNode().hasProperty(m_editorValue->name()) && m_editorValue->modelNode().property(m_editorValue->name()).isNodeProperty()) {
            m_modelNode = m_editorValue->modelNode().nodeProperty(m_editorValue->name()).modelNode();
        }
        setup();
        emit existsChanged();
        emit typeChanged();
    }
}
362