diff options
author | saturneric <[email protected]> | 2024-11-25 22:41:32 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-11-25 22:41:32 +0000 |
commit | 60c40b3ef9b00cbb686d495b32aa6031f1ec62d2 (patch) | |
tree | c7a2e158cab7d9050256c6b1cc9c63f7898e6c94 | |
parent | feat: support drop down and open directory (diff) | |
download | GpgFrontend-60c40b3ef9b00cbb686d495b32aa6031f1ec62d2.tar.gz GpgFrontend-60c40b3ef9b00cbb686d495b32aa6031f1ec62d2.zip |
fix: handling of split CRLF sequences during text insertion
-rw-r--r-- | src/ui/widgets/PlainTextEditorPage.cpp | 40 | ||||
-rw-r--r-- | src/ui/widgets/PlainTextEditorPage.h | 1 |
2 files changed, 33 insertions, 8 deletions
diff --git a/src/ui/widgets/PlainTextEditorPage.cpp b/src/ui/widgets/PlainTextEditorPage.cpp index bd1c9837..b6066a30 100644 --- a/src/ui/widgets/PlainTextEditorPage.cpp +++ b/src/ui/widgets/PlainTextEditorPage.cpp @@ -124,21 +124,21 @@ void PlainTextEditorPage::slot_format_gpg_header() { sign_marked_ = true; // Set the fontstyle for the header - QTextCharFormat signFormat; - signFormat.setForeground(QBrush(QColor::fromRgb(80, 80, 80))); - signFormat.setFontPointSize(9); + QTextCharFormat sign_format; + sign_format.setForeground(QBrush(QColor::fromRgb(80, 80, 80))); + sign_format.setFontPointSize(9); // set font style for the signature QTextCursor cursor(ui_->textPage->document()); cursor.setPosition(start_sig, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, end_sig); - cursor.setCharFormat(signFormat); + cursor.setCharFormat(sign_format); // set the font style for the header - int headEnd = content.indexOf("\n\n", start); + int head_end = content.indexOf("\n\n", start); cursor.setPosition(start, QTextCursor::MoveAnchor); - cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, headEnd); - cursor.setCharFormat(signFormat); + cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, head_end); + cursor.setCharFormat(sign_format); } void PlainTextEditorPage::ReadFile() { @@ -167,7 +167,7 @@ void PlainTextEditorPage::ReadFile() { [=]() { emit read_task->SignalTaskShouldEnd(0); }); connect(read_task, &FileReadTask::SignalFileBytesReadEnd, this, [=]() { // set the UI - FLOG_D("signal file bytes read end rised"); + FLOG_D("file read done"); this->read_done_ = true; text_page->setEnabled(true); text_page->document()->setModified(false); @@ -189,8 +189,32 @@ auto BinaryToString(const QByteArray &source) -> QString { } void PlainTextEditorPage::slot_insert_text(QByteArray bytes_data) { + // If the previous data ended with a '\r' and the current data starts with + // '\n', combine them to form a complete '\r\n' to avoid incorrect line + // breaks. + if (last_insert_has_partial_cr_ && !bytes_data.isEmpty() && + bytes_data.startsWith('\n')) { + bytes_data.prepend('\r'); // Prepend '\r' to ensure '\r\n' stays together. + } + + // Check if the current data ends with '\r'. + if (!bytes_data.isEmpty() && bytes_data.endsWith('\r')) { + // If the data ends with '\r', set the flag indicating an incomplete line + // ending. + last_insert_has_partial_cr_ = true; + // Remove the trailing '\r' and hold it for the next chunk of data. + bytes_data.chop(1); + } else { + // If the data does not end with '\r', reset the flag. + last_insert_has_partial_cr_ = false; + } + read_bytes_ += bytes_data.size(); + if (bytes_data.contains("VusEk")) { + LOG_D() << "WWWWWWW PPP: " << bytes_data; + } + // insert the text to the text page this->GetTextPage()->insertPlainText(bytes_data); this->ui_->characterLabel->setText( diff --git a/src/ui/widgets/PlainTextEditorPage.h b/src/ui/widgets/PlainTextEditorPage.h index 18ed41b2..9c2edc6e 100644 --- a/src/ui/widgets/PlainTextEditorPage.h +++ b/src/ui/widgets/PlainTextEditorPage.h @@ -115,6 +115,7 @@ class PlainTextEditorPage : public QWidget { bool read_done_ = false; ///< size_t read_bytes_ = 0; ///< bool is_crlf_ = false; ///< + bool last_insert_has_partial_cr_ = false; private slots: |