Cleanupination/Prettyfication/Refactorination of the BufferModel
[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   connect(_selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
57           this, SLOT(setCurrentIndex(QModelIndex, QModelIndex)));
58   
59   setCurrentRow(selectionModel->currentIndex(), QModelIndex());
60 }
61
62 QItemSelectionModel *ModelPropertyMapper::selectionModel() const {
63   return _selectionModel;
64 }
65
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);
70 }
71
72 void ModelPropertyMapper::removeMapping(int column, int role, QObject *target, const QByteArray &property) {
73   if(column == 0 && role == 0 && target == 0 && !property.isNull()) {
74     _mappings.clear();
75     return;
76   }
77   
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);
83     }
84     return;
85   }
86   _mappings.removeAll(Mapping(column, role, target, property));
87 }
88
89 void ModelPropertyMapper::setCurrentIndex(const QModelIndex &current, const QModelIndex &previous) {
90   if(current.row() == previous.row() && current.parent() != previous.parent())
91     setCurrentRow(current, previous);
92 }
93
94 void ModelPropertyMapper::setCurrentRow(const QModelIndex &current, const QModelIndex &previous) {
95   Q_UNUSED(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));
100   }
101 }
102
103
104 void ModelPropertyMapper::targetDestroyed() {
105   QObject *obj = static_cast<QObject *>(sender());
106   removeMapping(0, 0, obj, QByteArray());
107 }