navigatorview.cpp 16.1 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25 26 27 28

#include "navigatorview.h"
#include "navigatortreemodel.h"
#include "navigatorwidget.h"
29
#include "nameitemdelegate.h"
30
#include "iconcheckboxitemdelegate.h"
31 32
#include "qmldesignerconstants.h"
#include "qmldesignericons.h"
33

34
#include <coreplugin/editormanager/editormanager.h>
35
#include <coreplugin/icore.h>
36

37
#include <utils/icon.h>
38

39
#include <bindingproperty.h>
40
#include <designmodecontext.h>
41
#include <nodeproperty.h>
42
#include <nodelistproperty.h>
43
#include <variantproperty.h>
44
#include <QHeaderView>
Marco Bubke's avatar
Marco Bubke committed
45
#include <qmlitemnode.h>
46

47 48
static inline void setScenePos(const QmlDesigner::ModelNode &modelNode,const QPointF &pos)
{
49 50
    if (modelNode.hasParentProperty() && QmlDesigner::QmlItemNode::isValidQmlItemNode(modelNode.parentProperty().parentModelNode())) {
        QmlDesigner::QmlItemNode parentNode = modelNode.parentProperty().parentQmlObjectNode().toQmlItemNode();
51 52 53 54 55 56 57 58 59

        if (!parentNode.modelNode().metaInfo().isLayoutable()) {
            QPointF localPos = parentNode.instanceSceneTransform().inverted().map(pos);
            modelNode.variantProperty("x").setValue(localPos.toPoint().x());
            modelNode.variantProperty("y").setValue(localPos.toPoint().y());
        } else { //Items in Layouts do not have a position
            modelNode.removeProperty("x");
            modelNode.removeProperty("y");
        }
60 61
    }
}
62 63 64 65

namespace QmlDesigner {

NavigatorView::NavigatorView(QObject* parent) :
Marco Bubke's avatar
Marco Bubke committed
66
        AbstractView(parent),
67
        m_blockSelectionChangedSignal(false),
68
        m_widget(new NavigatorWidget(this)),
69 70
        m_treeModel(new NavigatorTreeModel(this))
{
71 72 73
    Internal::NavigatorContext *navigatorContext = new Internal::NavigatorContext(m_widget.data());
    Core::ICore::addContextObject(navigatorContext);

74 75 76
    m_widget->setTreeModel(m_treeModel.data());

    connect(treeWidget()->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(changeSelection(QItemSelection,QItemSelection)));
77 78 79 80 81 82

    connect(m_widget.data(), SIGNAL(leftButtonClicked()), this, SLOT(leftButtonClicked()));
    connect(m_widget.data(), SIGNAL(rightButtonClicked()), this, SLOT(rightButtonClicked()));
    connect(m_widget.data(), SIGNAL(downButtonClicked()), this, SLOT(downButtonClicked()));
    connect(m_widget.data(), SIGNAL(upButtonClicked()), this, SLOT(upButtonClicked()));

83
    treeWidget()->setIndentation(treeWidget()->indentation() * 0.5);
84

85 86
    NameItemDelegate *idDelegate = new NameItemDelegate(this,
                                                        m_treeModel.data());
87 88
    IconCheckboxItemDelegate *showDelegate =
            new IconCheckboxItemDelegate(this,
89 90
                                         Icons::EYE_OPEN.pixmap(),
                                         Icons::EYE_CLOSED.pixmap(),
91 92 93 94
                                         m_treeModel.data());

    IconCheckboxItemDelegate *exportDelegate =
            new IconCheckboxItemDelegate(this,
95 96
                                         Icons::EXPORT_CHECKED.pixmap(),
                                         Icons::EXPORT_UNCHECKED.pixmap(),
97
                                         m_treeModel.data());
98 99 100 101 102 103 104 105 106 107 108 109

#ifdef _LOCK_ITEMS_
    IconCheckboxItemDelegate *lockDelegate = new IconCheckboxItemDelegate(this,":/qmldesigner/images/lock.png",
                                                          ":/qmldesigner/images/hole.png",m_treeModel.data());
#endif


    treeWidget()->setItemDelegateForColumn(0,idDelegate);
#ifdef _LOCK_ITEMS_
    treeWidget()->setItemDelegateForColumn(1,lockDelegate);
    treeWidget()->setItemDelegateForColumn(2,showDelegate);
#else
110 111
    treeWidget()->setItemDelegateForColumn(1,exportDelegate);
    treeWidget()->setItemDelegateForColumn(2,showDelegate);
112 113
#endif

114 115 116 117 118 119 120 121
}

NavigatorView::~NavigatorView()
{
    if (m_widget && !m_widget->parent())
        delete m_widget.data();
}

122 123 124 125 126 127 128
bool NavigatorView::hasWidget() const
{
    return true;
}

WidgetInfo NavigatorView::widgetInfo()
{
129 130
    return createWidgetInfo(m_widget.data(),
                            new WidgetInfo::ToolBarWidgetDefaultFactory<NavigatorWidget>(m_widget.data()),
131
                            QStringLiteral("Navigator"),
132 133
                            WidgetInfo::LeftPane,
                            0);
134 135
}

136 137
void NavigatorView::modelAttached(Model *model)
{
Marco Bubke's avatar
Marco Bubke committed
138
    AbstractView::modelAttached(model);
139 140 141

    m_treeModel->setView(this);

142 143
    QTreeView *treeView = treeWidget();
    treeView->expandAll();
144

145
    treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
146 147 148
    treeView->header()->resizeSection(1,26);
    treeView->setRootIsDecorated(false);
    treeView->setIndentation(20);
149
#ifdef _LOCK_ITEMS_
150
    treeView->header()->resizeSection(2,20);
151
#endif
152 153 154 155
}

void NavigatorView::modelAboutToBeDetached(Model *model)
{
156
    m_treeModel->removeSubTree(rootModelNode());
157
    m_treeModel->clearView();
Marco Bubke's avatar
Marco Bubke committed
158
    AbstractView::modelAboutToBeDetached(model);
159 160
}

161
void NavigatorView::importsChanged(const QList<Import> &/*addedImports*/, const QList<Import> &/*removedImports*/)
162 163 164 165
{
    treeWidget()->update();
}

166 167 168 169 170 171 172 173 174 175 176
void NavigatorView::bindingPropertiesChanged(const QList<BindingProperty> & propertyList, PropertyChangeFlags /*propertyChange*/)
{
    foreach (const BindingProperty &bindingProperty, propertyList) {
        /* If a binding property that exports an item using an alias property has
         * changed, we have to update the affected item.
         */
        if (bindingProperty.isAliasExport())
            m_treeModel->updateItemRow(modelNodeForId(bindingProperty.expression()));
    }
}

177 178
void NavigatorView::nodeAboutToBeRemoved(const ModelNode &removedNode)
{
Marco Bubke's avatar
Marco Bubke committed
179
    m_treeModel->removeSubTree(removedNode);
180 181
}

182
void NavigatorView::nodeReparented(const ModelNode &node, const NodeAbstractProperty & newPropertyParent, const NodeAbstractProperty & /*oldPropertyParent*/, AbstractView::PropertyChangeFlags /*propertyChange*/)
183 184 185
{
    bool blocked = blockSelectionChangedSignal(true);

Marco Bubke's avatar
Marco Bubke committed
186 187
    m_treeModel->removeSubTree(node);

188 189 190 191 192 193
    if (node.isInHierarchy())
        m_treeModel->addSubTree(node);

    // make sure selection is in sync again
    updateItemSelection();

194 195 196 197 198
    if (newPropertyParent.parentModelNode().isValid()) {
        QModelIndex index = m_treeModel->indexForNode(newPropertyParent.parentModelNode());
        treeWidget()->expand(index);
    }

199 200 201
    blockSelectionChangedSignal(blocked);
}

202
void NavigatorView::nodeIdChanged(const ModelNode& node, const QString & /*newId*/, const QString & /*oldId*/)
203 204 205 206 207 208 209 210
{
    if (m_treeModel->isInTree(node))
        m_treeModel->updateItemRow(node);
}

void NavigatorView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& propertyList)
{
    foreach (const AbstractProperty &property, propertyList) {
211 212 213 214
        if (property.isNodeAbstractProperty()) {
            NodeAbstractProperty nodeAbstractProperty(property.toNodeListProperty());
            foreach (const ModelNode &childNode, nodeAbstractProperty.directSubNodes()) {
                m_treeModel->removeSubTree(childNode);
215 216 217 218 219
            }
        }
    }
}

220
void NavigatorView::rootNodeTypeChanged(const QString & /*type*/, int /*majorVersion*/, int /*minorVersion*/)
221
{
222 223
    if (m_treeModel->isInTree(rootModelNode()))
        m_treeModel->updateItemRow(rootModelNode());
224 225
}

226
void NavigatorView::auxiliaryDataChanged(const ModelNode &modelNode, const PropertyName & name, const QVariant & /*data*/)
227
{
228
    if (name == "invisible" && m_treeModel->isInTree(modelNode))
229 230
    {
        // update model
231
        m_treeModel->updateItemRow(modelNode);
232 233

        // repaint row (id and icon)
234 235 236 237 238
        foreach (const ModelNode &currentModelNode, modelNode.allSubModelNodesAndThisNode()) {
            QModelIndex index = m_treeModel->indexForNode(currentModelNode);
            treeWidget()->update(index);
            treeWidget()->update(index.sibling(index.row(),index.column()+1));
        }
239
    }
240 241
}

242 243 244 245 246 247
void NavigatorView::instanceErrorChange(const QVector<ModelNode> &errorNodeList)
{
    foreach (const ModelNode &currentModelNode, errorNodeList)
        m_treeModel->updateItemRow(currentModelNode);
}

248
void NavigatorView::nodeOrderChanged(const NodeListProperty &listProperty, const ModelNode &node, int /*oldIndex*/)
249
{
250 251 252 253 254 255
    if (m_treeModel->isInTree(node)) {
        m_treeModel->removeSubTree(listProperty.parentModelNode());

        if (node.isInHierarchy())
            m_treeModel->addSubTree(listProperty.parentModelNode());

256 257 258 259
        if (listProperty.parentModelNode().isValid()) {
            QModelIndex index = m_treeModel->indexForNode(listProperty.parentModelNode());
            treeWidget()->expand(index);
        }
260
    }
261 262
}

263 264 265 266
void NavigatorView::changeToComponent(const QModelIndex &index)
{
    if (index.isValid() && m_treeModel->data(index, Qt::UserRole).isValid()) {
        ModelNode doubleClickNode = m_treeModel->nodeForIndex(index);
Marco Bubke's avatar
Marco Bubke committed
267
        if (doubleClickNode.metaInfo().isFileComponent())
Eike Ziller's avatar
Eike Ziller committed
268 269
            Core::EditorManager::openEditor(doubleClickNode.metaInfo().componentFileName(),
                                            Core::Id(), Core::EditorManager::DoNotMakeVisible);
270 271 272
    }
}

273 274 275 276 277 278 279 280
void NavigatorView::leftButtonClicked()
{
    if (selectedModelNodes().count() > 1)
        return; //Semantics are unclear for multi selection.

    bool blocked = blockSelectionChangedSignal(true);

    foreach (const ModelNode &node, selectedModelNodes()) {
281
        if (!node.isRootNode() && !node.parentProperty().parentModelNode().isRootNode()) {
282
            if (QmlItemNode::isValidQmlItemNode(node)) {
283
                QPointF scenePos = QmlItemNode(node).instanceScenePosition();
284
                node.parentProperty().parentProperty().reparentHere(node);
285 286 287
                if (!scenePos.isNull())
                    setScenePos(node, scenePos);
            } else {
288
                node.parentProperty().parentProperty().reparentHere(node);
289 290
            }
        }
291 292 293 294 295 296 297 298 299 300 301 302
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

void NavigatorView::rightButtonClicked()
{
    if (selectedModelNodes().count() > 1)
        return; //Semantics are unclear for multi selection.

    bool blocked = blockSelectionChangedSignal(true);
    foreach (const ModelNode &node, selectedModelNodes()) {
303 304
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty() && node.parentProperty().count() > 1) {
            int index = node.parentProperty().indexOf(node);
305 306 307
            index--;
            if (index >= 0) { //for the first node the semantics are not clear enough. Wrapping would be irritating.
                ModelNode newParent = node.parentProperty().toNodeListProperty().at(index);
308

309 310 311
                if (QmlItemNode::isValidQmlItemNode(node)
                        && QmlItemNode::isValidQmlItemNode(newParent)
                        && !newParent.metaInfo().defaultPropertyIsComponent()) {
312 313 314 315 316
                    QPointF scenePos = QmlItemNode(node).instanceScenePosition();
                    newParent.nodeAbstractProperty(newParent.metaInfo().defaultPropertyName()).reparentHere(node);
                    if (!scenePos.isNull())
                        setScenePos(node, scenePos);
                } else {
317 318
                    if (newParent.metaInfo().isValid() && !newParent.metaInfo().defaultPropertyIsComponent())
                        newParent.nodeAbstractProperty(newParent.metaInfo().defaultPropertyName()).reparentHere(node);
319
                }
320 321 322 323 324 325 326 327 328 329 330 331
            }
        }
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

void NavigatorView::upButtonClicked()
{
    bool blocked = blockSelectionChangedSignal(true);
    foreach (const ModelNode &node, selectedModelNodes()) {
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty()) {
332
            int oldIndex = node.parentProperty().indexOf(node);
333 334 335
            int index = oldIndex;
            index--;
            if (index < 0)
336
                index = node.parentProperty().count() - 1; //wrap around
337 338 339 340 341 342 343 344 345 346 347 348
            node.parentProperty().toNodeListProperty().slide(oldIndex, index);
        }
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

void NavigatorView::downButtonClicked()
{
    bool blocked = blockSelectionChangedSignal(true);
    foreach (const ModelNode &node, selectedModelNodes()) {
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty()) {
349
            int oldIndex = node.parentProperty().indexOf(node);
350 351
            int index = oldIndex;
            index++;
352
            if (index >= node.parentProperty().count())
353 354 355 356 357 358 359 360
                index = 0; //wrap around
            node.parentProperty().toNodeListProperty().slide(oldIndex, index);
        }
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

361 362 363 364 365 366
void NavigatorView::changeSelection(const QItemSelection & /*newSelection*/, const QItemSelection &/*deselected*/)
{
    if (m_blockSelectionChangedSignal)
        return;
    QSet<ModelNode> nodeSet;
    foreach (const QModelIndex &index, treeWidget()->selectionModel()->selectedIndexes()) {
367 368
        if (m_treeModel->data(index, Qt::UserRole).isValid())
            nodeSet.insert(m_treeModel->nodeForIndex(index));
369 370 371 372 373 374 375
    }

    bool blocked = blockSelectionChangedSignal(true);
    setSelectedModelNodes(nodeSet.toList());
    blockSelectionChangedSignal(blocked);
}

376
void NavigatorView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/, const QList<ModelNode> &/*lastSelectedNodeList*/)
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
    updateItemSelection();
}

void NavigatorView::updateItemSelection()
{
    QItemSelection itemSelection;
    foreach (const ModelNode &node, selectedModelNodes()) {
        const QModelIndex index = m_treeModel->indexForNode(node);
        if (index.isValid()) {
            const QModelIndex beginIndex(m_treeModel->index(index.row(), 0, index.parent()));
            const QModelIndex endIndex(m_treeModel->index(index.row(), m_treeModel->columnCount(index.parent()) - 1, index.parent()));
            if (beginIndex.isValid() && endIndex.isValid())
                itemSelection.select(beginIndex, endIndex);
        }
    }

    bool blocked = blockSelectionChangedSignal(true);
    treeWidget()->selectionModel()->select(itemSelection, QItemSelectionModel::ClearAndSelect);
    blockSelectionChangedSignal(blocked);

398 399 400
    if (!selectedModelNodes().isEmpty())
        treeWidget()->scrollTo(m_treeModel->indexForNode(selectedModelNodes().first()));

401
    // make sure selected nodes a visible
402
    foreach (const QModelIndex &selectedIndex, itemSelection.indexes()) {
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
        if (selectedIndex.column() == 0)
            expandRecursively(selectedIndex);
    }
}

QTreeView *NavigatorView::treeWidget()
{
    if (m_widget)
        return m_widget->treeView();
    return 0;
}

NavigatorTreeModel *NavigatorView::treeModel()
{
    return m_treeModel.data();
}

// along the lines of QObject::blockSignals
bool NavigatorView::blockSelectionChangedSignal(bool block)
{
    bool oldValue = m_blockSelectionChangedSignal;
    m_blockSelectionChangedSignal = block;
    return oldValue;
}

void NavigatorView::expandRecursively(const QModelIndex &index)
{
    QModelIndex currentIndex = index;
    while (currentIndex.isValid()) {
        if (!treeWidget()->isExpanded(currentIndex))
            treeWidget()->expand(currentIndex);
        currentIndex = currentIndex.parent();
    }
}

} // namespace QmlDesigner