From 7b64944738d91eb2eef92f93f526cc29436adf15 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Fri, 29 Nov 2013 00:16:40 -0300 Subject: Try to keep cursor and scroll in place on refresh When the file is saving, the cursor position was resetting to the top and you should keep scrolling by hand every time you save the file. This patch saves the scroll and cursor position and set it again after the file is refreshed so we can continue edition from where we were. diff --git a/src/mainwin.cc b/src/mainwin.cc index 5b986b1..8c21b20 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -75,6 +75,7 @@ #include #include #include +#include #include @@ -605,7 +606,15 @@ void MainWindow::refreshDocument() reader.setCodec("UTF-8"); QString text = reader.readAll(); PRINTB("Loaded design '%s'.", this->fileName.toLocal8Bit().constData()); + int y = editor->verticalScrollBar()->sliderPosition(); + // Save current cursor position + QTextCursor cursor = editor->textCursor(); + int n = cursor.position(); editor->setPlainText(text); + // Restore cursor position + cursor.setPosition(n); + editor->setTextCursor(cursor); + editor->verticalScrollBar()->setSliderPosition(y); } } setCurrentOutput(); -- cgit v0.10.1 From afe1fbcb53ff286b2f6efdb99efb6ed9947304f9 Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Fri, 29 Nov 2013 01:40:28 -0300 Subject: Move main code inside Editor class Before open a new file now we cleanup the Editor, so the new file get the cursor on the first character. diff --git a/src/editor.cc b/src/editor.cc index 08bf005..069101f 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -108,3 +108,17 @@ void Editor::wheelEvent ( QWheelEvent * event ) } } +void Editor::setPlainText(const QString &text) +{ + int y = verticalScrollBar()->sliderPosition(); + // Save current cursor position + QTextCursor cursor = textCursor(); + int n = cursor.position(); + QTextEdit::setPlainText(text); + // Restore cursor position + if (n < text.length()) { + cursor.setPosition(n); + setTextCursor(cursor); + verticalScrollBar()->setSliderPosition(y); + } +} diff --git a/src/editor.h b/src/editor.h index 09484f5..8d092a9 100644 --- a/src/editor.h +++ b/src/editor.h @@ -2,6 +2,7 @@ #include #include #include +#include #include class Editor : public QTextEdit @@ -9,6 +10,7 @@ class Editor : public QTextEdit Q_OBJECT public: Editor(QWidget *parent) : QTextEdit(parent) { setAcceptRichText(false); } + void setPlainText(const QString &text); public slots: void zoomIn(); void zoomOut(); diff --git a/src/mainwin.cc b/src/mainwin.cc index 8c21b20..1ad8bc8 100644 --- a/src/mainwin.cc +++ b/src/mainwin.cc @@ -75,7 +75,6 @@ #include #include #include -#include #include @@ -505,6 +504,7 @@ MainWindow::openFile(const QString &new_filename) } #endif setFileName(actual_filename); + editor->setPlainText(""); fileChangedOnDisk(); // force cached autoReloadId to update refreshDocument(); @@ -606,15 +606,7 @@ void MainWindow::refreshDocument() reader.setCodec("UTF-8"); QString text = reader.readAll(); PRINTB("Loaded design '%s'.", this->fileName.toLocal8Bit().constData()); - int y = editor->verticalScrollBar()->sliderPosition(); - // Save current cursor position - QTextCursor cursor = editor->textCursor(); - int n = cursor.position(); editor->setPlainText(text); - // Restore cursor position - cursor.setPosition(n); - editor->setTextCursor(cursor); - editor->verticalScrollBar()->setSliderPosition(y); } } setCurrentOutput(); -- cgit v0.10.1