From ba14801a660031f3a2ad06a2054fd8a6bc4b10b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 8 Mar 2011 23:30:20 +0100
Subject: Fixed crash on Qt 4.7.0 + Windows XP.


diff --git a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp
index 135b4ce..2fa24c2 100644
--- a/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp
+++ b/Swift/QtUI/MUCSearch/QtMUCSearchWindow.cpp
@@ -169,11 +169,11 @@ void QtMUCSearchWindow::handleSelectionChanged(const QItemSelection&, const QIte
 }
 
 MUCSearchRoomItem* QtMUCSearchWindow::getSelectedRoom() const {
-	QItemSelection ranges = ui_.results_->selectionModel()->selection();
 	// Not using selectedIndexes(), because this seems to cause a crash in Qt (4.7.0) in the
 	// QModelIndexList destructor.
 	// This is a workaround posted in http://www.qtcentre.org/threads/16933 (although this case
 	// was resolved by linking against the debug libs, ours isn't, and we're not alone)
+	QItemSelection ranges = ui_.results_->selectionModel()->selection();
 	QModelIndexList lstIndex;
 	for (int i = 0; i < ranges.count(); ++i) {
 		QModelIndex parent = ranges.at(i).parent();
diff --git a/Swift/QtUI/Roster/QtTreeWidget.cpp b/Swift/QtUI/Roster/QtTreeWidget.cpp
index 3198ca5..571169a 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.cpp
+++ b/Swift/QtUI/Roster/QtTreeWidget.cpp
@@ -69,9 +69,29 @@ void QtTreeWidget::handleClicked(const QModelIndex& index) {
 	currentChanged(index, QModelIndex());
 }
 
+QModelIndexList QtTreeWidget::getSelectedIndexes() const {
+	// Not using selectedIndexes(), because this seems to cause a crash in Qt (4.7.0) in the
+	// QModelIndexList destructor.
+	// This is a workaround posted in http://www.qtcentre.org/threads/16933 (although this case
+	// was resolved by linking against the debug libs, ours isn't, and we're not alone)
+	QItemSelection ranges = selectionModel()->selection();
+	QModelIndexList selectedIndexList;
+	for (int i = 0; i < ranges.count(); ++i) {
+		QModelIndex parent = ranges.at(i).parent();
+		int right = ranges.at(i).model()->columnCount(parent) - 1;
+		if (ranges.at(i).left() == 0 && ranges.at(i).right() == right) {
+			for (int r = ranges.at(i).top(); r <= ranges.at(i).bottom(); ++r) {
+				selectedIndexList.append(ranges.at(i).model()->index(r, 0, parent));
+			}
+		}
+	}
+	return selectedIndexList;
+}
+
 void QtTreeWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous) {
 	bool valid = false;
-	if (!editable_ || selectedIndexes().empty() || !selectedIndexes()[0].isValid()) {
+	QModelIndexList selectedIndexList = getSelectedIndexes();
+	if (!editable_ || selectedIndexList.empty() || !selectedIndexList[0].isValid()) {
 		/* I didn't quite understand why using current didn't seem to work here.*/
 	}
 	else if (current.isValid()) {
@@ -96,10 +116,11 @@ void QtTreeWidget::handleEditUserActionTriggered(bool /*checked*/) {
 	if (!editable_) {
 		return;
 	}
-	if (selectedIndexes().empty()) {
+	QModelIndexList selectedIndexList = getSelectedIndexes();
+	if (selectedIndexList.empty()) {
 		return;
 	}
-	QModelIndex index = selectedIndexes()[0];
+	QModelIndex index = selectedIndexList[0];
 	if (!index.isValid()) {
 		return;
 	}
diff --git a/Swift/QtUI/Roster/QtTreeWidget.h b/Swift/QtUI/Roster/QtTreeWidget.h
index 1ab8c8e..4ecba83 100644
--- a/Swift/QtUI/Roster/QtTreeWidget.h
+++ b/Swift/QtUI/Roster/QtTreeWidget.h
@@ -45,6 +45,9 @@ class QtTreeWidget : public QTreeView{
 	private:
 		void renameGroup(GroupRosterItem* group);
 		void drawBranches(QPainter*, const QRect&, const QModelIndex&) const;
+		QModelIndexList getSelectedIndexes() const;
+	
+	private:
 		RosterModel* model_;
 		Roster* roster_;
 		RosterDelegate* delegate_;
-- 
cgit v0.10.2-6-g49f6