From d22842f662b1442cf324065e0fe954619358ce89 Mon Sep 17 00:00:00 2001 From: John Thacker Date: Fri, 23 Feb 2024 07:51:35 -0500 Subject: [PATCH] Qt: Add case sensitive find to Follow Stream and Show Packet Bytes Add a check box for case sensitivity when finding in follow stream and show packet bytes. Note that QPlainTextEdit::find() has a bit unexpected behavior with QRegularExpression (https://bugreports.qt.io/browse/QTBUG-88721). Searches are case-insensitive by default there too, respecting the default options. This is a change from the older QRegExp, where the option to find was ignored, and only the regex option was used, so for the last few releases regexp searches have been case-insensitive as well by default. (?-i) has been available to mode switch. We might want to move the various keyboard handling from FollowStreamDialog and ShowPacketBytesDialog and instead have FindLineEdit install shortcuts on the parents when constructed. That would be a little cleaner separation. We might also want to move the buttons and label to a separate composite widget class, that signals the parent to start a find. Fix #3784 --- ui/qt/follow_stream_dialog.cpp | 24 +++++++++++++++++++++--- ui/qt/follow_stream_dialog.ui | 7 +++++++ ui/qt/show_packet_bytes_dialog.cpp | 24 +++++++++++++++++++++--- ui/qt/show_packet_bytes_dialog.ui | 7 +++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/ui/qt/follow_stream_dialog.cpp b/ui/qt/follow_stream_dialog.cpp index 5b861527a6..2bdfa15825 100644 --- a/ui/qt/follow_stream_dialog.cpp +++ b/ui/qt/follow_stream_dialog.cpp @@ -279,15 +279,33 @@ void FollowStreamDialog::findText(bool go_back) if (ui->leFind->text().isEmpty()) return; bool found; + + QTextDocument::FindFlags options; + if (ui->caseCheckBox->isChecked()) { + options |= QTextDocument::FindCaseSensitively; + } if (use_regex_find_) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)) + // https://bugreports.qt.io/browse/QTBUG-88721 + // QPlainTextEdit::find() searches case-insensitively unless + // QTextDocument::FindCaseSensitively is explicitly given. + // This *does* apply to QRegularExpression (overriding + // CaseInsensitiveOption), but not QRegExp. + // + // QRegularExpression and QRegExp do not support Perl's /i, but + // the former at least does support the mode modifiers (?i) and + // (?-i), which can override QTextDocument::FindCaseSensitively. + // + // To make matters worse, while the QTextDocument::find() documentation + // is correct, QPlainTextEdit::find() claims that QRegularExpression + // works like QRegExp, which is incorrect. QRegularExpression regex(ui->leFind->text(), QRegularExpression::UseUnicodePropertiesOption); #else - QRegExp regex(ui->leFind->text()); + QRegExp regex(ui->leFind->text(), (options & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); #endif - found = ui->teStreamContent->find(regex); + found = ui->teStreamContent->find(regex, options); } else { - found = ui->teStreamContent->find(ui->leFind->text()); + found = ui->teStreamContent->find(ui->leFind->text(), options); } if (found) { diff --git a/ui/qt/follow_stream_dialog.ui b/ui/qt/follow_stream_dialog.ui index 08282c49bc..dc77586edd 100644 --- a/ui/qt/follow_stream_dialog.ui +++ b/ui/qt/follow_stream_dialog.ui @@ -155,6 +155,13 @@ + + + + Case sensitive + + + diff --git a/ui/qt/show_packet_bytes_dialog.cpp b/ui/qt/show_packet_bytes_dialog.cpp index d85a1ea50a..7a20774711 100644 --- a/ui/qt/show_packet_bytes_dialog.cpp +++ b/ui/qt/show_packet_bytes_dialog.cpp @@ -247,15 +247,33 @@ void ShowPacketBytesDialog::findText(bool go_back) if (ui->leFind->text().isEmpty()) return; bool found; + + QTextDocument::FindFlags options; + if (ui->caseCheckBox->isChecked()) { + options |= QTextDocument::FindCaseSensitively; + } if (use_regex_find_) { #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)) + // https://bugreports.qt.io/browse/QTBUG-88721 + // QPlainTextEdit::find() searches case-insensitively unless + // QTextDocument::FindCaseSensitively is explicitly given. + // This *does* apply to QRegularExpression (overriding + // CaseInsensitiveOption), but not QRegExp. + // + // QRegularExpression and QRegExp do not support Perl's /i, but + // the former at least does support the mode modifiers (?i) and + // (?-i), which can override QTextDocument::FindCaseSensitively. + // + // To make matters worse, while the QTextDocument::find() documentation + // is correct, QPlainTextEdit::find() claims that QRegularExpression + // works like QRegExp, which is incorrect. QRegularExpression regex(ui->leFind->text(), QRegularExpression::UseUnicodePropertiesOption); #else - QRegExp regex(ui->leFind->text()); + QRegExp regex(ui->leFind->text(), (options & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); #endif - found = ui->tePacketBytes->find(regex); + found = ui->tePacketBytes->find(regex, std::move(options)); } else { - found = ui->tePacketBytes->find(ui->leFind->text()); + found = ui->tePacketBytes->find(ui->leFind->text(), std::move(options)); } if (found) { diff --git a/ui/qt/show_packet_bytes_dialog.ui b/ui/qt/show_packet_bytes_dialog.ui index f60e625d60..dfe03757d5 100644 --- a/ui/qt/show_packet_bytes_dialog.ui +++ b/ui/qt/show_packet_bytes_dialog.ui @@ -116,6 +116,13 @@ + + + + Case sensitive + + +