From 6a4e622d9c406ea45bcc8c9740f666ad4a7e589f Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Mon, 19 Jan 2015 16:39:54 +0100
Subject: Fix focus handling bug with regard to tab changing

QtChatWindow: Require now parameter is set in QtChatWindow::qAppFocusChanged
which is required so only the visible tab that is changed to gets focus.

QtDynamicGridLayout: A change of tab in a QTabWidget set focus on the new
tab even if the QTabWidget has no keyboard focus.

Test-Information:

Tested on Mac OS X 10.9.5 and Qt 5.4.0.

Change-Id: I3d9d230c7753f1898b251b49c06fe7318ad953fb

diff --git a/Swift/QtUI/QtChatWindow.cpp b/Swift/QtUI/QtChatWindow.cpp
index da1cf18..6324e9d 100644
--- a/Swift/QtUI/QtChatWindow.cpp
+++ b/Swift/QtUI/QtChatWindow.cpp
@@ -446,8 +446,8 @@ void QtChatWindow::convertToMUC(MUCType mucType) {
 	subject_->setVisible(!impromptu_);
 }
 
-void QtChatWindow::qAppFocusChanged(QWidget* /*old*/, QWidget* /*now*/) {
-	if (isWidgetSelected()) {
+void QtChatWindow::qAppFocusChanged(QWidget* /*old*/, QWidget* now) {
+	if (now && isWidgetSelected()) {
 		lastLineTracker_.setHasFocus(true);
 		input_->setFocus();
 		onAllMessagesRead();
diff --git a/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp b/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp
index f9ce295..18abecd 100644
--- a/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp
+++ b/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp
@@ -379,6 +379,14 @@ void QtDynamicGridLayout::handleTabCloseRequested(int index) {
 	}
 }
 
+void QtDynamicGridLayout::handleTabCurrentChanged(int index) {
+	if (index >= 0) {
+		QTabWidget *sendingTabWidget = dynamic_cast<QTabWidget*>(sender());
+		assert(sendingTabWidget);
+		sendingTabWidget->widget(index)->setFocus();
+	}
+}
+
 void QtDynamicGridLayout::updateTabPositions() {
 	for (int y = 0; y < gridLayout_->rowCount(); y++) {
 		for (int x = 0; x < gridLayout_->columnCount(); x++) {
@@ -422,6 +430,7 @@ QtTabWidget* QtDynamicGridLayout::createDNDTabWidget(QWidget* parent) {
 	tab->setTabsClosable(true);
 	tab->setMovable(true);
 	connect(tab, SIGNAL(tabCloseRequested(int)), this, SLOT(handleTabCloseRequested(int)));
+	connect(tab, SIGNAL(currentChanged(int)), this, SLOT(handleTabCurrentChanged(int)));
 #else
 #warning Qt 4.5 or later is needed. Trying anyway, some things will be disabled.
 #endif
diff --git a/Swift/QtUI/Trellis/QtDynamicGridLayout.h b/Swift/QtUI/Trellis/QtDynamicGridLayout.h
index 0a8a0b6..3b798bd 100644
--- a/Swift/QtUI/Trellis/QtDynamicGridLayout.h
+++ b/Swift/QtUI/Trellis/QtDynamicGridLayout.h
@@ -61,6 +61,7 @@ namespace Swift {
 
 	private slots:
 		void handleTabCloseRequested(int index);
+		void handleTabCurrentChanged(int index);
 
 	private:
 		void moveTab(QtTabWidget* tabWidget, int oldIndex, int newIndex);
-- 
cgit v0.10.2-6-g49f6