Commit 8c5a4fc9 authored by Marco Bubke's avatar Marco Bubke
Browse files

QmlDesigner: Refactor NavigatorTreeModel::dropMimeData



Change-Id: I08db749f9b284b34e14f684f2369eee42ee67414
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
parent 117a5c9b
...@@ -152,68 +152,95 @@ QMimeData *NavigatorTreeModel::mimeData(const QModelIndexList &indexList) const ...@@ -152,68 +152,95 @@ QMimeData *NavigatorTreeModel::mimeData(const QModelIndexList &indexList) const
return mimeData; return mimeData;
} }
bool NavigatorTreeModel::dropMimeData(const QMimeData *data, static QList<ModelNode> modelNodesFromMimeData(const QMimeData *mineData, AbstractView *view)
Qt::DropAction action,
int row,
int column,
const QModelIndex &dropIndex)
{ {
if (action == Qt::IgnoreAction) QByteArray encodedModelNodeData = mineData->data("application/vnd.modelnode.list");
return true; QDataStream modelNodeStream(&encodedModelNodeData, QIODevice::ReadOnly);
if (action != Qt::LinkAction)
return false; QList<ModelNode> modelNodeList;
if (!data->hasFormat("application/vnd.modelnode.list")) while (!modelNodeStream.atEnd()) {
return false; qint32 internalId;
if (column > 1) modelNodeStream >> internalId;
return false; if (view->hasModelNodeForInternalId(internalId))
if (dropIndex.model() != this) modelNodeList.append(view->modelNodeForInternalId(internalId));
return false; }
QModelIndex parentIndex, parentItemIndex; return modelNodeList;
PropertyName parentPropertyName; }
int targetIndex;
parentIndex = dropIndex.sibling(dropIndex.row(), 0); bool fitsToTargetProperty(const NodeAbstractProperty &targetProperty,
targetIndex = (row > -1)? row : rowCount(parentIndex); const QList<ModelNode> &modelNodeList)
{
return !(targetProperty.isNodeProperty() &&
modelNodeList.count() > 1);
}
if (this->data(parentIndex, InternalIdRole).isValid()) { static bool computeTarget(const QModelIndex &rowModelIndex,
parentItemIndex = parentIndex; NavigatorTreeModel *navigatorTreeModel,
ModelNode parentNode = nodeForIndex(parentItemIndex); NodeAbstractProperty *targetProperty,
if (!parentNode.metaInfo().hasDefaultProperty()) int *targetRowNumber)
{
QModelIndex targetItemIndex;
PropertyName targetPropertyName;
if (*targetRowNumber < 0 || *targetRowNumber > navigatorTreeModel->rowCount(rowModelIndex))
*targetRowNumber = navigatorTreeModel->rowCount(rowModelIndex);
if (navigatorTreeModel->hasNodeForIndex(rowModelIndex)) {
targetItemIndex = rowModelIndex;
ModelNode targetNode = navigatorTreeModel->nodeForIndex(targetItemIndex);
if (!targetNode.metaInfo().hasDefaultProperty())
return false; return false;
targetIndex -= visibleProperties(parentNode).count(); *targetRowNumber -= visibleProperties(targetNode).count();
parentPropertyName = parentNode.metaInfo().defaultPropertyName(); targetPropertyName = targetNode.metaInfo().defaultPropertyName();
} else { } else {
parentItemIndex = parentIndex.parent(); targetItemIndex = rowModelIndex.parent();
parentPropertyName = parentIndex.data(Qt::DisplayRole).toByteArray(); targetPropertyName = rowModelIndex.data(Qt::DisplayRole).toByteArray();
} }
// Disallow dropping items between properties, which are listed first. // Disallow dropping items between properties, which are listed first.
if (targetIndex < 0) if (*targetRowNumber < 0)
return false; return false;
Q_ASSERT(parentItemIndex.isValid()); ModelNode targetNode(navigatorTreeModel->nodeForIndex(targetItemIndex));
*targetProperty = targetNode.nodeAbstractProperty(targetPropertyName);
return true;
}
QByteArray encodedData = data->data("application/vnd.modelnode.list"); bool NavigatorTreeModel::dropMimeData(const QMimeData *mimeData,
QDataStream stream(&encodedData, QIODevice::ReadOnly); Qt::DropAction action,
int rowNumber,
int columnNumber,
const QModelIndex &dropModelIndex)
{
if (action == Qt::IgnoreAction)
return true;
QList<ModelNode> nodeList; if (action != Qt::LinkAction)
while (!stream.atEnd()) { return false;
qint32 internalId;
stream >> internalId;
if (m_view->hasModelNodeForInternalId(internalId))
nodeList.append(m_view->modelNodeForInternalId(internalId));
}
ModelNode parentNode(nodeForIndex(parentItemIndex)); if (!mimeData->hasFormat("application/vnd.modelnode.list"))
NodeAbstractProperty parentProperty = parentNode.nodeAbstractProperty(parentPropertyName); return false;
if (parentProperty.isNodeProperty() && if (columnNumber > 1)
nodeList.count() > 1) { return false;
if (dropModelIndex.model() != this)
return false; return false;
}
moveNodesInteractive(parentProperty, nodeList, targetIndex); QModelIndex rowModelIndex = dropModelIndex.sibling(dropModelIndex.row(), 0);
int targetRowNumber = rowNumber;
NodeAbstractProperty targetProperty;
bool foundTarget = computeTarget(rowModelIndex, this, &targetProperty, &targetRowNumber);
if (foundTarget) {
QList<ModelNode> modelNodeList = modelNodesFromMimeData(mimeData, m_view);
if (fitsToTargetProperty(targetProperty, modelNodeList))
moveNodesInteractive(targetProperty, modelNodeList, targetRowNumber);
}
return false; // don't let the view do drag&drop on its own return false; // don't let the view do drag&drop on its own
} }
......
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