bool TableModelVariables::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { if (_dragActions == Qt::CopyAction && _dropActions == Qt::MoveAction && action == Qt::MoveAction) // if delete return true; if ( ! canDropMimeData(data, action, row, column, parent)) return false; if (action == Qt::IgnoreAction) return true; if (data->hasFormat(_mimeType)) { QByteArray encodedData = data->data(_mimeType); Terms variables; variables.set(encodedData); beginResetModel(); _variables.add(variables); endResetModel(); return true; } return false; }
/* bool RunsTableModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const { qfLogFuncFrame() << "row:" << row << "col:" << column << action << parent; Q_UNUSED(action); Q_UNUSED(row); Q_UNUSED(parent); if (!data->hasFormat(MIME_TYPE)) return false; if (column > 0) return false; qfInfo() << "TRUE"; return true; } */ bool RunsTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { qfLogFuncFrame() << "row:" << row << "col:" << column << "parent:" << parent; if (!canDropMimeData(data, action, row, column, parent)) return false; if (action == Qt::IgnoreAction) return true; if (!parent.isValid()) { // not drop on item return false; } int r1 = QString::fromUtf8(data->data(MIME_TYPE)).toInt(); int r2 = parent.row(); qfDebug() << "switch:" << r1 << "and" << r2; switchStartTimes(r1, r2); return true; }
//! [0] //! [1] bool DragDropListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { if (!canDropMimeData(data, action, row, column, parent)) return false; if (action == Qt::IgnoreAction) return true; //! [1] //! [2] int beginRow; if (row != -1) beginRow = row; //! [2] //! [3] else if (parent.isValid()) beginRow = parent.row(); //! [3] //! [4] else beginRow = rowCount(QModelIndex()); //! [4] //! [5] QByteArray encodedData = data->data("application/vnd.text.list"); QDataStream stream(&encodedData, QIODevice::ReadOnly); QStringList newItems; int rows = 0; while (!stream.atEnd()) { QString text; stream >> text; newItems << text; ++rows; } //! [5] //! [6] insertRows(beginRow, rows, QModelIndex()); foreach (const QString &text, newItems) { QModelIndex idx = index(beginRow, 0, QModelIndex()); setData(idx, text); beginRow++; }
bool RSSModel::dropMimeData(const QMimeData *data, Qt::DropAction action , int row, int column, const QModelIndex &parent) { Q_UNUSED(row); Q_UNUSED(column); if (!canDropMimeData(data, action, row, column, parent)) return false; if (action == Qt::IgnoreAction) return true; // check if the format is supported const QStringList types = mimeTypes(); if (types.isEmpty()) return false; const QString format = types.at(0); Q_ASSERT(format == INTERNAL_MIME_TYPE); if (!data->hasFormat(format)) return false; RSS::Folder *destFolder = parent.isValid() ? static_cast<RSS::Folder *>(getAttachedItem(parent)) : RSS::Manager::instance()->rootFolder(); // move as much items as possible QByteArray encoded = data->data(format); QDataStream stream(&encoded, QIODevice::ReadOnly); while (!stream.atEnd()) { quintptr itemPtr; stream >> itemPtr; RSS::Item *item = reinterpret_cast<RSS::Item *>(itemPtr); Q_ASSERT(item); try { RSS::Manager::instance()->moveItem(*item, *destFolder); } catch (const RuntimeError &) {} } return true; }