mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36:34 +01:00
TabModel: Implement drag&drop
This commit is contained in:
parent
8fe53f1c2c
commit
72c4b605a5
@ -21,6 +21,8 @@
|
||||
#include "tabbedwebview.h"
|
||||
#include "browserwindow.h"
|
||||
|
||||
#include <QMimeData>
|
||||
|
||||
TabModel::TabModel(BrowserWindow *window, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
, m_window(window)
|
||||
@ -41,6 +43,14 @@ int TabModel::rowCount(const QModelIndex &parent) const
|
||||
return m_window ? m_window->tabCount() : 0;
|
||||
}
|
||||
|
||||
Qt::ItemFlags TabModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return Qt::ItemIsDropEnabled;
|
||||
}
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
|
||||
}
|
||||
|
||||
QVariant TabModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!m_window || index.row() < 0 || index.row() > m_window->tabCount()) {
|
||||
@ -81,6 +91,71 @@ QVariant TabModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
}
|
||||
|
||||
Qt::DropActions TabModel::supportedDropActions() const
|
||||
{
|
||||
return Qt::MoveAction;
|
||||
}
|
||||
|
||||
#define MIMETYPE QStringLiteral("application/falkon.tabmodel.tab")
|
||||
|
||||
QStringList TabModel::mimeTypes() const
|
||||
{
|
||||
return {MIMETYPE};
|
||||
}
|
||||
|
||||
QMimeData *TabModel::mimeData(const QModelIndexList &indexes) const
|
||||
{
|
||||
QByteArray data;
|
||||
QDataStream stream(&data, QIODevice::WriteOnly);
|
||||
|
||||
for (const QModelIndex &index : indexes) {
|
||||
if (index.isValid() && index.column() == 0) {
|
||||
stream << index.row();
|
||||
}
|
||||
}
|
||||
|
||||
QMimeData *mimeData = new QMimeData();
|
||||
mimeData->setData(MIMETYPE, data);
|
||||
return mimeData;
|
||||
}
|
||||
|
||||
bool TabModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
if (action == Qt::IgnoreAction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!m_window || !data->hasFormat(MIMETYPE) || parent.isValid() || column != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray encodedData = data->data(MIMETYPE);
|
||||
QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
||||
|
||||
QVector<WebTab*> tabs;
|
||||
while (!stream.atEnd()) {
|
||||
int index;
|
||||
stream >> index;
|
||||
WebTab *tab = webTab(index);
|
||||
if (tab) {
|
||||
tabs.append(tab);
|
||||
}
|
||||
}
|
||||
|
||||
if (tabs.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < tabs.count(); ++i) {
|
||||
const int from = tabs.at(i)->tabIndex();
|
||||
const int to = row >= from ? row - 1 : row++;
|
||||
// FIXME: This switches order when moving > 2 non-contiguous indices
|
||||
m_window->tabWidget()->moveTab(from, to);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TabModel::init()
|
||||
{
|
||||
for (int i = 0; i < m_window->tabCount(); ++i) {
|
||||
|
@ -44,8 +44,14 @@ public:
|
||||
WebTab *webTab(int row) const;
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
Qt::DropActions supportedDropActions() const override;
|
||||
QStringList mimeTypes() const override;
|
||||
QMimeData *mimeData(const QModelIndexList &indexes) const override;
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
|
||||
|
||||
private:
|
||||
void init();
|
||||
void tabInserted(int index);
|
||||
|
Loading…
Reference in New Issue
Block a user