X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fclient%2Ftreemodel.cpp;h=42a40f7fd1d68d74d01aa700a697aa04c40688d3;hb=23eed68958b7585552be04fab4e5871a781b7f38;hp=05b2635764d84a15d472fbfbac96ef8dad6dd891;hpb=7f9a9f7627d590ae99d489c343b75d76c9db33ae;p=quassel.git diff --git a/src/client/treemodel.cpp b/src/client/treemodel.cpp index 05b26357..42a40f7f 100644 --- a/src/client/treemodel.cpp +++ b/src/client/treemodel.cpp @@ -86,18 +86,32 @@ void AbstractTreeItem::removeChild(int row) { removeChild(defaultColumn(), row); } +void AbstractTreeItem::removeChildById(int column, const quint64 &id) { + if(!_childHash[column].contains(id)) + return; + + AbstractTreeItem *treeItem = _childHash[column][id]; + int row = _childItems[column].indexOf(treeItem); + Q_ASSERT(row >= 0); + removeChild(column, row); +} + +void AbstractTreeItem::removeChildById(const quint64 &id) { + removeChildById(defaultColumn(), id); +} + void AbstractTreeItem::removeAllChilds() { if(childCount() == 0) return; emit beginRemoveChilds(0, childCount() - 1); - AbstractTreeItem *child; - foreach(int key, _childItems.keys()) { - QList::iterator iter = _childItems[key].begin(); - while(iter != _childItems[key].end()) { + foreach(int column, _childItems.keys()) { + QList::iterator iter = _childItems[column].begin(); + while(iter != _childItems[column].end()) { child = *iter; - iter = _childItems[key].erase(iter); + _childHash[column].remove(_childHash[column].key(child)); + iter = _childItems[column].erase(iter); disconnect(child, 0, this, 0); child->removeAllChilds(); child->deleteLater(); @@ -218,10 +232,23 @@ SimpleTreeItem::~SimpleTreeItem() { } QVariant SimpleTreeItem::data(int column, int role) const { - if(role == Qt::DisplayRole && column < _itemData.count()) + if(column >= columnCount() || role != Qt::DisplayRole) + return QVariant(); + else return _itemData[column]; +} + +bool SimpleTreeItem::setData(int column, const QVariant &value, int role) { + if(column > columnCount() || role != Qt::DisplayRole) + return false; + + if(column == columnCount()) + _itemData.append(value); else - return QVariant(); + _itemData[column] = value; + + emit dataChanged(column); + return true; } int SimpleTreeItem::columnCount() const { @@ -248,15 +275,20 @@ PropertyMapItem::~PropertyMapItem() { } QVariant PropertyMapItem::data(int column, int role) const { - if(column >= columnCount()) - return QVariant(); - - if(role != Qt::DisplayRole) + if(column >= columnCount() || role != Qt::DisplayRole) return QVariant(); return property(_propertyOrder[column].toAscii()); } +bool PropertyMapItem::setData(int column, const QVariant &value, int role) { + if(column >= columnCount() || role != Qt::DisplayRole) + return false; + + emit dataChanged(column); + return setProperty(_propertyOrder[column].toAscii(), value); +} + int PropertyMapItem::columnCount() const { return _propertyOrder.count(); } @@ -274,6 +306,19 @@ TreeModel::TreeModel(const QList &data, QObject *parent) : QAbstractItemModel(parent) { rootItem = new SimpleTreeItem(data, 0); + + connect(rootItem, SIGNAL(dataChanged(int)), + this, SLOT(itemDataChanged(int))); + + connect(rootItem, SIGNAL(newChild(AbstractTreeItem *)), + this, SLOT(newChild(AbstractTreeItem *))); + + connect(rootItem, SIGNAL(beginRemoveChilds(int, int)), + this, SLOT(beginRemoveChilds(int, int))); + + connect(rootItem, SIGNAL(endRemoveChilds()), + this, SLOT(endRemoveChilds())); + } TreeModel::~TreeModel() { @@ -375,10 +420,18 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const { if(!index.isValid()) return QVariant(); - AbstractTreeItem *item = static_cast(index.internalPointer()); + AbstractTreeItem *item = static_cast(index.internalPointer()); return item->data(index.column(), role); } +bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if(!index.isValid()) + return false; + + AbstractTreeItem *item = static_cast(index.internalPointer()); + return item->setData(index.column(), value, role); +} + Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const { AbstractTreeItem *item; if(!index.isValid()) @@ -414,7 +467,7 @@ void TreeModel::itemDataChanged(int column) { } void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) { - if(parent == 0 or child == 0) { + if(parent == 0 || child == 0) { qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child; return; } @@ -430,9 +483,6 @@ void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) { connect(child, SIGNAL(newChild(AbstractTreeItem *)), this, SLOT(newChild(AbstractTreeItem *))); -// connect(child, SIGNAL(childRemoved(int)), -// this, SLOT(childRemoved(int))); - connect(child, SIGNAL(beginRemoveChilds(int, int)), this, SLOT(beginRemoveChilds(int, int))); @@ -453,19 +503,6 @@ void TreeModel::endRemoveChilds() { endRemoveRows(); } -void TreeModel::childRemoved(int row) { - QModelIndex parent = indexByItem(static_cast(sender())); - beginRemoveRows(parent, row, row); - endRemoveRows(); -} - -void TreeModel::childsRemoved(int firstRow, int lastRow) { - QModelIndex parent = indexByItem(static_cast(sender())); - beginRemoveRows(parent, firstRow, lastRow); - endRemoveRows(); - -} - bool TreeModel::removeRow(int row, const QModelIndex &parent) { if(row > rowCount(parent)) return false; @@ -508,6 +545,5 @@ bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) { } void TreeModel::clear() { - removeRows(0, rowCount()); - reset(); + rootItem->removeAllChilds(); }