Fixed a bug, where the customviews were not so customizable on Linux.
[quassel.git] / src / client / buffertreemodel.cpp
index 568f7e6..93c9063 100644 (file)
@@ -22,6 +22,9 @@
 
 #include "buffertreemodel.h"
 
+#include "mappedselectionmodel.h"
+#include <QAbstractItemView>
+
 #include "bufferinfo.h"
 #include "client.h"
 #include "signalproxy.h"
@@ -79,10 +82,10 @@ QVariant BufferTreeItem::data(int column, int role) const {
     case BufferTreeModel::BufferNameRole:
       return buf->bufferName();
     case BufferTreeModel::BufferTypeRole:
-      return buf->bufferType();
+      return int(buf->bufferType());
     case BufferTreeModel::BufferActiveRole:
       return buf->isActive();
-    case BufferTreeModel::BufferInfoRole:
+    case BufferTreeModel::BufferUidRole:
       return buf->bufferInfo().uid();
     default:
       return QVariant();
@@ -91,7 +94,7 @@ QVariant BufferTreeItem::data(int column, int role) const {
 
 Qt::ItemFlags BufferTreeItem::flags() const {
   Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
-  if(buf->bufferType() == Buffer::QueryBuffer)
+  if(buf->bufferType() == Buffer::QueryType)
     flags |= Qt::ItemIsDropEnabled;
 
   return flags;
@@ -117,9 +120,19 @@ Qt::ItemFlags NetworkTreeItem::flags() const {
  * BufferTreeModel
  *****************************************/
 BufferTreeModel::BufferTreeModel(QObject *parent)
-  : TreeModel(BufferTreeModel::defaultHeader(), parent)
+  : TreeModel(BufferTreeModel::defaultHeader(), parent),
+    _selectionModelSynchronizer(new SelectionModelSynchronizer(this)),
+    _propertyMapper(new ModelPropertyMapper(this))
 {
-  Client::signalProxy()->attachSignal(this, SIGNAL(fakeUserInput(BufferInfo, QString)), SIGNAL(sendInput(BufferInfo, QString)));
+  rootItem->setFlags(rootItem->flags() | Qt::ItemIsDropEnabled);
+  _propertyMapper->setModel(this);
+  delete _propertyMapper->selectionModel();
+  MappedSelectionModel *mappedSelectionModel = new MappedSelectionModel(this);
+  _propertyMapper->setSelectionModel(mappedSelectionModel);
+  synchronizeSelectionModel(mappedSelectionModel);
+  
+  connect(_selectionModelSynchronizer, SIGNAL(setCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)),
+         this, SLOT(setCurrentIndex(QModelIndex, QItemSelectionModel::SelectionFlags)));
 }
 
 QList<QVariant >BufferTreeModel::defaultHeader() {
@@ -128,6 +141,22 @@ QList<QVariant >BufferTreeModel::defaultHeader() {
   return data;
 }
 
+void BufferTreeModel::synchronizeSelectionModel(MappedSelectionModel *selectionModel) {
+  selectionModelSynchronizer()->addSelectionModel(selectionModel);
+}
+
+void BufferTreeModel::synchronizeView(QAbstractItemView *view) {
+  MappedSelectionModel *mappedSelectionModel = new MappedSelectionModel(view->model());
+  selectionModelSynchronizer()->addSelectionModel(mappedSelectionModel);
+  Q_ASSERT(mappedSelectionModel);
+  delete view->selectionModel();
+  view->setSelectionModel(mappedSelectionModel);
+}
+
+void BufferTreeModel::mapProperty(int column, int role, QObject *target, const QByteArray &property) {
+  propertyMapper()->addMapping(column, role, target, property);
+}
+
 bool BufferTreeModel::isBufferIndex(const QModelIndex &index) const {
   // not so purdy...
   return parent(index) != QModelIndex();
@@ -209,7 +238,7 @@ bool BufferTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction /*actio
   Buffer *sourceBuffer = static_cast<BufferTreeItem *>(rootItem->childById(qHash(network))->child(sourcerow))->buffer();
   Buffer *targetBuffer = getBufferByIndex(parent);
 
-  if(!(sourceBuffer->bufferType() & targetBuffer->bufferType() & Buffer::QueryBuffer)) // only queries can be merged
+  if(!(sourceBuffer->bufferType() & targetBuffer->bufferType() & Buffer::QueryType)) // only queries can be merged
     return false;
   
   if(sourceBuffer == targetBuffer) // we won't merge with ourself :)
@@ -229,21 +258,13 @@ void BufferTreeModel::bufferUpdated(Buffer *buffer) {
 }
 
 // This Slot indicates that the user has selected a different buffer in the gui
-void BufferTreeModel::changeCurrent(const QModelIndex &current, const QModelIndex &/*previous*/) {
-  if(isBufferIndex(current)) {
-    currentBuffer = getBufferByIndex(current);
+void BufferTreeModel::setCurrentIndex(const QModelIndex &index, QItemSelectionModel::SelectionFlags command) {
+  Q_UNUSED(command)
+  if(isBufferIndex(index)) {
+    currentBuffer = getBufferByIndex(index);
     bufferActivity(Buffer::NoActivity, currentBuffer);
     emit bufferSelected(currentBuffer);
-    emit selectionChanged(current);
-  }
-}
-
-// we received a double click on a buffer, so we're going to join it
-void BufferTreeModel::doubleClickReceived(const QModelIndex &clicked) {
-  if(isBufferIndex(clicked)) {
-    Buffer *buffer = getBufferByIndex(clicked);
-    if(!buffer->isStatusBuffer()) 
-      emit fakeUserInput(buffer->bufferInfo(), QString("/join " + buffer->bufferName()));
+    emit selectionChanged(index);
   }
 }
 
@@ -258,6 +279,6 @@ void BufferTreeModel::bufferActivity(Buffer::ActivityLevel level, Buffer *buffer
 
 void BufferTreeModel::selectBuffer(Buffer *buffer) {
   QModelIndex index = getOrCreateBufferItemIndex(buffer);
-  //emit selectionChanged(index);
-  changeCurrent(index, QModelIndex());
+  // SUPER UGLY!
+  setCurrentIndex(index, 0);
 }