diff options
author | Ken Moore <ken@pcbsd.org> | 2016-08-05 13:42:02 -0400 |
---|---|---|
committer | Ken Moore <ken@pcbsd.org> | 2016-08-05 13:42:02 -0400 |
commit | 9eae518250b2359921c35699d923b781466a7960 (patch) | |
tree | af67b678d30a593e525d62bc14b654db1ae86803 | |
parent | Update the RSS feeds for FreeBSD to point to the TrueOS news feed instead of ... (diff) | |
download | lumina-9eae518250b2359921c35699d923b781466a7960.tar.gz lumina-9eae518250b2359921c35699d923b781466a7960.tar.bz2 lumina-9eae518250b2359921c35699d923b781466a7960.zip |
Add file watching capabilities to lumina-textedit, so the file will automatically re-load if changed externally and no pending changes in the editor.
3 files changed, 44 insertions, 4 deletions
diff --git a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp index 281c4bec..1ec746a3 100644 --- a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp +++ b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.cpp @@ -11,6 +11,7 @@ #include <QTextBlock> #include <QFileDialog> #include <QDebug> +#include <QApplication> #include <LuminaUtils.h> @@ -21,6 +22,7 @@ PlainTextEditor::PlainTextEditor(QSettings *set, QWidget *parent) : QPlainTextEd settings = set; LNW = new LNWidget(this); showLNW = true; + watcher = new QFileSystemWatcher(this); hasChanges = false; lastSaveContents.clear(); matchleft = matchright = -1; @@ -33,6 +35,7 @@ PlainTextEditor::PlainTextEditor(QSettings *set, QWidget *parent) : QPlainTextEd connect(this, SIGNAL(updateRequest(const QRect&, int)), this, SLOT(LNW_update(const QRect&, int)) ); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(checkMatchChar()) ); connect(this, SIGNAL(textChanged()), this, SLOT(textChanged()) ); + connect(watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(fileChanged()) ); LNW_updateWidth(); LNW_highlightLine(); } @@ -59,12 +62,26 @@ void PlainTextEditor::updateSyntaxColors(){ //File loading/setting options void PlainTextEditor::LoadFile(QString filepath){ + if( !watcher->files().isEmpty() ){ watcher->removePaths(watcher->files()); } + bool diffFile = (filepath != this->whatsThis()); this->setWhatsThis(filepath); this->clear(); SYNTAX->loadRules( Custom_Syntax::ruleForFile(filepath.section("/",-1)) ); lastSaveContents = LUtils::readFile(filepath).join("\n"); - this->setPlainText( lastSaveContents ); + if(diffFile){ + this->setPlainText( lastSaveContents ); + }else{ + //Try to keep the mouse cursor/scroll in the same position + int curpos = this->textCursor().position();; + this->setPlainText( lastSaveContents ); + QApplication::processEvents(); + QTextCursor cur = this->textCursor(); + cur.setPosition(curpos); + this->setTextCursor( cur ); + this->centerCursor(); //scroll until cursor is centered (if possible) + } hasChanges = false; + watcher->addPath(filepath); emit FileLoaded(this->whatsThis()); } @@ -78,9 +95,11 @@ void PlainTextEditor::SaveFile(bool newname){ SYNTAX->loadRules( Custom_Syntax::ruleForFile(this->whatsThis().section("/",-1)) ); SYNTAX->rehighlight(); } + if( !watcher->files().isEmpty() ){ watcher->removePaths(watcher->files()); } bool ok = LUtils::writeFile(this->whatsThis(), this->toPlainText().split("\n"), true); hasChanges = !ok; if(ok){ lastSaveContents = this->toPlainText(); emit FileLoaded(this->whatsThis()); } + watcher->addPath(currentFile()); //qDebug() << " - Success:" << ok << hasChanges; } @@ -258,6 +277,24 @@ void PlainTextEditor::textChanged(){ else{ emit FileLoaded(this->whatsThis()); } } +//Function for prompting the user if the file changed externally +void PlainTextEditor::fileChanged(){ + qDebug() << "File Changed:" << currentFile(); + bool update = !hasChanges; //Go ahead and reload the file automatically - no custom changes in the editor + /*QString text = tr("The following file has been changed by some other utility. Do you want to re-load it?"); + text.append("\n"); + text.append( tr("(Note: You will lose all currently-unsaved changes)") ); + text.append("\n\n%1");*/ + + if(!update){ + //update = (QMessageBox::Yes == QMessageBox::question(this->parent(), tr("File Modified"),text.arg(currentFile()) , QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ); + } + //Now update the text in the editor as needed + if(update){ + LoadFile( currentFile() ); + } +} + //================== // PROTECTED //================== diff --git a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.h b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.h index 8ba1e4bc..075ed742 100644 --- a/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.h +++ b/src-qt5/desktop-utils/lumina-textedit/PlainTextEditor.h @@ -11,6 +11,7 @@ #include <QWidget> #include <QResizeEvent> #include <QPaintEvent> +#include <QFileSystemWatcher> #include "syntaxSupport.h" @@ -42,6 +43,7 @@ private: bool showLNW; QSettings *settings; QString lastSaveContents; + QFileSystemWatcher *watcher; //Syntax Highlighting class Custom_Syntax *SYNTAX; @@ -61,6 +63,8 @@ private slots: void checkMatchChar(); //Functions for notifying the parent widget of changes void textChanged(); + //Function for prompting the user if the file changed externally + void fileChanged(); protected: void resizeEvent(QResizeEvent *ev); @@ -96,4 +100,3 @@ protected: } }; #endif - diff --git a/src-qt5/desktop-utils/lumina-textedit/tests/test.rst b/src-qt5/desktop-utils/lumina-textedit/tests/test.rst index 156f0eca..2185cc1c 100644 --- a/src-qt5/desktop-utils/lumina-textedit/tests/test.rst +++ b/src-qt5/desktop-utils/lumina-textedit/tests/test.rst @@ -1,7 +1,7 @@ .. comment grays out continues with same spacing breaks when justify left - +sample2 .. TODO Black highlighting and brown/orange text for visibility on "TODO" with the rest greyed out like a comment. TODO @@ -43,7 +43,7 @@ SysAdm™ files are currently available from the `github repository <https://git Building SysAdm™ ---------------- - +sample The following Qt Modules are required before attempting to build SysAdm™: :: |