Add dropping of URLs and text on the data browser view

See issue #441.
This commit is contained in:
Martin Kleusberg
2015-12-19 19:02:38 +01:00
parent 672b6f693b
commit 9582786237
5 changed files with 65 additions and 1 deletions

View File

@@ -7,6 +7,9 @@
#include <QMessageBox>
#include <QApplication>
#include <QTextCodec>
#include <QMimeData>
#include <QFile>
#include <QUrl>
SqliteTableModel::SqliteTableModel(QObject* parent, DBBrowserDB* db, size_t chunkSize, const QString& encoding)
: QAbstractTableModel(parent)
@@ -336,7 +339,7 @@ Qt::ItemFlags SqliteTableModel::flags(const QModelIndex& index) const
if(!index.isValid())
return Qt::ItemIsEnabled;
Qt::ItemFlags ret = QAbstractTableModel::flags(index);
Qt::ItemFlags ret = QAbstractTableModel::flags(index) | Qt::ItemIsDropEnabled;
// Custom display format set?
bool custom_display_format = false;
@@ -621,3 +624,29 @@ QByteArray SqliteTableModel::decode(const QByteArray& str) const
else
return QTextCodec::codecForName(m_encoding.toUtf8())->toUnicode(str).toUtf8();
}
Qt::DropActions SqliteTableModel::supportedDropActions() const
{
return Qt::CopyAction;
}
bool SqliteTableModel::dropMimeData(const QMimeData* data, Qt::DropAction, int row, int column, const QModelIndex& parent)
{
// What has been dropped on the widget?
if(data->hasUrls())
{
// If it's a URL, open the file and paste the content in the current cell
QList<QUrl> urls = data->urls();
QFile file(urls.first().toLocalFile());
if(file.exists() && file.open(QFile::ReadOnly))
{
setData(index(row, column, parent), file.readAll());
return true;
}
} else if(data->hasText()) {
// If it's just text we can set the cell data directly
setData(index(row, column, parent), data->text());
}
return false;
}