Fix off-by-one bug in a line that was even more buggy until a few hours ago.
[quassel.git] / src / qtgui / bufferviewfilter.cpp
index caa1fb3..0e7fcab 100644 (file)
 /*****************************************
 * The Filter for the Tree View
 *****************************************/
-BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, Modes filtermode, QStringList nets, QObject *parent) : QSortFilterProxyModel(parent) {
+BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QStringList &nets) : QSortFilterProxyModel(model) {
   setSourceModel(model);
+  setSortRole(BufferTreeModel::BufferNameRole);
+  setSortCaseSensitivity(Qt::CaseInsensitive);
+    
   mode = filtermode;
   networks = nets;
   
   connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateMe()));
-  connect(model, SIGNAL(updateSelection(const QModelIndex &, QItemSelectionModel::SelectionFlags)), this, SLOT(select(const QModelIndex &, QItemSelectionModel::SelectionFlags)));
-    
-  connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
-  connect(this, SIGNAL(doubleClicked(const QModelIndex &)), model, SLOT(doubleClickReceived(const QModelIndex &)));
+  connect(model, SIGNAL(selectionChanged(const QModelIndex &)),
+          this, SLOT(select(const QModelIndex &)));
+  
+  connect(this, SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
+          model, SLOT(changeCurrent(const QModelIndex &, const QModelIndex &)));
+  
+  connect(this, SIGNAL(doubleClicked(const QModelIndex &)),
+          model, SLOT(doubleClickReceived(const QModelIndex &)));
 }
 
 void BufferViewFilter::invalidateMe() {
   invalidateFilter();
 }
 
-void BufferViewFilter::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
-  emit updateSelection(mapFromSource(index), command);
+void BufferViewFilter::select(const QModelIndex &index) {
+  emit selectionChanged(mapFromSource(index));
 }
 
 void BufferViewFilter::changeCurrent(const QModelIndex &current, const QModelIndex &previous) {
@@ -51,22 +58,86 @@ void BufferViewFilter::doubleClickReceived(const QModelIndex &clicked) {
   emit doubleClicked(mapToSource(clicked));
 }
 
-bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
-  QModelIndex child = source_parent.child(source_row, 0);
-  if(!child.isValid())
-    return true; // can't imagine this case but true sounds good :)
+/*
+void BufferViewFilter::enterDrag() {
+  connect(sourceModel(), SIGNAL(addBuffer(const uint &, const QString &)),
+          this, SLOT(addBuffer(const uint &, const QString &)));
+}
+
+void BufferViewFilter::leaveDrag() {
+  disconnect(sourceModel(), SIGNAL(addBuffer(const uint &, const QString &)),
+             this, SLOT(addBuffer(const uint &, const QString &)));
+}
+*/
+
+void BufferViewFilter::dropEvent(QDropEvent *event) {
+  const QMimeData *data = event->mimeData();
+  if(!(data->hasFormat("application/Quassel/BufferItem/row")
+       && data->hasFormat("application/Quassel/BufferItem/network")
+       && data->hasFormat("application/Quassel/BufferItem/bufferId")))
+    return; // whatever the drop is... it's not a buffer...
+  
+  event->accept();
+  uint bufferid = data->data("application/Quassel/BufferItem/bufferId").toUInt();
+  QString network = QString::fromUtf8(data->data("application/Quassel/BufferItem/network"));
+  addBuffer(bufferid, network);
+}
+
+
+void BufferViewFilter::addBuffer(const uint &bufferuid, const QString &network) {
+  if(!networks.contains(network)) {
+    networks << network;
+  }
+  
+  if(!customBuffers.contains(bufferuid)) {
+    customBuffers << bufferuid;
+    invalidateFilter();
+  }
+  
+}
 
-  Buffer::Type bufferType = (Buffer::Type) child.data(BufferTreeModel::BufferTypeRole).toInt();
+bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
+  Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
   if((mode & NoChannels) && bufferType == Buffer::ChannelBuffer) return false;
   if((mode & NoQueries) && bufferType == Buffer::QueryBuffer) return false;
   if((mode & NoServers) && bufferType == Buffer::ServerBuffer) return false;
 
-  bool isActive = child.data(BufferTreeModel::BufferActiveRole).toBool();
+  bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
   if((mode & NoActive) && isActive) return false;
   if((mode & NoInactive) && !isActive) return false;
 
-  QString net = child.data(Qt::DisplayRole).toString();
-  if((mode & SomeNets) && !networks.contains(net)) return false;
+  if((mode & FullCustom)) {
+    uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferIdRole).toUInt();
+    if(!customBuffers.contains(bufferuid))
+      return false;
+  }
     
   return true;
 }
+
+bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
+  QString net = source_index.data(Qt::DisplayRole).toString();
+  if((mode & SomeNets) && !networks.contains(net))
+    return false;
+  else
+    return true;
+}
+
+bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
+  QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
+  
+  if(!child.isValid()) {
+    qDebug() << "filterAcceptsRow has been called with an invalid Child";
+    return false;
+  }
+
+  if(source_parent == QModelIndex())
+    return filterAcceptNetwork(child);
+  else
+    return filterAcceptBuffer(child);
+}
+
+bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) {
+  return QSortFilterProxyModel::lessThan(left, right);
+}
+