diff options
author | Ricardo Markiewicz <gazer.arg@gmail.com> | 2013-11-29 04:40:28 (GMT) |
---|---|---|
committer | Ricardo Markiewicz <gazer.arg@gmail.com> | 2013-11-29 04:40:28 (GMT) |
commit | afe1fbcb53ff286b2f6efdb99efb6ed9947304f9 (patch) | |
tree | 0f95851e9aa974d156272631562820dd9a206a65 /src | |
parent | 7b64944738d91eb2eef92f93f526cc29436adf15 (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/editor.cc | 14 | ||||
-rw-r--r-- | src/editor.h | 2 | ||||
-rw-r--r-- | src/mainwin.cc | 10 |
3 files changed, 17 insertions, 9 deletions
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 <QString> #include <QWidget> #include <QWheelEvent> +#include <QScrollBar> #include <QTextEdit> 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 <QSettings> #include <QProgressDialog> #include <QMutexLocker> -#include <QScrollBar> #include <fstream> @@ -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(); |