Skip to content

Commit

Permalink
DbStructureModel: Enable dragging of DB objects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MKleusberg committed Jul 19, 2013
1 parent 64a9387 commit 0e880cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/DbStructureModel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "DbStructureModel.h"
#include "sqlitedb.h"
#include <QTreeWidgetItem>
#include <QMimeData>

DbStructureModel::DbStructureModel(QObject* parent)
: QAbstractItemModel(parent)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions src/DbStructureModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class DbStructureModel : public QAbstractItemModel
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;
};
Expand Down
8 changes: 7 additions & 1 deletion src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -266,7 +272,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>278</width>
<width>763</width>
<height>444</height>
</rect>
</property>
Expand Down

0 comments on commit 0e880cc

Please sign in to comment.