Skip to content

Commit

Permalink
DbStructureModel: Allow dropping of database objects
Browse files Browse the repository at this point in the history
Make it possible to drop database objects (i.e. SQL code) on the tree
view in the Database Structure tab. This is far from being perfect as of
now but enables you to drag and drop entire objects between databases.
  • Loading branch information
MKleusberg committed Jul 19, 2013
1 parent 0e880cc commit 6e0c68a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
38 changes: 35 additions & 3 deletions src/DbStructureModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "sqlitedb.h"
#include <QTreeWidgetItem>
#include <QMimeData>
#include <QMessageBox>
#include <QApplication>

DbStructureModel::DbStructureModel(QObject* parent)
: QAbstractItemModel(parent)
Expand Down Expand Up @@ -42,15 +44,15 @@ QVariant DbStructureModel::data(const QModelIndex& index, int role) const
Qt::ItemFlags DbStructureModel::flags(const QModelIndex &index) const
{
if(!index.isValid())
return 0;
return Qt::ItemIsDropEnabled;

// All items are enabled and selectable
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;

// 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;
flags |= Qt::ItemIsDragEnabled;

return flags;
}
Expand Down Expand Up @@ -109,6 +111,9 @@ int DbStructureModel::rowCount(const QModelIndex& parent) const

void DbStructureModel::reloadData(DBBrowserDB* db)
{
// Save pointer to DB object
m_db = db;

// Remove all data except for the root item
while(rootItem->childCount())
{
Expand Down Expand Up @@ -183,6 +188,33 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const

// Create the MIME data object
QMimeData* mime = new QMimeData();
mime->setProperty("db_file", m_db->curDBFilename); // Also save the file name to avoid dropping an object on the same database as it comes from
mime->setData("text/plain", d);
return mime;
}

bool DbStructureModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&)
{
if(action == Qt::IgnoreAction)
return true;

if(!data->hasFormat("text/plain"))
return false;

if(data->property("db_file") == m_db->curDBFilename)
return false;

// Get data
QByteArray d = data->data("text/plain");

// Try to execute the SQL statement
if(m_db->executeMultiSQL(d, true, true))
{
m_db->updateSchema();
reloadData(m_db);
return true;
} else {
QMessageBox::warning(0, QApplication::applicationName(), m_db->lastErrorMessage);
return false;
}
}
2 changes: 2 additions & 0 deletions src/DbStructureModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class DbStructureModel : public QAbstractItemModel

QStringList mimeTypes() const;
QMimeData* mimeData(const QModelIndexList& indices) const;
bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);

private:
QTreeWidgetItem* rootItem;
DBBrowserDB* m_db;
};

#endif
2 changes: 1 addition & 1 deletion src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::DragOnly</enum>
<enum>QAbstractItemView::DragDrop</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
Expand Down

0 comments on commit 6e0c68a

Please sign in to comment.