fixed a bug when selecting a buffer in a different view not resulting in a bufferswitch
[quassel.git] / src / client / mappedselectionmodel.cpp
index 58f2966..99b1e4f 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -56,21 +56,11 @@ QModelIndex MappedSelectionModel::mapFromSource(const QModelIndex &sourceIndex)
     return sourceIndex;
 }
 
-QItemSelection MappedSelectionModel::mapFromSource(const QItemSelection &sourceSelection) {
-  if(isProxyModel()) {
-    QItemSelection mappedSelection;
-    foreach(QItemSelectionRange range, sourceSelection) {
-      QModelIndex topleft = mapFromSource(range.topLeft());
-      QModelIndex bottomright = mapFromSource(range.bottomRight());
-      if(topleft.isValid() && bottomright.isValid())
-       mappedSelection << QItemSelectionRange(topleft, bottomright);
-      else 
-       Q_ASSERT(!topleft.isValid() && !bottomright.isValid());
-    }
-    return mappedSelection;
-  } else {
+QItemSelection MappedSelectionModel::mapSelectionFromSource(const QItemSelection &sourceSelection) {
+  if(isProxyModel())
+    return proxyModel()->mapSelectionFromSource(sourceSelection);
+  else
     return sourceSelection;
-  }
 }
                                    
 QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) {
@@ -80,16 +70,11 @@ QModelIndex MappedSelectionModel::mapToSource(const QModelIndex &proxyIndex) {
     return proxyIndex;
 }
 
-QItemSelection MappedSelectionModel::mapToSource(const QItemSelection &proxySelection) {
-  if(isProxyModel()) {
-    QItemSelection mappedSelection;
-    foreach(QItemSelectionRange range, proxySelection) {
-      mappedSelection << QItemSelectionRange(mapToSource(range.topLeft()), mapToSource(range.bottomRight()));
-    }
-    return mappedSelection;
-  } else {
+QItemSelection MappedSelectionModel::mapSelectionToSource(const QItemSelection &proxySelection) {
+  if(isProxyModel())
+    return proxyModel()->mapSelectionToSource(proxySelection);
+  else
     return proxySelection;
-  }
 }
                                                                        
 void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
@@ -99,7 +84,7 @@ void MappedSelectionModel::mappedSelect(const QModelIndex &index, QItemSelection
 }
 
 void MappedSelectionModel::mappedSelect(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) {
-  QItemSelection mappedSelection = mapFromSource(selection);
+  QItemSelection mappedSelection = mapSelectionFromSource(selection);
   if(mappedSelection != QItemSelectionModel::selection())
     select(mappedSelection, command);  
 }
@@ -108,22 +93,19 @@ void MappedSelectionModel::mappedSetCurrentIndex(const QModelIndex &index, QItem
   QModelIndex mappedIndex = mapFromSource(index);
   if(mappedIndex == currentIndex())
     return;
-
-  if(mappedIndex.isValid())
-    setCurrentIndex(mappedIndex, command);
-  else if(hasSelection())
-    setCurrentIndex(currentIndex(), QItemSelectionModel::Clear);
+  setCurrentIndex(mappedIndex, command);
 }
 
 
 void MappedSelectionModel::_currentChanged(const QModelIndex &current, const QModelIndex &previous) {
   Q_UNUSED(previous)
-  emit mappedCurrentChanged(mapToSource(current));
+  if(current.isValid())
+    emit mappedCurrentChanged(mapToSource(current));
 }
 
 void MappedSelectionModel::_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
   Q_UNUSED(selected)
   Q_UNUSED(deselected)
-  emit mappedSelectionChanged(mapToSource(QItemSelectionModel::selection()));
+  emit mappedSelectionChanged(mapSelectionToSource(QItemSelectionModel::selection()));
 }
-  
+