Commit f4e0327f authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[KateFileTreeModel] Implement dragging files from it

Allows to drag a file from the file tree.

BUG: 372201
FIXED-IN: 17.08.0

Differential Revision: https://phabricator.kde.org/D5600
parent ca266eed
......@@ -56,6 +56,8 @@ KateFileTree::KateFileTree(QWidget *parent): QTreeView(parent)
setIndentation(12);
setAllColumnsShowFocus(true);
setFocusPolicy(Qt::NoFocus);
setDragEnabled(true);
setDragDropMode(QAbstractItemView::DragOnly);
// handle activated (e.g. for pressing enter) + clicked (to avoid to need to do double-click e.g. on Windows)
connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(mouseClicked(QModelIndex)));
......
......@@ -22,6 +22,7 @@
#include <QDir>
#include <QFileInfo>
#include <QList>
#include <QMimeData>
#include <QMimeDatabase>
#include <QIcon>
#include <QStack>
......@@ -488,8 +489,14 @@ Qt::ItemFlags KateFileTreeModel::flags(const QModelIndex &index) const
}
const ProxyItem *item = static_cast<ProxyItem *>(index.internalPointer());
if (item && !item->childCount()) {
flags |= Qt::ItemIsSelectable;
if (item) {
if (!item->childCount()) {
flags |= Qt::ItemIsSelectable;
}
if (item->doc() && item->doc()->url().isValid()) {
flags |= Qt::ItemIsDragEnabled;
}
}
return flags;
......@@ -561,6 +568,28 @@ QVariant KateFileTreeModel::data(const QModelIndex &index, int role) const
return QVariant();
}
QMimeData *KateFileTreeModel::mimeData(const QModelIndexList &indexes) const
{
QList<QUrl> urls;
for (const auto &index : indexes) {
ProxyItem *item = static_cast<ProxyItem *>(index.internalPointer());
if (!item || !item->doc() || !item->doc()->url().isValid()) {
continue;
}
urls.append(item->doc()->url());
}
if (urls.isEmpty()) {
return nullptr;
}
QMimeData *mimeData = new QMimeData();
mimeData->setUrls(urls);
return mimeData;
}
QVariant KateFileTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(orientation);
......
......@@ -56,6 +56,8 @@ public:
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
/* extra api for view */
QModelIndex docIndex(const KTextEditor::Document *) const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment