Save open tabs state in project files

The ordered set of open tabs is saved in the project file using a new
element main_tabs with attributes open (ordered list of open tabs like the
"MainWindow/openTabs" setting) and current (the current index inside that
set).

When the project load code finds the old element (current_tab) it falls
back to restoring all the main tabs and then setting that tab index as
current. In this way, old project files are restored as they were saved,
even when the user has closed or moved some tabs.

See issue #1675.
This commit is contained in:
mgrojo
2018-12-26 15:02:50 +01:00
parent fc8f23f299
commit b89adc4122
2 changed files with 48 additions and 21 deletions
+46 -21
View File
@@ -227,18 +227,8 @@ void MainWindow::init()
restoreState(Settings::getValue("MainWindow", "windowState").toByteArray());
// Restore open tab order if the openTabs setting is saved.
// Clear the tabs and then add them in the order specified by the setting.
// Use the statusTip attribute for restoring the tab label.
if (!Settings::getValue("MainWindow", "openTabs").toString().isEmpty()) {
ui->mainTab->clear();
for (QString objectName : Settings::getValue("MainWindow", "openTabs").toString().split(' ')) {
for (QWidget* widget : {ui->structure, ui->browser, ui->pragmas, ui->query})
if (widget->objectName() == objectName) {
ui->mainTab->addTab(widget, widget->statusTip());
break;
}
}
}
restoreOpenTabs(Settings::getValue("MainWindow", "openTabs").toString().split(' '));
// Restore dock state settings
ui->comboLogSubmittedBy->setCurrentIndex(ui->comboLogSubmittedBy->findText(Settings::getValue("SQLLogDock", "Log").toString()));
@@ -932,11 +922,7 @@ void MainWindow::closeEvent( QCloseEvent* event )
{
Settings::setValue("MainWindow", "geometry", saveGeometry());
Settings::setValue("MainWindow", "windowState", saveState());
QString openTabs;
for (int i=0; i < ui->mainTab->count(); i++)
openTabs.append(ui->mainTab->widget(i)->objectName() + ' ');
Settings::setValue("MainWindow", "openTabs", openTabs);
Settings::setValue("MainWindow", "openTabs", saveOpenTabs());
Settings::setValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText());
Settings::setValue("SchemaDock", "dropQualifiedNames", ui->actionDropQualifiedCheck->isChecked());
@@ -2654,9 +2640,19 @@ bool MainWindow::loadProject(QString filename, bool readOnly)
// Window settings
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != "window")
{
// Currently selected tab
if(xml.name() == "current_tab")
if(xml.name() == "main_tabs") {
// Currently open tabs
restoreOpenTabs(xml.attributes().value("open").toString().split(' '));
// Currently selected open tab
ui->mainTab->setCurrentIndex(xml.attributes().value("current").toString().toInt());
xml.skipCurrentElement();
} else if(xml.name() == "current_tab") {
// Currently selected tab (3.11 or older format, first restore default open tabs)
restoreOpenTabs({ui->structure->objectName(), ui->browser->objectName(),
ui->pragmas->objectName(), ui->query->objectName()});
ui->mainTab->setCurrentIndex(xml.attributes().value("id").toString().toInt());
xml.skipCurrentElement();
}
}
} else if(xml.name() == "tab_structure") {
// Database Structure tab settings
@@ -2927,8 +2923,9 @@ void MainWindow::saveProject()
// Window settings
xml.writeStartElement("window");
xml.writeStartElement("current_tab"); // Currently selected tab
xml.writeAttribute("id", QString::number(ui->mainTab->currentIndex()));
xml.writeStartElement("main_tabs"); // Currently open tabs
xml.writeAttribute("open", saveOpenTabs());
xml.writeAttribute("current", QString::number(ui->mainTab->currentIndex()));
xml.writeEndElement();
xml.writeEndElement();
@@ -3750,3 +3747,31 @@ void MainWindow::toggleTabVisible(QWidget* tabWidget, bool show)
else
ui->mainTab->removeTab(ui->mainTab->indexOf(tabWidget));
}
void MainWindow::restoreOpenTabs(QStringList tabList)
{
// Clear the tabs and then add them in the order specified by the setting.
// Use the statusTip attribute for restoring the tab label.
if (!tabList.isEmpty()) {
ui->mainTab->clear();
for (QString objectName : tabList) {
for (QWidget* widget : {ui->structure, ui->browser, ui->pragmas, ui->query})
if (widget->objectName() == objectName) {
ui->mainTab->addTab(widget, widget->statusTip());
break;
}
}
// Force the update of the View menu toggable entries
// (it doesn't seem to be a better way)
ui->mainTab->tabCloseRequested(-1);
}
}
QString MainWindow::saveOpenTabs()
{
QString openTabs;
for (int i=0; i < ui->mainTab->count(); i++)
openTabs.append(ui->mainTab->widget(i)->objectName() + ' ');
openTabs.chop(1);
return openTabs;
}
+2
View File
@@ -180,6 +180,8 @@ private:
void applyBrowseTableSettings(BrowseDataTableSettings storedData, bool skipFilters = false);
void toggleTabVisible(QWidget* tabWidget, bool show);
void restoreOpenTabs(QStringList tabList);
QString saveOpenTabs();
protected:
void closeEvent(QCloseEvent *) override;