Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 172 lines (149 sloc) 5.305 kb
418ce8a @gnuvince initial commit
authored
1 /*
2 * reservationproxy.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 "reservationproxymodel.h"
20 #include <QDebug>
21
22 ReservationProxyModel::ReservationProxyModel(QObject *parent) :
23 QSortFilterProxyModel(parent),
24 debut(QDateTime()), fin(QDateTime()), station(-1), vehicule(-1), options(0)
25 {
26 }
27
28 void ReservationProxyModel::filter()
29 {
30 ReservationModel *model = qobject_cast<ReservationModel*>(sourceModel());
31 if (!model)
32 return;
33
34 QList<Reservation*> reservations = model->getReservations();
35 QHash<qint64,Data> filtered;
36 kept.clear();
37 res.clear();
38
39 if (debut.isValid() && fin.isValid()) {
40 foreach (Reservation *r, reservations) {
41 Data d = filtered.value(r->getVehicule());
42 QDateTime cdebut = r->getDebut();
43 QDateTime cfin = r->getFin();
44
45 if (cfin <= debut) {
46 //toutes les reservations passees
47 if (!d.firstP) d.firstP = r;
48 else if (cfin > d.firstP->getFin())
49 d.firstP = r;
50 } else if (cdebut >= fin) {
51 //toutes les reservations futures
52 if (!d.firstF) d.firstF = r;
53 else if (cdebut < d.firstF->getDebut())
54 d.firstF = r;
55 }
56 filtered[r->getVehicule()]=d;
57 }
58 }
59
60 foreach (Reservation *r, reservations) {
61 QDateTime cdebut = r->getDebut();
62 QDateTime cfin = r->getFin();
63 qint64 id = r->getId();
64 qint64 cvehicule = r->getVehicule();
65 bool bt, bs, bv;
66
67 //test sur l'interval
68 if (!debut.isValid() && !fin.isValid()) bt = true;
69 else {
70 bt = false;
71 if (cfin <= debut && options.testFlag(Passe)) {
72 //tester le plus proche :(
73 if (options.testFlag(PlusProche)) {
74 if (filtered[cvehicule].firstP && id == filtered[cvehicule].firstP->getId())
75 bt = true;
76 } else
77 bt = true;
78 }
79 else if (cdebut >= fin && options.testFlag(Futur)) {
80 bt = true;
81 //tester le plus proche :(
82 if (options.testFlag(PlusProche)) {
83 if (filtered[cvehicule].firstF && id == filtered[cvehicule].firstF->getId())
84 bt = true;
85 } else
86 bt = true;
87
88 }
89 else if ( (cfin > debut && cdebut < fin) && options.testFlag(Actif) ) {
90 bt = true;
91 //on ignore le plus proche
92 }
93 }
94
95 if (options.testFlag(InverseInterval)) bt = !bt;
96
97 if (!bt) {
98 kept.append(false);
99 continue;
100 }
101
102 //test sur le vehicule
103 if (vehicule < 0) bv = true;
104 else {
105 bv = false;
106 if (cvehicule == vehicule) bv = true;
107 }
108
109 if (options.testFlag(InverseVehicule)) bv = !bv;
110
111 if (!bv) {
112 kept.append(false);
113 continue;
114 }
115
116 //test sur la station
117 if (station < 0) bs = true;
118 else {
119 bs = false;
120 if (filtered[cvehicule].firstP) {
121 bs=( filtered[cvehicule].firstP->getStation() == station );
122 }
123 }
124
125 if (options.testFlag(InverseStation)) bs = !bs;
126
127 if (!bs) {
128 kept.append(false);
129 } else {
130 kept.append(true);
131 res.append(r);
132 }
133 }
134 }
135
136 bool ReservationProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
137 {
138 Q_UNUSED(source_parent)
139 return kept.value(source_row,false);
140 }
141
142 void ReservationProxyModel::setDynamicSortFilter(bool enable)
143 {
144 if (!enable) {
145 if (sourceModel()) {
146 ReservationModel *model = qobject_cast<ReservationModel*>(sourceModel());
147 disconnect(model, SIGNAL(mayNeedFilterUpdate()), this, SLOT(updateFilter()));
148 }
149 } else {
150 ReservationModel *model = qobject_cast<ReservationModel*>(sourceModel());
151 connect(model, SIGNAL(mayNeedFilterUpdate()), this, SLOT(updateFilter()));
152 }
153 QSortFilterProxyModel::setDynamicSortFilter(enable);
154 }
155
156 void ReservationProxyModel::updateFilter()
157 {
158 filter();
159 }
160
161 void ReservationProxyModel::setFilterFields(const QDateTime &debut, const QDateTime &fin, qint64 vehicule, qint64 station, Options options)
162 {
163 this->options = options;
164 this->debut = debut;
165 this->fin = fin;
166 this->vehicule = vehicule;
167 this->station = station;
168
169 updateFilter();
170 invalidateFilter();
171 }
Something went wrong with that request. Please try again.