From 182cb4efb7281a3c8f92f2ef178783562032be20 Mon Sep 17 00:00:00 2001 From: rekols Date: Wed, 5 Dec 2018 11:36:50 +0800 Subject: [PATCH] fix: draft modify codec. Change-Id: Ic19dafabeed5c08e9dffb25035be0ee317c0b6a6 --- src/editwrapper.cpp | 31 ++++++++++++++++++++++++++----- src/widgets/bottombar.cpp | 1 - 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/editwrapper.cpp b/src/editwrapper.cpp index 668fd5e9..b743c823 100644 --- a/src/editwrapper.cpp +++ b/src/editwrapper.cpp @@ -148,14 +148,10 @@ void EditWrapper::updatePath(const QString &file) void EditWrapper::refresh() { - if (filePath().isEmpty()) { + if (filePath().isEmpty() || Utils::isDraftFile(filePath())) { return; } - if (Utils::isDraftFile(filePath())) { - saveFile(); - } - QFile file(filePath()); int curPos = m_textEdit->textCursor().position(); int yoffset = m_textEdit->verticalScrollBar()->value(); @@ -199,6 +195,31 @@ void EditWrapper::setTextCodec(QTextCodec *codec) { m_textCodec = codec; + 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(); + } + // TODO: enforce bom for some encodings } diff --git a/src/widgets/bottombar.cpp b/src/widgets/bottombar.cpp index 1fa58b08..998dba9a 100644 --- a/src/widgets/bottombar.cpp +++ b/src/widgets/bottombar.cpp @@ -78,7 +78,6 @@ void BottomBar::handleEncodeChanged(const QString &name) QFontMetrics fm(m_encodeComboBox->font()); m_encodeComboBox->setFixedWidth(fm.width(name) + 30); m_wrapper->setTextCodec(QTextCodec::codecForName(name.toUtf8())); - m_wrapper->refresh(); } void BottomBar::paintEvent(QPaintEvent *e)