buffer views are now sorted case insensitive if alphabetical sort is enabled
[quassel.git] / src / uisupport / bufferviewfilter.cpp
index 896bd11..1245579 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <QColor>
 
+#include "buffermodel.h"
 #include "client.h"
 #include "networkmodel.h"
 
@@ -36,6 +37,8 @@ BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *
 {
   setConfig(config);
   setSourceModel(model);
+  connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(source_rowsInserted(const QModelIndex &, int, int)));
+                       
   // setSortCaseSensitivity(Qt::CaseInsensitive);
   setDynamicSortFilter(true);
 }
@@ -88,6 +91,8 @@ bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action
     networkId = bufferList[i].first;
     bufferId = bufferList[i].second;
     if(droppedNetworkId == networkId) {
+      if(row < 0)
+       row = 0;
       if(row < rowCount(parent)) {
        BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
        pos = config()->bufferList().indexOf(beforeBufferId);
@@ -142,18 +147,23 @@ void BufferViewFilter::removeBuffer(const QModelIndex &index) {
 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
   if(!_config)
     return true;
-  
+
+  if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
+    return false;
+
   if(!(_config->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
     return false;
 
   if(_config->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool())
     return false;
 
-   if(_config->minimumActivity() > source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt())
-    return false;
+  if(_config->minimumActivity() > source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt()) {
+    if(!Client::bufferModel()->standardSelectionModel()->isSelected(source_bufferIndex))
+      return false;
+  }
 
-   BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
-   return _config->bufferList().contains(bufferId);
+  BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
+  return _config->bufferList().contains(bufferId);
 }
 
 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
@@ -199,7 +209,7 @@ bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QMod
   if(config()) {
     return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
   } else
-    return leftBufferId < rightBufferId;
+    return bufferIdLessThan(leftBufferId, rightBufferId);
 }
 
 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
@@ -243,6 +253,17 @@ QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
 
 }
 
+void BufferViewFilter::source_rowsInserted(const QModelIndex &parent, int start, int end) {
+  if(parent.data(NetworkModel::ItemTypeRole) != NetworkModel::NetworkItemType)
+    return;
+
+  if(!config() || !config()->addNewBuffersAutomatically())
+    return;
+
+  for(int row = start; row <= end; row++) {
+    addBuffer(parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>());
+  }
+}
 
 // ******************************
 //  Helper
@@ -261,6 +282,6 @@ bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
   if(leftType != rightType)
     return leftType < rightType;
   else
-    return Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString() < Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString();
+    return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;
 }