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

@@ -155,3 +155,20 @@ void ExtendedTableWidget::cellClicked(const QModelIndex& index)
emit foreignKeyClicked(fk.table(), fk.columns().size() ? fk.columns().at(0) : "", m->data(index, Qt::EditRole).toByteArray());
}
}
void ExtendedTableWidget::dragEnterEvent(QDragEnterEvent* event)
{
event->accept();
}
void ExtendedTableWidget::dragMoveEvent(QDragMoveEvent* event)
{
event->accept();
}
void ExtendedTableWidget::dropEvent(QDropEvent* event)
{
QModelIndex index = indexAt(event->pos());
model()->dropMimeData(event->mimeData(), Qt::CopyAction, index.row(), index.column(), QModelIndex());
event->acceptProposedAction();
}

View File

@@ -4,6 +4,8 @@
#include <QTableView>
#include "FilterTableHeader.h"
#include <QSet>
#include <QDropEvent>
#include <QDragMoveEvent>
class ExtendedTableWidget : public QTableView
{
@@ -31,6 +33,9 @@ private slots:
protected:
virtual void keyPressEvent(QKeyEvent* event);
virtual void updateGeometries();
virtual void dragEnterEvent(QDragEnterEvent* event);
virtual void dragMoveEvent(QDragMoveEvent* event);
virtual void dropEvent(QDropEvent* event);
FilterTableHeader* m_tableHeader;
};

View File

@@ -182,12 +182,21 @@
</item>
<item>
<widget class="ExtendedTableWidget" name="dataTable">
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="whatsThis">
<string>This is the database view. You can double-click any record to edit its contents in the cell editor window.</string>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::CopyAction</enum>
</property>
<attribute name="horizontalHeaderShowSortIndicator" stdset="0">
<bool>true</bool>
</attribute>

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;
}

View File

@@ -50,6 +50,10 @@ public:
public slots:
void updateFilter(int column, const QString& value);
protected:
virtual Qt::DropActions supportedDropActions() const;
virtual bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
private:
void fetchData(unsigned int from, unsigned to);
void clearCache();