Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 161 lines (132 sloc) 3.883 kb
418ce8a @gnuvince initial commit
authored
1 /*
2 * usagermodel.cpp
3 * Copyright (C) 2011 Nicolas Martin, Université de Montréal
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 #include "usagermodel.h"
20
21 UsagerModel::UsagerModel(QObject *parent) :
22 QAbstractTableModel(parent)
23 {
24 }
25
26 QVariant UsagerModel::usagerAt(const QModelIndex &index, int role) const
27 {
28 uint row = index.row();
29 uint column = index.column();
30
31 qint64 id = usagers.keys()[row];
32 Usager *u = usagers[id];
33
34 return u->field(column, role);
35 }
36
37 QVariant UsagerModel::data(const QModelIndex &index, int role) const
38 {
39 if (!index.isValid())
40 return QVariant();
41 return usagerAt(index, role);
42 }
43
44 int UsagerModel::rowCount(const QModelIndex &parent) const
45 {
46 if (parent.isValid()) return 0;
47 return usagers.count();
48 }
49
50 int UsagerModel::columnCount(const QModelIndex &parent) const
51 {
52 if (parent.isValid()) return 0;
53 return Usager::headerCount();
54 }
55
56 QVariant UsagerModel::headerData(int section, Qt::Orientation orientation, int role) const
57 {
58 if (role != Qt::DisplayRole)
59 return QVariant();
60 if (orientation == Qt::Horizontal) {
61 return Usager::header(section);
62 } else if (orientation == Qt::Vertical) {
63 if (section < 0 || section >= usagers.count())
64 return QVariant();
65 return section;
66 }
67 return QVariant();
68 }
69
70 bool UsagerModel::setData(const QModelIndex &index, const QVariant &value, int role)
71 {
72 if (!index.isValid())
73 return false;
74 if (role != Qt::EditRole)
75 return false;
76
77 uint row = index.row();
78 uint column = index.column();
79
80 qint64 id = usagers.keys().value(row,-1);
81 if (id < 0) return false;
82
83 Usager *u = usagers[id];
84
85 if (u->changeField(column, value)) {
86 emit dataChanged(index,index);
87 return true;
88 }
89
90 return false;
91 }
92
93 Qt::ItemFlags UsagerModel::flags(const QModelIndex &index) const
94 {
95 if (!index.isValid())
96 return Qt::ItemFlags();
97 switch (index.column()) {
98 case Usager::COL_NOM:
99 case Usager::COL_POS:
100 case Usager::COL_PERSO:
101 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
102 default:
103 break;
104 }
105 return QAbstractTableModel::flags(index);
106 }
107
108 bool UsagerModel::addUsager(Usager *usager)
109 {
110 //tester si elle est valide
111
112 int pos = usagers.count();
113 beginInsertRows(QModelIndex(), pos, pos);
114 usagers[usager->getId()] = usager;
115 endInsertRows();
116
117 return true;
118 }
119
120 bool UsagerModel::removeUsager(qint64 id)
121 {
122 if (usagers.count(id) == 0) return false;
123
124 QList<qint64>::const_iterator it = qFind(usagers.keys(),id);
125 int pos = it-usagers.keys().begin();
126 beginRemoveRows(QModelIndex(), pos, pos);
127 usagers.remove(id);
128 endRemoveRows();
129
130 return true;
131 }
132
133
134
135 QDataStream& operator<<(QDataStream &ds, const UsagerModel &model)
136 {
137 int count = model.usagers.count();
138
139 ds << count;
140 foreach (Usager *u, model.usagers) {
141 ds << (*u);
142 }
143 return ds;
144 }
145
146 QDataStream& operator>>(QDataStream &ds, UsagerModel &model)
147 {
148 Usager u;
149 int count;
150
151 model.beginResetModel();
152 model.usagers.clear();
153 ds >> count;
154 for (int i=0; i<count; ++i) {
155 ds >> u;
156 model.usagers[u.id] = new Usager(u);
157 }
158 model.endResetModel();
159 return ds;
160 }
Something went wrong with that request. Please try again.