From c2b07f4f09bf50154ce33d930aa668309bf70d6a Mon Sep 17 00:00:00 2001 From: rekols Date: Fri, 14 Dec 2018 11:30:23 +0800 Subject: [PATCH] fix: hide toast after refresh. Change-Id: I27cda4cd64f5c32eb856be3459db7b30fc4d15f3 --- src/editwrapper.cpp | 47 +++++++++++++++------------------------------ src/editwrapper.h | 2 ++ 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/editwrapper.cpp b/src/editwrapper.cpp index 92bd8978..dd3806a1 100644 --- a/src/editwrapper.cpp +++ b/src/editwrapper.cpp @@ -44,7 +44,8 @@ EditWrapper::EditWrapper(QWidget *parent) m_textCodec(QTextCodec::codecForName("UTF-8")), m_endOfLineMode(eolUnix), m_isLoadFinished(true), - m_toast(new Toast(this)) + m_toast(new Toast(this)), + m_isRefreshing(false) { // Init layout and widgets. m_layout->setContentsMargins(0, 0, 0, 0); @@ -174,7 +175,7 @@ void EditWrapper::updatePath(const QString &file) void EditWrapper::refresh() { - if (filePath().isEmpty() || Utils::isDraftFile(filePath())) { + if (filePath().isEmpty() || Utils::isDraftFile(filePath()) || m_isRefreshing) { return; } @@ -182,14 +183,15 @@ void EditWrapper::refresh() int curPos = m_textEdit->textCursor().position(); int yoffset = m_textEdit->verticalScrollBar()->value(); int xoffset = m_textEdit->horizontalScrollBar()->value(); - m_textEdit->setPlainText(QString()); if (file.open(QIODevice::ReadOnly)) { + m_isRefreshing = true; + QTextStream out(&file); out.setCodec(m_textCodec); QString content = out.readAll(); - m_textEdit->setUpdatesEnabled(false); + m_textEdit->setPlainText(QString()); m_textEdit->setPlainText(content); m_textEdit->setModified(false); @@ -202,11 +204,17 @@ void EditWrapper::refresh() QFileInfo fi(filePath()); m_modified = fi.lastModified(); - QTimer::singleShot(50, this, [=] { + file.close(); + m_toast->hideAnimation(); + + m_textEdit->setUpdatesEnabled(false); + + QTimer::singleShot(10, this, [=] { m_textEdit->setUpdatesEnabled(true); + m_isRefreshing = false; }); - - file.close(); + } else { + m_isRefreshing = false; } } @@ -227,30 +235,7 @@ void EditWrapper::setTextCodec(QTextCodec *codec, bool reload) if (!reload) return; - QFile file(filePath()); - int curPos = m_textEdit->textCursor().position(); - int yoffset = m_textEdit->verticalScrollBar()->value(); - int xoffset = m_textEdit->horizontalScrollBar()->value(); - m_textEdit->setPlainText(QString()); - - if (file.open(QIODevice::ReadOnly)) { - QTextStream out(&file); - out.setCodec(codec); - QString content = out.readAll(); - - m_textEdit->setUpdatesEnabled(false); - m_textEdit->setPlainText(content); - m_textEdit->setModified(false); - - QTextCursor textcur = m_textEdit->textCursor(); - textcur.setPosition(curPos); - m_textEdit->setTextCursor(textcur); - m_textEdit->verticalScrollBar()->setValue(yoffset); - m_textEdit->horizontalScrollBar()->setValue(xoffset); - m_textEdit->setUpdatesEnabled(true); - - file.close(); - } + refresh(); // TODO: enforce bom for some encodings } diff --git a/src/editwrapper.h b/src/editwrapper.h index e9007037..a3b500d8 100644 --- a/src/editwrapper.h +++ b/src/editwrapper.h @@ -90,6 +90,8 @@ class EditWrapper : public QWidget bool m_isLoadFinished; QDateTime m_modified; Toast *m_toast; + + bool m_isRefreshing; }; #endif