From 265b779d6766130afa8f2f166733dc172ba22dca Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Thu, 15 Jan 2015 21:58:00 +0100
Subject: Fix shortcut handling for chat window navigation

This commit fixes CTRL-Tab, CTRL-Backtab and CTRL-W shortcuts on Mac,
Windows and Linux.

These shortcuts work in all mode except --no-tabs mode. This will be fixed
in a future commit. There is still different behavior across platforms:
on Windows and Linux they are handled on key press and repeat, and on
Mac they are handled on key release and do not repeat.

Test-Information:

Tested on OS X 10.9.5, Windows 7 and Elementary OS.

Change-Id: I2179f234cfd76b86cf261f2cf7500fc1cf0439a4

diff --git a/Swift/QtUI/QtChatTabs.cpp b/Swift/QtUI/QtChatTabs.cpp
index 0701d33..04c6a49 100644
--- a/Swift/QtUI/QtChatTabs.cpp
+++ b/Swift/QtUI/QtChatTabs.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -70,9 +70,19 @@ QtChatTabs::QtChatTabs(bool singleWindow, SettingsProvider* settingsProvider, bo
 	}
 
 	gridSelectionDialog_ = new QtGridSelectionDialog();
+
+	// setup shortcuts
+	shortcuts_ << new QShortcut(QKeySequence(tr("CTRL+W", "Close chat tab.")), window(), SLOT(handleCloseTabShortcut()));
+	shortcuts_ << new QShortcut(QKeySequence(Qt::CTRL, Qt::Key_PageUp), window(), SLOT(handleRequestedPreviousTab()));
+	shortcuts_ << new QShortcut(QKeySequence(Qt::CTRL, Qt::Key_PageDown), window(), SLOT(handleRequestedNextTab()));
+	shortcuts_ << new QShortcut(QKeySequence(Qt::ALT + Qt::Key_A), window(), SLOT(handleRequestedActiveTab()));
 }
 
 QtChatTabs::~QtChatTabs() {
+	foreach (QShortcut* shortcut, shortcuts_) {
+		delete shortcut;
+	}
+
 	if (trellisMode_) {
 		storeTabPositions();
 	}
@@ -230,6 +240,13 @@ void QtChatTabs::handleRequestedActiveTab() {
 }
 
 
+void QtChatTabs::handleCloseTabShortcut() {
+	QWidget* currentWidget = dynamicGrid_->currentWidget();
+	if (currentWidget) {
+		currentWidget->close();
+	}
+}
+
 void QtChatTabs::handleTabCloseRequested(int index) {
 	if (trellisMode_) {
 		storeTabPositions();
diff --git a/Swift/QtUI/QtChatTabs.h b/Swift/QtUI/QtChatTabs.h
index 6cb4dfe..04e9eca 100644
--- a/Swift/QtUI/QtChatTabs.h
+++ b/Swift/QtUI/QtChatTabs.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010-2014 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -8,6 +8,7 @@
 
 #include <QWidget>
 #include <QRect>
+#include <QShortcut>
 
 class QTabWidget;
 class QMenu;
@@ -54,6 +55,8 @@ namespace Swift {
 
 			void handleOpenLayoutChangeDialog();
 
+			void handleCloseTabShortcut();
+
 		private:
 			void storeTabPositions();
 			void checkForFirstShow();
@@ -64,6 +67,8 @@ namespace Swift {
 			bool trellisMode_;
 			QtDynamicGridLayout* dynamicGrid_;
 			QtGridSelectionDialog* gridSelectionDialog_;
+
+			QList<QShortcut*> shortcuts_;
 	};
 }
 
diff --git a/Swift/QtUI/QtTabbable.cpp b/Swift/QtUI/QtTabbable.cpp
index 962bd9d..feab35e 100644
--- a/Swift/QtUI/QtTabbable.cpp
+++ b/Swift/QtUI/QtTabbable.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -19,16 +19,10 @@
 namespace Swift {
 
 QtTabbable::QtTabbable() : QWidget() {
-	shortcuts << new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), window(), SLOT(close()));
-	shortcuts << new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_PageUp), window(), SIGNAL(requestPreviousTab()));
-	shortcuts << new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_PageDown), window(), SIGNAL(requestNextTab()));
-	shortcuts << new QShortcut(QKeySequence(Qt::ALT + Qt::Key_A), window(), SIGNAL(requestActiveTab()));
+
 }
 
 QtTabbable::~QtTabbable() {
-	foreach (QShortcut* shortcut, shortcuts) {
-		delete shortcut;
-	}
 	emit windowClosing();
 }
 
@@ -45,16 +39,20 @@ bool QtTabbable::event(QEvent* event) {
 	QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
 	if (keyEvent) {
 		// According to Qt's focus documentation, one can only override CTRL+TAB via reimplementing QWidget::event().
-#ifdef SWIFTEN_PLATFORM_LINUX
-		if (keyEvent->modifiers().testFlag(QtUtilities::ctrlHardwareKeyModifier) && keyEvent->key() == Qt::Key_Tab && event->type() != QEvent::KeyRelease) {
-#else
 		if (keyEvent->modifiers().testFlag(QtUtilities::ctrlHardwareKeyModifier) && keyEvent->key() == Qt::Key_Tab) {
-#endif
-			emit requestNextTab();
+			if (keyEvent->type() != QEvent::ShortcutOverride) {
+				emit requestNextTab();
+			}
 			return true;
 		}
+#ifdef SWIFTEN_PLATFORM_LINUX
+		else if (keyEvent->modifiers().testFlag(QtUtilities::ctrlHardwareKeyModifier) && keyEvent->key() == Qt::Key_Backtab && keyEvent->type() != QEvent::KeyRelease) {
+#else
 		else if (keyEvent->modifiers().testFlag(QtUtilities::ctrlHardwareKeyModifier) && keyEvent->key() == Qt::Key_Backtab) {
-			emit requestPreviousTab();
+#endif
+			if (keyEvent->type() != QEvent::ShortcutOverride) {
+				emit requestPreviousTab();
+			}
 			return true;
 		}
 	}
diff --git a/Swift/QtUI/QtTabbable.h b/Swift/QtUI/QtTabbable.h
index c3cfa76..db83ba9 100644
--- a/Swift/QtUI/QtTabbable.h
+++ b/Swift/QtUI/QtTabbable.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Isode Limited.
+ * Copyright (c) 2014-2015 Isode Limited.
  * All rights reserved.
  * See the COPYING file for more information.
  */
@@ -8,11 +8,8 @@
 
 #include <string>
 
-#include <QList>
 #include <QWidget>
 
-class QShortcut;
-
 namespace Swift {
 	class QtTabbable : public QWidget {
 		Q_OBJECT
@@ -38,8 +35,5 @@ namespace Swift {
 			void requestNextTab();
 			void requestActiveTab();
 			void requestFlash();
-
-		private:
-			QList<QShortcut*> shortcuts;
 	};
 }
-- 
cgit v0.10.2-6-g49f6