abstractview.cpp 15.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42

#include "abstractview.h"

#include "model.h"
#include "model_p.h"
#include "internalnode_p.h"
#include <qmlmodelview.h>

namespace QmlDesigner {


/*!
\class QmlDesigner::AbstractView
\ingroup CoreModel
43 44
\brief The AbstractView class provides an abstract interface that views and
editors can implement to be notified about model changes.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

\see QmlDesigner::WidgetQueryView, QmlDesigner::NodeInstanceView
*/

AbstractView::~AbstractView()
{
    if (m_model)
        m_model.data()->detachView(this, Model::DoNotNotifyView);
}

/*!
\brief sets the view of the model. this is handled automatically by AbstractView::modelAttached.
\param model new Model
*/
void AbstractView::setModel(Model *model)
{
    Q_ASSERT(model != 0);
    if (model == m_model.data())
        return;

    if (m_model)
        m_model.data()->detachView(this);

    m_model = model;
}

RewriterTransaction AbstractView::beginRewriterTransaction()
{
    return RewriterTransaction(this);
}

76
ModelNode AbstractView::createModelNode(const TypeName &typeName,
77 78
                            int majorVersion,
                            int minorVersion,
79 80
                            const QList<QPair<PropertyName, QVariant> > &propertyList,
                            const QList<QPair<PropertyName, QVariant> > &auxPropertyList,
81 82
                            const QString &nodeSource,
                            ModelNode::NodeSourceType nodeSourceType)
83
{
84
    return ModelNode(model()->d->createNode(typeName, majorVersion, minorVersion, propertyList, auxPropertyList, nodeSource, nodeSourceType), model(), this);
85 86 87 88 89 90 91 92 93 94 95
}


/*! \brief returns the root model node
\return constant root model node

*/

const ModelNode AbstractView::rootModelNode() const
{
    Q_ASSERT(model());
hjk's avatar
hjk committed
96
    return ModelNode(model()->d->rootNode(), model(), const_cast<AbstractView*>(this));
97 98 99 100 101 102 103 104 105 106 107
}


/*! \brief returns the root model node
\return root model node

*/

ModelNode AbstractView::rootModelNode()
{
    Q_ASSERT(model());
hjk's avatar
hjk committed
108
    return  ModelNode(model()->d->rootNode(), model(), this);
109 110 111 112 113 114 115 116 117 118 119
}

/*!
\brief sets the reference to model to a null pointer

*/
void AbstractView::removeModel()
{
    m_model.clear();
}

120
WidgetInfo AbstractView::createWidgetInfo(QWidget *widget,
121
                                          WidgetInfo::ToolBarWidgetFactoryInterface *toolBarWidgetFactory,
122 123 124 125 126 127 128 129
                                          const QString &uniqueId,
                                          WidgetInfo::PlacementHint placementHint,
                                          int placementPriority,
                                          const QString &tabName)
{
    WidgetInfo widgetInfo;

    widgetInfo.widget = widget;
130
    widgetInfo.toolBarWidgetFactory = toolBarWidgetFactory;
131 132 133 134 135 136 137 138
    widgetInfo.uniqueId = uniqueId;
    widgetInfo.placementHint = placementHint;
    widgetInfo.placementPriority = placementPriority;
    widgetInfo.tabName = tabName;

    return widgetInfo;
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152
/*!
\name Model related functions
\{
*/

/*!
\brief returns the model
\return the model of the view
*/
Model* AbstractView::model() const
{
    return m_model.data();
}

Marco Bubke's avatar
Marco Bubke committed
153 154 155 156 157
bool AbstractView::isAttached() const
{
    return model();
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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
/*!
\brief is called if a view is being attached to a model
\param model which is being attached
The default implementation is setting the reference of the model to the view.
\see Model::attachView
*/
void AbstractView::modelAttached(Model *model)
{
    setModel(model);
}

/*!
\brief is called before a view is being detached from a model
\param model which is being detached

This is not called if Model::detachViewWithOutNotification is used! The default implementation
is removing the reference to the model from the view.

\see Model::detachView
*/
void AbstractView::modelAboutToBeDetached(Model *)
{
    removeModel();
}

//\}


/*!
\name Property related functions
\{
 */

/*!
\fn void QmlDesigner::AbstractView::propertyAdded(const ModelNode &, const AbstractProperty &)
\brief node notifies about that this property is added
\param node node to which the property is added
\param property added property
*/


/*!
\fn void AbstractView::propertyValueChanged(const ModelNode &, const AbstractProperty& , const QVariant& , const QVariant& )
\brief this notifies about that the value of this proeprty will be changes
\param node node of the property
\param property changed property
\param newValue the variant of the new value
\param oldValue the variant of the old value
*/
//\}

/*!
\name Node related functions
\{
 */

/*!
\fn void AbstractView::nodeCreated(const ModelNode &)
\brief this function is called if a new node was created
\param createdNode created node
*/


/*!
\fn AbstractView::fileUrlChanged(const QUrl &oldBaseUrl, const QUrl &newBaseUrl)
\brief Called when the file url (e.g. needed to to resolve relative paths against) has changed
\param oldBaseUrl old search path
\param newBaseUrl new search path
*/
void AbstractView::fileUrlChanged(const QUrl &/*oldUrl*/, const QUrl &/*newUrl*/)
{
}

/*!
\fn void AbstractView::nodeAboutToBeRemoved(const ModelNode &)
\brief this is called if a node will be removed
\param removedNode to be removed node
*/

/*!
\brief this is called after a propererty was removed
\param propertyList removed property list
*/
void AbstractView::propertiesRemoved(const QList<AbstractProperty>& /*propertyList*/)
{
}

/*!
\fn void AbstractView::nodeReparented(const ModelNode &, const ModelNode &, const ModelNode &)
\brief this is called if a node was reparented
\param node the parent for this node will be changed
\param oldParent old parent of the node
\param newParent new parent of the node
*/

/*!
\fn void QmlDesigner::AbstractView::selectedNodesChanged(const QList< ModelNode > &, const QList< ModelNode > &)
\brief this function is called if the selection was changed
\param selectedNodeList the new selection list
\param lastSelectedNodeList the old selection list
*/
//\}

261
void AbstractView::auxiliaryDataChanged(const ModelNode &/*node*/, const PropertyName &/*name*/, const QVariant &/*data*/)
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
{

}

void AbstractView::customNotification(const AbstractView * /*view*/, const QString & /*identifier*/, const QList<ModelNode> & /*nodeList*/, const QList<QVariant> & /*data*/)
{
}

QList<ModelNode> AbstractView::toModelNodeList(const QList<Internal::InternalNode::Pointer> &nodeList) const
{
    return QmlDesigner::toModelNodeList(nodeList, const_cast<AbstractView*>(this));
}

QList<ModelNode> toModelNodeList(const QList<Internal::InternalNode::Pointer> &nodeList, AbstractView *view)
{
    QList<ModelNode> newNodeList;
278
    foreach (const Internal::InternalNode::Pointer &node, nodeList)
279 280 281 282 283 284 285 286
        newNodeList.append(ModelNode(node, view->model(), view));

    return newNodeList;
}

QList<Internal::InternalNode::Pointer> toInternalNodeList(const QList<ModelNode> &nodeList)
{
    QList<Internal::InternalNode::Pointer> newNodeList;
287
    foreach (const ModelNode &node, nodeList)
288 289 290 291 292 293 294 295 296 297 298
        newNodeList.append(node.internalNode());

    return newNodeList;
}

/*!
\brief set this list nodes to the actual selected nodes
\param focusNodeList list the selected nodes
*/
void AbstractView::setSelectedModelNodes(const QList<ModelNode> &selectedNodeList)
{
hjk's avatar
hjk committed
299
    model()->d->setSelectedNodes(toInternalNodeList(selectedNodeList));
300 301
}

302 303 304 305 306
void AbstractView::setSelectedModelNode(const ModelNode &modelNode)
{
    setSelectedModelNodes(QList<ModelNode>() << modelNode);
}

307 308 309 310 311
/*!
\brief clears the selection
*/
void AbstractView::clearSelectedModelNodes()
{
hjk's avatar
hjk committed
312
    model()->d->clearSelectedNodes();
313 314
}

315 316 317 318 319 320 321 322 323 324
bool AbstractView::hasSelectedModelNodes() const
{
    return !model()->d->selectedNodes().isEmpty();
}

bool AbstractView::hasSingleSelectedModelNode() const
{
    return model()->d->selectedNodes().count() == 1;
}

325 326 327 328 329 330
/*!
\brief set this list nodes to the actual selected nodes
\return list the selected nodes
*/
QList<ModelNode> AbstractView::selectedModelNodes() const
{
hjk's avatar
hjk committed
331
    return toModelNodeList(model()->d->selectedNodes());
332 333
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
ModelNode AbstractView::firstSelectedModelNode() const
{
    if (hasSelectedModelNodes())
        return ModelNode(model()->d->selectedNodes().first(), model(), this);

    return ModelNode();
}

ModelNode AbstractView::singleSelectedModelNode() const
{
    if (hasSingleSelectedModelNode())
        return ModelNode(model()->d->selectedNodes().first(), model(), this);

    return ModelNode();
}

350 351 352 353 354 355
/*!
\brief adds a node to the selection list
\param node to be added to the selection list
*/
void AbstractView::selectModelNode(const ModelNode &node)
{
hjk's avatar
hjk committed
356
    model()->d->selectNode(node.internalNode());
357 358 359 360 361 362 363 364
}

/*!
\brief removes a node from the selection list
\param node to be removed from the selection list
*/
void AbstractView::deselectModelNode(const ModelNode &node)
{
hjk's avatar
hjk committed
365
    model()->d->deselectNode(node.internalNode());
366 367 368 369
}

ModelNode AbstractView::modelNodeForId(const QString &id)
{
hjk's avatar
hjk committed
370
    return ModelNode(model()->d->nodeForId(id), model(), this);
371 372 373 374
}

bool AbstractView::hasId(const QString &id) const
{
hjk's avatar
hjk committed
375
    return model()->d->hasId(id);
376 377
}

378 379
ModelNode AbstractView::modelNodeForInternalId(qint32 internalId)
{
hjk's avatar
hjk committed
380
     return ModelNode(model()->d->nodeForInternalId(internalId), model(), this);
381 382
}

383
bool AbstractView::hasModelNodeForInternalId(qint32 internalId) const
384
{
hjk's avatar
hjk committed
385
    return model()->d->hasNodeForInternalId(internalId);
386 387
}

388 389 390 391 392
QmlModelView *AbstractView::toQmlModelView()
{
    return qobject_cast<QmlModelView*>(this);
}

393 394
NodeInstanceView *AbstractView::nodeInstanceView() const
{
395
    if (model())
hjk's avatar
hjk committed
396
        return model()->d->nodeInstanceView();
397
    else
398 399 400
        return 0;
}

401 402
RewriterView *AbstractView::rewriterView() const
{
403
    if (model())
hjk's avatar
hjk committed
404
        return model()->d->rewriterView();
405
    else
406 407 408
        return 0;
}

409 410 411 412 413 414 415 416 417 418
void AbstractView::resetView()
{
    if (!model())
        return;
    Model *currentModel = model();

    currentModel->detachView(this);
    currentModel->attachView(this);
}

419 420 421 422 423 424 425 426 427 428
bool AbstractView::hasWidget() const
{
    return false;
}

WidgetInfo AbstractView::widgetInfo()
{
    return createWidgetInfo();
}

Marco Bubke's avatar
Marco Bubke committed
429
QList<ModelNode> AbstractView::allModelNodes() const
430
{
hjk's avatar
hjk committed
431
   return toModelNodeList(model()->d->allNodes());
432 433 434 435 436 437 438 439 440 441 442 443 444 445
}

void AbstractView::emitCustomNotification(const QString &identifier)
{
    emitCustomNotification(identifier, QList<ModelNode>());
}

void AbstractView::emitCustomNotification(const QString &identifier, const QList<ModelNode> &nodeList)
{
    emitCustomNotification(identifier, nodeList, QList<QVariant>());
}

void AbstractView::emitCustomNotification(const QString &identifier, const QList<ModelNode> &nodeList, const QList<QVariant> &data)
{
hjk's avatar
hjk committed
446
    model()->d->notifyCustomNotification(this, identifier, nodeList, data);
447 448
}

449
void AbstractView::emitInstancePropertyChange(const QList<QPair<ModelNode, PropertyName> > &propertyList)
450
{
451
    if (model() && nodeInstanceView() == this)
hjk's avatar
hjk committed
452
        model()->d->notifyInstancePropertyChange(propertyList);
453 454
}

455 456 457
void AbstractView::emitInstancesCompleted(const QVector<ModelNode> &nodeVector)
{
    if (model() && nodeInstanceView() == this)
hjk's avatar
hjk committed
458
        model()->d->notifyInstancesCompleted(nodeVector);
459 460
}

461
void AbstractView::emitInstanceInformationsChange(const QMultiHash<ModelNode, InformationName> &informationChangeHash)
462 463
{
    if (model() && nodeInstanceView() == this)
hjk's avatar
hjk committed
464
        model()->d->notifyInstancesInformationsChange(informationChangeHash);
465 466 467 468 469
}

void AbstractView::emitInstancesRenderImageChanged(const QVector<ModelNode> &nodeVector)
{
    if (model() && nodeInstanceView() == this)
hjk's avatar
hjk committed
470
        model()->d->notifyInstancesRenderImageChanged(nodeVector);
471 472 473 474 475
}

void AbstractView::emitInstancesPreviewImageChanged(const QVector<ModelNode> &nodeVector)
{
    if (model() && nodeInstanceView() == this)
hjk's avatar
hjk committed
476
        model()->d->notifyInstancesPreviewImageChanged(nodeVector);
477 478 479 480 481
}

void AbstractView::emitInstancesChildrenChanged(const QVector<ModelNode> &nodeVector)
{
    if (model() && nodeInstanceView() == this)
hjk's avatar
hjk committed
482
        model()->d->notifyInstancesChildrenChanged(nodeVector);
483 484 485 486 487
}

void AbstractView::emitRewriterBeginTransaction()
{
    if (model())
hjk's avatar
hjk committed
488
        model()->d->notifyRewriterBeginTransaction();
489 490
}

491 492 493 494 495 496 497 498 499
void AbstractView::sendTokenToInstances(const QString &token, int number, const QVector<ModelNode> &nodeVector)
{
    if (nodeInstanceView())
        nodeInstanceView()->sendToken(token, number, nodeVector);
}

void AbstractView::emitInstanceToken(const QString &token, int number, const QVector<ModelNode> &nodeVector)
{
    if (nodeInstanceView())
hjk's avatar
hjk committed
500
        model()->d->notifyInstanceToken(token, number, nodeVector);
501 502
}

503 504 505
void AbstractView::emitRewriterEndTransaction()
{
    if (model())
hjk's avatar
hjk committed
506
        model()->d->notifyRewriterEndTransaction();
507 508
}

Marco Bubke's avatar
Marco Bubke committed
509
void AbstractView::setAcutalStateNode(const ModelNode &node)
510
{
Marco Bubke's avatar
Marco Bubke committed
511
    Internal::WriteLocker locker(m_model.data());
512
    if (model())
hjk's avatar
hjk committed
513
        model()->d->notifyActualStateChanged(node);
514 515
}

516
void AbstractView::changeRootNodeType(const TypeName &type, int majorVersion, int minorVersion)
517 518 519
{
    Internal::WriteLocker locker(m_model.data());

hjk's avatar
hjk committed
520
    m_model.data()->d->changeRootNodeType(type, majorVersion, minorVersion);
521 522
}

Marco Bubke's avatar
Marco Bubke committed
523 524
ModelNode AbstractView::actualStateNode() const
{
525
    if (model())
hjk's avatar
hjk committed
526
        return ModelNode(m_model.data()->d->actualStateNode(), m_model.data(), const_cast<AbstractView*>(this));
527 528

    return ModelNode();
Marco Bubke's avatar
Marco Bubke committed
529 530
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
static int getMajorVersionFromImport(const Model *model)
{
    foreach (const Import &import, model->imports()) {
        if (import.isLibraryImport() && import.url() == QLatin1String("QtQuick")) {
            const QString versionString = import.version();
            if (versionString.contains(QLatin1String("."))) {
                const QString majorVersionString = versionString.split(QLatin1String(".")).first();
                return majorVersionString.toInt();
            }
        }
    }

    return -1;
}

static int getMajorVersionFromNode(const ModelNode &modelNode)
{
    if (modelNode.metaInfo().isValid()) {
        if (modelNode.type() == "QtQuick.QtObject" || modelNode.type() == "QtQuick.Item")
            return modelNode.majorVersion();

        foreach (const NodeMetaInfo &superClass,  modelNode.metaInfo().superClasses()) {
            if (modelNode.type() == "QtQuick.QtObject" || modelNode.type() == "QtQuick.Item")
                return superClass.majorVersion();
        }
    }

    return 1; //default
}

int AbstractView::majorQtQuickVersion() const
{
    int majorVersionFromImport = getMajorVersionFromImport(model());
    if (majorVersionFromImport >= 0)
        return majorVersionFromImport;

    return getMajorVersionFromNode(rootModelNode());
}

570
} // namespace QmlDesigner