X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fmodelpropertymapper.cpp;h=2e3b100ffe68660c24fe843d12a98769fa29cecf;hp=31f8d5465026be3917081a2f44efbd07e4fdae3a;hb=46d75f41de7c1aaee605c096da28d4b0d8abf138;hpb=cd122ca8e0d2c0ffc5397e0a813c75d791a7e6e3 diff --git a/src/client/modelpropertymapper.cpp b/src/client/modelpropertymapper.cpp index 31f8d546..2e3b100f 100644 --- a/src/client/modelpropertymapper.cpp +++ b/src/client/modelpropertymapper.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-07 by The Quassel Team * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -34,9 +34,14 @@ ModelPropertyMapper::~ModelPropertyMapper() { } void ModelPropertyMapper::setModel(QAbstractItemModel *model) { - if(_model) - setSelectionModel(new QItemSelectionModel(model)); + if(_model) { + disconnect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), + this, SLOT(dataChanged(QModelIndex, QModelIndex))); + } _model = model; + connect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), + this, SLOT(dataChanged(QModelIndex, QModelIndex))); + setSelectionModel(new QItemSelectionModel(model)); } QAbstractItemModel *ModelPropertyMapper::model() const { @@ -53,6 +58,8 @@ void ModelPropertyMapper::setSelectionModel(QItemSelectionModel *selectionModel) _selectionModel = selectionModel; connect(_selectionModel, SIGNAL(currentRowChanged(QModelIndex, QModelIndex)), this, SLOT(setCurrentRow(QModelIndex, QModelIndex))); + connect(_selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)), + this, SLOT(setCurrentIndex(QModelIndex, QModelIndex))); setCurrentRow(selectionModel->currentIndex(), QModelIndex()); } @@ -74,27 +81,41 @@ void ModelPropertyMapper::removeMapping(int column, int role, QObject *target, c } if(column == 0 && role == 0 && !property.isNull()) { - QList::iterator iter; - for(iter = _mappings.begin(); iter != _mappings.end(); iter++) { + QList::iterator iter = _mappings.begin(); + while(iter != _mappings.end()) { if((*iter).target == target) - _mappings.erase(iter); + iter = _mappings.erase(iter); + else + iter++; } return; } _mappings.removeAll(Mapping(column, role, target, property)); } +void ModelPropertyMapper::setCurrentIndex(const QModelIndex ¤t, const QModelIndex &previous) { + if(current.row() == previous.row() && current.parent() != previous.parent()) + setCurrentRow(current, previous); +} + void ModelPropertyMapper::setCurrentRow(const QModelIndex ¤t, const QModelIndex &previous) { Q_UNUSED(previous) foreach(Mapping mapping, _mappings) { QModelIndex index = current.sibling(current.row(), mapping.column); - // qDebug() << mapping.target << mapping.property << index.data(mapping.role); mapping.target->setProperty(mapping.property, index.data(mapping.role)); } } +void ModelPropertyMapper::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) { + QItemSelectionRange changedRange(topLeft, bottomRight); + foreach(Mapping mapping, _mappings) { + QModelIndex index = _selectionModel->currentIndex().sibling(_selectionModel->currentIndex().row(), mapping.column); + if(changedRange.contains(index)) { + mapping.target->setProperty(mapping.property, index.data(mapping.role)); + } + } +} void ModelPropertyMapper::targetDestroyed() { - QObject *obj = static_cast(sender()); - removeMapping(0, 0, obj, QByteArray()); + removeMapping(0, 0, sender(), QByteArray()); }