[qet] [4550] Elements collection widget : improve the search

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 4550
Author:   blacksun
Date:     2016-06-10 18:20:01 +0200 (Fri, 10 Jun 2016)
Log Message:
-----------
Elements collection widget : improve the search

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementscollectionwidget.cpp
    trunk/sources/ElementsCollection/elementscollectionwidget.h

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-06-09 20:46:27 UTC (rev 4549)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.cpp	2016-06-10 16:20:01 UTC (rev 4550)
@@ -49,6 +49,12 @@
 	setUpWidget();
 	setUpAction();
 	setUpConnection();
+
+		//Timer is used to avoid launching a new search for each letter typed by user
+		//Timer is started or restarted at every time user type a new letter.
+		//When the timer emit timeout, we start the search.
+	m_search_timer.setInterval(500);
+	m_search_timer.setSingleShot(true);
 }
 
 /**
@@ -148,7 +154,8 @@
 void ElementsCollectionWidget::setUpConnection()
 {
 	connect(m_tree_view,      &QTreeView::customContextMenuRequested, this, &ElementsCollectionWidget::customContextMenu);
-	connect(m_search_field,   &QLineEdit::textEdited, this, &ElementsCollectionWidget::search);
+	connect(m_search_field,   &QLineEdit::textEdited, [this]() {m_search_timer.start();});
+	connect(&m_search_timer,   &QTimer::timeout,   this, &ElementsCollectionWidget::search);
 	connect(m_open_dir,       &QAction::triggered, this, &ElementsCollectionWidget::openDir);
 	connect(m_edit_element,   &QAction::triggered, this, &ElementsCollectionWidget::editElement);
 	connect(m_delete_element, &QAction::triggered, this, &ElementsCollectionWidget::deleteElement);
@@ -413,7 +420,7 @@
 		ElementCollectionItem *eci = elementCollectionItemForIndex(m_showed_index);
 		if (eci)
 			eci->setBackground(QBrush(Qt::yellow));
-		search(m_search_field->text());
+		search();
 	}
 	else
 		resetShowThisDir();
@@ -434,7 +441,7 @@
 	}
 
 	m_showed_index = QModelIndex();
-	search(m_search_field->text());
+	search();
 }
 
 /**
@@ -476,12 +483,12 @@
 
 /**
  * @brief ElementsCollectionWidget::search
- * Search every item (directory or element) that match the string @text
+ * Search every item (directory or element) that match the text of m_search_field
  * and display it, other item who does not match @text is hidden
- * @param text
  */
-void ElementsCollectionWidget::search(const QString &text)
+void ElementsCollectionWidget::search()
 {
+	QString text = m_search_field->text();
 		//Reset the search
 	if (text.isEmpty())
 	{

Modified: trunk/sources/ElementsCollection/elementscollectionwidget.h
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionwidget.h	2016-06-09 20:46:27 UTC (rev 4549)
+++ trunk/sources/ElementsCollection/elementscollectionwidget.h	2016-06-10 16:20:01 UTC (rev 4550)
@@ -20,6 +20,7 @@
 
 #include <QWidget>
 #include <QModelIndex>
+#include <QTimer>
 
 class ElementsCollectionModel;
 class QVBoxLayout;
@@ -65,7 +66,7 @@
 		void newElement();
 		void showThisDir();
 		void resetShowThisDir();
-		void search(const QString &text);
+		void search();
 		void hideCollection(bool hide = true);
 		void hideItem(bool hide, const QModelIndex &index = QModelIndex(), bool recursive = true);
 		void showAndExpandItem (const QModelIndex &index, bool parent = true, bool child = false);
@@ -78,6 +79,7 @@
 	private:
 		ElementsCollectionModel *m_model;
 		QLineEdit *m_search_field;
+		QTimer m_search_timer;
 		ElementsTreeView *m_tree_view;
 		QVBoxLayout *m_main_vlayout;
 		QMenu *m_context_menu;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/