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

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
return mimeData;
}
bool NavigatorTreeModel::dropMimeData(const QMimeData *data,
Qt::DropAction action,
int row,
int column,
const QModelIndex &dropIndex)
static QList<ModelNode> modelNodesFromMimeData(const QMimeData *mineData, AbstractView *view)
{
if (action == Qt::IgnoreAction)
return true;
if (action != Qt::LinkAction)
return false;
if (!data->hasFormat("application/vnd.modelnode.list"))
return false;
if (column > 1)
return false;
if (dropIndex.model() != this)
return false;
QByteArray encodedModelNodeData = mineData->data("application/vnd.modelnode.list");
QDataStream modelNodeStream(&encodedModelNodeData, QIODevice::ReadOnly);
QList<ModelNode> modelNodeList;
while (!modelNodeStream.atEnd()) {
qint32 internalId;
modelNodeStream >> internalId;
if (view->hasModelNodeForInternalId(internalId))
modelNodeList.append(view->modelNodeForInternalId(internalId));
}
QModelIndex parentIndex, parentItemIndex;
PropertyName parentPropertyName;
int targetIndex;
return modelNodeList;
}
parentIndex = dropIndex.sibling(dropIndex.row(), 0);
targetIndex = (row > -1)? row : rowCount(parentIndex);
bool fitsToTargetProperty(const NodeAbstractProperty &targetProperty,
const QList<ModelNode> &modelNodeList)
{
return !(targetProperty.isNodeProperty() &&
modelNodeList.count() > 1);
}
if (this->data(parentIndex, InternalIdRole).isValid()) {
parentItemIndex = parentIndex;
ModelNode parentNode = nodeForIndex(parentItemIndex);
if (!parentNode.metaInfo().hasDefaultProperty())
static bool computeTarget(const QModelIndex &rowModelIndex,
NavigatorTreeModel *navigatorTreeModel,
NodeAbstractProperty *targetProperty,
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;
targetIndex -= visibleProperties(parentNode).count();
parentPropertyName = parentNode.metaInfo().defaultPropertyName();
*targetRowNumber -= visibleProperties(targetNode).count();
targetPropertyName = targetNode.metaInfo().defaultPropertyName();
} else {
parentItemIndex = parentIndex.parent();
parentPropertyName = parentIndex.data(Qt::DisplayRole).toByteArray();
targetItemIndex = rowModelIndex.parent();
targetPropertyName = rowModelIndex.data(Qt::DisplayRole).toByteArray();
}
// Disallow dropping items between properties, which are listed first.
if (targetIndex < 0)
if (*targetRowNumber < 0)
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");
QDataStream stream(&encodedData, QIODevice::ReadOnly);
bool NavigatorTreeModel::dropMimeData(const QMimeData *mimeData,
Qt::DropAction action,
int rowNumber,
int columnNumber,
const QModelIndex &dropModelIndex)
{
if (action == Qt::IgnoreAction)
return true;
QList<ModelNode> nodeList;
while (!stream.atEnd()) {
qint32 internalId;
stream >> internalId;
if (m_view->hasModelNodeForInternalId(internalId))
nodeList.append(m_view->modelNodeForInternalId(internalId));
}
if (action != Qt::LinkAction)
return false;
ModelNode parentNode(nodeForIndex(parentItemIndex));
NodeAbstractProperty parentProperty = parentNode.nodeAbstractProperty(parentPropertyName);
if (!mimeData->hasFormat("application/vnd.modelnode.list"))
return false;
if (parentProperty.isNodeProperty() &&
nodeList.count() > 1) {
if (columnNumber > 1)
return false;
if (dropModelIndex.model() != this)
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
}
......
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