navigatorview.cpp 17.2 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://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
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18 19 20 21 22
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.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 30
#include "qmldesignerconstants.h"
#include "qmldesignericons.h"
31

32 33
#include "nameitemdelegate.h"
#include "iconcheckboxitemdelegate.h"
34

35
#include <bindingproperty.h>
36
#include <designmodecontext.h>
37
#include <nodeproperty.h>
38
#include <nodelistproperty.h>
39
#include <variantproperty.h>
Marco Bubke's avatar
Marco Bubke committed
40
#include <qmlitemnode.h>
41
#include <rewritingexception.h>
42

43 44 45 46 47 48 49 50 51
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>

#include <utils/icon.h>
#include <utils/utilsicons.h>

#include <QHeaderView>


52 53
static inline void setScenePos(const QmlDesigner::ModelNode &modelNode,const QPointF &pos)
{
54 55
    if (modelNode.hasParentProperty() && QmlDesigner::QmlItemNode::isValidQmlItemNode(modelNode.parentProperty().parentModelNode())) {
        QmlDesigner::QmlItemNode parentNode = modelNode.parentProperty().parentQmlObjectNode().toQmlItemNode();
56 57 58 59 60 61 62 63 64

        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");
        }
65 66
    }
}
67 68 69 70

namespace QmlDesigner {

NavigatorView::NavigatorView(QObject* parent) :
71 72 73 74
    AbstractView(parent),
    m_blockSelectionChangedSignal(false),
    m_widget(new NavigatorWidget(this)),
    m_treeModel(new NavigatorTreeModel(this))
75
{
76
#ifndef QMLDESIGNER_TEST
77 78
    Internal::NavigatorContext *navigatorContext = new Internal::NavigatorContext(m_widget.data());
    Core::ICore::addContextObject(navigatorContext);
79
#endif
80

81
    m_treeModel->setView(this);
82
    m_widget->setTreeModel(m_treeModel.data());
83
    m_currentModelInterface = m_treeModel;
84

85
    connect(treeWidget()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &NavigatorView::changeSelection);
86

87 88 89 90
    connect(m_widget.data(), &NavigatorWidget::leftButtonClicked, this, &NavigatorView::leftButtonClicked);
    connect(m_widget.data(), &NavigatorWidget::rightButtonClicked, this, &NavigatorView::rightButtonClicked);
    connect(m_widget.data(), &NavigatorWidget::downButtonClicked, this, &NavigatorView::downButtonClicked);
    connect(m_widget.data(), &NavigatorWidget::upButtonClicked, this, &NavigatorView::upButtonClicked);
91

92
#ifndef QMLDESIGNER_TEST
93
    NameItemDelegate *idDelegate = new NameItemDelegate(this);
94 95
    IconCheckboxItemDelegate *showDelegate =
            new IconCheckboxItemDelegate(this,
Ulf Hermann's avatar
Ulf Hermann committed
96
                                         Utils::Icons::EYE_OPEN_TOOLBAR.pixmap(),
97
                                         Utils::Icons::EYE_CLOSED_TOOLBAR.pixmap());
98 99 100

    IconCheckboxItemDelegate *exportDelegate =
            new IconCheckboxItemDelegate(this,
101
                                         Icons::EXPORT_CHECKED.pixmap(),
102
                                         Icons::EXPORT_UNCHECKED.pixmap());
103 104

#ifdef _LOCK_ITEMS_
105 106 107 108
    IconCheckboxItemDelegate *lockDelegate =
            new IconCheckboxItemDelegate(this,
                                         Utils::Icons::LOCKED_TOOLBAR.pixmap(),
                                         Utils::Icons::UNLOCKED_TOOLBAR.pixmap());
109 110 111
#endif


112
    treeWidget()->setItemDelegateForColumn(0, idDelegate);
113 114 115 116
#ifdef _LOCK_ITEMS_
    treeWidget()->setItemDelegateForColumn(1,lockDelegate);
    treeWidget()->setItemDelegateForColumn(2,showDelegate);
#else
117 118
    treeWidget()->setItemDelegateForColumn(1, exportDelegate);
    treeWidget()->setItemDelegateForColumn(2, showDelegate);
119 120
#endif

121
#endif //QMLDESIGNER_TEST
122 123 124 125 126 127 128 129
}

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

130 131 132 133 134 135 136
bool NavigatorView::hasWidget() const
{
    return true;
}

WidgetInfo NavigatorView::widgetInfo()
{
137 138
    return createWidgetInfo(m_widget.data(),
                            new WidgetInfo::ToolBarWidgetDefaultFactory<NavigatorWidget>(m_widget.data()),
139
                            QStringLiteral("Navigator"),
140 141
                            WidgetInfo::LeftPane,
                            0);
142 143
}

144 145
void NavigatorView::modelAttached(Model *model)
{
Marco Bubke's avatar
Marco Bubke committed
146
    AbstractView::modelAttached(model);
147

148 149
    QTreeView *treeView = treeWidget();
    treeView->expandAll();
150

151
    treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch);
152 153
    treeView->header()->resizeSection(1,26);
    treeView->setIndentation(20);
154
#ifdef _LOCK_ITEMS_
155
    treeView->header()->resizeSection(2,20);
156
#endif
157 158 159 160
}

void NavigatorView::modelAboutToBeDetached(Model *model)
{
Marco Bubke's avatar
Marco Bubke committed
161
    AbstractView::modelAboutToBeDetached(model);
162 163
}

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

169 170
void NavigatorView::bindingPropertiesChanged(const QList<BindingProperty> & propertyList, PropertyChangeFlags /*propertyChange*/)
{
171
    for (const BindingProperty &bindingProperty : propertyList) {
172 173 174
        /* If a binding property that exports an item using an alias property has
         * changed, we have to update the affected item.
         */
175

176
        if (bindingProperty.isAliasExport())
177
            m_currentModelInterface->notifyDataChanged(modelNodeForId(bindingProperty.expression()));
178 179 180
    }
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
void NavigatorView::handleChangedExport(const ModelNode &modelNode, bool exported)
{
    const ModelNode rootNode = rootModelNode();
    Q_ASSERT(rootNode.isValid());
    const PropertyName modelNodeId = modelNode.id().toUtf8();
    if (rootNode.hasProperty(modelNodeId))
        rootNode.removeProperty(modelNodeId);
    if (exported) {
        try {
            RewriterTransaction transaction =
                    beginRewriterTransaction(QByteArrayLiteral("NavigatorTreeModel:exportItem"));

            QmlObjectNode qmlObjectNode(modelNode);
            qmlObjectNode.ensureAliasExport();
            transaction.commit();
        }  catch (RewritingException &exception) { //better safe than sorry! There always might be cases where we fail
            exception.showException();
        }
    }
}

202
bool NavigatorView::isNodeInvisible(const ModelNode &modelNode) const
203
{
204 205 206 207 208 209
    return modelNode.auxiliaryData("invisible").toBool();
}

ModelNode NavigatorView::modelNodeForIndex(const QModelIndex &modelIndex) const
{
    return modelIndex.model()->data(modelIndex, ModelNodeRole).value<ModelNode>();
210 211
}

212
void NavigatorView::nodeAboutToBeRemoved(const ModelNode & /*removedNode*/)
213 214 215
{
}

216 217 218
void NavigatorView::nodeRemoved(const ModelNode &removedNode,
                                const NodeAbstractProperty & /*parentProperty*/,
                                AbstractView::PropertyChangeFlags /*propertyChange*/)
219
{
220
    m_currentModelInterface->notifyModelNodesRemoved({removedNode});
221
}
222

223 224 225 226 227 228
void NavigatorView::nodeReparented(const ModelNode &modelNode,
                                   const NodeAbstractProperty & /*newPropertyParent*/,
                                   const NodeAbstractProperty & oldPropertyParent,
                                   AbstractView::PropertyChangeFlags /*propertyChange*/)
{
    if (!oldPropertyParent.isValid())
229
        m_currentModelInterface->notifyModelNodesInserted({modelNode});
230
    else
231 232
        m_currentModelInterface->notifyModelNodesMoved({modelNode});
    treeWidget()->expand(indexForModelNode(modelNode));
233 234
}

235
void NavigatorView::nodeIdChanged(const ModelNode& modelNode, const QString & /*newId*/, const QString & /*oldId*/)
236
{
237
    m_currentModelInterface->notifyDataChanged(modelNode);
238 239
}

240
void NavigatorView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/)
241 242 243
{
}

244 245
void NavigatorView::propertiesRemoved(const QList<AbstractProperty> &propertyList)
{
246
    QList<ModelNode> modelNodes;
247
    for (const AbstractProperty &property : propertyList) {
248 249 250
        if (property.isNodeAbstractProperty()) {
            NodeAbstractProperty nodeAbstractProperty(property.toNodeListProperty());
            modelNodes.append(nodeAbstractProperty.directSubNodes());
251 252
        }
    }
253

254
    m_currentModelInterface->notifyModelNodesRemoved(modelNodes);
255 256
}

257
void NavigatorView::rootNodeTypeChanged(const QString & /*type*/, int /*majorVersion*/, int /*minorVersion*/)
258
{
259
    m_currentModelInterface->notifyDataChanged(rootModelNode());
260 261
}

262
void NavigatorView::nodeTypeChanged(const ModelNode &modelNode, const TypeName &, int , int)
263
{
264
    m_currentModelInterface->notifyDataChanged(modelNode);
265 266
}

267 268 269
void NavigatorView::auxiliaryDataChanged(const ModelNode &modelNode,
                                         const PropertyName & /*name*/,
                                         const QVariant & /*data*/)
270
{
271
    m_currentModelInterface->notifyDataChanged(modelNode);
272 273
}

274
void NavigatorView::instanceErrorChanged(const QVector<ModelNode> &errorNodeList)
275
{
276
    foreach (const ModelNode &modelNode, errorNodeList)
277
        m_currentModelInterface->notifyDataChanged(modelNode);
278 279
}

280 281 282
void NavigatorView::nodeOrderChanged(const NodeListProperty & listProperty,
                                     const ModelNode & /*node*/,
                                     int /*oldIndex*/)
283
{
284
    bool blocked = blockSelectionChangedSignal(true);
285

286
    m_currentModelInterface->notifyModelNodesMoved(listProperty.directSubNodes());
287 288 289 290 291

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

    blockSelectionChangedSignal(blocked);
292 293
}

294 295
void NavigatorView::changeToComponent(const QModelIndex &index)
{
296
    if (index.isValid() && currentModel()->data(index, Qt::UserRole).isValid()) {
297
        const ModelNode doubleClickNode = modelNodeForIndex(index);
Marco Bubke's avatar
Marco Bubke committed
298
        if (doubleClickNode.metaInfo().isFileComponent())
Eike Ziller's avatar
Eike Ziller committed
299 300
            Core::EditorManager::openEditor(doubleClickNode.metaInfo().componentFileName(),
                                            Core::Id(), Core::EditorManager::DoNotMakeVisible);
301 302 303
    }
}

304 305 306 307 308 309 310 311 312 313
QModelIndex NavigatorView::indexForModelNode(const ModelNode &modelNode) const
{
    return m_currentModelInterface->indexForModelNode(modelNode);
}

QAbstractItemModel *NavigatorView::currentModel() const
{
    return treeWidget()->model();
}

314 315 316 317 318 319 320 321
void NavigatorView::leftButtonClicked()
{
    if (selectedModelNodes().count() > 1)
        return; //Semantics are unclear for multi selection.

    bool blocked = blockSelectionChangedSignal(true);

    foreach (const ModelNode &node, selectedModelNodes()) {
322
        if (!node.isRootNode() && !node.parentProperty().parentModelNode().isRootNode()) {
323
            if (QmlItemNode::isValidQmlItemNode(node)) {
324
                QPointF scenePos = QmlItemNode(node).instanceScenePosition();
325
                node.parentProperty().parentProperty().reparentHere(node);
326 327 328
                if (!scenePos.isNull())
                    setScenePos(node, scenePos);
            } else {
329
                node.parentProperty().parentProperty().reparentHere(node);
330 331
            }
        }
332
    }
333

334 335 336 337 338 339 340 341 342 343 344
    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()) {
345 346
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty() && node.parentProperty().count() > 1) {
            int index = node.parentProperty().indexOf(node);
347 348 349
            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);
350

351 352 353
                if (QmlItemNode::isValidQmlItemNode(node)
                        && QmlItemNode::isValidQmlItemNode(newParent)
                        && !newParent.metaInfo().defaultPropertyIsComponent()) {
354 355 356 357 358
                    QPointF scenePos = QmlItemNode(node).instanceScenePosition();
                    newParent.nodeAbstractProperty(newParent.metaInfo().defaultPropertyName()).reparentHere(node);
                    if (!scenePos.isNull())
                        setScenePos(node, scenePos);
                } else {
359 360
                    if (newParent.metaInfo().isValid() && !newParent.metaInfo().defaultPropertyIsComponent())
                        newParent.nodeAbstractProperty(newParent.metaInfo().defaultPropertyName()).reparentHere(node);
361
                }
362 363 364 365 366 367 368 369 370 371 372 373
            }
        }
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

void NavigatorView::upButtonClicked()
{
    bool blocked = blockSelectionChangedSignal(true);
    foreach (const ModelNode &node, selectedModelNodes()) {
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty()) {
374
            int oldIndex = node.parentProperty().indexOf(node);
375 376 377
            int index = oldIndex;
            index--;
            if (index < 0)
378
                index = node.parentProperty().count() - 1; //wrap around
379 380 381 382 383 384 385 386 387 388 389 390
            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()) {
391
            int oldIndex = node.parentProperty().indexOf(node);
392 393
            int index = oldIndex;
            index++;
394
            if (index >= node.parentProperty().count())
395 396 397 398 399 400 401 402
                index = 0; //wrap around
            node.parentProperty().toNodeListProperty().slide(oldIndex, index);
        }
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

403 404 405 406
void NavigatorView::changeSelection(const QItemSelection & /*newSelection*/, const QItemSelection &/*deselected*/)
{
    if (m_blockSelectionChangedSignal)
        return;
407

408
    QSet<ModelNode> nodeSet;
409

410 411 412 413 414 415
    for (const QModelIndex &index : treeWidget()->selectionModel()->selectedIndexes()) {

        const ModelNode modelNode = modelNodeForIndex(index);
        if (modelNode.isValid())
            nodeSet.insert(modelNode);
    }
416 417 418 419 420 421

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

422
void NavigatorView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/, const QList<ModelNode> &/*lastSelectedNodeList*/)
423 424 425 426 427 428 429 430
{
    updateItemSelection();
}

void NavigatorView::updateItemSelection()
{
    QItemSelection itemSelection;
    foreach (const ModelNode &node, selectedModelNodes()) {
431
        const QModelIndex index = indexForModelNode(node);
432
        if (index.isValid()) {
433 434
            const QModelIndex beginIndex(currentModel()->index(index.row(), 0, index.parent()));
            const QModelIndex endIndex(currentModel()->index(index.row(), currentModel()->columnCount(index.parent()) - 1, index.parent()));
435 436 437 438 439 440 441 442 443
            if (beginIndex.isValid() && endIndex.isValid())
                itemSelection.select(beginIndex, endIndex);
        }
    }

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

444
    if (!selectedModelNodes().isEmpty())
445
        treeWidget()->scrollTo(indexForModelNode(selectedModelNodes().first()));
446

447
    // make sure selected nodes a visible
448
    foreach (const QModelIndex &selectedIndex, itemSelection.indexes()) {
449 450 451 452 453
        if (selectedIndex.column() == 0)
            expandRecursively(selectedIndex);
    }
}

454
QTreeView *NavigatorView::treeWidget() const
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
{
    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