From 0c6d1216fe7ced55b8c7ce173f28dfe85aa317fc Mon Sep 17 00:00:00 2001 From: David Perry Date: Mon, 12 Jul 2021 07:50:36 -0400 Subject: [PATCH] Rework how comments show in edit menu Addresses [this issue][1] reported with the revised comment editing UI, wherein comments with embedded newlines may not appear properly in the menu. [1]: https://gitlab.com/wireshark/wireshark/-/merge_requests/2859#note_621024711 --- ui/qt/main_window.h | 1 + ui/qt/main_window_slots.cpp | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ui/qt/main_window.h b/ui/qt/main_window.h index fcd4fdb131..e67a52e486 100644 --- a/ui/qt/main_window.h +++ b/ui/qt/main_window.h @@ -397,6 +397,7 @@ private slots: void actionAddPacketComment(); void actionEditPacketComment(); void actionDeletePacketComment(); + QString commentToMenuText(QString text, int max_len = 40); void setEditCommentsMenu(); void setMenusForSelectedPacket(); void setMenusForSelectedTreeRow(FieldInformation *fi = NULL); diff --git a/ui/qt/main_window_slots.cpp b/ui/qt/main_window_slots.cpp index ded526d013..eed66d9ad9 100644 --- a/ui/qt/main_window_slots.cpp +++ b/ui/qt/main_window_slots.cpp @@ -1114,6 +1114,21 @@ void MainWindow::recentActionTriggered() { } } +QString MainWindow::commentToMenuText(QString text, int max_len) +{ + text = text.trimmed().replace(QRegExp("(\\r?\\n|\\r\\n?)+"), " "); + if (text.size() > 0) { + if (text.size() > max_len) { + text.truncate(max_len); + text += "…"; + } + } + else { + text = tr("(empty comment)", "placeholder for empty comment"); + } + return text; +} + void MainWindow::setEditCommentsMenu() { main_ui_->menuPacketComment->clear(); @@ -1127,11 +1142,8 @@ void MainWindow::setEditCommentsMenu() QAction *aPtr; main_ui_->menuPacketComment->addSeparator(); for (guint i = 0; i < nComments; i++) { - QString comment = packet_list_->getPacketComment(i).trimmed(); - if (comment.size() > 40) { - comment.truncate(40); - comment += "…"; - } + QString comment = packet_list_->getPacketComment(i); + comment = this->commentToMenuText(comment); aPtr = main_ui_->menuPacketComment->addAction(tr("Edit \"%1\"", "edit packet comment").arg(comment), this, SLOT(actionEditPacketComment())); aPtr->setData(i); @@ -1139,11 +1151,8 @@ void MainWindow::setEditCommentsMenu() main_ui_->menuPacketComment->addSeparator(); for (guint i = 0; i < nComments; i++) { - QString comment = packet_list_->getPacketComment(i).trimmed(); - if (comment.size() > 40) { - comment.truncate(40); - comment += "…"; - } + QString comment = packet_list_->getPacketComment(i); + comment = this->commentToMenuText(comment); aPtr = main_ui_->menuPacketComment->addAction(tr("Delete \"%1\"", "delete packet comment").arg(comment), this, SLOT(actionDeletePacketComment())); aPtr->setData(i);