Pressing enter in the topic line now sets the channel topic.
[quassel.git] / src / client / modelpropertymapper.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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     disconnect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
39                this, SLOT(dataChanged(QModelIndex, QModelIndex)));
40   }
41   _model = model;
42   connect(_model, SIGNAL(dataChanged(QModelIndex, QModelIndex)),
43           this, SLOT(dataChanged(QModelIndex, QModelIndex)));
44   setSelectionModel(new QItemSelectionModel(model));
45 }
46
47 QAbstractItemModel *ModelPropertyMapper::model() const {
48   return _model;
49 }
50
51 void ModelPropertyMapper::setSelectionModel(QItemSelectionModel *selectionModel) {
52   if(selectionModel->model() != model()) {
53     qWarning() << "cannot set itemSelectionModel" << selectionModel << "which uses different basemodel than" << model();
54     return;
55   }
56   if(_selectionModel)
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)));
63   
64   setCurrentRow(selectionModel->currentIndex(), QModelIndex());
65 }
66
67 QItemSelectionModel *ModelPropertyMapper::selectionModel() const {
68   return _selectionModel;
69 }
70
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);
75 }
76
77 void ModelPropertyMapper::removeMapping(int column, int role, QObject *target, const QByteArray &property) {
78   if(column == 0 && role == 0 && target == 0 && !property.isNull()) {
79     _mappings.clear();
80     return;
81   }
82   
83   if(column == 0 && role == 0 && !property.isNull()) {
84     QList<Mapping>::iterator iter;
85     for(iter = _mappings.begin(); iter != _mappings.end(); iter++) {
86       if((*iter).target == target)
87         _mappings.erase(iter);
88     }
89     return;
90   }
91   _mappings.removeAll(Mapping(column, role, target, property));
92 }
93
94 void ModelPropertyMapper::setCurrentIndex(const QModelIndex &current, const QModelIndex &previous) {
95   if(current.row() == previous.row() && current.parent() != previous.parent())
96     setCurrentRow(current, previous);
97 }
98
99 void ModelPropertyMapper::setCurrentRow(const QModelIndex &current, const QModelIndex &previous) {
100   Q_UNUSED(previous)
101   foreach(Mapping mapping, _mappings) {
102     QModelIndex index = current.sibling(current.row(), mapping.column);
103     mapping.target->setProperty(mapping.property, index.data(mapping.role));
104   }
105 }
106
107 void ModelPropertyMapper::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
108   QItemSelectionRange changedRange(topLeft, bottomRight);
109   foreach(Mapping mapping, _mappings) {
110     QModelIndex index = _selectionModel->currentIndex().sibling(_selectionModel->currentIndex().row(), mapping.column);
111     if(changedRange.contains(index)) {
112       mapping.target->setProperty(mapping.property, index.data(mapping.role));
113     }
114   }
115 }
116
117 void ModelPropertyMapper::targetDestroyed() {
118   removeMapping(0, 0, sender(), QByteArray());
119 }