-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelation.cpp
174 lines (155 loc) · 6.55 KB
/
relation.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
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
#include "note.h"
#include "relation.h"
#include "notesmanager.h"
#include <Qstring>
#include <QFile>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
NotesManager* notesManager = NotesManager::getInstance(); //get this Instance for the load method
RelationManager* RelationManager::instance_RelationManager = nullptr;
RelationManager::RelationManager(): relations(nullptr),nbRelations(0),maxRelations(0) {}
void Relation::ajouterCouple(Couple* newCouple){
if (nbCouples == maxCouples){
Couple** newCouples = new Couple*[maxCouples+10];
for (unsigned int i=0; i < nbCouples; i++) newCouples[i] = couples[i];
Couple** oldCouples = couples;
couples = newCouples;
delete[] oldCouples;
maxCouples += 10;
}
couples[nbCouples++] = newCouple;
}
void Relation::supprimerCouple(Couple* supCouple){
int pos = -1;
for (unsigned int i=0; i < nbCouples; i++){
if (couples[i] == supCouple) pos = i;
}
if (pos != -1){
for (unsigned int i=pos; i < nbCouples-1; i++){
couples[i] = couples[i+1];
}
delete supCouple;
nbCouples--;
} throw Exception("Le couple n'est pas existe!");
}
RelationPreexistente* RelationPreexistente::instance_RelationPreexistente = 0;
RelationPreexistente::RelationPreexistente(): Relation("REFERENCE", "preexistente") {}
void RelationManager::ajouterRelation(Relation* newRelation){
if (nbRelations == maxRelations){
Relation** newRelations = new Relation*[maxRelations+10];
for (unsigned int i=0; i < nbRelations; i++) newRelations[i] = relations[i];
Relation** oldRelations = relations;
relations = newRelations;
delete[] oldRelations;
maxRelations += 10;
}
relations[nbRelations++] = newRelation;
}
void RelationManager::supprimerRelation(Relation* supRelation){
int pos = -1;
for (unsigned int i=1; i < nbRelations; i++){
if (relations[i] == supRelation) pos = i;
}
if (pos != -1){
for (unsigned int i=pos; i < nbRelations-1; i++){
relations[i] = relations[i+1];
}
delete supRelation;
nbRelations--;
} throw Exception("La relation n'est pas existe ou n'est pas supprime!");
}
Relation* RelationManager::getRelationFromTitle(QString& title) {
RelationManager* RM = RelationManager::getRelationManager();
for (RelationManager::iterator it = RM->begin(); it != RM->end(); it++){
if ((*it)->getTitre() == title) return (*it);
}
}
////////////////////////////////////////////////////////////////////
/////////////////////method save, load//////////////////////////////
////////////////////////////////////////////////////////////////////
void RelationManager::saveRelationManager(const QString & filename){
QFile newFile(filename);
if (!newFile.open(QIODevice::WriteOnly | QIODevice::Text))
throw Exception(QString("Error open file xml: cannot save file"));
QXmlStreamWriter stream(&newFile);
stream.setAutoFormatting(true);
stream.writeStartDocument();
for (unsigned int i=0; i < nbRelations; i++){
stream.writeStartElement("relation");
stream.writeStartElement("titre", relations[i]->getTitre());
stream.writeStartElement("description", relations[i]->getDescription());
if (relations[i]->getOrientee()) stream.writeStartElement("orientation","true");
else stream.writeStartElement("orientation","false");
for (Relation::iterator couple = relations[i]->begin(); couple != relations[i]->end(); couple++){
stream.writeStartElement("couple");
stream.writeStartElement("label", (*couple)->getLabel());
stream.writeStartElement("note1", (*couple)->getNote1().getId());
stream.writeEndElement();
}
stream.writeEndElement();
}
stream.writeEndElement();
newFile.close();
}
void RelationManager::loadRelationManager(const QString & filename){
QFile loadFile(filename);
if (!loadFile.open(QIODevice::ReadOnly | QIODevice::Text))
throw Exception(QString("Error open file xml: cannot load file"));
QXmlStreamReader xml(&loadFile);
while(!xml.atEnd() && !xml.hasError()) {
QXmlStreamReader::TokenType token = xml.readNext();
if(token == QXmlStreamReader::StartDocument) continue;
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "relation")){
xml.readNext();
if (xml.tokenString() == QXmlStreamReader::StartElement){
QString titre;
QString description;
bool orientee;
if (xml.name() == "titre"){
xml.readNext();
titre = xml.text().toString();
}
if (xml.name() == "description"){
xml.readNext();
description = xml.text().toString();
}
if (xml.name() == "orientation"){
xml.readNext();
if (xml.text().toString() == "true") orientee = true;
else orientee = false;
}
Relation* relation;
if (titre == "\ref") relation = relations[0];
else relation = new RelationNormale(titre, description, orientee);
//loop to get Couple
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "couple")){
QString label;
QString note1;
QString note2;
if (xml.tokenString() == QXmlStreamReader::StartElement){
if (xml.name() == "label"){
xml.readNext();
label=xml.text().toString();
}
if (xml.name() == "note1"){
xml.readNext();
note1=xml.text().toString();
}
if (xml.name() == "note2"){
xml.readNext();
note2=xml.text().toString();
}
}
xml.readNext();
Couple* newCouple = new Couple(notesManager->getNote(note1), notesManager->getNote(note2), label);
relation->ajouterCouple(newCouple);
}
if (titre != "\ref") ajouterRelation(relation);
}
}
}
if(xml.hasError()) {
throw Exception("Error parsing xml : cannot read file");
}
xml.clear();
}