began drag n drop support in projectexplorer

This commit is contained in:
itsmattkc
2019-07-02 19:29:04 -07:00
parent f9543fba4e
commit a84add82f1
4 changed files with 92 additions and 0 deletions
+6
View File
@@ -28,6 +28,12 @@ Project::Project()
Folder* f = new Folder();
f->set_name("Foldery Boy");
root()->add_child(f);
Folder* f2 = new Folder();
f2->set_name("DarnMan");
root()->add_child(f2);
Folder* f3 = new Folder();
f3->set_name("DamnDude");
f2->add_child(f3);
// END Test code
}
+69
View File
@@ -20,6 +20,9 @@
#include "projectviewmodel.h"
#include <QDebug>
#include <QMimeData>
ProjectViewModel::ProjectViewModel(QObject *parent) :
QAbstractItemModel(parent),
project_(nullptr)
@@ -193,3 +196,69 @@ bool ProjectViewModel::hasChildren(const QModelIndex &parent) const
// Otherwise, return default behavior
return QAbstractItemModel::hasChildren(parent);
}
Qt::ItemFlags ProjectViewModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | QAbstractItemModel::flags(index);
}
QStringList ProjectViewModel::mimeTypes() const
{
return {"application/x-oliveprojectitemdata"};
}
QMimeData *ProjectViewModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData* data = new QMimeData();
QByteArray encoded_data;
QDataStream stream(&encoded_data, QIODevice::WriteOnly);
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
stream << reinterpret_cast<quintptr>(index.internalPointer());
}
}
data->setData("application/x-oliveprojectitemdata", encoded_data);
return data;
}
bool ProjectViewModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
// Default recommended checks from https://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views
if (!canDropMimeData(data, action, row, column, parent)) {
return false;
}
if (action == Qt::IgnoreAction) {
return true;
}
// Probe mime data for its format
QStringList mime_formats = data->formats();
if (mime_formats.contains("application/x-oliveprojectitemdata")) {
// Data is drag/drop data from this model
QByteArray model_data = data->data("application/x-oliveprojectitemdata");
// Use stream to deserialize the data
QDataStream stream(&model_data, QIODevice::ReadOnly);
// Variables to deserialize into
quintptr item_ptr;
// Loop through all data
while (!stream.atEnd()) {
stream >> item_ptr;
Item* item = reinterpret_cast<Item*>(item_ptr);
qDebug() << "Dragged" << item->name();
}
return true;
}
return false;
}
+5
View File
@@ -83,6 +83,11 @@ public:
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
/** Drag and drop support */
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
virtual QStringList mimeTypes() const override;
virtual QMimeData * mimeData(const QModelIndexList &indexes) const override;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
private:
Project* project_;
@@ -27,6 +27,18 @@ ProjectExplorerTreeView::ProjectExplorerTreeView(QWidget *parent) :
{
// Set selection mode (allows multiple item selection)
setSelectionMode(QAbstractItemView::ExtendedSelection);
// Allow dragging and dropping
setDragDropMode(QAbstractItemView::DragDrop);
// Enable dragging
setDragEnabled(true);
// Allow dropping from external sources
setAcceptDrops(true);
// Set context menu to emit a signal
setContextMenuPolicy(Qt::CustomContextMenu);
}
void ProjectExplorerTreeView::mouseDoubleClickEvent(QMouseEvent *event)