From 0e880cc4e65e1e1d4460c9009b6ba07ea8dfa2fd Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 19 Jul 2013 20:55:48 +0200 Subject: [PATCH] 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. --- src/DbStructureModel.cpp | 35 ++++++++++++++++++++++++++++++++++- src/DbStructureModel.h | 3 +++ src/MainWindow.ui | 8 +++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/DbStructureModel.cpp b/src/DbStructureModel.cpp index e9a6e5031..e77b6be46 100644 --- a/src/DbStructureModel.cpp +++ b/src/DbStructureModel.cpp @@ -1,6 +1,7 @@ #include "DbStructureModel.h" #include "sqlitedb.h" #include +#include 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; +} diff --git a/src/DbStructureModel.h b/src/DbStructureModel.h index 7cb18f432..476bd5aa7 100644 --- a/src/DbStructureModel.h +++ b/src/DbStructureModel.h @@ -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; }; diff --git a/src/MainWindow.ui b/src/MainWindow.ui index 11038f62f..a86dd4176 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -44,6 +44,12 @@ Qt::CustomContextMenu + + true + + + QAbstractItemView::DragOnly + true @@ -266,7 +272,7 @@ 0 0 - 278 + 763 444