-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelations.cpp
More file actions
332 lines (302 loc) · 10.6 KB
/
relations.cpp
File metadata and controls
332 lines (302 loc) · 10.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "relations.h"
#include "Notes.h"
#include <QDebug>
RelationManager* RelationManager::instance = nullptr;
RelationManager::RelationManager(): tabRelations(nullptr), nbRelations(0), nbMaxRelations(0){}
RelationManager::~RelationManager(){
for(unsigned int i=0; i<nbRelations; i++) delete tabRelations[i];
delete[] tabRelations;
}
void RelationManager::addRelation(Relation* rel){
if(rel->getTitre() == "Reference") throw("erreur, on ne peut ajouter une relation Reference. ");
for(unsigned int i=0; i <nbRelations; i++){
if(tabRelations[i]->getTitre() == rel->getTitre())
throw NotesException("erreur, relation existe déjà");
}
if(nbRelations == nbMaxRelations){
Relation** newRelations = new Relation*[nbMaxRelations+5];
for(unsigned int i=0; i<nbRelations; i++)
newRelations[i] = tabRelations[i];
Relation** oldRelations = tabRelations;
tabRelations = newRelations;
nbMaxRelations+=5;
if(oldRelations) delete[] oldRelations;
}
tabRelations[nbRelations++] = rel;
}
Relation& RelationManager::getRelation(QString _titre){
for(unsigned int i=0; i<nbRelations; i++){
if (tabRelations[i]->getTitre()==_titre) return *tabRelations[i];
}
throw NotesException(QString("Erreur, la relation n'existe pas."));
}
bool RelationManager::existeRelation(QString _titre){
for(unsigned int i=0; i<nbRelations; i++){
if (tabRelations[i]->getTitre()==_titre) return true;
}
return false;
}
void RelationManager::deleteRelation(QString _titre){
if(_titre == "Reference") throw("erreur, on ne peut supprimer la relation Reference.");
unsigned int i;
for(i=0; i<nbRelations && tabRelations[i]->getTitre() != _titre; i++){}
if(nbRelations != 0 && i <nbRelations)
{
if(tabRelations[i]->getTitre() == _titre)
{
delete tabRelations[i];
tabRelations[i] = tabRelations[--nbRelations];
}
}
}
void RelationManager::saveEveryRelationsXML(QXmlStreamWriter *stream){
stream->writeStartElement("Relations");
for(unsigned int i=0; i<nbRelations; i++){
this->tabRelations[i]->saveXML(stream);
}
stream->writeEndElement();
}
void RelationManager::createReference(){
for(unsigned int i=0; i <nbRelations; i++){
if(tabRelations[i]->getTitre() == "Reference")
throw NotesException("erreur, Reference existe déjà");
}
if(nbRelations == nbMaxRelations){
Relation** newRelations = new Relation*[nbMaxRelations+5];
for(unsigned int i=0; i<nbRelations; i++)
newRelations[i] = tabRelations[i];
Relation** oldRelations = tabRelations;
tabRelations = newRelations;
nbMaxRelations+=5;
if(oldRelations) delete[] oldRelations;
}
Relation* rel = new Relation("Reference", "relations fortes");
tabRelations[nbRelations++] = rel;
}
bool RelationManager::updateReference(const QString &idNote, const QString &texte){
NotesManager& NM = NotesManager::donneInstance();
if(NM.existeNote(idNote))
{
bool suite = true;
QRegExp regex("\\\\ref[{]([\\w]+)[}]");
QStringList list;
int pos = 0;
while( (pos = regex.indexIn(texte, pos)) != -1){
if(NM.existeNote(regex.cap(1)) && regex.cap(1) != idNote)
{
list << regex.cap(1);
}
else
{
suite = false;
}
pos += regex.matchedLength();
}
if(suite)
{
Relation& reference = getRelation("Reference");
Relation::Iterator it2 = reference.getIterator();
while (!it2.isdone()) {
if((*it2)->getx() == idNote)
{
reference.deleteCouple((*it2)->getx(), (*it2)->gety());
}
it2++;
}
QStringList::iterator it = list.begin();
while (it != list.end()) {
reference.addCouple(idNote, (*it));
++it;
}
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Relation::~Relation(){
for(unsigned int i=0; i<nbCouples; i++) delete tabCouples[i];
delete[] tabCouples;
}
void Relation::addCouple(QString _x, QString _y, QString _label){
bool stop = false;
for(unsigned int i=0; i <nbCouples; i++){
if(tabCouples[i]->getx() == _x && tabCouples[i]->gety() == _y)
throw NotesException("erreur, couple existe déjà");
if(tabCouples[i]->getx() == _y && tabCouples[i]->gety() == _x)
stop = true;
}
NotesManager& notes = NotesManager::donneInstance();
if(!notes.existeNote(_x) || !notes.existeNote(_y)){
throw NotesException("erreur, un des id n'existe pas");
}
if(nbCouples == nbMaxCouples){
Couple** newCouples = new Couple*[nbMaxCouples+5];
for(unsigned int i=0; i<nbCouples; i++)
newCouples[i] = tabCouples[i];
Couple** oldCouples = tabCouples;
tabCouples = newCouples;
nbMaxCouples+=5;
if(oldCouples) delete[] oldCouples;
}
Couple* couple = new Couple(_x,_y,_label);
tabCouples[nbCouples++] = couple;
if(!orientee && !stop){
addCouple(_y,_x, _label);
}
}
void Relation::deleteCouple(QString _x, QString _y){
unsigned int i;
for(i=0; i<nbCouples && ((tabCouples[i]->getx() != _x) || (tabCouples[i]->gety() != _y)); i++){}
if(nbCouples != 0 && i < nbCouples)
{
if(tabCouples[i]->getx() == _x && tabCouples[i]->gety() == _y)
{
delete tabCouples[i];
tabCouples[i] = tabCouples[--nbCouples];
if(!orientee)
{
deleteCouple(_y,_x);
}
}
}
}
Couple& Relation::getCouple(QString _x, QString _y){
for(unsigned int i=0; i<nbCouples; i++){
if (tabCouples[i]->getx()==_x && tabCouples[i]->gety() == _y) return *tabCouples[i];
}
throw NotesException(QString("Erreur, le couple n'existe pas."));
}
bool Relation::existeCouple(QString _x, QString _y){
for(unsigned int i=0; i <nbCouples; i++){
if(tabCouples[i]->getx() == _x && tabCouples[i]->gety() == _y) return true;
}
return false;
}
void Relation::saveXML(QXmlStreamWriter *stream){
stream->writeStartElement("relation");
stream->writeTextElement("titre", this->getTitre());
stream->writeTextElement("description", this->getDescription());
stream->writeTextElement("orientee", QString::number(this->orientee));
stream->writeStartElement("Couples");
if(!orientee)
{
bool existe = false;
QVector<QString> listeX;
QVector<QString> listeY;
for(unsigned int i = 0; i<nbCouples; i++)
{
existe = false;
for (int j = 0; j < listeX.size(); j++) {
if(tabCouples[i]->getx() == listeY.at(j) && tabCouples[i]->gety() == listeX.at(j))
{
existe = true;
}
}
if(!existe)
{
this->tabCouples[i]->saveXML(stream);
listeX << tabCouples[i]->getx();
listeY << tabCouples[i]->gety();
}
}
}
else
{
for(unsigned int i=0; i<nbCouples; i++)
{
this->tabCouples[i]->saveXML(stream);
}
}
stream->writeEndElement();
stream->writeEndElement();
}
void Couple::saveXML(QXmlStreamWriter *stream){
stream->writeStartElement("couple");
stream->writeTextElement("x", this->getx());
stream->writeTextElement("y", this->gety());
stream->writeTextElement("label", this->getLabel());
(*stream).writeEndElement();
}
void RelationManager::LoadRelationXML(QXmlStreamReader *stream){
unsigned int j = nbRelations;
QString titre;
QString description;
bool orientee;
QString x;
QString y;
QString label;
//QXmlStreamAttributes attributes = stream.attributes();
stream->readNext();
while(!(stream->tokenType() == QXmlStreamReader::EndElement && ( stream->name() == "relation" || stream->name() == "Relations" || stream->name() == "PluriNotes" && stream->name() != "Couples"))){
if(stream->tokenType() == QXmlStreamReader::StartElement) {
if(stream->name() == "titre"){
stream->readNext();
titre=stream->text().toString();
}
if(stream->name() == "description"){
stream->readNext();
description=stream->text().toString();
}
if(stream->name() == "orientee"){
stream->readNext();
orientee=stream->text().toInt();
if(titre =="Reference")
{
this->createReference();
}
else{
Relation* rel = new Relation(titre,description, orientee);
addRelation(rel);
}
j++;
}
if(stream->name() == "couple"){
while(stream->tokenType() != QXmlStreamReader::EndElement || (stream->name() != "couple" && stream->name() != "relation" && stream->name() != "Relations" && stream->name() != "PluriNotes" && stream->name() != "Couples")){
if(stream->tokenType() == QXmlStreamReader::StartElement) {
if(stream->name() == "x" )
{
stream->readNext();
x=stream->text().toString();
}
if(stream->name() == "y" )
{
stream->readNext();
y=stream->text().toString();
}
if(stream->name() == "label" )
{
stream->readNext();
label=stream->text().toString();
}
}
stream->readNext();
}
tabRelations[j-1]->addCouple(x,y,label);
}
}
stream->readNext();
}
}
void Relation::inverseOrientation(){
if(!orientee)
{
orientee = true;
}
else
{
for(unsigned int i=0; i < nbCouples; i++ ){
if(!existeCouple(tabCouples[i]->gety(),tabCouples[i]->getx()))
{
addCouple(tabCouples[i]->gety(),tabCouples[i]->getx(), tabCouples[i]->getLabel());
}
}
orientee = false;
}
}