Commit d2588c82 authored by jhammen's avatar jhammen

prompt for reload when editor file changes externally

parent 996398e9
......@@ -50,6 +50,7 @@ class EditorWidget : public QPlainTextEdit {
EditorWidget(NamedFile &file);
const QString &path() { return mfile.path; }
const QString &name() { return mfile.name; }
void fileChanged() { document()->setModified(true); }
bool isDirty() { return document()->isModified(); }
bool isUndoAvailable() { return undoable; }
bool isRedoAvailable() { return redoable; }
......@@ -90,7 +91,7 @@ class InfoStrip : public QWidget {
public:
InfoStrip(EditorWidget *parent) : QWidget(parent), editor(*parent) {}
QSize sizeHint() const override { return QSize(stripWidth(), 0); }
int stripWidth() const { return fontMetrics().width(QLatin1String("999")) + paddingSide * 2; }
int stripWidth() const { return fontMetrics().width(QLatin1String("9999")) + paddingSide * 2; }
};
#endif // EDITORWIDGET_H
......@@ -66,9 +66,10 @@ MainWindow::MainWindow(QWidget *parent, const char *folder)
connect(searchWidget, SIGNAL(clearSearch()), this, SLOT(clearSearch()));
connect(searchWidget, SIGNAL(navigateResult(EditorLocation &)), this,
SLOT(handleLink(EditorLocation &)));
connect(&fileWatcher, SIGNAL(fileChanged(QString)), this, SLOT(fileOutsideChange(QString)));
projectList.load();
if(folder) { // from CLI
if (folder) { // from CLI
QDir dir(folder);
projectList.select(dir.canonicalPath());
}
......@@ -124,6 +125,7 @@ void MainWindow::closeEditorTab(int index) {
EditorWidget *editor = qobject_cast<EditorWidget *>(ui->tabWidget->widget(index));
if (saveOrAbandon(editor)) {
projectList.currentProject().closeFile(editor->path());
fileWatcher.removePath(editor->path());
delete editor;
}
}
......@@ -152,6 +154,28 @@ void MainWindow::engineDisconnect() {
ui->actionJackConnect->setEnabled(true);
}
void MainWindow::fileOutsideChange(QString path) {
// block signals to prevent double-firing
// TODO: is this the best way to handle this?
fileWatcher.blockSignals(true);
QMessageBox::StandardButton choice = QMessageBox::warning(
this, tr("File Changed"),
tr("File %1 has changed outside of the IDE.\nDo you wish to reload from disk?").arg(path),
QMessageBox::Yes | QMessageBox::Cancel);
EditorWidget *widget = findEditor(path);
if (choice == QMessageBox::Yes) {
if (widget) {
widget->load();
} else {
QMessageBox::warning(this, tr("Reload Failed"), tr("Reloading %1 failed").arg(path),
QMessageBox::Ok);
}
} else {
widget->fileChanged();
}
fileWatcher.blockSignals(false);
}
bool MainWindow::createOrOpenEditor(NamedFile &file) {
EditorWidget *editor = findEditor(file.path);
if (editor) {
......@@ -168,6 +192,7 @@ bool MainWindow::createOrOpenEditor(NamedFile &file) {
connect(editor, SIGNAL(redoAvailable(bool)), ui->actionRedo, SLOT(setEnabled(bool)));
connect(editor, SIGNAL(copyAvailable(bool)), ui->actionCut, SLOT(setEnabled(bool)));
connect(editor, SIGNAL(copyAvailable(bool)), ui->actionCopy, SLOT(setEnabled(bool)));
fileWatcher.addPath(file.path);
// add to project list as open file
projectList.currentProject().openFile(file.path);
} else {
......@@ -594,7 +619,7 @@ void MainWindow::on_actionRewind_triggered() {
audioEngine.reposition(transportPosition);
}
void MainWindow::on_actionSaveFile_triggered() { currentEditor()->save(); }
void MainWindow::on_actionSaveFile_triggered() { saveEditor(currentEditor()); }
void MainWindow::on_actionFocusFind_triggered() {
searchWidget->newFileSearch();
......@@ -690,6 +715,14 @@ void MainWindow::loadProject() {
ui->leftTabWidget->setCurrentWidget(ui->folderTree);
}
bool MainWindow::saveEditor(EditorWidget *widget) {
// suspend file watcher for save
fileWatcher.removePath(widget->path());
bool result = widget->save();
fileWatcher.addPath(widget->path());
return result;
}
bool MainWindow::saveAllTabs() {
for (int i = 0; i < ui->tabWidget->count(); i++) {
EditorWidget *editor = static_cast<EditorWidget *>(ui->tabWidget->widget(i));
......@@ -698,7 +731,7 @@ bool MainWindow::saveAllTabs() {
if (choice == QMessageBox::Cancel) {
return false;
} else if (choice == QMessageBox::Save) {
editor->save();
saveEditor(editor);
}
}
}
......@@ -711,7 +744,7 @@ bool MainWindow::saveOrAbandon(EditorWidget *editor) {
if (choice == QMessageBox::Cancel) {
return false;
} else if (choice == QMessageBox::Save) {
if (!editor->save()) {
if (!saveEditor(editor)) {
return false;
}
}
......
......@@ -26,6 +26,7 @@
#include "searchwidget.h"
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QLabel>
#include <QMainWindow>
#include <QMessageBox>
......@@ -48,6 +49,7 @@ class MainWindow : public QMainWindow {
void closeOtherEditors();
void codeError(const QString &, int);
void engineDisconnect();
void fileOutsideChange(QString);
void folderTreeClicked(QTreeWidgetItem *item, int col);
void handleLink(const QUrl &);
void handleLink(EditorLocation &);
......@@ -102,6 +104,7 @@ class MainWindow : public QMainWindow {
QLabel timeWidget;
QLabel bbtWidget;
QLabel engineWidget;
QFileSystemWatcher fileWatcher;
// private methods
bool createOrOpenEditor(NamedFile &);
bool createOrOpenEditor(const QString &);
......@@ -113,6 +116,7 @@ class MainWindow : public QMainWindow {
bool killAllScripts();
bool killOrDetach(OutputWidget *output, const QString &name);
void loadProject();
bool saveEditor(EditorWidget *);
bool saveAllTabs();
bool saveOrAbandon(EditorWidget *editor);
QList<EditorLink> searchFile(const QString &term, const QString &path);
......
......@@ -137,8 +137,7 @@ bool ProjectList::open(int index) {
return false;
}
bool ProjectList::select(QString path)
{
bool ProjectList::select(QString path) {
int index = findProject(path);
return index == -1 ? false : open(index);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment