-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodeltabellaallenamenti.cpp
134 lines (122 loc) · 4.37 KB
/
modeltabellaallenamenti.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Andrea Favero
* 1125545
*/
#include "modeltabellaallenamenti.h"
#include <QtGlobal>
#include <QString>
#include "persona.h"
#include "allenamento.h"
ModelTabellaAllenamenti::ModelTabellaAllenamenti(Contenitore<DeepPtr<Allenamento>>& dati_, QObject *parent)
: QAbstractTableModel(parent), dati(dati_)
{}
int ModelTabellaAllenamenti::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return static_cast<int>(dati.Size());
}
int ModelTabellaAllenamenti::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
// 0) Atleta, 1) Data 2) Allenamento, 3) durata, 4) magnesio, 5) calorie,
// 6) grasso perso, 7) sali cosumati, 8) elimina (delegato), 9) modifica (delegato)
return 10;
}
QVariant ModelTabellaAllenamenti::data(const QModelIndex &index, int role) const {
int riga = index.row();
int colonna = index.column();
switch (role) {
case Qt::DisplayRole:
switch (colonna) {
case 0:
if( !dati.At(riga)->getAtleta().getSesso() ) {
return QString::fromStdString( dati.At(riga)->getAtleta().getNome())
+ " " + QString::fromStdString( dati.At(riga)->getAtleta().getCognome()) + " " + QString::fromUtf8("\u2642");
} else {
return QString::fromStdString( dati.At(riga)->getAtleta().getNome())
+ " " + QString::fromStdString( dati.At(riga)->getAtleta().getCognome()) + " " + QString::fromUtf8("\u2640");
}
case 1:
return QString::fromStdString( dati.At(riga)->getData().toString());
case 2:
return QString::fromStdString( dati.At(riga)->tipo() );
case 3:
return QString::fromStdString( std::to_string( dati.At(riga)->getDurata() ) ) + " min";
case 4:
return QString::fromStdString( std::to_string( dati.At(riga)->getMgMagnesioAssunti() ) )
+ " mg";
case 5:
return QString::fromStdString( std::to_string( dati.At(riga)->calorie() )) + " cal";
case 6:
return QString::fromStdString( std::to_string( dati.At(riga)->grassoPerso() )) + " g";
case 7:
return QString::fromStdString( std::to_string( dati.At(riga)->saliMinerali() )) + " mg";
}
}
return QVariant();
}
QVariant ModelTabellaAllenamenti::headerData(int section, Qt::Orientation orientation, int role) const {
if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("Atleta");
case 1:
return QString("Data");
case 2:
return QString("Allenamento");
case 3:
return QString("Durata");
case 4:
return QString("Magnesio");
case 5:
return QString("Calorie");
case 6:
return QString("Dimagrimento");
case 7:
return QString("Sali Consumati");
case 8:
return QString("Eliminazione");
case 9:
return QString("Modifica");
}
}
return QVariant();
}
//viene chiamato da removeRow
bool ModelTabellaAllenamenti::removeRows(int position, int rows, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), position, position+rows-1);
// elimina righe dai dati sottostanti
endRemoveRows();
return true;
}
//viene chiamato da insertRow
bool ModelTabellaAllenamenti::insertRows(int position, int rows, const QModelIndex &parent) {
Q_UNUSED(parent);
beginInsertRows(QModelIndex(), position, position+rows-1);
// inserisce nuove righe nei dati sottostanti
endInsertRows();
return true;
}
void ModelTabellaAllenamenti::inserimentoNuovoAllenamentoEsterno() {
beginResetModel();
insertRow(rowCount());
endResetModel();
}
void ModelTabellaAllenamenti::eliminazioneAllenamento(int riga) {
if(riga >= 0 && riga < rowCount()) {
dati.removeAt(static_cast<unsigned int>(riga));
removeRow(riga);
}
}
void ModelTabellaAllenamenti::eliminazioneAllenamenti(std::shared_ptr<Persona> atleta) {
Contenitore<DeepPtr<Allenamento>>::iterator it = dati.begin();
for(int i = 0; it != dati.end(); ++i) {
if( (*it)->getAtleta() == *atleta ) {
it = dati.erase(it);
removeRow(i);
--i;
} else
++it;
}
}