Added ModelPropertyMapper which allows to keep track of /current/ changes in the...
[quassel.git] / src / client / modelpropertymapper.h
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 #ifndef _MODELPROPERTYMAPPER_H_
22 #define _MODELPROPERTYMAPPER_H_
23
24 #include <QPointer>
25 #include <QModelIndex>
26 #include <QObject>
27
28 class QAbstractItemModel;
29 class QItemSelectionModel;
30
31
32 class ModelPropertyMapper : public QObject {
33   Q_OBJECT
34
35 public:
36   ModelPropertyMapper(QObject *parent = 0);
37   virtual ~ModelPropertyMapper();
38
39   void setModel(QAbstractItemModel *model);
40   QAbstractItemModel *model() const;
41
42   void setSelectionModel(QItemSelectionModel *selectionModel);
43   QItemSelectionModel *selectionModel() const;
44
45   void addMapping(int column, int role, QObject *target, const QByteArray &property);
46   void removeMapping(int column, int role, QObject *target, const QByteArray &property);
47                                                                                       
48 public slots:
49   void setCurrentRow(const QModelIndex &current, const QModelIndex &previous);
50
51 private slots:
52   void targetDestroyed();
53   
54 private:
55   struct Mapping {
56     int column;
57     int role;
58     QPointer<QObject> target;
59     QByteArray property;
60
61     Mapping(int _column, int _role, QObject *_target, const QByteArray &_property)
62       : column(_column), role(_role), target(_target), property(_property) {};
63     inline bool operator==(const Mapping &other) {
64       return (column == other.column && role == other.role && target == other.target && property == other.property); }
65   };
66     
67   QPointer<QAbstractItemModel> _model;
68   QPointer<QItemSelectionModel> _selectionModel;
69   QList<Mapping> _mappings;
70   
71 };
72
73 #endif