From 4e954b9e852e69d929c5fc3d4aaee774a58c8aa3 Mon Sep 17 00:00:00 2001 From: mgrojo Date: Mon, 2 Mar 2020 21:09:54 +0100 Subject: [PATCH] Show a useful icon in SQL tabs 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: https://github.com/sqlitebrowser/sqlitebrowser/issues/2073#issuecomment-570900792 --- src/MainWindow.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 904eb40259..f64a637087 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -301,7 +301,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); }); @@ -1082,7 +1082,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); @@ -1180,8 +1180,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); @@ -1197,9 +1204,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); @@ -1871,7 +1878,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 " @@ -1923,6 +1930,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; } @@ -1934,7 +1944,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 @@ -1973,6 +1983,8 @@ void MainWindow::openSqlFile() QFileInfo fileinfo(file); ui->tabSqlAreas->setTabText(index, fileinfo.fileName()); + ui->tabSqlAreas->setTabIcon(index, QIcon(":/icons/document_open")); + } } @@ -2018,6 +2030,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")); } }