Added ModelPropertyMapper which allows to keep track of /current/ changes in the...
[quassel.git] / src / client / modelpropertymapper.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "modelpropertymapper.h"
22
23 #include <QItemSelectionModel>
24 #include <QDebug>
25
26 ModelPropertyMapper::ModelPropertyMapper(QObject *parent)
27   : QObject(parent),
28     _model(0),
29     _selectionModel(0)
30 {
31 }
32
33 ModelPropertyMapper::~ModelPropertyMapper() {
34 }
35
36 void ModelPropertyMapper::setModel(QAbstractItemModel *model) {
37   if(_model)
38     setSelectionModel(new QItemSelectionModel(model));
39   _model = model;
40 }
41
42 QAbstractItemModel *ModelPropertyMapper::model() const {
43   return _model;
44 }
45
46 void ModelPropertyMapper::setSelectionModel(QItemSelectionModel *selectionModel) {
47   if(selectionModel->model() != model()) {
48     qWarning() << "cannot set itemSelectionModel" << selectionModel << "which uses different basemodel than" << model();
49     return;
50   }
51   if(_selectionModel)
52     disconnect(_selectionModel, 0, this, 0);
53   _selectionModel = selectionModel;
54   connect(_selectionModel, SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),
55           this, SLOT(setCurrentRow(QModelIndex, QModelIndex)));
56   
57   setCurrentRow(selectionModel->currentIndex(), QModelIndex());
58 }
59
60 QItemSelectionModel *ModelPropertyMapper::selectionModel() const {
61   return _selectionModel;
62 }
63
64 void ModelPropertyMapper::addMapping(int column, int role, QObject *target, const QByteArray &property) {
65   Mapping mapping(column, role, target, property);
66   if(!_mappings.contains(mapping))
67     _mappings.append(mapping);
68 }
69
70 void ModelPropertyMapper::removeMapping(int column, int role, QObject *target, const QByteArray &property) {
71   if(column == 0 && role == 0 && target == 0 && !property.isNull()) {
72     _mappings.clear();
73     return;
74   }
75   
76   if(column == 0 && role == 0 && !property.isNull()) {
77     QList<Mapping>::iterator iter;
78     for(iter = _mappings.begin(); iter != _mappings.end(); iter++) {
79       if((*iter).target == target)
80         _mappings.erase(iter);
81     }
82     return;
83   }
84   _mappings.removeAll(Mapping(column, role, target, property));
85 }
86
87 void ModelPropertyMapper::setCurrentRow(const QModelIndex &current, const QModelIndex &previous) {
88   Q_UNUSED(previous)
89   foreach(Mapping mapping, _mappings) {
90     QModelIndex index = current.sibling(current.row(), mapping.column);
91     // qDebug() << mapping.target << mapping.property << index.data(mapping.role);
92     mapping.target->setProperty(mapping.property, index.data(mapping.role));
93   }
94 }
95
96
97 void ModelPropertyMapper::targetDestroyed() {
98   QObject *obj = static_cast<QObject *>(sender());
99   removeMapping(0, 0, obj, QByteArray());
100 }