DbStructureModel: Enable dragging of DB objects

Allow dragging (as in Drag & Drop) of database objects like tables and
views into other applications by sending the SQL code used to create
them.
This commit is contained in:
Martin Kleusberg
2013-07-19 20:55:48 +02:00
parent 64a938716f
commit 0e880cc4e6
3 changed files with 44 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
#include "DbStructureModel.h"
#include "sqlitedb.h"
#include <QTreeWidgetItem>
#include <QMimeData>
DbStructureModel::DbStructureModel(QObject* parent)
: QAbstractItemModel(parent)
@@ -43,7 +44,15 @@ Qt::ItemFlags DbStructureModel::flags(const QModelIndex &index) const
if(!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
// All items are enabled and selectable
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
// Only enable dragging for entire table objects
QString type = data(index.sibling(index.row(), 1), Qt::DisplayRole).toString();
if(type == "table" || type == "view" || type == "index" || type == "trigger")
flags |= Qt::ItemIsDragEnabled;
return flags;
}
QVariant DbStructureModel::headerData(int section, Qt::Orientation orientation, int role) const
@@ -153,3 +162,27 @@ void DbStructureModel::reloadData(DBBrowserDB* db)
// Refresh the view
reset();
}
QStringList DbStructureModel::mimeTypes() const
{
QStringList types;
types << "text/plain";
return types;
}
QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
{
// Loop through selected indices
QByteArray d;
foreach(QModelIndex index, indices)
{
// Only export data for valid indices and only for the SQL column, i.e. only once per row
if(index.isValid() && index.column() == 3)
d = d.append(data(index, Qt::DisplayRole).toString() + ";\n");
}
// Create the MIME data object
QMimeData* mime = new QMimeData();
mime->setData("text/plain", d);
return mime;
}

View File

@@ -23,6 +23,9 @@ public:
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& = QModelIndex()) const;
QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList& indices) const;
private:
QTreeWidgetItem* rootItem;
};

View File

@@ -44,6 +44,12 @@
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragOnly</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
@@ -266,7 +272,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>278</width>
<width>763</width>
<height>444</height>
</rect>
</property>