mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
Synchronize PlotDock with 'Execute SQL' table. Continuation of PR #1271
Make use of signals to connect the selection in plot to the associated table widget. Every time that the plot is updated from the Main Window the table widget associated to the table or query is connected to the plot and the previous widget is disconnected. This allows the selection of the correct table widget. Line selection methods moved to the Extended Table Widget to be used as slots for this connection. The destroyed signal is also connected for resetting the plot. This fixes a crash that already existed before this PR, when closing a SQL tab while the plot is still associated to the table results model.
This commit is contained in:
@@ -657,3 +657,40 @@ void ExtendedTableWidget::dropEvent(QDropEvent* event)
|
||||
model()->dropMimeData(event->mimeData(), Qt::CopyAction, index.row(), index.column(), QModelIndex());
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
|
||||
void ExtendedTableWidget::selectTableLine(int lineToSelect)
|
||||
{
|
||||
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(model());
|
||||
|
||||
// Are there even that many lines?
|
||||
if(lineToSelect >= m->totalRowCount())
|
||||
return;
|
||||
|
||||
QApplication::setOverrideCursor( Qt::WaitCursor );
|
||||
// Make sure this line has already been fetched
|
||||
while(lineToSelect >= m->rowCount() && m->canFetchMore())
|
||||
m->fetchMore();
|
||||
|
||||
// Select it
|
||||
clearSelection();
|
||||
selectRow(lineToSelect);
|
||||
scrollTo(currentIndex(), QAbstractItemView::PositionAtTop);
|
||||
QApplication::restoreOverrideCursor();
|
||||
}
|
||||
|
||||
void ExtendedTableWidget::selectTableLines(int firstLine, int count)
|
||||
{
|
||||
SqliteTableModel* m = qobject_cast<SqliteTableModel*>(model());
|
||||
|
||||
int lastLine = firstLine+count-1;
|
||||
// Are there even that many lines?
|
||||
if(lastLine >= m->totalRowCount())
|
||||
return;
|
||||
|
||||
selectTableLine(firstLine);
|
||||
|
||||
QModelIndex topLeft = m->index(firstLine, 0);
|
||||
QModelIndex bottomRight = m->index(lastLine, m->columnCount()-1);
|
||||
|
||||
selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ public:
|
||||
|
||||
public slots:
|
||||
void reloadSettings();
|
||||
void selectTableLine(int lineToSelect);
|
||||
void selectTableLines(int firstLine, int count);
|
||||
|
||||
signals:
|
||||
void foreignKeyClicked(const sqlb::ObjectIdentifier& table, const QString& column, const QByteArray& value);
|
||||
|
||||
@@ -102,7 +102,7 @@ void MainWindow::init()
|
||||
connect(m_browseTableModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataTableSelectionChanged(QModelIndex)));
|
||||
|
||||
// Select in table the rows correspoding to the selected points in plot
|
||||
connect(plotDock, SIGNAL(pointsSelected(int,int)), this, SLOT(selectTableLines(int,int)));
|
||||
connect(plotDock, SIGNAL(pointsSelected(int,int)), ui->dataTable, SLOT(selectTableLines(int,int)));
|
||||
|
||||
// Set up DB structure tab
|
||||
dbStructureModel = new DbStructureModel(db, this);
|
||||
@@ -531,7 +531,7 @@ void MainWindow::populateTable()
|
||||
m_browseTableModel->setEncoding(defaultBrowseTableEncoding);
|
||||
|
||||
// Plot
|
||||
plotDock->updatePlot(m_browseTableModel, &browseTableSettings[tablename]);
|
||||
attachPlot(ui->dataTable, m_browseTableModel, &browseTableSettings[tablename]);
|
||||
|
||||
// The filters can be left empty as they are
|
||||
} else {
|
||||
@@ -588,7 +588,7 @@ void MainWindow::populateTable()
|
||||
m_browseTableModel->setEncoding(storedData.encoding);
|
||||
|
||||
// Plot
|
||||
plotDock->updatePlot(m_browseTableModel, &browseTableSettings[tablename], true, false);
|
||||
attachPlot(ui->dataTable, m_browseTableModel, &browseTableSettings[tablename], false);
|
||||
}
|
||||
|
||||
// Show/hide menu options depending on whether this is a table or a view
|
||||
@@ -630,8 +630,8 @@ bool MainWindow::fileClose()
|
||||
// Reset the recordset label inside the Browse tab now
|
||||
setRecordsetLabel();
|
||||
|
||||
// Reset the plot dock model
|
||||
plotDock->updatePlot(nullptr);
|
||||
// Reset the plot dock model and connection
|
||||
attachPlot(nullptr, nullptr);
|
||||
|
||||
activateFields(false);
|
||||
|
||||
@@ -695,69 +695,22 @@ void MainWindow::deleteRecord()
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::selectCurrentTabTableLines(int firstLine, int lastLine)
|
||||
void MainWindow::attachPlot(ExtendedTableWidget* tableWidget, SqliteTableModel* model, BrowseDataTableSettings* settings, bool keepOrResetSelection)
|
||||
{
|
||||
if(lastLine >= m_currentTabTableModel->totalRowCount())
|
||||
return;
|
||||
plotDock->updatePlot(model, settings, true, keepOrResetSelection);
|
||||
// Disconnect previous connection
|
||||
disconnect(plotDock, SIGNAL(pointsSelected(int,int)), nullptr, nullptr);
|
||||
if(tableWidget) {
|
||||
// Connect plot selection to the current table results widget.
|
||||
connect(plotDock, SIGNAL(pointsSelected(int,int)), tableWidget, SLOT(selectTableLines(int,int)));
|
||||
connect(tableWidget, SIGNAL(destroyed()), plotDock, SLOT(resetPlot()));
|
||||
|
||||
SqlExecutionArea *qw = (SqlExecutionArea*)ui->tabSqlAreas->currentWidget();
|
||||
ExtendedTableWidget *tw = qw->getTableResult();
|
||||
|
||||
if(firstLine >= m_currentTabTableModel->totalRowCount())
|
||||
return;
|
||||
|
||||
QApplication::setOverrideCursor( Qt::WaitCursor );
|
||||
// Make sure this line has already been fetched
|
||||
while(firstLine >= m_currentTabTableModel->rowCount() && m_currentTabTableModel->canFetchMore())
|
||||
m_currentTabTableModel->fetchMore();
|
||||
|
||||
// Select it
|
||||
tw->clearSelection();
|
||||
tw->selectRow(firstLine);
|
||||
tw->scrollTo(tw->currentIndex(), QAbstractItemView::PositionAtTop);
|
||||
QApplication::restoreOverrideCursor();
|
||||
|
||||
QModelIndex topLeft = m_currentTabTableModel->index(firstLine, 0);
|
||||
QModelIndex bottomRight = m_currentTabTableModel->index(lastLine, m_currentTabTableModel->columnCount()-1);
|
||||
|
||||
tw->selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::selectTableLine(int lineToSelect)
|
||||
{
|
||||
// Are there even that many lines?
|
||||
if(lineToSelect >= m_browseTableModel->totalRowCount())
|
||||
return;
|
||||
|
||||
QApplication::setOverrideCursor( Qt::WaitCursor );
|
||||
// Make sure this line has already been fetched
|
||||
while(lineToSelect >= m_browseTableModel->rowCount() && m_browseTableModel->canFetchMore())
|
||||
m_browseTableModel->fetchMore();
|
||||
|
||||
// Select it
|
||||
ui->dataTable->clearSelection();
|
||||
ui->dataTable->selectRow(lineToSelect);
|
||||
ui->dataTable->scrollTo(ui->dataTable->currentIndex(), QAbstractItemView::PositionAtTop);
|
||||
QApplication::restoreOverrideCursor();
|
||||
}
|
||||
|
||||
void MainWindow::selectTableLines(int firstLine, int count)
|
||||
{
|
||||
int lastLine = firstLine+count-1;
|
||||
if(ui->mainTab->currentIndex() == BrowseTab) {
|
||||
// Are there even that many lines?
|
||||
if(lastLine >= m_browseTableModel->totalRowCount())
|
||||
return;
|
||||
|
||||
selectTableLine(firstLine);
|
||||
|
||||
QModelIndex topLeft = ui->dataTable->model()->index(firstLine, 0);
|
||||
QModelIndex bottomRight = ui->dataTable->model()->index(lastLine, ui->dataTable->model()->columnCount()-1);
|
||||
|
||||
ui->dataTable->selectionModel()->select(QItemSelection(topLeft, bottomRight), QItemSelectionModel::Select | QItemSelectionModel::Rows);
|
||||
}else if(ui->mainTab->currentIndex() == ExecuteTab) {
|
||||
selectCurrentTabTableLines(firstLine, lastLine);
|
||||
}
|
||||
ui->dataTable->selectTableLine(lineToSelect);
|
||||
}
|
||||
|
||||
void MainWindow::navigatePrevious()
|
||||
@@ -1231,7 +1184,7 @@ void MainWindow::executeQuery()
|
||||
qApp->processEvents();
|
||||
}
|
||||
sqlWidget->finishExecution(statusMessage, ok);
|
||||
plotDock->updatePlot(sqlWidget->getModel());
|
||||
attachPlot(sqlWidget->getTableResult(), sqlWidget->getModel());
|
||||
|
||||
connect(sqlWidget->getTableResult(), &ExtendedTableWidget::activated, this, &MainWindow::dataTableSelectionChanged);
|
||||
connect(sqlWidget->getTableResult(), SIGNAL(doubleClicked(QModelIndex)), this, SLOT(doubleClickTable(QModelIndex)));
|
||||
@@ -1677,7 +1630,7 @@ void MainWindow::browseTableHeaderClicked(int logicalindex)
|
||||
// we might try to select the last selected item
|
||||
ui->dataTable->setCurrentIndex(ui->dataTable->currentIndex().sibling(0, logicalindex));
|
||||
|
||||
plotDock->updatePlot(m_browseTableModel, &browseTableSettings[currentlyBrowsedTableName()]);
|
||||
attachPlot(ui->dataTable, m_browseTableModel, &browseTableSettings[currentlyBrowsedTableName()]);
|
||||
}
|
||||
|
||||
void MainWindow::resizeEvent(QResizeEvent*)
|
||||
|
||||
@@ -18,6 +18,7 @@ class DbStructureModel;
|
||||
class RemoteDock;
|
||||
class RemoteDatabase;
|
||||
class FindReplaceDialog;
|
||||
class ExtendedTableWidget;
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
@@ -194,6 +195,8 @@ private:
|
||||
void loadExtensionsFromSettings();
|
||||
void saveAsView(QString query);
|
||||
void duplicateRecord(int currentRow);
|
||||
void selectTableLine(int lineToSelect);
|
||||
void attachPlot(ExtendedTableWidget* tableWidget, SqliteTableModel* model, BrowseDataTableSettings* settings = nullptr, bool keepOrResetSelection = true);
|
||||
|
||||
sqlb::ObjectIdentifier currentlyBrowsedTableName() const;
|
||||
|
||||
@@ -224,9 +227,6 @@ private slots:
|
||||
bool fileClose();
|
||||
void addRecord();
|
||||
void deleteRecord();
|
||||
void selectTableLine(int lineToSelect);
|
||||
void selectCurrentTabTableLines(int firstLine, int lastLine);
|
||||
void selectTableLines(int firstLine, int count);
|
||||
void navigatePrevious();
|
||||
void navigateNext();
|
||||
void navigateBegin();
|
||||
|
||||
@@ -365,6 +365,11 @@ void PlotDock::updatePlot(SqliteTableModel* model, BrowseDataTableSettings* sett
|
||||
}
|
||||
}
|
||||
|
||||
void PlotDock::resetPlot()
|
||||
{
|
||||
updatePlot(nullptr);
|
||||
}
|
||||
|
||||
void PlotDock::on_treePlotColumns_itemChanged(QTreeWidgetItem* changeitem, int column)
|
||||
{
|
||||
// disable change updates, or we get unwanted redrawing and weird behavior
|
||||
|
||||
@@ -66,6 +66,7 @@ public:
|
||||
public slots:
|
||||
void updatePlot(SqliteTableModel* model, BrowseDataTableSettings* settings = nullptr, bool update = true, bool keepOrResetSelection = true);
|
||||
void fetchAllData();
|
||||
void resetPlot();
|
||||
|
||||
signals:
|
||||
void pointsSelected(int firstIndex, int count);
|
||||
|
||||
Reference in New Issue
Block a user