Skip to content

Commit

Permalink
Show a useful icon in SQL tabs (sqlitebrowser#2153)
Browse files Browse the repository at this point in the history
The icon shows whether the tab is linked to a project file (open_sql icon)
or to a file in disk (document_open icon).

This has a lateral effect of fixing this, since the tab always has an icon
and the height of the tab bar never changes:

sqlitebrowser#2073 (comment)
  • Loading branch information
mgrojo authored May 2, 2020
1 parent ebeef14 commit 81b9b5a
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ void MainWindow::init()

// Add entries for toggling the visibility of main tabs
for (QWidget* widget : {ui->structure, ui->browser, ui->pragmas, ui->query}) {
QAction* action = ui->viewMenu->addAction(QIcon(":/icons/tab"), widget->accessibleName());
QAction* action = ui->viewMenu->addAction(QIcon(":/icons/open_sql"), widget->accessibleName());
action->setCheckable(true);
action->setChecked(ui->mainTab->indexOf(widget) != -1);
connect(action, &QAction::toggled, [=](bool show) { toggleTabVisible(widget, show); });
Expand Down Expand Up @@ -1116,7 +1116,7 @@ void MainWindow::executeQuery()
int execute_from_line, execute_from_index;
editor->lineIndexFromPosition(from_position, &execute_from_line, &execute_from_index);

// Special case: if the start position is at the end of a line, then move to the beggining of next line.
// Special case: if the start position is at the end of a line, then move to the beginning of next line.
// Otherwise for the typical case, the line reference is one less than expected.
// Note that execute_from_index uses character positions and not byte positions, so at() can be used.
QChar char_at_index = editor->text(execute_from_line).at(execute_from_index);
Expand Down Expand Up @@ -1214,8 +1214,15 @@ void MainWindow::executeQuery()

}, Qt::BlockingQueuedConnection);
connect(execute_sql_worker.get(), &RunSql::finished, sqlWidget, [this, current_tab, sqlWidget]() {
// We work with a pointer to the current tab here instead of its index because the user might reorder the tabs in the meantime
ui->tabSqlAreas->setTabIcon(ui->tabSqlAreas->indexOf(current_tab), QIcon());
// We work with a pointer to the current tab here instead of its index because the user might reorder the tabs in the meantime.
// We set different icons for general tabs, which are either new or loaded from the project file, and for tabs loaded from a file.
if(sqlWidget->fileName().isEmpty())
ui->tabSqlAreas->setTabIcon(ui->tabSqlAreas->indexOf(current_tab), QIcon(":/icons/open_sql"));
else
ui->tabSqlAreas->setTabIcon(ui->tabSqlAreas->indexOf(current_tab), QIcon(":/icons/document_open"));

// Set no-running-query state
ui->tabSqlAreas->tabBar()->setTabData(ui->tabSqlAreas->indexOf(current_tab), QVariant(false));

// We don't need to check for the current SQL tab here because two concurrently running queries are not allowed
ui->actionSqlExecuteLine->setEnabled(true);
Expand All @@ -1231,9 +1238,9 @@ void MainWindow::executeQuery()
});

// Add an hourglass icon to the current tab to indicate that there's a running execution in there.
// NOTE It's a bit hack-ish but we don't use this icon just as a signal to the user but also check for it in various places to check whether a
// specific SQL tab is currently running a query or not.
ui->tabSqlAreas->setTabIcon(ui->tabSqlAreas->currentIndex(), QIcon(":icons/hourglass"));
// We use the tab data to check whether a specific SQL tab is currently running a query or not.
ui->tabSqlAreas->tabBar()->setTabData(ui->tabSqlAreas->currentIndex(), QVariant(true));

// Deactivate the buttons to start a query and activate the button to stop the query
ui->actionSqlExecuteLine->setEnabled(false);
Expand Down Expand Up @@ -1901,7 +1908,7 @@ bool MainWindow::askSaveSqlTab(int index, bool& ignoreUnattachedBuffers)
void MainWindow::closeSqlTab(int index, bool force)
{
// Check if we're still executing statements from this tab and stop them before proceeding
if(!ui->tabSqlAreas->tabIcon(index).isNull())
if(ui->tabSqlAreas->tabBar()->tabData(index).toBool())
{
if(QMessageBox::warning(this, qApp->applicationName(), tr("The statements in this tab are still executing. Closing the tab will stop the "
"execution. This might leave the database in an inconsistent state. Are you sure "
Expand Down Expand Up @@ -1953,6 +1960,9 @@ int MainWindow::openSqlTab(bool resetCounter)
// Connect now the find shortcut to the editor with widget context, so it isn't ambiguous with other Scintilla Widgets.
QShortcut* shortcutFind = new QShortcut(ui->actionSqlFind->shortcut(), w->getEditor(), nullptr, nullptr, Qt::WidgetShortcut);
connect(shortcutFind, &QShortcut::activated, ui->actionSqlFind, &QAction::toggle);
ui->tabSqlAreas->setTabIcon(index, QIcon(":icons/open_sql"));
// The new tab is not currently running a query
ui->tabSqlAreas->tabBar()->setTabData(index, false);

return index;
}
Expand All @@ -1964,7 +1974,7 @@ void MainWindow::changeSqlTab(int index)
ui->actionSqlResultsSave->setEnabled(false);

// Check if the new tab is currently running a query or not
if(ui->tabSqlAreas->tabIcon(index).isNull())
if(!ui->tabSqlAreas->tabBar()->tabData(index).toBool())
{
// Not running a query

Expand Down Expand Up @@ -2005,6 +2015,7 @@ void MainWindow::openSqlFile()

QFileInfo fileinfo(file);
ui->tabSqlAreas->setTabText(index, fileinfo.fileName());
ui->tabSqlAreas->setTabIcon(index, QIcon(":/icons/document_open"));
}
}
}
Expand Down Expand Up @@ -2051,6 +2062,7 @@ void MainWindow::saveSqlFileAs()

QFileInfo fileinfo(file);
ui->tabSqlAreas->setTabText(ui->tabSqlAreas->currentIndex(), fileinfo.fileName());
ui->tabSqlAreas->setTabIcon(ui->tabSqlAreas->currentIndex(), QIcon(":/icons/document_open"));
}
}

Expand Down

0 comments on commit 81b9b5a

Please sign in to comment.