From 8ab7ca17fdde8f8fb62a0c574478aa2c4c01a9bc Mon Sep 17 00:00:00 2001
From: Richard Maudsley <richard.maudsley@isode.com>
Date: Wed, 2 Jul 2014 11:40:38 +0100
Subject: Added close/clear button to roster filter search term box.

Test-Information:

Verify that the clear button resets the roster filter and hides the search box. Verify that the roster filter continues to behave as normal.

Change-Id: Ifa5de1e611334b83634ac31d30bf912fd5c4da87

diff --git a/COPYING.thirdparty b/COPYING.thirdparty
index aeaf066..a4b505c 100644
--- a/COPYING.thirdparty
+++ b/COPYING.thirdparty
@@ -81,6 +81,7 @@ These projects contain source code from third parties:
 - Expat
 - ZLib
 - LibIDN
+- Trolltech
 
 The licenses under which these contributions and libraries are released are listed below.
 
@@ -756,3 +757,19 @@ necessary.  Here is a sample; alter the names:
   Ty Coon, President of Vice
 
 That's all there is to it!
+
+
+=========
+Trolltech
+=========
+
+Demo code from http://git.forwardbias.in/?p=lineeditclearbutton.git with license:
+
+/****************************************************************************
+**
+** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
+**
+** Use, modification and distribution is allowed without limitation,
+** warranty, liability or support of any kind.
+**
+****************************************************************************/
diff --git a/Swift/QtUI/QtClosableLineEdit.cpp b/Swift/QtUI/QtClosableLineEdit.cpp
new file mode 100644
index 0000000..512194b
--- /dev/null
+++ b/Swift/QtUI/QtClosableLineEdit.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+/* Contains demo Trolltech code from http://git.forwardbias.in/?p=lineeditclearbutton.git with license: */
+/****************************************************************************
+**
+** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
+**
+** Use, modification and distribution is allowed without limitation,
+** warranty, liability or support of any kind.
+**
+****************************************************************************/
+
+#include <Swift/QtUI/QtClosableLineEdit.h>
+#include <QApplication>
+#include <QToolButton>
+#include <QStyle>
+#include <QKeyEvent>
+
+namespace Swift {
+
+const int QtClosableLineEdit::clearButtonPadding = 2;
+
+QtClosableLineEdit::QtClosableLineEdit(QWidget *parent) : QLineEdit(parent) {
+	clearButton = new QToolButton(this);
+	clearButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton));
+	clearButton->setIconSize(QSize(16,16));
+	clearButton->setCursor(Qt::ArrowCursor);
+	clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
+	clearButton->hide();
+	connect(clearButton, SIGNAL(clicked()), this, SLOT(handleCloseButtonClicked()));
+	connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&)));
+	int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
+	setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(clearButton->sizeHint().width() + frameWidth + 1));
+	QSize minimumSize = minimumSizeHint();
+	setMinimumSize(qMax(minimumSize.width(), clearButton->sizeHint().width() + frameWidth * 2 + clearButtonPadding),
+								qMax(minimumSize.height(), clearButton->sizeHint().height() + frameWidth * 2 + clearButtonPadding));
+}
+
+void QtClosableLineEdit::resizeEvent(QResizeEvent *) {
+	QSize size = clearButton->sizeHint();
+	int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
+	clearButton->move(rect().right() - frameWidth - size.width(), (rect().bottom() + 1 - size.height())/2);
+}
+
+void QtClosableLineEdit::updateCloseButton(const QString& text) {
+	clearButton->setVisible(!text.isEmpty());
+}
+
+void QtClosableLineEdit::handleCloseButtonClicked() {
+	clear();
+	QApplication::postEvent(this, new QKeyEvent(QEvent::KeyPress, Qt::Key_Escape, Qt::NoModifier));
+	QApplication::postEvent(this, new QKeyEvent(QEvent::KeyRelease, Qt::Key_Escape, Qt::NoModifier));
+}
+
+}
diff --git a/Swift/QtUI/QtClosableLineEdit.h b/Swift/QtUI/QtClosableLineEdit.h
new file mode 100644
index 0000000..91b4f0e
--- /dev/null
+++ b/Swift/QtUI/QtClosableLineEdit.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+/* Contains demo Trolltech code from http://git.forwardbias.in/?p=lineeditclearbutton.git with license: */
+/****************************************************************************
+**
+** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
+**
+** Use, modification and distribution is allowed without limitation,
+** warranty, liability or support of any kind.
+**
+****************************************************************************/
+
+#pragma once
+
+#include <QLineEdit>
+
+class QToolButton;
+
+namespace Swift {
+
+class QtClosableLineEdit : public QLineEdit
+{
+	Q_OBJECT
+	public:
+		QtClosableLineEdit(QWidget *parent = 0);
+
+	protected:
+		void resizeEvent(QResizeEvent *);
+
+	private slots:
+		void updateCloseButton(const QString &text);
+		void handleCloseButtonClicked();
+
+	private:
+		static const int clearButtonPadding;
+		QToolButton *clearButton;
+};
+
+}
diff --git a/Swift/QtUI/Roster/QtFilterWidget.cpp b/Swift/QtUI/Roster/QtFilterWidget.cpp
index 2f08981..a4c9449 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.cpp
+++ b/Swift/QtUI/Roster/QtFilterWidget.cpp
@@ -1,17 +1,24 @@
 /*
+ * Copyright (c) 2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+/*
  * Copyright (c) 2013 Tobias Markmann
  * Licensed under the simplified BSD license.
  * See Documentation/Licenses/BSD-simplified.txt for more information.
  */
 
-#include <QLayout>
-#include <QVBoxLayout>
-#include <QKeyEvent>
 #include <QEvent>
+#include <QKeyEvent>
+#include <QLayout>
 #include <QString>
-#include <QEvent>
+#include <QVBoxLayout>
+
 #include <Swift/Controllers/UIEvents/RequestChatUIEvent.h>
 #include <Swift/Controllers/UIEvents/UIEventStream.h>
+#include <Swift/QtUI/QtClosableLineEdit.h>
 #include <Swift/QtUI/QtSwiftUtil.h>
 #include <Swift/QtUI/Roster/QtFilterWidget.h>
 
@@ -24,7 +31,7 @@ QtFilterWidget::QtFilterWidget(QWidget* parent, QtTreeWidget* treeView, UIEventS
 	vboxLayout->setSpacing(0);
 	vboxLayout->setContentsMargins(0,0,0,0);
 
-	filterLineEdit_ = new QLineEdit(this);
+	filterLineEdit_ = new QtClosableLineEdit(this);
 	filterLineEdit_->hide();
 	vboxLayout->addWidget(filterLineEdit_);
 
diff --git a/Swift/QtUI/Roster/QtFilterWidget.h b/Swift/QtUI/Roster/QtFilterWidget.h
index 94ebc2a..3e17566 100644
--- a/Swift/QtUI/Roster/QtFilterWidget.h
+++ b/Swift/QtUI/Roster/QtFilterWidget.h
@@ -9,7 +9,6 @@
 #include <vector>
 
 #include <QBoxLayout>
-#include <QLineEdit>
 #include <QWidget>
 
 #include <Swift/Controllers/Roster/RosterFilter.h>
@@ -18,9 +17,8 @@
 #include <Swift/QtUI/Roster/QtTreeWidget.h>
 
 namespace Swift {
-
 class UIEventStream;
-
+class QtClosableLineEdit;
 class QtFilterWidget : public QWidget {
 	Q_OBJECT
 	public:
@@ -38,7 +36,7 @@ class QtFilterWidget : public QWidget {
 		void updateSearchFilter();
 
 	private:
-		QLineEdit* filterLineEdit_;
+		QtClosableLineEdit* filterLineEdit_;
 		QtTreeWidget* treeView_;
 		UIEventStream* eventStream_;
 		std::vector<RosterFilter*> filters_;
diff --git a/Swift/QtUI/SConscript b/Swift/QtUI/SConscript
index 06b04fa..53dbea9 100644
--- a/Swift/QtUI/SConscript
+++ b/Swift/QtUI/SConscript
@@ -130,6 +130,7 @@ sources = [
     "QtHighlightRulesItemModel.cpp",
     "QtHighlightRuleWidget.cpp",
     "QtColorToolButton.cpp",
+    "QtClosableLineEdit.cpp",
     "ChatSnippet.cpp",
     "MessageSnippet.cpp",
     "SystemMessageSnippet.cpp",
-- 
cgit v0.10.2-6-g49f6