1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "modelpropertymapper.h"
23 #include <QItemSelectionModel>
26 ModelPropertyMapper::ModelPropertyMapper(QObject *parent)
33 ModelPropertyMapper::~ModelPropertyMapper() {
36 void ModelPropertyMapper::setModel(QAbstractItemModel *model) {
38 setSelectionModel(new QItemSelectionModel(model));
42 QAbstractItemModel *ModelPropertyMapper::model() const {
46 void ModelPropertyMapper::setSelectionModel(QItemSelectionModel *selectionModel) {
47 if(selectionModel->model() != model()) {
48 qWarning() << "cannot set itemSelectionModel" << selectionModel << "which uses different basemodel than" << model();
52 disconnect(_selectionModel, 0, this, 0);
53 _selectionModel = selectionModel;
54 connect(_selectionModel, SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
55 this, SLOT(setCurrentRow(QModelIndex, QModelIndex)));
56 connect(_selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
57 this, SLOT(setCurrentIndex(QModelIndex, QModelIndex)));
59 setCurrentRow(selectionModel->currentIndex(), QModelIndex());
62 QItemSelectionModel *ModelPropertyMapper::selectionModel() const {
63 return _selectionModel;
66 void ModelPropertyMapper::addMapping(int column, int role, QObject *target, const QByteArray &property) {
67 Mapping mapping(column, role, target, property);
68 if(!_mappings.contains(mapping))
69 _mappings.append(mapping);
72 void ModelPropertyMapper::removeMapping(int column, int role, QObject *target, const QByteArray &property) {
73 if(column == 0 && role == 0 && target == 0 && !property.isNull()) {
78 if(column == 0 && role == 0 && !property.isNull()) {
79 QList<Mapping>::iterator iter;
80 for(iter = _mappings.begin(); iter != _mappings.end(); iter++) {
81 if((*iter).target == target)
82 _mappings.erase(iter);
86 _mappings.removeAll(Mapping(column, role, target, property));
89 void ModelPropertyMapper::setCurrentIndex(const QModelIndex ¤t, const QModelIndex &previous) {
90 if(current.row() == previous.row() && current.parent() != previous.parent())
91 setCurrentRow(current, previous);
94 void ModelPropertyMapper::setCurrentRow(const QModelIndex ¤t, const QModelIndex &previous) {
96 foreach(Mapping mapping, _mappings) {
97 QModelIndex index = current.sibling(current.row(), mapping.column);
98 // qDebug() << mapping.target << mapping.property << index.data(mapping.role);
99 mapping.target->setProperty(mapping.property, index.data(mapping.role));
104 void ModelPropertyMapper::targetDestroyed() {
105 QObject *obj = static_cast<QObject *>(sender());
106 removeMapping(0, 0, obj, QByteArray());