summaryrefslogtreecommitdiff
path: root/src/dic-options/DraggableListModel.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/dic-options/DraggableListModel.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/dic-options/DraggableListModel.cpp')
-rw-r--r--src/dic-options/DraggableListModel.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/dic-options/DraggableListModel.cpp b/src/dic-options/DraggableListModel.cpp
new file mode 100644
index 0000000..2ea0523
--- /dev/null
+++ b/src/dic-options/DraggableListModel.cpp
@@ -0,0 +1,83 @@
+#include "DraggableListModel.h"
+#include "../dictionary/CardPack.h"
+#include "../dictionary/Field.h"
+
+#include <QMimeData>
+
+Qt::ItemFlags DraggableListModel::flags(const QModelIndex &index) const
+ {
+ Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
+ if (index.isValid())
+ return Qt::ItemIsDragEnabled | defaultFlags;
+ else
+ return Qt::ItemIsDropEnabled | defaultFlags;
+ }
+
+Qt::DropActions DraggableListModel::supportedDropActions() const
+ {
+ return Qt::MoveAction;
+ }
+
+QStringList DraggableListModel::mimeTypes() const
+ {
+ QStringList types;
+ types << "application/octet-stream";
+ return types;
+ }
+
+QMimeData *DraggableListModel::mimeData(const QModelIndexList &indexes) const
+ {
+ QStringList list;
+ QModelIndexList validIndexes;
+ foreach (QModelIndex index, indexes)
+ if (index.isValid() && index.column() == 0)
+ validIndexes << index;
+ qSort(validIndexes);
+
+ int num = validIndexes.size();
+ QByteArray encodedData;
+ QDataStream stream( &encodedData, QIODevice::WriteOnly );
+ const void** ptrs = new const void*[num];
+ int i=0;
+ foreach (QModelIndex index, validIndexes)
+ ptrs[i++] = dataPtr( index );
+ stream.writeBytes( (char*)ptrs, num*sizeof(void*) );
+ delete ptrs;
+ QMimeData *mimeData = new QMimeData();
+ mimeData->setData( "application/octet-stream", encodedData );
+ return mimeData;
+ }
+
+bool DraggableListModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
+ int row, int /*column*/, const QModelIndex &parent)
+ {
+ if (action == Qt::IgnoreAction)
+ return true;
+ if (!data->hasFormat("application/octet-stream"))
+ return false;
+
+ int beginRow;
+ if (row != -1)
+ beginRow = row;
+ else if (parent.isValid())
+ beginRow = parent.row();
+ else
+ beginRow = rowCount(QModelIndex());
+
+ QByteArray encodedData = data->data("application/octet-stream");
+ QDataStream stream( &encodedData, QIODevice::ReadOnly );
+ void** ptrs = new void*[ rowCount() ];
+ uint num;
+ stream.readBytes( (char*&)ptrs, num ); //TODO: Avoid converting pointer to other types
+ num /= sizeof(void*);
+
+ QList<QPersistentModelIndex> movedIndexes;
+ for(uint i=0; i<num; i++)
+ {
+ insertPointer( beginRow + i, ptrs[i] );
+ movedIndexes << QPersistentModelIndex( index(beginRow + i, 0, QModelIndex()) );
+ }
+ delete ptrs;
+ emit indexesDropped( movedIndexes );
+ return true;
+ }