From d376c5a7ef5463094b83993d58be58d69b467c4d Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Tue, 4 Aug 2015 21:13:00 +0200
Subject: Fix crash on trellis shortcuts if no chat window is open

Test-Information:

Verified that using trellis shortcuts does not crash Swift
anymore if no chat window is open.

Change-Id: Ie5c7b4675c89c8481cad48f2a348e6520ac42b13

diff --git a/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp b/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp
index e2b6e27..3f3e292 100644
--- a/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp
+++ b/Swift/QtUI/Trellis/QtDynamicGridLayout.cpp
@@ -315,94 +315,102 @@ void QtDynamicGridLayout::setDimensions(const QSize& dim) {
 
 void QtDynamicGridLayout::moveCurrentTabRight() {
 	int index = currentIndex();
-	int tabIndex = -1;
-	QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
-	assert(tabWidget);
-	int newTabIndex = (tabIndex + 1) % tabWidget->count();
-	moveTab(tabWidget, tabIndex, newTabIndex);
+	if (index >= 0) {
+		int tabIndex = -1;
+		QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
+		assert(tabWidget);
+		int newTabIndex = (tabIndex + 1) % tabWidget->count();
+		moveTab(tabWidget, tabIndex, newTabIndex);
+	}
 }
 
 void QtDynamicGridLayout::moveCurrentTabLeft() {
 	int index = currentIndex();
-	int tabIndex = -1;
-	QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
-	assert(tabWidget);
-	int newTabIndex = (tabWidget->count() + tabIndex - 1) % tabWidget->count();
-	moveTab(tabWidget, tabIndex, newTabIndex);
+	if (index >= 0) {
+		int tabIndex = -1;
+		QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
+		assert(tabWidget);
+		int newTabIndex = (tabWidget->count() + tabIndex - 1) % tabWidget->count();
+		moveTab(tabWidget, tabIndex, newTabIndex);
+	}
 }
 
 void QtDynamicGridLayout::moveCurrentTabToNextGroup() {
 	int index = currentIndex();
-	int tabIndex = -1;
-	QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
-
-	int row = -1;
-	int col = -1;
-	int tmp;
-	gridLayout_->getItemPosition(gridLayout_->indexOf(tabWidget), &row, &col, &tmp, &tmp);
-
-	// calculate next cell
-	col++;
-	if (!(col < gridLayout_->columnCount())) {
-		col = 0;
-		row++;
-		if (!(row < gridLayout_->rowCount())) {
-			row = 0;
+	if (index >= 0) {
+		int tabIndex = -1;
+		QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
+
+		int row = -1;
+		int col = -1;
+		int tmp;
+		gridLayout_->getItemPosition(gridLayout_->indexOf(tabWidget), &row, &col, &tmp, &tmp);
+
+		// calculate next cell
+		col++;
+		if (!(col < gridLayout_->columnCount())) {
+			col = 0;
+			row++;
+			if (!(row < gridLayout_->rowCount())) {
+				row = 0;
+			}
 		}
-	}
 
-	QtTabWidget* targetTabWidget = dynamic_cast<QtTabWidget*>(gridLayout_->itemAtPosition(row, col)->widget());
-	assert(tabWidget);
-	assert(targetTabWidget);
+		QtTabWidget* targetTabWidget = dynamic_cast<QtTabWidget*>(gridLayout_->itemAtPosition(row, col)->widget());
+		assert(tabWidget);
+		assert(targetTabWidget);
 
-	// fetch tab information
-	QWidget* tab = tabWidget->widget(tabIndex);
-	QString tabText = tabWidget->tabText(tabIndex);
+		// fetch tab information
+		QWidget* tab = tabWidget->widget(tabIndex);
+		QString tabText = tabWidget->tabText(tabIndex);
 
-	// move tab
-	tab->blockSignals(true);
-	targetTabWidget->addTab(tab, tabText);
-	tab->blockSignals(false);
-	tab->setFocus(Qt::TabFocusReason);
+		// move tab
+		tab->blockSignals(true);
+		targetTabWidget->addTab(tab, tabText);
+		tab->blockSignals(false);
+		tab->setFocus(Qt::TabFocusReason);
 
-	updateTabPositions();
+		updateTabPositions();
+	}
 }
 
 void QtDynamicGridLayout::moveCurrentTabToPreviousGroup() {
 	int index = currentIndex();
-	int tabIndex = -1;
-	QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
-
-	int row = -1;
-	int col = -1;
-	int tmp;
-	gridLayout_->getItemPosition(gridLayout_->indexOf(tabWidget), &row, &col, &tmp, &tmp);
-
-	// calculate next cell
-	col--;
-	if (col < 0) {
-		col = gridLayout_->columnCount() - 1;
-		row--;
-		if (row < 0) {
-			row = gridLayout_->rowCount() - 1;
+	if (index >= 0) {
+		int tabIndex = -1;
+		QtTabWidget* tabWidget = indexToTabWidget(index, tabIndex);
+
+		int row = -1;
+		int col = -1;
+		int tmp;
+		gridLayout_->getItemPosition(gridLayout_->indexOf(tabWidget), &row, &col, &tmp, &tmp);
+
+		// calculate next cell
+		col--;
+		if (col < 0) {
+			col = gridLayout_->columnCount() - 1;
+			row--;
+			if (row < 0) {
+				row = gridLayout_->rowCount() - 1;
+			}
 		}
-	}
 
-	QtTabWidget* targetTabWidget = dynamic_cast<QtTabWidget*>(gridLayout_->itemAtPosition(row, col)->widget());
-	assert(tabWidget);
-	assert(targetTabWidget);
+		QtTabWidget* targetTabWidget = dynamic_cast<QtTabWidget*>(gridLayout_->itemAtPosition(row, col)->widget());
+		assert(tabWidget);
+		assert(targetTabWidget);
 
-	// fetch tab information
-	QWidget* tab = tabWidget->widget(tabIndex);
-	QString tabText = tabWidget->tabText(tabIndex);
+		// fetch tab information
+		QWidget* tab = tabWidget->widget(tabIndex);
+		QString tabText = tabWidget->tabText(tabIndex);
 
-	// move tab
-	tab->blockSignals(true);
-	targetTabWidget->addTab(tab, tabText);
-	tab->blockSignals(false);
-	tab->setFocus(Qt::TabFocusReason);
+		// move tab
+		tab->blockSignals(true);
+		targetTabWidget->addTab(tab, tabText);
+		tab->blockSignals(false);
+		tab->setFocus(Qt::TabFocusReason);
 
-	updateTabPositions();
+		updateTabPositions();
+	}
 }
 
 void QtDynamicGridLayout::handleTabCloseRequested(int index) {
-- 
cgit v0.10.2-6-g49f6