navigatorview.cpp 15.8 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

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

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

#include <QHeaderView>


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

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

namespace QmlDesigner {

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

80
    m_treeModel->setView(this);
81 82
    m_widget->setTreeModel(m_treeModel.data());

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

85 86 87 88
    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);
89

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

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

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


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

119
#endif //QMLDESIGNER_TEST
120 121 122 123 124 125 126 127
}

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

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

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

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

146 147
    QTreeView *treeView = treeWidget();
    treeView->expandAll();
148

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

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

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

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

174
        if (bindingProperty.isAliasExport())
175
            m_treeModel->notifyDataChanged(modelNodeForId(bindingProperty.expression()));
176 177 178
    }
}

179
void NavigatorView::nodeAboutToBeRemoved(const ModelNode & /*removedNode*/)
180 181 182
{
}

183 184 185
void NavigatorView::nodeRemoved(const ModelNode &removedNode,
                                const NodeAbstractProperty & /*parentProperty*/,
                                AbstractView::PropertyChangeFlags /*propertyChange*/)
186
{
187 188
    m_treeModel->notifyModelNodesRemoved({removedNode});
}
189

190 191 192 193 194 195 196 197 198 199
void NavigatorView::nodeReparented(const ModelNode &modelNode,
                                   const NodeAbstractProperty & /*newPropertyParent*/,
                                   const NodeAbstractProperty & oldPropertyParent,
                                   AbstractView::PropertyChangeFlags /*propertyChange*/)
{
    if (!oldPropertyParent.isValid())
        m_treeModel->notifyModelNodesInserted({modelNode});
    else
        m_treeModel->notifyModelNodesMoved({modelNode});
    treeWidget()->expand(m_treeModel->indexForModelNode(modelNode));
200 201
}

202
void NavigatorView::nodeIdChanged(const ModelNode& modelNode, const QString & /*newId*/, const QString & /*oldId*/)
203
{
204
    m_treeModel->notifyDataChanged(modelNode);
205 206
}

207
void NavigatorView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/)
208 209 210
{
}

211 212
void NavigatorView::propertiesRemoved(const QList<AbstractProperty> &propertyList)
{
213
    QList<ModelNode> modelNodes;
214
    for (const AbstractProperty &property : propertyList) {
215 216 217
        if (property.isNodeAbstractProperty()) {
            NodeAbstractProperty nodeAbstractProperty(property.toNodeListProperty());
            modelNodes.append(nodeAbstractProperty.directSubNodes());
218 219
        }
    }
220 221

    m_treeModel->notifyModelNodesRemoved(modelNodes);
222 223
}

224
void NavigatorView::rootNodeTypeChanged(const QString & /*type*/, int /*majorVersion*/, int /*minorVersion*/)
225
{
226
    m_treeModel->notifyDataChanged(rootModelNode());
227 228
}

229
void NavigatorView::nodeTypeChanged(const ModelNode &modelNode, const TypeName &, int , int)
230
{
231
    m_treeModel->notifyDataChanged(modelNode);
232 233
}

234 235 236
void NavigatorView::auxiliaryDataChanged(const ModelNode &modelNode,
                                         const PropertyName & /*name*/,
                                         const QVariant & /*data*/)
237
{
238
    m_treeModel->notifyDataChanged(modelNode);
239 240
}

241
void NavigatorView::instanceErrorChanged(const QVector<ModelNode> &errorNodeList)
242
{
243 244
    foreach (const ModelNode &modelNode, errorNodeList)
        m_treeModel->notifyDataChanged(modelNode);
245 246
}

247 248 249
void NavigatorView::nodeOrderChanged(const NodeListProperty & listProperty,
                                     const ModelNode & /*node*/,
                                     int /*oldIndex*/)
250
{
251
    bool blocked = blockSelectionChangedSignal(true);
252

253
    m_treeModel->notifyModelNodesMoved(listProperty.directSubNodes());
254 255 256 257 258

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

    blockSelectionChangedSignal(blocked);
259 260
}

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

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

    bool blocked = blockSelectionChangedSignal(true);

    foreach (const ModelNode &node, selectedModelNodes()) {
279
        if (!node.isRootNode() && !node.parentProperty().parentModelNode().isRootNode()) {
280
            if (QmlItemNode::isValidQmlItemNode(node)) {
281
                QPointF scenePos = QmlItemNode(node).instanceScenePosition();
282
                node.parentProperty().parentProperty().reparentHere(node);
283 284 285
                if (!scenePos.isNull())
                    setScenePos(node, scenePos);
            } else {
286
                node.parentProperty().parentProperty().reparentHere(node);
287 288
            }
        }
289
    }
290

291 292 293 294 295 296 297 298 299 300 301
    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()) {
302 303
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty() && node.parentProperty().count() > 1) {
            int index = node.parentProperty().indexOf(node);
304 305 306
            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);
307

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

void NavigatorView::upButtonClicked()
{
    bool blocked = blockSelectionChangedSignal(true);
    foreach (const ModelNode &node, selectedModelNodes()) {
        if (!node.isRootNode() && node.parentProperty().isNodeListProperty()) {
331
            int oldIndex = node.parentProperty().indexOf(node);
332 333 334
            int index = oldIndex;
            index--;
            if (index < 0)
335
                index = node.parentProperty().count() - 1; //wrap around
336 337 338 339 340 341 342 343 344 345 346 347
            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()) {
348
            int oldIndex = node.parentProperty().indexOf(node);
349 350
            int index = oldIndex;
            index++;
351
            if (index >= node.parentProperty().count())
352 353 354 355 356 357 358 359
                index = 0; //wrap around
            node.parentProperty().toNodeListProperty().slide(oldIndex, index);
        }
    }
    updateItemSelection();
    blockSelectionChangedSignal(blocked);
}

360 361 362 363
void NavigatorView::changeSelection(const QItemSelection & /*newSelection*/, const QItemSelection &/*deselected*/)
{
    if (m_blockSelectionChangedSignal)
        return;
364

365
    QSet<ModelNode> nodeSet;
366 367 368

    foreach (const QModelIndex &index, treeWidget()->selectionModel()->selectedIndexes())
        nodeSet.insert(m_treeModel->modelNodeForIndex(index));
369 370 371 372 373 374

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

375
void NavigatorView::selectedNodesChanged(const QList<ModelNode> &/*selectedNodeList*/, const QList<ModelNode> &/*lastSelectedNodeList*/)
376 377 378 379 380 381 382 383
{
    updateItemSelection();
}

void NavigatorView::updateItemSelection()
{
    QItemSelection itemSelection;
    foreach (const ModelNode &node, selectedModelNodes()) {
384
        const QModelIndex index = m_treeModel->indexForModelNode(node);
385 386 387 388 389 390 391 392 393 394 395 396
        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);

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

400
    // make sure selected nodes a visible
401
    foreach (const QModelIndex &selectedIndex, itemSelection.indexes()) {
402 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
        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