From 3a4d7d8dfe349d14c0e327bf1a2757234ba1610d Mon Sep 17 00:00:00 2001 From: mgrojo Date: Sun, 13 Feb 2022 19:03:21 +0100 Subject: [PATCH] Open SQL files are now linked when saving project file This restores the original state of each of the SQL "tabs". If the tab was opened without a file, the content is saved in the project file itself (unmodified); when the tab was opened from a file, the reference to the file is stored in the project file and the contents are restored from that disk file when loading the project. See issues #2834, #2972, #2959 --- src/MainWindow.cpp | 43 +++++++++++++++++++++++++++++----------- src/MainWindow.h | 1 + src/SqlExecutionArea.cpp | 2 +- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 0c5efba4..18344e26 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -2139,6 +2139,15 @@ void MainWindow::changeSqlTab(int index) } } +void MainWindow::openSqlFile(int tabindex, QString filename) +{ + SqlExecutionArea* sqlarea = qobject_cast(ui->tabSqlAreas->widget(tabindex)); + sqlarea->openFile(filename); + QFileInfo fileinfo(filename); + ui->tabSqlAreas->setTabText(tabindex, fileinfo.fileName()); + ui->tabSqlAreas->setTabIcon(tabindex, QIcon(":/icons/document_open")); +} + void MainWindow::openSqlFile() { const QStringList wfiles = FileDialog::getOpenFileNames( @@ -2159,12 +2168,7 @@ void MainWindow::openSqlFile() else index = openSqlTab(); - SqlExecutionArea* sqlarea = qobject_cast(ui->tabSqlAreas->widget(index)); - sqlarea->openFile(file); - - QFileInfo fileinfo(file); - ui->tabSqlAreas->setTabText(index, fileinfo.fileName()); - ui->tabSqlAreas->setTabIcon(index, QIcon(":/icons/document_open")); + openSqlFile(index, file); } } } @@ -2851,10 +2855,18 @@ bool MainWindow::loadProject(QString filename, bool readOnly) { // SQL editor tab int index = openSqlTab(); - ui->tabSqlAreas->setTabText(index, xml.attributes().value("name").toString()); - SqlTextEdit* sqlEditor = qobject_cast(ui->tabSqlAreas->widget(index))->getEditor(); - sqlEditor->setText(xml.readElementText()); - sqlEditor->setModified(false); + + // if it has the filename attribute, it's a linked file, otherwise it's + // a free SQL buffer saved in the project. + if(xml.attributes().hasAttribute("filename")) { + openSqlFile(index, xml.attributes().value("filename").toString()); + xml.skipCurrentElement(); + } else { + ui->tabSqlAreas->setTabText(index, xml.attributes().value("name").toString()); + SqlTextEdit* sqlEditor = qobject_cast(ui->tabSqlAreas->widget(index))->getEditor(); + sqlEditor->setText(xml.readElementText()); + sqlEditor->setModified(false); + } } else if(xml.name() == "current_tab") { // Currently selected tab ui->tabSqlAreas->setCurrentIndex(xml.attributes().value("id").toString().toInt()); @@ -3135,10 +3147,17 @@ void MainWindow::saveProject(const QString& currentFilename) for(int i=0;itabSqlAreas->count();i++) // All SQL tabs content { SqlExecutionArea* sqlArea = qobject_cast(ui->tabSqlAreas->widget(i)); + QString sqlFilename = sqlArea->fileName(); xml.writeStartElement("sql"); xml.writeAttribute("name", ui->tabSqlAreas->tabText(i)); - xml.writeCharacters(sqlArea->getSql()); - sqlArea->getEditor()->setModified(false); + if(sqlFilename.isEmpty()) { + xml.writeCharacters(sqlArea->getSql()); + sqlArea->getEditor()->setModified(false); + } else { + xml.writeAttribute("filename", sqlFilename); + // Backwards compatibility, so older versions do not break. + xml.writeCharacters(tr("-- Reference to file \"%1\" (not supported by this version) --").arg(sqlFilename)); + } xml.writeEndElement(); } xml.writeStartElement("current_tab"); // Currently selected tab diff --git a/src/MainWindow.h b/src/MainWindow.h index 8e176804..a89baf1d 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -194,6 +194,7 @@ private slots: void updatePragmaUi(); void savePragmas(); void mainTabSelected( int tabindex ); + void openSqlFile(int tabindex, QString filename); void openSqlFile(); void saveSqlFile(); void saveSqlFileAs(); diff --git a/src/SqlExecutionArea.cpp b/src/SqlExecutionArea.cpp index e4dacb68..fc380cc4 100644 --- a/src/SqlExecutionArea.cpp +++ b/src/SqlExecutionArea.cpp @@ -243,7 +243,7 @@ void SqlExecutionArea::openFile(const QString& filename) f.open(QIODevice::ReadOnly); if(!f.isOpen()) { - QMessageBox::warning(this, qApp->applicationName(), tr("Couldn't read file: %1.").arg(f.errorString())); + QMessageBox::warning(this, qApp->applicationName(), tr("Couldn't read file \"%1\": %2.").arg(filename, f.errorString())); return; }