From eccff5358af0eaf9c900262221e592b5cfe773ff Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 16:45:51 +0100 Subject: [PATCH] Add a timer in SharedFilesDialog filter to not so often triggs search in files. --- .../gui/FileTransfer/SharedFilesDialog.cpp | 62 +++++++++++++------ .../src/gui/FileTransfer/SharedFilesDialog.h | 9 ++- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index ae6377120..e423472e2 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -186,7 +186,14 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD connect(ui.filterClearButton, SIGNAL(clicked()), this, SLOT(clearFilter())); connect(ui.filterStartButton, SIGNAL(clicked()), this, SLOT(startFilter())); connect(ui.filterPatternLineEdit, SIGNAL(returnPressed()), this, SLOT(startFilter())); - connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(filterRegExpChanged())); + connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); + //Hidden by default, shown on onFilterTextEdited + ui.filterClearButton->hide(); + ui.filterStartButton->hide(); + + mFilterTimer = new RsProtectedTimer( this ); + mFilterTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started + connect(mFilterTimer, SIGNAL(timeout()), this, SLOT(filterRegExpChanged())); /* Set header resize modes and initial section sizes */ QHeaderView * header = ui.dirTreeView->header () ; @@ -1246,34 +1253,40 @@ void SharedFilesDialog::indicatorChanged(int index) updateDisplay() ; } -void SharedFilesDialog::filterRegExpChanged() +void SharedFilesDialog::onFilterTextEdited() { - QString text = ui.filterPatternLineEdit->text(); + QString text = ui.filterPatternLineEdit->text(); - if (text.isEmpty()) { - ui.filterClearButton->hide(); - } else { - ui.filterClearButton->show(); - } + if (text.isEmpty()) { + ui.filterClearButton->hide(); + } else { + ui.filterClearButton->show(); + } - if (text == lastFilterString) { - ui.filterStartButton->hide(); - } else { - ui.filterStartButton->show(); - } - - //bool valid = false ; - //QColor color ; + if (text == lastFilterString) { + ui.filterStartButton->hide(); + } else { + ui.filterStartButton->show(); + } if(text.length() > 0 && text.length() < 3) { - //valid = false; - ui.filterStartButton->setEnabled(false) ; ui.filterPatternFrame->setToolTip(tr("Search string should be at least 3 characters long.")) ; return ; } + ui.filterStartButton->setEnabled(true) ; + ui.filterPatternFrame->setToolTip(QString()); + + mFilterTimer->start( 500 ); // This will fire filterRegExpChanged after 500 ms. + // If the user types something before it fires, the timer restarts counting +} + +void SharedFilesDialog::filterRegExpChanged() +{ + QString text = ui.filterPatternLineEdit->text(); + if(text.length() > 0 && proxyModel == tree_proxyModel) { std::list result_list ; @@ -1497,8 +1510,19 @@ void SharedFilesDialog::FilterItems() std::cerr << "Found " << result_list.size() << " results" << std::endl; #endif - if(result_list.size() > MAX_SEARCH_RESULTS) + size_t resSize = result_list.size(); + if(resSize == 0) + { + ui.filterPatternFrame->setToolTip(tr("No result.")) ; return ; + } + if(resSize > MAX_SEARCH_RESULTS) + { + ui.filterPatternFrame->setToolTip(tr("More than %1 results. Add more/longer search words to select less.").arg(MAX_SEARCH_RESULTS)) ; + return ; + } + ui.filterPatternFrame->setToolTip(tr("Found %1 results.").arg(resSize)) ; + #ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "Found this result: " << std::endl; #endif diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h index 982abd275..63a57683c 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h @@ -22,10 +22,13 @@ #ifndef _SHAREDFILESDIALOG_H #define _SHAREDFILESDIALOG_H -#include +#include "ui_SharedFilesDialog.h" + #include "RsAutoUpdatePage.h" #include "gui/RetroShareLink.h" -#include "ui_SharedFilesDialog.h" +#include "util/RsProtectedTimer.h" + +#include class RetroshareDirModel; class QSortFilterProxyModel; @@ -73,6 +76,7 @@ private slots: void indicatorChanged(int index); + void onFilterTextEdited(); void filterRegExpChanged(); void clearFilter(); void startFilter(); @@ -137,6 +141,7 @@ protected: QString lastFilterString; QString mLastFilterText ; + RsProtectedTimer* mFilterTimer; QList mHiddenIndexes; };