Browse Source

UI: Add LineEditChanged and LineEditCanceled

These functions allow item delegates (editors) or item widgets with
event filters to detect whether a user has finished editing a line edit
control.  This separates the code so it can be used elsewhere than just
in the source tree widget.
jp9000 6 years ago
parent
commit
d5d8492bb3
3 changed files with 40 additions and 19 deletions
  1. 30 0
      UI/qt-wrappers.cpp
  2. 3 0
      UI/qt-wrappers.hpp
  3. 7 19
      UI/source-tree.cpp

+ 30 - 0
UI/qt-wrappers.cpp

@@ -24,6 +24,7 @@
 #include <QLayout>
 #include <QMessageBox>
 #include <QDataStream>
+#include <QKeyEvent>
 
 #if !defined(_WIN32) && !defined(__APPLE__)
 #include <QX11Info>
@@ -293,3 +294,32 @@ void ExecThreadedWithoutBlocking(std::function<void()> func,
 	else
 		ExecuteFuncSafeBlockMsgBox(func, title, text);
 }
+
+bool LineEditCanceled(QEvent *event)
+{
+	if (event->type() == QEvent::KeyPress) {
+		QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
+		return keyEvent->key() == Qt::Key_Escape;
+	}
+
+	return false;
+}
+
+bool LineEditChanged(QEvent *event)
+{
+	if (event->type() == QEvent::KeyPress) {
+		QKeyEvent *keyEvent = reinterpret_cast<QKeyEvent *>(event);
+
+		switch (keyEvent->key()) {
+		case Qt::Key_Tab:
+		case Qt::Key_Backtab:
+		case Qt::Key_Enter:
+		case Qt::Key_Return:
+			return true;
+		}
+	} else if (event->type() == QEvent::FocusOut) {
+		return true;
+	}
+
+	return false;
+}

+ 3 - 0
UI/qt-wrappers.hpp

@@ -102,3 +102,6 @@ static inline Qt::ConnectionType WaitConnection()
 		       ? Qt::DirectConnection
 		       : Qt::BlockingQueuedConnection;
 }
+
+bool LineEditCanceled(QEvent *event);
+bool LineEditChanged(QEvent *event);

+ 7 - 19
UI/source-tree.cpp

@@ -342,25 +342,13 @@ bool SourceTreeItem::eventFilter(QObject *object, QEvent *event)
 	if (editor != object)
 		return false;
 
-	if (event->type() == QEvent::KeyPress) {
-		QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
-
-		switch (keyEvent->key()) {
-		case Qt::Key_Escape:
-			QMetaObject::invokeMethod(this, "ExitEditMode",
-						  Qt::QueuedConnection,
-						  Q_ARG(bool, false));
-			return true;
-		case Qt::Key_Tab:
-		case Qt::Key_Backtab:
-		case Qt::Key_Enter:
-		case Qt::Key_Return:
-			QMetaObject::invokeMethod(this, "ExitEditMode",
-						  Qt::QueuedConnection,
-						  Q_ARG(bool, true));
-			return true;
-		}
-	} else if (event->type() == QEvent::FocusOut) {
+	if (LineEditCanceled(event)) {
+		QMetaObject::invokeMethod(this, "ExitEditMode",
+					  Qt::QueuedConnection,
+					  Q_ARG(bool, false));
+		return true;
+	}
+	if (LineEditChanged(event)) {
 		QMetaObject::invokeMethod(this, "ExitEditMode",
 					  Qt::QueuedConnection,
 					  Q_ARG(bool, true));