From 012ad9217a9bb71264bb6c5ad3a1da847610effc Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Mon, 15 Jan 2018 23:10:23 +0100 Subject: [PATCH] Fix drag & drop of tables onto the structure view When dragging and dropping a table from one instance of the application to the other, the tree structure representing the database was broken. We would show the 'Browsables' and 'All' nodes at the top level instead of the child nodes of the 'All' node. This happened because after dropping a table, we would reload the database structure and rebuild the tree structure but didn't notify the tree view in the main window about the update. This is fixed by this commit, so the main window's widgets are always notified about the new tree structure. See issue #1288. --- src/DbStructureModel.cpp | 3 ++- src/DbStructureModel.h | 8 ++++++-- src/MainWindow.cpp | 13 +++++++------ src/MainWindow.h | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/DbStructureModel.cpp b/src/DbStructureModel.cpp index 73fdf1cc..e9623548 100644 --- a/src/DbStructureModel.cpp +++ b/src/DbStructureModel.cpp @@ -138,6 +138,7 @@ void DbStructureModel::reloadData() if(!m_db.isOpen()) { endResetModel(); + emit structureUpdated(); return; } @@ -179,6 +180,7 @@ void DbStructureModel::reloadData() // Refresh the view endResetModel(); + emit structureUpdated(); } QStringList DbStructureModel::mimeTypes() const @@ -263,7 +265,6 @@ bool DbStructureModel::dropMimeData(const QMimeData* data, Qt::DropAction action if(m_db.executeMultiSQL(d, true, true)) { m_db.updateSchema(); - reloadData(); return true; } else { QMessageBox::warning(nullptr, QApplication::applicationName(), m_db.lastError()); diff --git a/src/DbStructureModel.h b/src/DbStructureModel.h index 83d3446c..43b0d5cd 100644 --- a/src/DbStructureModel.h +++ b/src/DbStructureModel.h @@ -15,8 +15,6 @@ public: explicit DbStructureModel(DBBrowserDB& db, QObject* parent = nullptr); ~DbStructureModel(); - void reloadData(); - QVariant data(const QModelIndex& index, int role) const; Qt::ItemFlags flags(const QModelIndex& index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; @@ -38,6 +36,12 @@ public: ColumnSchema, }; +public slots: + void reloadData(); + +signals: + void structureUpdated(); + private: DBBrowserDB& m_db; QTreeWidgetItem* rootItem; diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 8f22c924..fdc0dcd8 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -92,7 +92,6 @@ void MainWindow::init() // Connect SQL logging and database state setting to main window connect(&db, SIGNAL(dbChanged(bool)), this, SLOT(dbState(bool))); connect(&db, SIGNAL(sqlExecuted(QString, int)), this, SLOT(logSql(QString,int))); - connect(&db, SIGNAL(structureUpdated()), this, SLOT(populateStructure())); connect(&db, &DBBrowserDB::requestCollation, this, &MainWindow::requestCollation); // Set the validator for the goto line edit @@ -107,6 +106,11 @@ void MainWindow::init() // Set up DB structure tab dbStructureModel = new DbStructureModel(db, this); + connect(&db, &DBBrowserDB::structureUpdated, [this]() { + QString old_table = ui->comboBrowseTable->currentText(); + dbStructureModel->reloadData(); + populateStructure(old_table); + }); ui->dbTreeWidget->setModel(dbStructureModel); ui->dbTreeWidget->setColumnWidth(DbStructureModel::ColumnName, 300); ui->dbTreeWidget->setColumnHidden(DbStructureModel::ColumnObjectType, true); @@ -396,12 +400,9 @@ void MainWindow::fileNew() } } -void MainWindow::populateStructure() +void MainWindow::populateStructure(const QString& old_table) { - QString old_table = ui->comboBrowseTable->currentText(); - // Refresh the structure tab - dbStructureModel->reloadData(); ui->dbTreeWidget->setRootIndex(dbStructureModel->index(1, 0)); // Show the 'All' part of the db structure ui->dbTreeWidget->expandToDepth(0); ui->treeSchemaDock->setRootIndex(dbStructureModel->index(1, 0)); // Show the 'All' part of the db structure @@ -1970,7 +1971,7 @@ void MainWindow::reloadSettings() loadExtensionsFromSettings(); // Refresh view - populateStructure(); + dbStructureModel->reloadData(); populateTable(); // Hide or show the remote dock as needed diff --git a/src/MainWindow.h b/src/MainWindow.h index dbbdc75b..242617af 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -213,7 +213,7 @@ public slots: void refresh(); void jumpToRow(const sqlb::ObjectIdentifier& table, QString column, const QByteArray& value); void switchToBrowseDataTab(QString tableToBrowse = QString()); - void populateStructure(); + void populateStructure(const QString& old_table = QString()); private slots: void createTreeContextMenu(const QPoint & qPoint);