componentview.cpp 8.93 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
** 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.
23
**
hjk's avatar
hjk committed
24 25
** 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

#include "componentview.h"
31
#include "componentaction.h"
hjk's avatar
hjk committed
32
#include <QDebug>
33 34

#include <nodemetainfo.h>
35
#include <nodeabstractproperty.h>
36
#include <QStandardItemModel>
Marco Bubke's avatar
Marco Bubke committed
37
#include <QAbstractItemView>
38 39 40 41 42 43 44 45

// silence gcc warnings about unused parameters

namespace QmlDesigner {

ComponentView::ComponentView(QObject *parent)
  : AbstractView(parent),
    m_standardItemModel(new QStandardItemModel(this)),
46
    m_componentAction(new ComponentAction(this))
47 48 49 50 51
{
}

void ComponentView::nodeAboutToBeRemoved(const ModelNode &removedNode)
{
52
    removeSingleNodeFromList(removedNode);
53
    searchForComponentAndRemoveFromList(removedNode);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

QStandardItemModel *ComponentView::standardItemModel() const
{
    return m_standardItemModel;
}

ModelNode ComponentView::modelNode(int index) const
{
    if (m_standardItemModel->hasIndex(index, 0)) {
        QStandardItem *item = m_standardItemModel->item(index, 0);
        return item->data(ModelNodeRole).value<ModelNode>();
    }

    return ModelNode();
}

71 72 73 74 75
void ComponentView::setComponentNode(const ModelNode &node)
{
    m_componentAction->setCurrentIndex(indexForNode(node));
}

Marco Bubke's avatar
Marco Bubke committed
76 77 78 79 80
QWidget *ComponentView::widget()
{
    return 0;
}

81 82 83 84 85 86 87 88
void ComponentView::removeSingleNodeFromList(const ModelNode &node)
{
    for (int row = 0; row < m_standardItemModel->rowCount(); row++) {
        if (m_standardItemModel->item(row)->data(ModelNodeRole).value<ModelNode>() == node)
            m_standardItemModel->removeRow(row);
    }
}

89 90 91 92 93 94 95 96 97 98

int ComponentView::indexForNode(const ModelNode &node)
{
    for (int row = 0; row < m_standardItemModel->rowCount(); row++) {
        if (m_standardItemModel->item(row)->data(ModelNodeRole).value<ModelNode>() == node)
            return row;
    }
    return -1;
}

99 100 101 102 103
void ComponentView::modelAttached(Model *model)
{
    if (AbstractView::model() == model)
        return;

104 105 106
    bool block = m_componentAction->blockSignals(true);
    m_standardItemModel->clear();

107 108 109
    AbstractView::modelAttached(model);

    searchForComponentAndAddToList(rootModelNode());
110 111

    m_componentAction->blockSignals(block);
112 113 114 115
}

void ComponentView::modelAboutToBeDetached(Model *model)
{
116
    bool block = m_componentAction->blockSignals(true);
117 118
    m_standardItemModel->clear();
    AbstractView::modelAboutToBeDetached(model);
119 120 121 122 123 124
    m_componentAction->blockSignals(block);
}

ComponentAction *ComponentView::action()
{
    return m_componentAction;
125 126 127 128 129 130 131 132 133 134 135 136 137 138
}

void ComponentView::nodeCreated(const ModelNode &createdNode)
{
    searchForComponentAndAddToList(createdNode);
}

void ComponentView::searchForComponentAndAddToList(const ModelNode &node)
{
    QList<ModelNode> nodeList;
    nodeList.append(node);
    nodeList.append(node.allSubModelNodes());


139
    foreach (const ModelNode &node, nodeList) {
140
        if (node.nodeSourceType() == ModelNode::NodeWithComponentSource) {
141 142 143
            if (!node.id().isEmpty()) {
                QStandardItem *item = new QStandardItem(node.id());
                item->setData(QVariant::fromValue(node), ModelNodeRole);
144
                item->setEditable(false);
145
                removeSingleNodeFromList(node); //remove node if already present
146
                m_standardItemModel->appendRow(item);
147 148
            } else {
                QString description;
149
                ModelNode parentNode = node.parentProperty().parentModelNode();
150
                if (parentNode.isValid()) {
151
                    if (parentNode.id().isEmpty())
152
                        description = parentNode.simplifiedTypeName() + QLatin1Char(' ');
153
                    else
154
                        description = parentNode.id() + QLatin1Char(' ');
155
                }
156
                description += node.parentProperty().name();
157
                QStandardItem *item = new QStandardItem(description);
158
                item->setData(QVariant::fromValue(node), ModelNodeRole);
159
                item->setEditable(false);
160
                removeSingleNodeFromList(node); //remove node if already present
161
                m_standardItemModel->appendRow(item);
162
            }
163
        }
164 165 166
    }
}

167
void ComponentView::nodeRemoved(const ModelNode & /* removedNode */, const NodeAbstractProperty & /*parentProperty*/, PropertyChangeFlags /*propertyChange*/)
168
{
169
}
170

171 172 173 174 175 176 177 178
void ComponentView::searchForComponentAndRemoveFromList(const ModelNode &node)
{
    QList<ModelNode> nodeList;
    nodeList.append(node);
    nodeList.append(node.allSubModelNodes());


    foreach (const ModelNode &childNode, nodeList) {
179
        if (childNode.nodeSourceType() == ModelNode::NodeWithComponentSource)
180
            removeSingleNodeFromList(childNode);
181
    }
182 183
}

184
void ComponentView::nodeAboutToBeReparented(const ModelNode &/*node*/, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/) {}
185 186 187 188 189 190

void ComponentView::nodeReparented(const ModelNode &node, const NodeAbstractProperty &/*newPropertyParent*/, const NodeAbstractProperty &/*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
{
    searchForComponentAndAddToList(node);
}

191 192 193 194 195
void ComponentView::nodeIdChanged(const ModelNode& /*node*/, const QString& /*newId*/, const QString& /*oldId*/) {}
void ComponentView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
void ComponentView::propertiesRemoved(const QList<AbstractProperty>& /*propertyList*/) {}
void ComponentView::variantPropertiesChanged(const QList<VariantProperty>& /*propertyList*/, PropertyChangeFlags /*propertyChange*/) {}
void ComponentView::bindingPropertiesChanged(const QList<BindingProperty>& /*propertyList*/, PropertyChangeFlags /*propertyChange*/) {}
196
void ComponentView::rootNodeTypeChanged(const QString &/*type*/, int /*majorVersion*/, int /*minorVersion*/) {}
Marco Bubke's avatar
Marco Bubke committed
197
void ComponentView::scriptFunctionsChanged(const ModelNode &/*node*/, const QStringList &/*scriptFunctionList*/) {}
198
void ComponentView::instancePropertyChange(const QList<QPair<ModelNode, PropertyName> > &/*propertyList*/) {}
199
void ComponentView::instancesCompleted(const QVector<ModelNode> &/*completedNodeList*/) {}
200
void ComponentView::instanceInformationsChange(const QMultiHash<ModelNode, InformationName> &/*informationChangeHash*/) {}
201 202 203
void ComponentView::instancesRenderImageChanged(const QVector<ModelNode> &/*nodeList*/) {}
void ComponentView::instancesPreviewImageChanged(const QVector<ModelNode> &/*nodeList*/) {}
void ComponentView::instancesChildrenChanged(const QVector<ModelNode> &/*nodeList*/) {}
204 205
void ComponentView::instancesToken(const QString &/*tokenName*/, int /*tokenNumber*/, const QVector<ModelNode> &/*nodeVector*/) {}

206
void ComponentView::nodeSourceChanged(const ModelNode &, const QString & /*newNodeSource*/) {}
207

208 209
void ComponentView::rewriterBeginTransaction() {}
void ComponentView::rewriterEndTransaction() {}
210
void ComponentView::actualStateChanged(const ModelNode &/*node*/) {}
211 212 213 214 215
void ComponentView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/,
                                  const QList<ModelNode> &/*lastSelectedNodeList*/) {}

void ComponentView::fileUrlChanged(const QUrl &/*oldUrl*/, const QUrl &/*newUrl*/) {}

216
void ComponentView::nodeOrderChanged(const NodeListProperty &/*listProperty*/, const ModelNode & /*movedNode*/, int /*oldIndex*/) {}
217 218


219
void ComponentView::auxiliaryDataChanged(const ModelNode &/*node*/, const PropertyName &/*name*/, const QVariant &/*data*/) {}
220

Kai Koehne's avatar
Kai Koehne committed
221
void ComponentView::customNotification(const AbstractView * /*view*/, const QString &/*identifier*/, const QList<ModelNode> &/*nodeList*/, const QList<QVariant> &/*data*/) {}
222
void ComponentView::importsChanged(const QList<Import> &/*addedImports*/, const QList<Import> &/*removedImports*/) {}
223 224

} // namespace QmlDesigner