Commit 1f748400 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Allow drag and drop between different kate instances

parent 54f3ce88
......@@ -11,6 +11,7 @@
#include "tabmimedata.h"
#include <QApplication>
#include <QDataStream>
#include <QDrag>
#include <QIcon>
#include <QMimeData>
......@@ -224,7 +225,21 @@ void KateTabBar::mouseMoveEvent(QMouseEvent *event)
auto parentViewSpace = qobject_cast<KateViewSpace *>(parentWidget());
Q_ASSERT(parentViewSpace);
auto mime = new TabMimeData(parentViewSpace, tabDocument(tab), tab);
auto view = parentViewSpace->currentView();
if (!view) {
return;
}
QByteArray data;
KTextEditor::Cursor cp = view->cursorPosition();
QDataStream ds(&data, QIODevice::WriteOnly);
ds << cp.line();
ds << cp.column();
ds << view->document()->url();
auto mime = new TabMimeData(parentViewSpace, tabDocument(tab));
mime->setData(QStringLiteral("application/kate.tab.mimedata"), data);
drag->setMimeData(mime);
drag->setPixmap(p);
QPoint hp;
......
......@@ -24,6 +24,7 @@
#include <QApplication>
#include <QClipboard>
#include <QDataStream>
#include <QHelpEvent>
#include <QMenu>
#include <QMessageBox>
......@@ -430,23 +431,26 @@ void KateViewSpace::closeDocument(KTextEditor::Document *doc)
}
}
bool KateViewSpace::acceptsDroppedTab(const class TabMimeData *tabMimeData)
bool KateViewSpace::acceptsDroppedTab(const QMimeData *md)
{
return tabMimeData && this != tabMimeData->sourceVS && // must not be same viewspace
viewManger() == tabMimeData->sourceVS->viewManger() && // for now we don't support dropping into different windows
!hasDocument(tabMimeData->doc);
if (auto tabMimeData = qobject_cast<const TabMimeData *>(md)) {
return this != tabMimeData->sourceVS && // must not be same viewspace
viewManger() == tabMimeData->sourceVS->viewManger() && // for now we don't support dropping into different windows
!hasDocument(tabMimeData->doc);
}
return TabMimeData::hasValidData(md);
}
void KateViewSpace::dragEnterEvent(QDragEnterEvent *e)
{
auto mimeData = qobject_cast<const TabMimeData *>(e->mimeData());
if (acceptsDroppedTab(mimeData)) {
if (acceptsDroppedTab(e->mimeData())) {
m_dropIndicator.reset(new QRubberBand(QRubberBand::Rectangle, this));
m_dropIndicator->setGeometry(rect());
m_dropIndicator->show();
e->acceptProposedAction();
return;
}
QWidget::dragEnterEvent(e);
}
......@@ -464,6 +468,17 @@ void KateViewSpace::dropEvent(QDropEvent *e)
e->accept();
return;
}
auto droppedData = TabMimeData::data(e->mimeData());
if (droppedData.has_value()) {
auto doc = KateApp::self()->documentManager()->openUrl(droppedData.value().url);
auto view = m_viewManager->activateView(doc);
if (view) {
view->setCursorPosition({droppedData.value().line, droppedData.value().col});
e->accept();
return;
}
}
QWidget::dropEvent(e);
}
......
......@@ -222,7 +222,7 @@ private Q_SLOTS:
void createNewDocument();
private:
bool acceptsDroppedTab(const class TabMimeData *tabMimeData);
bool acceptsDroppedTab(const class QMimeData *tabMimeData);
/**
* Returns the amount of documents in KateDocManager that currently
* have no tab in this tab bar.
......
#include "tabmimedata.h"
TabMimeData::TabMimeData(KateViewSpace *vs, KTextEditor::Document *d, int sourceTabIdx)
#include <QDataStream>
TabMimeData::TabMimeData(KateViewSpace *vs, KTextEditor::Document *d)
: QMimeData()
, sourceVS(vs)
, doc(d)
, tabIdx(sourceTabIdx)
{
}
bool TabMimeData::hasValidData(const QMimeData *md)
{
bool valid = md && md->hasFormat(QStringLiteral("application/kate.tab.mimedata"));
if (valid) {
QByteArray data = md->data(QStringLiteral("application/kate.tab.mimedata"));
QDataStream ds(data);
int line = 0;
int column = 0;
QUrl url;
ds >> line;
ds >> column;
ds >> url;
return url.isValid();
}
return false;
}
std::optional<TabMimeData::DroppedData> TabMimeData::data(const QMimeData *md)
{
if (!md || !md->hasFormat(QStringLiteral("application/kate.tab.mimedata"))) {
return {};
}
QByteArray data = md->data(QStringLiteral("application/kate.tab.mimedata"));
QDataStream ds(data);
TabMimeData::DroppedData d;
ds >> d.line;
ds >> d.col;
ds >> d.url;
if (!d.url.isValid()) {
return {};
}
return d;
}
......@@ -5,6 +5,8 @@
#include "kateviewspace.h"
#include <optional>
namespace KTextEditor
{
class Document;
......@@ -14,11 +16,20 @@ class TabMimeData : public QMimeData
{
Q_OBJECT
public:
TabMimeData(KateViewSpace *vs, KTextEditor::Document *d, int sourceTabIdx);
struct DroppedData {
int line = -1;
int col = -1;
QUrl url;
};
TabMimeData(KateViewSpace *vs, KTextEditor::Document *d);
static bool hasValidData(const QMimeData *md);
static std::optional<DroppedData> data(const QMimeData *md);
KateViewSpace *const sourceVS;
KTextEditor::Document *const doc;
const int tabIdx;
};
#endif
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