From f47176a16d877e30d7a48515c9ba0794bbcd6a55 Mon Sep 17 00:00:00 2001 From: ronso0 Date: Tue, 19 Oct 2021 21:50:22 +0200 Subject: [PATCH] Search: avoid storing duplicates in slotSaveSearch() --- src/widget/wsearchlineedit.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/widget/wsearchlineedit.cpp b/src/widget/wsearchlineedit.cpp index 220c2ec1a9d5..a36824bcdf5f 100644 --- a/src/widget/wsearchlineedit.cpp +++ b/src/widget/wsearchlineedit.cpp @@ -443,23 +443,32 @@ void WSearchLineEdit::slotTriggerSearch() { /// saves the current query as selection void WSearchLineEdit::slotSaveSearch() { - int cIndex = findCurrentTextIndex(); #if ENABLE_TRACE_LOG kLogger.trace() << "save search. Index: " << cIndex; #endif // ENABLE_TRACE_LOG m_saveTimer.stop(); - // entry already exists and is on top - if (cIndex == 0) { + if (currentText().isEmpty() || !isEnabled()) { return; } - if (!currentText().isEmpty() && isEnabled()) { - // we remove the existing item and add a new one at the top - if (cIndex != -1) { - removeItem(cIndex); - } - insertItem(0, currentText()); + QString cText(currentText()); + if (currentIndex == -1) { + removeItem(-1); + } + // Check if the text is already listed + QSet querySet; + for (int index = 0; index < count(); index++) { + querySet.insert(itemText(index)); + } + if (querySet.contains(cText)) { + // If query exists clear the box and use its index to set the currentIndex + int cIndex = findCurrentTextIndex(); + setCurrentIndex(cIndex); + return; + } else { + // Else add it at the top + insertItem(0, cText); setCurrentIndex(0); while (count() > kMaxSearchEntries) { removeItem(kMaxSearchEntries);