From eb2a6e0bd80ec4173ce631c083e9c7f867047938 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 4 Apr 2019 19:36:43 +0200 Subject: [PATCH] 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. --- src/SqlExecutionArea.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/SqlExecutionArea.h | 6 +++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/SqlExecutionArea.cpp b/src/SqlExecutionArea.cpp index dc609da2..0bd9fb07 100644 --- a/src/SqlExecutionArea.cpp +++ b/src/SqlExecutionArea.cpp @@ -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); + } +} diff --git a/src/SqlExecutionArea.h b/src/SqlExecutionArea.h index d4e904b3..3a7a8301 100644 --- a/src/SqlExecutionArea.h +++ b/src/SqlExecutionArea.h @@ -1,7 +1,8 @@ -#ifndef SQLEXECUTIONAREA_H +#ifndef SQLEXECUTIONAREA_H #define SQLEXECUTIONAREA_H #include +#include 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;