forked from felixmt/LO21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.cpp
150 lines (131 loc) · 4.02 KB
/
document.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
#include "document.h"
#include <QFile>
#include <QTextStream>
#include <iostream>
Document& Document::operator<<(Note* a)
{
notes<<a;
return *this ;
}
Note* Document::getNote(unsigned int i)
{
return notes[i];
}
const Note* Document::getNote(unsigned int i) const
{
return notes[i];
}
unsigned int Document::getPosNote(Note* note) const
{
for (unsigned int i=0; i<notes.size();i++)
if (notes[i]->getId()==note->getId()) return i;
return -1;
}
void Document::removeNote(unsigned int i){
if (i<notes.size()){
Corbeille::getInstance().addNote(notes[i]); //Problème : supprime toutes les notes du document.
notes.removeAt(i);
setModified(true);
}//else throw NotesException("error, removing an article");
}
void Document::moveNoteUp(unsigned int i)
{
if (i<notes.size() && i>0){
Note* n=notes[i];
notes[i]=notes[i-1];
notes[i-1]=n;
modified=true;
}//else throw NotesException("error, moving up an article");
}
void Document::moveNoteDown(unsigned int i)
{
if (i>=0 && i<notes.size()-1){
Note* n=notes[i];
notes[i]=notes[i+1];
notes[i+1]=n;
modified=true;
}//else throw NotesException("error, moving down an article");
}
Document& Document::operator=(const Document& d){
if (this!=&d){
filename=d.filename;
title=d.title;
notePath=d.notePath;
QList <Note*> newNotes;
for(unsigned int i=0; i<d.notes.size(); i++) newNotes[i]=d.notes[i];
QList <Note*> old=notes;
notes=newNotes;
old.clear();
}
return *this;
}
void Document::save()
{
if (this->isModified()) {
// Création d'un objet QFile
QString completePath = notePath + this->getFilename() + (QString)".doc";
QFile file(completePath);
//On ouvre notre fichier en écriture et on vÈrifie l'ouverture
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
std::cout <<"Erreur sauvegarde d'un article : impossible d'ouvrir un fichier en Ècriture";
QTextStream flux(&file);
flux<<this->getTitle() << "\n";
for (unsigned int i =0 ; i<notes.size() ; i++)
{
flux << notePath + notes[i]->getFilename() + (QString)"." << notes[i]->getType() << endl;
notes[i]->save();
}
file.close();
std::cout << "Une sauvegarde d'un document a été effectuée sur le chemin suivant:" << qPrintable(completePath) ;
this->setModified(false);
}
}
void Document::load(QString& path)
{
QFile fichier(path);
fichier.open(QIODevice::ReadOnly | QIODevice::Text);
//A FAIRE : Vérifier bonne ouverture et exceptions
QTextStream flux(&fichier);
QString title=flux.readLine();
this->setTitle(title);
QString fname = path.mid(0,path.size()-4);
this->setFilename(fname);
while (!flux.atEnd())
{
QString path = flux.readLine();
QString type = getFileType(path);
Note* n = NotesManager::getInstance().getFileNote(path,type);
n->load(path);
notes.push_back(n);
}
fichier.close();
}
void Document::addNote(Note *n)
{
notes.push_back(n);
}
QString Document::exportText()
{
textContent= this->getTitle()+"\n\n";
for (unsigned int i=0; i<this->getSize(); i++)
textContent+=notes[i]->exportText();
return textContent;
}
QString Document::exportHtml(bool appartenanceDoc)
{
Note::exportHtml(0); // sauf si un doc peut appartenir lui mm à un doc?--------------
html += "<h1>" + title + "</h1>";
for(int i=0; i<notes.size();i++)
html += notes[i]->exportHtml(1);
html += "</body></html>";
return html;
}
QString Document::exportLatex(bool appartenanceDoc)
{
Note::exportLatex(0); // sauf si un doc peut appartenir lui mm à un doc?--------------
latex += "\\section{"+title+"}\n";
for(int i=0; i<notes.size();i++)
latex += notes[i]->exportLatex(1);
latex += "\n \\end{document}";
return latex;
}