Add very basic reloading of modified SQL files

This adds a very basic implementation to watch for external
modifications in SQL files. Whenever another application modified a SQL
file which was opened from or saved to disk, a message box pops up to
notify the user of the changes.

See issue #1839.
This commit is contained in:
Martin Kleusberg
2019-04-04 19:36:43 +02:00
parent 1aa7bbef87
commit eb2a6e0bd8
2 changed files with 42 additions and 1 deletions

View File

@@ -38,8 +38,11 @@ SqlExecutionArea::SqlExecutionArea(DBBrowserDB& _db, QWidget* parent) :
connect(ui->findLineEdit, SIGNAL(returnPressed()), this, SLOT(findNext()));
connect(ui->hideFindButton, SIGNAL(clicked()), this, SLOT(hideFindFrame()));
connect(&fileSystemWatch, &QFileSystemWatcher::fileChanged, this, &SqlExecutionArea::fileChanged);
// Set collapsible the editErrors panel
ui->splitter_2->setCollapsible(1, true);
// Load settings
reloadSettings();
}
@@ -228,10 +231,19 @@ void SqlExecutionArea::openFile(const QString& filename)
// Remember file name
sqlFileName = filename;
// Start watching this file for changes and unwatch the previously watched file, if any
if(!fileSystemWatch.files().empty())
fileSystemWatch.removePaths(fileSystemWatch.files());
fileSystemWatch.addPath(filename);
}
void SqlExecutionArea::saveFile(const QString& filename)
{
// Unwatch all files now. By unwathing them before the actual saving, we are not notified of our own changes
if(!fileSystemWatch.files().empty())
fileSystemWatch.removePaths(fileSystemWatch.files());
// Open file for writing
QFile f(filename);
f.open(QIODevice::WriteOnly);
@@ -249,8 +261,33 @@ void SqlExecutionArea::saveFile(const QString& filename)
// Remember file name
sqlFileName = filename;
// Start watching this file
fileSystemWatch.addPath(filename);
} else {
QMessageBox::warning(this, qApp->applicationName(), tr("Couldn't save file: %1.").arg(f.errorString()));
return;
}
}
void SqlExecutionArea::fileChanged(const QString& filename)
{
// Check if there are unsaved changes in the file
QString changes;
if(ui->editEditor->isModified())
changes = QString(" ") + tr("Your changes will be lost when reloading it!");
// Ask user whether to realod the modified file
if(QMessageBox::question(
this,
qApp->applicationName(),
tr("The file \"%1\" was modified by another program. Do you want to reload it?%2").arg(filename).arg(changes),
QMessageBox::Yes | QMessageBox::Ignore) == QMessageBox::Yes)
{
// Read in the file
openFile(filename);
} else {
// The file does not match the file on the disk anymore. So set the modified flag
ui->editEditor->setModified(true);
}
}

View File

@@ -1,7 +1,8 @@
#ifndef SQLEXECUTIONAREA_H
#ifndef SQLEXECUTIONAREA_H
#define SQLEXECUTIONAREA_H
#include <QWidget>
#include <QFileSystemWatcher>
class SqlTextEdit;
class SqliteTableModel;
@@ -51,6 +52,8 @@ private slots:
void findLineEdit_textChanged(const QString& text);
void hideFindFrame();
void fileChanged(const QString& filename);
signals:
void findFrameVisibilityChanged(bool visible);
@@ -59,6 +62,7 @@ private:
DBBrowserDB& db;
SqliteTableModel* model;
QString sqlFileName;
QFileSystemWatcher fileSystemWatch;
Ui::SqlExecutionArea* ui;
bool m_columnsResized; // This is set to true if the columns of the table view were already adjusted to fit their contents
bool showErrorIndicators;