Skip to content

Commit 2f3f65b

Browse files
committed
Get object to modify or delete from the Table Browser
This will allow to edit or delete the currently browsed table or view from the "Browse Table" tab. Before this change, the selected table or view was always taken form the "Database Structure" tab, even when it was invisible to the user. See issue #3939.
1 parent f37fd5d commit 2f3f65b

File tree

2 files changed

+67
-42
lines changed

2 files changed

+67
-42
lines changed

src/MainWindow.cpp

Lines changed: 65 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "MainWindow.h"
2+
#include "sql/ObjectIdentifier.h"
3+
#include "sql/sqlitetypes.h"
24
#include "ui_MainWindow.h"
35

46
#include "Application.h"
@@ -441,7 +443,7 @@ void MainWindow::init()
441443
connect(editDock, &EditDialog::recordTextUpdated, this, &MainWindow::updateRecordText);
442444
connect(editDock, &EditDialog::evaluateText, this, &MainWindow::evaluateText);
443445
connect(editDock, &EditDialog::requestUrlOrFileOpen, this, &MainWindow::openUrlOrFile);
444-
connect(ui->dbTreeWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::changeTreeSelection);
446+
connect(ui->dbTreeWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::changeObjectSelection);
445447
connect(ui->dockEdit, &QDockWidget::visibilityChanged, this, &MainWindow::toggleEditDock);
446448
connect(remoteDock, SIGNAL(openFile(QString)), this, SLOT(fileOpen(QString)));
447449
connect(ui->actionDropSelectQueryCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropSelectQuery);
@@ -916,8 +918,9 @@ void MainWindow::compact()
916918
void MainWindow::deleteObject()
917919
{
918920
// Get name and type of object to delete
919-
sqlb::ObjectIdentifier obj = dbSelected->object();
920-
QString type = dbSelected->objectType();
921+
sqlb::ObjectIdentifier obj;
922+
QString type;
923+
getSelectedObject(type, obj);
921924

922925
// Due to different grammar in languages (e.g. gender or declension), each message must be given separately to translation.
923926
QString message;
@@ -951,19 +954,45 @@ void MainWindow::deleteObject()
951954
QMessageBox::warning(this, QApplication::applicationName(), message + " " + error);
952955
} else {
953956
refreshTableBrowsers();
954-
changeTreeSelection();
957+
changeObjectSelection();
958+
}
959+
}
960+
}
961+
962+
void MainWindow::getSelectedObject(QString &type, sqlb::ObjectIdentifier& obj) {
963+
964+
type = "";
965+
obj = sqlb::ObjectIdentifier();
966+
967+
QWidget* currentTab = ui->mainTab->currentWidget();
968+
if (currentTab == ui->structure) {
969+
970+
if(!dbSelected->hasSelection())
971+
return;
972+
973+
// Get name and type of the object to edit
974+
obj = dbSelected->object();
975+
type = dbSelected->objectType();
976+
977+
} else if (currentTab == ui->browser) {
978+
// Get name of the current table from the Data Browser
979+
obj = currentlyBrowsedTableName();
980+
981+
sqlb::TablePtr tablePtr = db.getTableByName(obj);
982+
if (!tablePtr) {
983+
return;
984+
} else {
985+
type = tablePtr->isView()? "view" : "table";
955986
}
956987
}
957988
}
958989

959990
void MainWindow::editObject()
960991
{
961-
if(!dbSelected->hasSelection())
962-
return;
963-
964-
// Get name and type of the object to edit
965-
sqlb::ObjectIdentifier obj = dbSelected->object();
966-
QString type = dbSelected->objectType();
992+
QString type;
993+
sqlb::ObjectIdentifier obj;
994+
// Get name and type of object to edit
995+
getSelectedObject(type, obj);
967996

968997
if(type == "table")
969998
{
@@ -1113,6 +1142,7 @@ void MainWindow::dataTableSelectionChanged(const QModelIndex& index)
11131142
if (editDock->isVisible()) {
11141143
editDock->setCurrentIndex(index);
11151144
}
1145+
changeObjectSelection();
11161146
}
11171147

11181148
/*
@@ -1457,40 +1487,32 @@ void MainWindow::exportTableToCSV()
14571487
{
14581488
// Get the current table name if we are in the Browse Data tab
14591489
sqlb::ObjectIdentifier current_table;
1460-
if(ui->mainTab->currentWidget() == ui->structure)
1461-
{
1462-
QString type = dbSelected->objectType();
1463-
if(type == "table" || type == "view")
1464-
{
1465-
current_table = dbSelected->object();
1466-
}
1467-
} else if(ui->mainTab->currentWidget() == ui->browser) {
1468-
current_table = currentlyBrowsedTableName();
1469-
}
14701490

1471-
// Open dialog
1472-
ExportDataDialog dialog(db, ExportDataDialog::ExportFormatCsv, this, "", current_table);
1473-
dialog.exec();
1491+
QString type;
1492+
// Get name and type of object to export
1493+
getSelectedObject(type, current_table);
1494+
1495+
if(type == "table" || type == "view") {
1496+
// Open dialog
1497+
ExportDataDialog dialog(db, ExportDataDialog::ExportFormatCsv, this, "", current_table);
1498+
dialog.exec();
1499+
}
14741500
}
14751501

14761502
void MainWindow::exportTableToJson()
14771503
{
14781504
// Get the current table name if we are in the Browse Data tab
14791505
sqlb::ObjectIdentifier current_table;
1480-
if(ui->mainTab->currentWidget() == ui->structure)
1481-
{
1482-
QString type = dbSelected->objectType();
1483-
if(type == "table" || type == "view")
1484-
{
1485-
current_table = dbSelected->object();
1486-
}
1487-
} else if(ui->mainTab->currentWidget() == ui->browser) {
1488-
current_table = currentlyBrowsedTableName();
1489-
}
14901506

1491-
// Open dialog
1492-
ExportDataDialog dialog(db, ExportDataDialog::ExportFormatJson, this, "", current_table);
1493-
dialog.exec();
1507+
QString type;
1508+
// Get name and type of object to export
1509+
getSelectedObject(type, current_table);
1510+
1511+
if(type == "table" || type == "view") {
1512+
// Open dialog
1513+
ExportDataDialog dialog(db, ExportDataDialog::ExportFormatJson, this, "", current_table);
1514+
dialog.exec();
1515+
}
14941516
}
14951517

14961518
void MainWindow::dbState(bool dirty)
@@ -1638,7 +1660,7 @@ void MainWindow::createTreeContextMenu(const QPoint &qPoint)
16381660
if(type == "table" || type == "view" || type == "trigger" || type == "index" || type == "database")
16391661
{
16401662
// needed for first click on treeView as for first time change QItemSelectionModel::currentChanged doesn't fire
1641-
changeTreeSelection();
1663+
changeObjectSelection();
16421664
popupTableMenu->exec(ui->dbTreeWidget->mapToGlobal(qPoint));
16431665
}
16441666
}
@@ -1663,7 +1685,7 @@ void MainWindow::createSchemaDockContextMenu(const QPoint &qPoint)
16631685
popupSchemaDockMenu->exec(ui->treeSchemaDock->mapToGlobal(qPoint));
16641686
}
16651687

1666-
void MainWindow::changeTreeSelection()
1688+
void MainWindow::changeObjectSelection()
16671689
{
16681690
// Just assume first that something's selected that can not be edited at all
16691691
ui->editDeleteObjectAction->setEnabled(false);
@@ -1675,12 +1697,14 @@ void MainWindow::changeTreeSelection()
16751697

16761698
ui->fileDetachAction->setVisible(false);
16771699

1678-
if(!dbSelected->hasSelection())
1700+
QString type;
1701+
sqlb::ObjectIdentifier obj;
1702+
getSelectedObject(type, obj);
1703+
if(obj.isEmpty())
16791704
return;
16801705

16811706
// Change the text and tooltips of the actions
1682-
QString type = dbSelected->objectType();
1683-
QString schema = dbSelected->schema();
1707+
QString schema = QString::fromStdString(obj.schema());
16841708

16851709
if (type.isEmpty())
16861710
{

src/MainWindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ friend TableBrowserDock;
148148
sqlb::ObjectIdentifier currentlyBrowsedTableName() const;
149149

150150
QList<TableBrowserDock*> allTableBrowserDocks() const;
151+
void getSelectedObject(QString &type, sqlb::ObjectIdentifier& obj);
151152

152153
protected:
153154
void closeEvent(QCloseEvent *) override;
@@ -172,7 +173,7 @@ public slots:
172173
private slots:
173174
void createTreeContextMenu(const QPoint & qPoint);
174175
void createSchemaDockContextMenu(const QPoint & qPoint);
175-
void changeTreeSelection();
176+
void changeObjectSelection();
176177
void fileNew();
177178
void fileNewInMemoryDatabase(bool open_create_dialog = true);
178179
// Refresh visible table browsers. When all is true, refresh all browsers.

0 commit comments

Comments
 (0)