source: branches/release-0.3.0-rc1/src/MediaDBLib/personsmodel.cpp @ 226

Last change on this file since 226 was 226, checked in by Joachim Langenbach, 11 years ago
  • All Tests should be passed now
File size: 4.6 KB
Line 
1/*
2    MediaDBLib - The shared components of MediaDB
3    Copyright (C) 2010  Joachim Langenbach
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 3 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, see <http://www.gnu.org/licenses/>.
17
18*/
19
20#include "personsmodel.h"
21
22#include "person.h"
23
24#include <QTableView>
25
26PersonsModel::PersonsModel ( QObject* parent )
27{
28        db = MSqlDatabase::database();
29}
30
31PersonsModel::~PersonsModel()
32{
33        qDeleteAll(items);
34        items.clear();
35}
36
37void PersonsModel::addPerson ( Person* person )
38{
39        connect(person, SIGNAL(changed(Person*)), this, SLOT(itemChanged(Person*)));
40        emit layoutAboutToBeChanged();
41  items.append(person);
42  emit layoutChanged();
43}
44
45Person* PersonsModel::addPerson()
46{
47        Person *newP = new Person(this);
48        connect(newP, SIGNAL(changed(Person*)), this, SLOT(itemChanged(Person*)));
49        emit layoutAboutToBeChanged();
50  items.append(newP);
51  emit layoutChanged();
52
53        return newP;
54}
55
56Person* PersonsModel::addPerson ( unsigned int personId )
57{
58        Person *newP = addPerson();
59        newP->setId(personId);
60        return newP;
61}
62
63Person* PersonsModel::addPerson ( QString name, QString givenName )
64{
65        Person *newP = addPerson();
66        newP->setName(name);
67        newP->setGivenName(givenName);
68        return newP;
69}
70
71Person* PersonsModel::addPerson ( unsigned int personId, QString name, QString givenName )
72{
73        Person *newP = addPerson();
74        newP->setId(personId, false);
75        newP->setName(name, false);
76        newP->setGivenName(givenName);
77        return newP;
78}
79
80Person* PersonsModel::personFromIndex ( QModelIndex index ) const
81{
82        if(!index.isValid())
83    return 0;
84 
85  return static_cast<Person*>(index.internalPointer());
86}
87
88QModelIndex PersonsModel::indexFromPerson ( Person* person )
89{
90        if(items.indexOf(person) == -1)
91                return QModelIndex();
92       
93        return index(items.indexOf(person), 0);
94}
95
96void PersonsModel::hideColumns ( QTableView* view )
97{
98        if(!view)
99                return;
100       
101        view->resizeColumnsToContents();
102}
103
104Person* PersonsModel::takePerson ( Person* person )
105{
106        if(items.indexOf(person) == -1)
107                return 0;
108        disconnect(person);
109        emit layoutAboutToBeChanged();
110        items.removeAll(person);
111        emit layoutChanged();
112        return person;
113}
114
115Person* PersonsModel::takePerson ( QModelIndex index )
116{
117        return takePerson(personFromIndex(index));
118}
119
120QVariant PersonsModel::data ( const QModelIndex& index, int role ) const
121{
122        Person *person = personFromIndex(index);
123  if(!person)
124    return QVariant();
125 
126  if((role != Qt::DisplayRole) && (role != Qt::EditRole) && (role != Qt::ForegroundRole))
127    return QVariant();
128 
129  if(role == Qt::ForegroundRole){
130    if(person->markForRemovel())
131      return QBrush(QColor(200, 200, 200));
132  }
133 
134  switch(index.column()){
135    case 0:
136      return person->name();
137    case 1:
138      return person->givenName();
139  }
140  return QVariant();
141}
142
143bool PersonsModel::setData ( const QModelIndex& index, const QVariant& value, int role )
144{
145        return false;
146}
147
148int PersonsModel::columnCount ( const QModelIndex& parent ) const
149{
150        if(parent.isValid())
151                return 0;
152       
153        return 2;
154}
155
156int PersonsModel::rowCount ( const QModelIndex& parent ) const
157{
158        if(parent.isValid())
159                return 0;
160       
161        return items.size();
162}
163
164QModelIndex PersonsModel::parent ( const QModelIndex& child ) const
165{
166        return QModelIndex();
167}
168
169QModelIndex PersonsModel::index ( int row, int column, const QModelIndex& parent ) const
170{
171  if(parent.isValid())
172    return QModelIndex();
173 
174        if(column < 0)
175                return QModelIndex();
176        if(columnCount() <= column)
177                return QModelIndex();
178
179  if(row < 0)
180    return QModelIndex();
181  if(items.size() <= row)
182    return QModelIndex();
183 
184  return createIndex(row, column, items[row]);
185}
186
187QVariant PersonsModel::headerData ( int section, Qt::Orientation orientation, int role ) const
188{
189        if((role != Qt::DisplayRole) && (role != Qt::EditRole))
190                return QAbstractItemModel::headerData(section, orientation, role);
191       
192        if(orientation == Qt::Vertical)
193                return section + 1;
194       
195        switch(section){
196                case 0:
197                        return tr("Name");
198                case 1:
199                        return tr("Given Name");
200        }
201        return QVariant();
202}
203
204void PersonsModel::itemChanged ( Person* person )
205{
206        QModelIndex pIndex = indexFromPerson(person);
207        emit dataChanged(pIndex, index(pIndex.row(), columnCount()));
208}
209
210#include "personsmodel.moc"
Note: See TracBrowser for help on using the repository browser.