Commit 00e9b991 authored by Thomas Hartmann's avatar Thomas Hartmann

QmlDesigner: Introduce NavigatorModelInterface

We use NavigatorModelInterface to encapsulate different models.

Change-Id: Ia7bcdfffea18942cb07ca3495e078a97ca51d97c
Reviewed-by: Thomas Hartmann's avatarThomas Hartmann <thomas.hartmann@qt.io>
parent 967f4d42
......@@ -12,6 +12,7 @@ HEADERS += navigatorview.h \
navigatorwidget.h \
nameitemdelegate.h \
iconcheckboxitemdelegate.h \
navigatortreeview.h
navigatortreeview.h \
navigatormodelinterface.h
RESOURCES += navigator.qrc
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** 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.
**
****************************************************************************/
#pragma once
#include <modelnode.h>
QT_BEGIN_NAMESPACE
class QItemSelection;
class QModelIndex;
QT_END_NAMESPACE
namespace QmlDesigner {
class NavigatorModelInterface
{
public:
virtual QModelIndex indexForModelNode(const ModelNode &modelNode) const = 0;
virtual void notifyDataChanged(const ModelNode &modelNode) = 0;
virtual void notifyModelNodesRemoved(const QList<ModelNode> &modelNodes) = 0;
virtual void notifyModelNodesInserted(const QList<ModelNode> &modelNodes) = 0;
virtual void notifyModelNodesMoved(const QList<ModelNode> &modelNodes) = 0;
};
} //QmlDesigner
......@@ -25,11 +25,12 @@
#pragma once
#include "navigatormodelinterface.h"
#include <modelnode.h>
#include <nodemetainfo.h>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QAbstractItemModel>
#include <QPointer>
namespace QmlDesigner {
......@@ -38,7 +39,7 @@ class Model;
class NavigatorView;
class ModelNode;
class NavigatorTreeModel : public QAbstractItemModel
class NavigatorTreeModel : public QAbstractItemModel, public NavigatorModelInterface
{
Q_OBJECT
......@@ -74,7 +75,7 @@ public:
const QModelIndex &parent) override;
QModelIndex indexForModelNode(const ModelNode &node) const;
QModelIndex indexForModelNode(const ModelNode &node) const override;
QModelIndex createIndexFromModelNode(int row, int column, const ModelNode &modelNode) const;
......@@ -82,10 +83,10 @@ public:
Qt::DropActions supportedDragActions() const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
void notifyDataChanged(const ModelNode &modelNode);
void notifyModelNodesRemoved(const QList<ModelNode> &modelNodes);
void notifyModelNodesInserted(const QList<ModelNode> &modelNodes);
void notifyModelNodesMoved(const QList<ModelNode> &modelNodes);
void notifyDataChanged(const ModelNode &modelNode) override;
void notifyModelNodesRemoved(const QList<ModelNode> &modelNodes) override;
void notifyModelNodesInserted(const QList<ModelNode> &modelNodes) override;
void notifyModelNodesMoved(const QList<ModelNode> &modelNodes) override;
private:
void moveNodesInteractive(NodeAbstractProperty &parentProperty, const QList<ModelNode> &modelNodes, int targetIndex);
......
......@@ -80,6 +80,7 @@ NavigatorView::NavigatorView(QObject* parent) :
m_treeModel->setView(this);
m_widget->setTreeModel(m_treeModel.data());
m_currentModelInterface = m_treeModel;
connect(treeWidget()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &NavigatorView::changeSelection);
......@@ -171,7 +172,7 @@ void NavigatorView::bindingPropertiesChanged(const QList<BindingProperty> & prop
*/
if (bindingProperty.isAliasExport())
m_treeModel->notifyDataChanged(modelNodeForId(bindingProperty.expression()));
m_currentModelInterface->notifyDataChanged(modelNodeForId(bindingProperty.expression()));
}
}
......@@ -214,7 +215,7 @@ void NavigatorView::nodeRemoved(const ModelNode &removedNode,
const NodeAbstractProperty & /*parentProperty*/,
AbstractView::PropertyChangeFlags /*propertyChange*/)
{
m_treeModel->notifyModelNodesRemoved({removedNode});
m_currentModelInterface->notifyModelNodesRemoved({removedNode});
}
void NavigatorView::nodeReparented(const ModelNode &modelNode,
......@@ -223,15 +224,15 @@ void NavigatorView::nodeReparented(const ModelNode &modelNode,
AbstractView::PropertyChangeFlags /*propertyChange*/)
{
if (!oldPropertyParent.isValid())
m_treeModel->notifyModelNodesInserted({modelNode});
m_currentModelInterface->notifyModelNodesInserted({modelNode});
else
m_treeModel->notifyModelNodesMoved({modelNode});
treeWidget()->expand(m_treeModel->indexForModelNode(modelNode));
m_currentModelInterface->notifyModelNodesMoved({modelNode});
treeWidget()->expand(indexForModelNode(modelNode));
}
void NavigatorView::nodeIdChanged(const ModelNode& modelNode, const QString & /*newId*/, const QString & /*oldId*/)
{
m_treeModel->notifyDataChanged(modelNode);
m_currentModelInterface->notifyDataChanged(modelNode);
}
void NavigatorView::propertiesAboutToBeRemoved(const QList<AbstractProperty>& /*propertyList*/)
......@@ -248,30 +249,30 @@ void NavigatorView::propertiesRemoved(const QList<AbstractProperty> &propertyLis
}
}
m_treeModel->notifyModelNodesRemoved(modelNodes);
m_currentModelInterface->notifyModelNodesRemoved(modelNodes);
}
void NavigatorView::rootNodeTypeChanged(const QString & /*type*/, int /*majorVersion*/, int /*minorVersion*/)
{
m_treeModel->notifyDataChanged(rootModelNode());
m_currentModelInterface->notifyDataChanged(rootModelNode());
}
void NavigatorView::nodeTypeChanged(const ModelNode &modelNode, const TypeName &, int , int)
{
m_treeModel->notifyDataChanged(modelNode);
m_currentModelInterface->notifyDataChanged(modelNode);
}
void NavigatorView::auxiliaryDataChanged(const ModelNode &modelNode,
const PropertyName & /*name*/,
const QVariant & /*data*/)
{
m_treeModel->notifyDataChanged(modelNode);
m_currentModelInterface->notifyDataChanged(modelNode);
}
void NavigatorView::instanceErrorChanged(const QVector<ModelNode> &errorNodeList)
{
foreach (const ModelNode &modelNode, errorNodeList)
m_treeModel->notifyDataChanged(modelNode);
m_currentModelInterface->notifyDataChanged(modelNode);
}
void NavigatorView::nodeOrderChanged(const NodeListProperty & listProperty,
......@@ -280,7 +281,7 @@ void NavigatorView::nodeOrderChanged(const NodeListProperty & listProperty,
{
bool blocked = blockSelectionChangedSignal(true);
m_treeModel->notifyModelNodesMoved(listProperty.directSubNodes());
m_currentModelInterface->notifyModelNodesMoved(listProperty.directSubNodes());
// make sure selection is in sync again
updateItemSelection();
......@@ -290,7 +291,7 @@ void NavigatorView::nodeOrderChanged(const NodeListProperty & listProperty,
void NavigatorView::changeToComponent(const QModelIndex &index)
{
if (index.isValid() && m_treeModel->data(index, Qt::UserRole).isValid()) {
if (index.isValid() && currentModel()->data(index, Qt::UserRole).isValid()) {
const ModelNode doubleClickNode = modelNodeForIndex(index);
if (doubleClickNode.metaInfo().isFileComponent())
Core::EditorManager::openEditor(doubleClickNode.metaInfo().componentFileName(),
......@@ -298,6 +299,16 @@ void NavigatorView::changeToComponent(const QModelIndex &index)
}
}
QModelIndex NavigatorView::indexForModelNode(const ModelNode &modelNode) const
{
return m_currentModelInterface->indexForModelNode(modelNode);
}
QAbstractItemModel *NavigatorView::currentModel() const
{
return treeWidget()->model();
}
void NavigatorView::leftButtonClicked()
{
if (selectedModelNodes().count() > 1)
......@@ -415,10 +426,10 @@ void NavigatorView::updateItemSelection()
{
QItemSelection itemSelection;
foreach (const ModelNode &node, selectedModelNodes()) {
const QModelIndex index = m_treeModel->indexForModelNode(node);
const QModelIndex index = indexForModelNode(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()));
const QModelIndex beginIndex(currentModel()->index(index.row(), 0, index.parent()));
const QModelIndex endIndex(currentModel()->index(index.row(), currentModel()->columnCount(index.parent()) - 1, index.parent()));
if (beginIndex.isValid() && endIndex.isValid())
itemSelection.select(beginIndex, endIndex);
}
......@@ -429,7 +440,7 @@ void NavigatorView::updateItemSelection()
blockSelectionChangedSignal(blocked);
if (!selectedModelNodes().isEmpty())
treeWidget()->scrollTo(m_treeModel->indexForModelNode(selectedModelNodes().first()));
treeWidget()->scrollTo(indexForModelNode(selectedModelNodes().first()));
// make sure selected nodes a visible
foreach (const QModelIndex &selectedIndex, itemSelection.indexes()) {
......@@ -438,7 +449,7 @@ void NavigatorView::updateItemSelection()
}
}
QTreeView *NavigatorView::treeWidget()
QTreeView *NavigatorView::treeWidget() const
{
if (m_widget)
return m_widget->treeView();
......
......@@ -25,6 +25,8 @@
#pragma once
#include "navigatormodelinterface.h"
#include <abstractview.h>
#include <QPointer>
......@@ -33,6 +35,7 @@ QT_BEGIN_NAMESPACE
class QTreeView;
class QItemSelection;
class QModelIndex;
class QAbstractItemModel;
QT_END_NAMESPACE
namespace QmlDesigner {
......@@ -89,6 +92,8 @@ private:
void changeSelection(const QItemSelection &selected, const QItemSelection &deselected);
void updateItemSelection();
void changeToComponent(const QModelIndex &index);
QModelIndex indexForModelNode(const ModelNode &modelNode) const;
QAbstractItemModel *currentModel() const;
void leftButtonClicked();
void rightButtonClicked();
......@@ -96,7 +101,7 @@ private:
void downButtonClicked();
protected: //functions
QTreeView *treeWidget();
QTreeView *treeWidget() const;
NavigatorTreeModel *treeModel();
bool blockSelectionChangedSignal(bool block);
void expandRecursively(const QModelIndex &index);
......@@ -107,6 +112,8 @@ private:
QPointer<NavigatorWidget> m_widget;
QPointer<NavigatorTreeModel> m_treeModel;
NavigatorModelInterface *m_currentModelInterface = nullptr;
friend class TestNavigator;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment