Skip to content
Permalink
Browse files

NOISSUE Try to enable dragging accross views

  • Loading branch information...
Janrupf authored and peterix committed Jun 19, 2019
1 parent cec5f73 commit 4233e0c014bc35d26ae7b362a71bfaae8bb58a97
@@ -21,6 +21,7 @@
#include <QString>
#include <QFileSystemWatcher>
#include <QDebug>
#include <QtCore/QDataStream>

SimpleModList::SimpleModList(const QString &dir) : QAbstractListModel(), m_dir(dir)
{
@@ -313,8 +314,7 @@ Qt::ItemFlags SimpleModList::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
if (index.isValid())
return Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled |
defaultFlags;
return Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
@@ -325,13 +325,19 @@ Qt::DropActions SimpleModList::supportedDropActions() const
return Qt::CopyAction | Qt::MoveAction;
}

Qt::DropActions SimpleModList::supportedDragActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}

QStringList SimpleModList::mimeTypes() const
{
QStringList types;
types << "text/uri-list";
return types;
}


bool SimpleModList::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&)
{
if (action == Qt::IgnoreAction)
@@ -363,3 +369,22 @@ bool SimpleModList::dropMimeData(const QMimeData* data, Qt::DropAction action, i
}
return false;
}

QMimeData *SimpleModList::mimeData(const QModelIndexList &indexes) const
{
auto *mimeData = new QMimeData();
QByteArray encodedData;

QDataStream stream(&encodedData, QIODevice::WriteOnly);
for(const auto &index : indexes)
{
if(index.isValid())
{
auto mod = mods[index.row()];
stream << "file://" << mod.filename().absoluteFilePath() << "\n";
}
}

mimeData->setData("text/uri-list", encodedData);
return mimeData;
}
@@ -49,11 +49,13 @@ class MULTIMC_LOGIC_EXPORT SimpleModList : public QAbstractListModel
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Qt::DropActions supportedDropActions() const override;
Qt::DropActions supportedDragActions() const override;

/// flags, mostly to support drag&drop
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
QStringList mimeTypes() const override;
bool dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent) override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;

virtual int rowCount(const QModelIndex &) const override
{
@@ -35,8 +35,10 @@ ModListView::ModListView ( QWidget* parent )
setHorizontalScrollBarPolicy ( Qt::ScrollBarAsNeeded );
setDropIndicatorShown(true);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DropOnly);
setDragDropMode(QAbstractItemView::DragDrop);
viewport()->setAcceptDrops(true);
setAcceptDrops(true);
setDefaultDropAction(Qt::CopyAction);
}

void ModListView::setModel ( QAbstractItemModel* model )
@@ -64,3 +66,18 @@ void ModListView::setModel ( QAbstractItemModel* model )
head->setSectionResizeMode(i, QHeaderView::ResizeToContents);
}
}

void ModListView::dragEnterEvent(QDragEnterEvent *event)
{
event->accept();
}

void ModListView::dragMoveEvent(QDragMoveEvent *event)
{
event->accept();
}

void ModListView::dropEvent(QDropEvent *event)
{
QAbstractItemView::dropEvent(event);
}
@@ -22,6 +22,10 @@ class ModListView: public QTreeView
{
Q_OBJECT
public:
explicit ModListView ( QWidget* parent = 0 );
virtual void setModel ( QAbstractItemModel* model );
explicit ModListView ( QWidget* parent = nullptr );
void setModel ( QAbstractItemModel* model ) override;

void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dropEvent(QDropEvent *event) override;
};

0 comments on commit 4233e0c

Please sign in to comment.
You can’t perform that action at this time.