bool DomModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { if (action == Qt::IgnoreAction) return true; if (!data->hasFormat("text/plain")) return false; // Find out after which row to insert int beginRow; if (row != -1) // given beginRow = row; else if (parent.isValid()) // 1st of parent item beginRow = 0; else // last of the root item beginRow = rowCount(QModelIndex()); DomItem *parentItem; if (!parent.isValid()) parentItem = rootItem; else parentItem = static_cast<DomItem*>(parent.internalPointer()); // Extract dropped items QByteArray encodedData = data->data("text/plain"); QDataStream stream(&encodedData, QIODevice::ReadOnly); QList<DomItem*> newItems; while (!stream.atEnd()) { quintptr p; stream >> p; newItems << static_cast<DomItem*>((void *)p); } int count = newItems.count(); switch(action) { case Qt::MoveAction: if (parentItem == rootItem) return false; for (int i=0; i<count; i++) { DomItem *item = newItems.at(i); QDomElement node = item->element().cloneNode().toElement(); DomItem *newItem = new DomItem(node); insertRow(beginRow, parent, newItem); } break; default: return false; } return true; }
int DomModel::rowCount(const QModelIndex &parent) const { if (parent.column() > 0) return 0; DomItem *parentItem; int sitesCount = 0; // This is the number of site elements of root item if (!parent.isValid()) { parentItem = rootItem; sitesCount = domDocument.elementsByTagName("site").count(); } else parentItem = static_cast<DomItem*>(parent.internalPointer()); return parentItem->element().childNodes().count() - sitesCount; }