qmlmodelview.cpp 15.2 KB
Newer Older
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 7 8
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
con's avatar
con committed
9
** No Commercial Usage
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
15 16 17 18 19 20 21 22 23 24
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
31 32 33 34 35 36 37 38
**
**************************************************************************/

#include "qmlmodelview.h"
#include "qmlobjectnode.h"
#include "qmlitemnode.h"
#include "itemlibraryinfo.h"
#include "mathutils.h"
39
#include "invalididexception.h"
40 41 42
#include <QDir>
#include <QFileInfo>
#include <QDebug>
43
#include <QMessageBox>
44 45
#include "nodeabstractproperty.h"
#include "variantproperty.h"
46

47

48 49 50
namespace QmlDesigner {

QmlModelView::QmlModelView(QObject *parent)
51
    : AbstractView(parent)
52 53 54 55 56
{
}

void QmlModelView::setCurrentState(const QmlModelState &state)
{
57
    if (!state.isValid())
58
        return;
59

60
    emitActualStateChanged(state.modelNode());
61 62 63 64 65 66 67 68 69 70 71 72
}

QmlModelState QmlModelView::currentState() const
{
    return m_state;
}

QmlModelState QmlModelView::baseState() const
{
    return QmlModelState::createBaseState(this);
}

73 74 75 76 77
QmlModelStateGroup QmlModelView::rootStateGroup() const
{
    return QmlModelStateGroup(rootModelNode());
}

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
QmlObjectNode QmlModelView::createQmlObjectNode(const QString &typeString,
                     int majorVersion,
                     int minorVersion,
                     const PropertyListType &propertyList)
{
    return QmlObjectNode(createModelNode(typeString, majorVersion, minorVersion, propertyList));
}

QmlItemNode QmlModelView::createQmlItemNode(const QString &typeString,
                                int majorVersion,
                                int minorVersion,
                                const PropertyListType &propertyList)
{
    return createQmlObjectNode(typeString, majorVersion, minorVersion, propertyList).toQmlItemNode();
}

QmlItemNode QmlModelView::createQmlItemNodeFromImage(const QString &imageName, const QPointF &position, QmlItemNode parentNode)
{
    if (!parentNode.isValid())
        parentNode = rootQmlItemNode();

    if (!parentNode.isValid())
        return QmlItemNode();

    QmlItemNode newNode;
    RewriterTransaction transaction = beginRewriterTransaction();
    {
105 106
        const QString newImportUrl = QLatin1String("Qt");
        const QString newImportVersion = QLatin1String("4.7");
107 108 109 110 111 112 113 114 115 116 117 118 119
        Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);

        foreach (const Import &import, model()->imports()) {
            if (import.isLibraryImport()
                && import.url() == newImport.url()
                && import.version() == newImport.version()) {
                // reuse this import
                newImport = import;
                break;
            }
        }

        if (!model()->imports().contains(newImport)) {
120
            model()->changeImports(QList<Import>() << newImport, QList<Import>());
121 122
        }

123 124 125 126 127 128 129 130 131 132 133 134 135
        QList<QPair<QString, QVariant> > propertyPairList;
        propertyPairList.append(qMakePair(QString("x"), QVariant( round(position.x(), 4))));
        propertyPairList.append(qMakePair(QString("y"), QVariant( round(position.y(), 4))));

        QString relativeImageName = imageName;

        //use relative path
        if (QFileInfo(model()->fileUrl().toLocalFile()).exists()) {
            QDir fileDir(QFileInfo(model()->fileUrl().toLocalFile()).absolutePath());
            relativeImageName = fileDir.relativeFilePath(imageName);
        }

        propertyPairList.append(qMakePair(QString("source"), QVariant(relativeImageName)));
136
        newNode = createQmlItemNode("Qt/Image", 4, 7, propertyPairList);
137 138 139 140 141 142 143
        parentNode.nodeAbstractProperty("data").reparentHere(newNode);

        Q_ASSERT(newNode.isValid());

        QString id;
        int i = 1;
        QString name("image");
144
        name.remove(QLatin1Char(' '));
145 146 147 148 149 150
        do {
            id = name + QString::number(i);
            i++;
        } while (hasId(id)); //If the name already exists count upwards

        newNode.setId(id);
151
        if (!currentState().isBaseState()) {
152 153
            newNode.modelNode().variantProperty("opacity") = 0;
            newNode.setVariantProperty("opacity", 1);
154
        }
155 156 157 158 159 160 161 162 163

        Q_ASSERT(newNode.isValid());
    }

    Q_ASSERT(newNode.isValid());

    return newNode;
}

164
QmlItemNode QmlModelView::createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, const QPointF &position, QmlItemNode parentNode)
165 166 167 168 169 170 171 172 173
{
    if (!parentNode.isValid())
        parentNode = rootQmlItemNode();

    Q_ASSERT(parentNode.isValid());

    QmlItemNode newNode;
    RewriterTransaction transaction = beginRewriterTransaction();
    {
174
        if (itemLibraryEntry.typeName().contains('/')) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
            const QString newImportUrl = itemLibraryEntry.typeName().split('/').first();
            const QString newImportVersion = QString("%1.%2").arg(QString::number(itemLibraryEntry.majorVersion()), QString::number(itemLibraryEntry.minorVersion()));
            Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);

            foreach (const Import &import, model()->imports()) {
                if (import.isLibraryImport()
                    && import.url() == newImport.url()
                    && import.version() == newImport.version()) {
                    // reuse this import
                    newImport = import;
                    break;
                }
            }

            if (!model()->imports().contains(newImport)) {
190
                model()->changeImports(QList<Import>() << newImport, QList<Import>());
191 192 193
            }
        }

194 195 196 197
        QList<QPair<QString, QVariant> > propertyPairList;
        propertyPairList.append(qMakePair(QString("x"), QVariant(round(position.x(), 4))));
        propertyPairList.append(qMakePair(QString("y"), QVariant(round(position.y(), 4))));

198
        foreach (const PropertyContainer &property, itemLibraryEntry.properties())
199 200
            propertyPairList.append(qMakePair(property.name(), property.value()));

201
        newNode = createQmlItemNode(itemLibraryEntry.typeName(), itemLibraryEntry.majorVersion(), itemLibraryEntry.minorVersion(), propertyPairList);
202 203 204
        if (parentNode.hasDefaultProperty()) {
            parentNode.nodeAbstractProperty(parentNode.defaultProperty()).reparentHere(newNode);
        }
205

206 207
        if (!newNode.isValid())
            return newNode;
208 209 210

        QString id;
        int i = 1;
211
        QString name(itemLibraryEntry.name().toLower());
212 213
        //remove forbidden characters
        name.replace(QRegExp(QLatin1String("[^a-zA-Z0-9_]")), QLatin1String("_"));
214 215 216 217 218
        do {
            id = name + QString::number(i);
            i++;
        } while (hasId(id)); //If the name already exists count upwards

219 220 221 222 223 224 225
        try {
            newNode.setId(id);
        } catch (InvalidIdException &e) {
            // should never happen
            QMessageBox::warning(0, tr("Invalid Id"), e.description());
        }

226
        if (!currentState().isBaseState()) {
227 228
            newNode.modelNode().variantProperty("opacity") = 0;
            newNode.setVariantProperty("opacity", 1);
229
        }
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

        Q_ASSERT(newNode.isValid());
    }

    Q_ASSERT(newNode.isValid());

    return newNode;
}

QmlObjectNode QmlModelView::rootQmlObjectNode() const
{
    return QmlObjectNode(rootModelNode());
}

QmlItemNode QmlModelView::rootQmlItemNode() const
{
    return rootQmlObjectNode().toQmlItemNode();
}

void QmlModelView::setSelectedQmlObjectNodes(const QList<QmlObjectNode> &selectedNodeList)
{
    setSelectedModelNodes(QmlDesigner::toModelNodeList(selectedNodeList));
}

void QmlModelView::selectQmlObjectNode(const QmlObjectNode &node)
{
     selectModelNode(node.modelNode());
}

void QmlModelView::deselectQmlObjectNode(const QmlObjectNode &node)
{
    deselectModelNode(node.modelNode());
}

QList<QmlObjectNode> QmlModelView::selectedQmlObjectNodes() const
{
    return toQmlObjectNodeList(selectedModelNodes());
}

QList<QmlItemNode> QmlModelView::selectedQmlItemNodes() const
{
    return toQmlItemNodeList(selectedModelNodes());
}

void QmlModelView::setSelectedQmlItemNodes(const QList<QmlItemNode> &selectedNodeList)
{
    return setSelectedModelNodes(QmlDesigner::toModelNodeList(selectedNodeList));
}

QmlObjectNode QmlModelView::fxObjectNodeForId(const QString &id)
{
    return QmlObjectNode(modelNodeForId(id));
}

NodeInstance QmlModelView::instanceForModelNode(const ModelNode &modelNode)
{
    return nodeInstanceView()->instanceForNode(modelNode);
}

bool QmlModelView::hasInstanceForModelNode(const ModelNode &modelNode)
{
    return nodeInstanceView()->hasInstanceForNode(modelNode);
}

void QmlModelView::modelAttached(Model *model)
{
296
    AbstractView::modelAttached(model);
297 298 299 300 301 302 303 304
    m_state = QmlModelState();
    m_state = baseState();
    Q_ASSERT(m_state.isBaseState());
}

void QmlModelView::modelAboutToBeDetached(Model *model)
{
    m_state = QmlModelState();
305
    AbstractView::modelAboutToBeDetached(model);
306 307
}

308
static bool isTransformProperty(const QString &name)
309
{
310 311 312 313 314 315 316
    static QStringList transformProperties(QStringList() << "x"
                                                         << "y"
                                                         << "width"
                                                         << "height"
                                                         << "z"
                                                         << "rotation"
                                                         << "scale"
317 318
                                                         << "transformOrigin"
                                                         << "paintedWidth"
319 320
                                                         << "paintedHeight"
                                                         << "border.width");
321 322

    return transformProperties.contains(name);
323 324
}

325 326 327 328 329 330 331 332
void QmlModelView::nodeOrderChanged(const NodeListProperty &/*listProperty*/, const ModelNode &/*movedNode*/, int /*oldIndex*/)
{

}

void QmlModelView::nodeCreated(const ModelNode &/*createdNode*/) {}
void QmlModelView::nodeAboutToBeRemoved(const ModelNode &/*removedNode*/) {}
void QmlModelView::nodeRemoved(const ModelNode &/*removedNode*/, const NodeAbstractProperty &/*parentProperty*/, PropertyChangeFlags /*propertyChange*/) {}
333
void QmlModelView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
void QmlModelView::nodeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
void QmlModelView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {}
void QmlModelView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
void QmlModelView::propertiesRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
void QmlModelView::variantPropertiesChanged(const QList<VariantProperty>& /*propertyList*/, PropertyChangeFlags /*propertyChange*/) {}
void QmlModelView::bindingPropertiesChanged(const QList<BindingProperty>& /*propertyList*/, PropertyChangeFlags /*propertyChange*/) {}
void QmlModelView::rootNodeTypeChanged(const QString &/*type*/, int, int /*minorVersion*/) {}
void QmlModelView::scriptFunctionsChanged(const ModelNode &/*node*/, const QStringList &/*scriptFunctionList*/) {}
void QmlModelView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/, const QList<ModelNode> &/*lastSelectedNodeList*/) {}

void QmlModelView::instancePropertyChange(const QList<QPair<ModelNode, QString> > &propertyList)
{
    typedef QPair<ModelNode, QString> ModelNodePropertyPair;
    foreach (const ModelNodePropertyPair &propertyPair, propertyList) {
        nodeInstancePropertyChanged(propertyPair.first, propertyPair.second);
    }
}
351 352 353
void QmlModelView::instancesCompleted(const QVector<ModelNode> &/*completedNodeList*/)
{
}
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
void QmlModelView::instanceInformationsChange(const QVector<ModelNode> &/*nodeList*/)
{

}

void QmlModelView::instancesRenderImageChanged(const QVector<ModelNode> &/*nodeList*/)
{

}

void QmlModelView::instancesPreviewImageChanged(const QVector<ModelNode> &/*nodeList*/)
{

}

void QmlModelView::instancesChildrenChanged(const QVector<ModelNode> &/*nodeList*/)
{

}

375 376 377 378
void QmlModelView::importsChanged(const QList<Import> &/*addedImports*/, const QList<Import> &/*removedImports*/)
{

}
379 380 381 382 383 384 385 386 387 388 389

void QmlModelView::rewriterBeginTransaction()
{

}

void QmlModelView::rewriterEndTransaction()
{

}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
void QmlModelView::actualStateChanged(const ModelNode &node)
{
    QmlModelState newState(node);
    QmlModelState oldState = currentState();

    if (!newState.isValid())
        newState = baseState();

    activateState(newState);

    m_state = newState;

    if (newState != oldState)
        stateChanged(newState, oldState);

}

407
void QmlModelView::nodeInstancePropertyChanged(const ModelNode &node, const QString &propertyName)
408
{
409 410 411 412
    QmlObjectNode qmlObjectNode(node);

    if (!qmlObjectNode.isValid())
        return;
413

414 415 416 417 418 419
    if (isTransformProperty(propertyName))
        transformChanged(qmlObjectNode, propertyName);
    else if (propertyName == "parent")
        parentChanged(qmlObjectNode);
    else
        otherPropertyChanged(qmlObjectNode, propertyName);
420 421
}

422 423 424 425 426 427
void QmlModelView::activateState(const QmlModelState &state)
{
    if (!state.isValid())
        return;

    if (m_state == state)
428 429 430 431
        return;    

    m_state = state; //This is hacky. m_state should be controlled by the instances
                     //### todo: If the state thumbnail code gets refactored.
Tobias Hunger's avatar
Tobias Hunger committed
432
                     //          this is not necessary anymore.
433 434 435


    NodeInstance newStateInstance = instanceForModelNode(state.modelNode());
436

437
    if (state.isBaseState()) {
438
        nodeInstanceView()->activateBaseState();
439
    } else {
440
        nodeInstanceView()->activateState(newStateInstance);
441
    }
Marco Bubke's avatar
Marco Bubke committed
442
}
443

444 445 446 447 448
void QmlModelView::transformChanged(const QmlObjectNode &/*qmlObjectNode*/, const QString &/*propertyName*/)
{
}

void QmlModelView::parentChanged(const QmlObjectNode &/*qmlObjectNode*/)
449 450 451
{
}

452
void QmlModelView::otherPropertyChanged(const QmlObjectNode &/*qmlObjectNode*/, const QString &/*propertyName*/)
453 454 455
{
}

456
void  QmlModelView::stateChanged(const QmlModelState &/*newQmlModelState*/, const QmlModelState &/*oldQmlModelState*/)
457 458 459 460
{
}

} //QmlDesigner