Skip to content

Commit

Permalink
Open SQL files are now linked when saving project file
Browse files Browse the repository at this point in the history
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 sqlitebrowser#2834, sqlitebrowser#2972, sqlitebrowser#2959
  • Loading branch information
mgrojo committed Feb 13, 2022
1 parent 3c9f218 commit 3a4d7d8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
43 changes: 31 additions & 12 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,6 +2139,15 @@ void MainWindow::changeSqlTab(int index)
}
}

void MainWindow::openSqlFile(int tabindex, QString filename)
{
SqlExecutionArea* sqlarea = qobject_cast<SqlExecutionArea*>(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(
Expand All @@ -2159,12 +2168,7 @@ void MainWindow::openSqlFile()
else
index = openSqlTab();

SqlExecutionArea* sqlarea = qobject_cast<SqlExecutionArea*>(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);
}
}
}
Expand Down Expand Up @@ -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<SqlExecutionArea*>(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<SqlExecutionArea*>(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());
Expand Down Expand Up @@ -3135,10 +3147,17 @@ void MainWindow::saveProject(const QString& currentFilename)
for(int i=0;i<ui->tabSqlAreas->count();i++) // All SQL tabs content
{
SqlExecutionArea* sqlArea = qobject_cast<SqlExecutionArea*>(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
Expand Down
1 change: 1 addition & 0 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/SqlExecutionArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3a4d7d8

Please sign in to comment.