From 9179b54ac93ddc88765c3cd984916d7ffd130d20 Mon Sep 17 00:00:00 2001
From: Richard Maudsley <richard.maudsley@isode.com>
Date: Tue, 1 Jul 2014 13:54:14 +0100
Subject: Reset roster filter when hitting enter to start chat.

Test-Information:

Enter search term and use keyboard arrows to move to select a contact and pressing enter will start a chat and clear the filter. Confirm that pressing escape still clears the filter without starting a chat and that the changes do not interfere with starting a chat normally by double clicking on a contact.

Change-Id: I90f5d431da56896eeb10f16c0ba23bdc143c4857

diff --git a/Swift/QtUI/QtMainWindow.cpp b/Swift/QtUI/QtMainWindow.cpp
index 7af9728..1acc519 100644
--- a/Swift/QtUI/QtMainWindow.cpp
+++ b/Swift/QtUI/QtMainWindow.cpp
@@ -79,7 +79,7 @@ QtMainWindow::QtMainWindow(SettingsProvider* settings, UIEventStream* uiEventStr
 	treeWidget_ = new QtRosterWidget(uiEventStream_, settings_, this);
 
 	contactTabLayout->addWidget(treeWidget_);
-	new QtFilterWidget(this, treeWidget_, contactTabLayout);
+	new QtFilterWidget(this, treeWidget_, uiEventStream_, contactTabLayout);
 
 	tabs_->addTab(contactsTabWidget_, tr("&Contacts"));
 
diff --git a/Swift/QtUI/Roster/QtFilterWidget.cpp b/Swift/QtUI/Roster/QtFilterWidget.cpp
index 5bd4669..2f08981 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.cpp
+++ b/Swift/QtUI/Roster/QtFilterWidget.cpp
@@ -4,20 +4,20 @@
  * See Documentation/Licenses/BSD-simplified.txt for more information.
  */
 
-#include <Swift/QtUI/Roster/QtFilterWidget.h>
-
 #include <QLayout>
 #include <QVBoxLayout>
 #include <QKeyEvent>
 #include <QEvent>
 #include <QString>
 #include <QEvent>
-
+#include <Swift/Controllers/UIEvents/RequestChatUIEvent.h>
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
 #include <Swift/QtUI/QtSwiftUtil.h>
+#include <Swift/QtUI/Roster/QtFilterWidget.h>
 
 namespace Swift {
 
-QtFilterWidget::QtFilterWidget(QWidget* parent, QtTreeWidget* treeView, QBoxLayout* layout) : QWidget(parent), treeView_(treeView), fuzzyRosterFilter_(0), isModifierSinglePressed_(false) {
+QtFilterWidget::QtFilterWidget(QWidget* parent, QtTreeWidget* treeView, UIEventStream* eventStream, QBoxLayout* layout) : QWidget(parent), treeView_(treeView), eventStream_(eventStream), fuzzyRosterFilter_(0), isModifierSinglePressed_(false) {
 	int targetIndex = layout->indexOf(treeView);
 
 	QVBoxLayout* vboxLayout = new QVBoxLayout(this);
@@ -65,8 +65,12 @@ bool QtFilterWidget::eventFilter(QObject*, QEvent* event) {
 				QApplication::postEvent(treeView_, new QContextMenuEvent(QContextMenuEvent::Keyboard, contextMenuPosition, treeView_->mapToGlobal(contextMenuPosition)));
 				return true;
 			} else if (keyEvent->key() == Qt::Key_Return) {
+				JID target = treeView_->selectedJID();
+				if (target.isValid()) {
+					eventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(target)));
+				}
 				filterLineEdit_->setText("");
-				return false;
+				updateRosterFilters();
 			} else if (keyEvent->key() == Qt::Key_Escape) {
 				filterLineEdit_->setText("");
 			} else {
@@ -77,26 +81,8 @@ bool QtFilterWidget::eventFilter(QObject*, QEvent* event) {
 		filterLineEdit_->event(event);
 		filterLineEdit_->setVisible(!filterLineEdit_->text().isEmpty());
 
-		// update roster filters
 		if (event->type() == QEvent::KeyRelease) {
-			if (fuzzyRosterFilter_) {
-				if (filterLineEdit_->text().isEmpty()) {
-					// remove currently installed search filter and put old filters back
-					treeView_->getRoster()->removeFilter(fuzzyRosterFilter_);
-					delete fuzzyRosterFilter_;
-					fuzzyRosterFilter_ = NULL;
-					pushAllFilters();
-				} else {
-					// remove currently intsalled search filter and put new search filter in place
-					updateSearchFilter();
-				}
-			} else {
-				if (!filterLineEdit_->text().isEmpty()) {
-					// remove currently installed filters and put a search filter in place
-					popAllFilters();
-					updateSearchFilter();
-				}
-			}
+			updateRosterFilters();
 		}
 		return true;
 	}
@@ -118,6 +104,27 @@ void QtFilterWidget::pushAllFilters() {
 	filters_.clear();
 }
 
+void QtFilterWidget::updateRosterFilters() {
+	if (fuzzyRosterFilter_) {
+		if (filterLineEdit_->text().isEmpty()) {
+			// remove currently installed search filter and put old filters back
+			treeView_->getRoster()->removeFilter(fuzzyRosterFilter_);
+			delete fuzzyRosterFilter_;
+			fuzzyRosterFilter_ = NULL;
+			pushAllFilters();
+		} else {
+			// remove currently intsalled search filter and put new search filter in place
+			updateSearchFilter();
+		}
+	} else {
+		if (!filterLineEdit_->text().isEmpty()) {
+			// remove currently installed filters and put a search filter in place
+			popAllFilters();
+			updateSearchFilter();
+		}
+	}
+}
+
 void QtFilterWidget::updateSearchFilter() {
 	if (fuzzyRosterFilter_) {
 		treeView_->getRoster()->removeFilter(fuzzyRosterFilter_);
diff --git a/Swift/QtUI/Roster/QtFilterWidget.h b/Swift/QtUI/Roster/QtFilterWidget.h
index e33616b..94ebc2a 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.h
+++ b/Swift/QtUI/Roster/QtFilterWidget.h
@@ -19,10 +19,12 @@
 
 namespace Swift {
 
+class UIEventStream;
+
 class QtFilterWidget : public QWidget {
 	Q_OBJECT
 	public:
-		QtFilterWidget(QWidget* parent, QtTreeWidget* treeView, QBoxLayout* layout = 0);
+		QtFilterWidget(QWidget* parent, QtTreeWidget* treeView, UIEventStream* eventStream, QBoxLayout* layout = 0);
 		virtual ~QtFilterWidget();
 
 	protected:
@@ -32,11 +34,13 @@ class QtFilterWidget : public QWidget {
 		void popAllFilters();
 		void pushAllFilters();
 
+		void updateRosterFilters();
 		void updateSearchFilter();
 
 	private:
 		QLineEdit* filterLineEdit_;
 		QtTreeWidget* treeView_;
+		UIEventStream* eventStream_;
 		std::vector<RosterFilter*> filters_;
 		QAbstractItemModel* sourceModel_;
 		FuzzyRosterFilter* fuzzyRosterFilter_;
diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index fbe85de..5333260 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -138,14 +138,7 @@ void QtTreeWidget::currentChanged(const QModelIndex& current, const QModelIndex&
 }
 
 void QtTreeWidget::handleItemActivated(const QModelIndex& index) {
-	JID target;
-	if (messageTarget_ == MessageDisplayJID) {
-		target = JID(Q2PSTRING(index.data(DisplayJIDRole).toString()));
-		target = target.toBare();
-	}
-	if (!target.isValid()) {
-		target = JID(Q2PSTRING(index.data(JIDRole).toString()));
-	}
+	JID target = jidFromIndex(index);
 	if (target.isValid()) {
 		eventStream_->send(boost::shared_ptr<UIEvent>(new RequestChatUIEvent(target)));
 	}
@@ -239,4 +232,24 @@ void QtTreeWidget::setMessageTarget(MessageTarget messageTarget) {
 	messageTarget_ = messageTarget;
 }
 
+JID QtTreeWidget::jidFromIndex(const QModelIndex& index) const {
+	JID target;
+	if (messageTarget_ == MessageDisplayJID) {
+		target = JID(Q2PSTRING(index.data(DisplayJIDRole).toString()));
+		target = target.toBare();
+	}
+	if (!target.isValid()) {
+		target = JID(Q2PSTRING(index.data(JIDRole).toString()));
+	}
+	return target;
+}
+
+JID QtTreeWidget::selectedJID() const {
+	QModelIndexList list = selectedIndexes();
+	if (list.size() != 1) {
+		return JID();
+	}
+	return jidFromIndex(list[0]);
+}
+
 }
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index 29e985d..cf2f73e 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -34,6 +34,8 @@ class QtTreeWidget : public QTreeView {
 		Roster* getRoster() {return roster_;}
 		void refreshTooltip();
 		void setMessageTarget(MessageTarget messageTarget);
+		JID jidFromIndex(const QModelIndex& index) const;
+		JID selectedJID() const;
 	public:
 		boost::signal<void (RosterItem*)> onSomethingSelectedChanged;
 
-- 
cgit v0.10.2-6-g49f6