1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
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) version 3. *
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 disconnect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
39 this, SLOT(dataChanged(QModelIndex, QModelIndex)));
42 connect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
43 this, SLOT(dataChanged(QModelIndex, QModelIndex)));
44 setSelectionModel(new QItemSelectionModel(model));
47 QAbstractItemModel *ModelPropertyMapper::model() const {
51 void ModelPropertyMapper::setSelectionModel(QItemSelectionModel *selectionModel) {
52 if(selectionModel->model() != model()) {
53 qWarning() << "cannot set itemSelectionModel" << selectionModel << "which uses different basemodel than" << model();
57 disconnect(_selectionModel, 0, this, 0);
58 _selectionModel = selectionModel;
59 connect(_selectionModel, SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
60 this, SLOT(setCurrentRow(QModelIndex, QModelIndex)));
61 connect(_selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
62 this, SLOT(setCurrentIndex(QModelIndex, QModelIndex)));
64 setCurrentRow(selectionModel->currentIndex(), QModelIndex());
67 QItemSelectionModel *ModelPropertyMapper::selectionModel() const {
68 return _selectionModel;
71 void ModelPropertyMapper::addMapping(int column, int role, QObject *target, const QByteArray &property) {
72 Mapping mapping(column, role, target, property);
73 if(!_mappings.contains(mapping))
74 _mappings.append(mapping);
77 void ModelPropertyMapper::removeMapping(int column, int role, QObject *target, const QByteArray &property) {
78 if(column == 0 && role == 0 && target == 0 && !property.isNull()) {
83 if(column == 0 && role == 0 && !property.isNull()) {
84 QList<Mapping>::iterator iter = _mappings.begin();
85 while(iter != _mappings.end()) {
86 if((*iter).target == target)
87 iter = _mappings.erase(iter);
93 _mappings.removeAll(Mapping(column, role, target, property));
96 void ModelPropertyMapper::setCurrentIndex(const QModelIndex ¤t, const QModelIndex &previous) {
97 if(current.row() == previous.row() && current.parent() != previous.parent())
98 setCurrentRow(current, previous);
101 void ModelPropertyMapper::setCurrentRow(const QModelIndex ¤t, const QModelIndex &previous) {
103 foreach(Mapping mapping, _mappings) {
104 QModelIndex index = current.sibling(current.row(), mapping.column);
105 mapping.target->setProperty(mapping.property, index.data(mapping.role));
109 void ModelPropertyMapper::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
110 QItemSelectionRange changedRange(topLeft, bottomRight);
111 foreach(Mapping mapping, _mappings) {
112 QModelIndex index = _selectionModel->currentIndex().sibling(_selectionModel->currentIndex().row(), mapping.column);
113 if(changedRange.contains(index)) {
114 mapping.target->setProperty(mapping.property, index.data(mapping.role));
119 void ModelPropertyMapper::targetDestroyed() {
120 removeMapping(0, 0, sender(), QByteArray());