Merge all stylesheet formats with the base format
[quassel.git] / src / uisupport / bufferviewfilter.cpp
index 51dfe15..5b58ee5 100644 (file)
@@ -45,6 +45,8 @@ BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *
   : QSortFilterProxyModel(model),
     _config(0),
     _sortOrder(Qt::AscendingOrder),
+    _channelJoinedIcon(SmallIcon("irc-channel-active")),
+    _channelPartedIcon(SmallIcon("irc-channel-inactive")),
     _userOfflineIcon(SmallIcon("im-user-offline")),
     _userAwayIcon(SmallIcon("im-user-away")),
     _userOnlineIcon(SmallIcon("im-user")),
@@ -139,13 +141,8 @@ void BufferViewFilter::enableEditMode(bool enable) {
     return;
 
   if(enable == false) {
-    int numBuffers = config()->bufferList().count();
+    addBuffers(QList<BufferId>::fromSet(_toAdd));
     QSet<BufferId>::const_iterator iter;
-    for(iter = _toAdd.constBegin(); iter != _toAdd.constEnd(); iter++) {
-      if(config()->bufferList().contains(*iter))
-       continue;
-      config()->requestAddBuffer(*iter, numBuffers);
-    }
     for(iter = _toTempRemove.constBegin(); iter != _toTempRemove.constEnd(); iter++) {
       if(config()->temporarilyRemovedBuffers().contains(*iter))
         continue;
@@ -169,14 +166,21 @@ Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
   QModelIndex source_index = mapToSource(index);
   Qt::ItemFlags flags = sourceModel()->flags(source_index);
   if(config()) {
-    if(source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
+    NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
+    BufferInfo::Type bufferType = (BufferInfo::Type)sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt();
+    if(source_index == QModelIndex() || itemType == NetworkModel::NetworkItemType) {
       flags |= Qt::ItemIsDropEnabled;
     } else if(_editMode) {
       flags |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
     }
-    ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
-    if(clientConf && clientConf->isLocked()) {
-      flags &= ~(Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled);
+
+    // prohibit dragging of most items. and most drop places
+    // only query to query is allowed for merging
+    if(bufferType != BufferInfo::QueryBuffer) {
+      ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
+      if(clientConf && clientConf->isLocked()) {
+       flags &= ~(Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled);
+      }
     }
   }
   return flags;
@@ -215,10 +219,12 @@ bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action
          pos = 0;
       }
 
-      if(config()->bufferList().contains(bufferId)) {
+      if(config()->bufferList().contains(bufferId) && !config()->sortAlphabetically()) {
        if(config()->bufferList().indexOf(bufferId) < pos)
          pos--;
-       config()->requestMoveBuffer(bufferId, pos);
+       ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
+       if(!clientConf || !clientConf->isLocked())
+         config()->requestMoveBuffer(bufferId, pos);
       } else {
        config()->requestAddBuffer(bufferId, pos);
       }
@@ -255,6 +261,33 @@ void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
   config()->requestAddBuffer(bufferId, pos);
 }
 
+void BufferViewFilter::addBuffers(const QList<BufferId> &bufferIds) const {
+  if(!config())
+    return;
+
+  QList<BufferId> bufferList = config()->bufferList();
+  foreach(BufferId bufferId, bufferIds) {
+    if(bufferList.contains(bufferId))
+      continue;
+
+    int pos = bufferList.count();
+    bool lt;
+    for(int i = 0; i < bufferList.count(); i++) {
+      if(config() && config()->sortAlphabetically())
+       lt = bufferIdLessThan(bufferId, bufferList[i]);
+      else
+       lt = bufferId < config()->bufferList()[i];
+
+      if(lt) {
+       pos = i;
+       bufferList.insert(pos, bufferId);
+       break;
+      }
+    }
+    config()->requestAddBuffer(bufferId, pos);
+  }
+}
+
 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
   // no config -> "all buffers" -> accept everything
   if(!config())
@@ -363,10 +396,7 @@ bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QMo
   NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
   NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
 
-  if(config() && config()->sortAlphabetically())
-    return QSortFilterProxyModel::lessThan(source_left, source_right);
-  else
-    return leftNetworkId < rightNetworkId;
+  return QSortFilterProxyModel::lessThan(source_left, source_right);
 }
 
 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
@@ -388,21 +418,29 @@ QVariant BufferViewFilter::icon(const QModelIndex &index) const {
     return QVariant();
 
   QModelIndex source_index = mapToSource(index);
-  if(sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt() != NetworkModel::BufferItemType)
-    return QVariant();
+  NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
+  BufferInfo::Type bufferType = (BufferInfo::Type)sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt();
+  bool isActive = sourceModel()->data(source_index, NetworkModel::ItemActiveRole).toBool();
 
-  if(sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt() != BufferInfo::QueryBuffer)
+  if(itemType != NetworkModel::BufferItemType)
     return QVariant();
 
-  if(!sourceModel()->data(source_index, NetworkModel::ItemActiveRole).toBool())
-    return _userOfflineIcon;
-
-  if(sourceModel()->data(source_index, NetworkModel::UserAwayRole).toBool())
-    return _userAwayIcon;
-  else
-    return _userOnlineIcon;
-
-  return QVariant();
+  switch(bufferType) {
+  case BufferInfo::ChannelBuffer:
+    if(isActive)
+      return _channelJoinedIcon;
+    else
+      return _channelPartedIcon;
+  case BufferInfo::QueryBuffer:
+    if(!isActive)
+      return _userOfflineIcon;
+    if(sourceModel()->data(source_index, NetworkModel::UserAwayRole).toBool())
+      return _userAwayIcon;
+    else
+      return _userOnlineIcon;
+  default:
+    return QVariant();
+  }
 }
 
 QVariant BufferViewFilter::checkedState(const QModelIndex &index) const {