-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocument.cpp
130 lines (114 loc) · 3.09 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
#include "Document.h"
#include "ExportStrategy.h"
#include "NotesManager.h"
#include <QList>
/***
* Document
*/
Document::Document(const QString &path)
:Note(path, "New Document")
{
qDebug()<<"Created Document";
type = document;
}
Document::Document(const QString &path, const QString &ti)
:Note(path, ti)
{
qDebug()<<"Created Document with title";
type = document;
}
Document::~Document(){
qDebug()<<"Destruction of doc "<<this->getTitle();
for(nListIt it = begin(); it!=end(); ++it){
(*it)->removeFromDocument(this);
}
}
nListIt Document::begin() const{
return notes.begin();
}
nListIt Document::end() const{
return notes.end();
}
// A shallow test of containing
bool Document::contains(Note *n) const
{
return notes.contains(n);
}
// a deep find
Note* Document::find(const QString& filepath){
for(QList<Note *>::const_iterator it = begin(); it!= end(); ++it){
// qDebug()<<"Comparing:"<<(*it)->getFilePath()<<" & "<<filepath;
if((*it)->getFilePath() == filepath)
{
return *it;
}
else if((*it)->isDocument()){
Note* result = dynamic_cast<Document*>(*it)->find(filepath);
if(result) return result;
}
}
return 0;
}
void Document::addNote(Note* note) throw (NotesException)
{
// Document-document case, test if there is a circular inclusion
if(note->isDocument()){
if(static_cast<Document*>(note)->find(this->getFilePath())){
qDebug()<<"Circular" << this->getTitle();
// circular inclusion!
throw NotesException("Cowardly refused your request...If you want to add A into B, this means that B is already included in A! Think twice...");
}
}
this->notes << note;
note->addToDocument(this);
qDebug()<<"Doc:"<<this->getTitle()<<" has "<< this->notes.count() << " notes";
setModified(true);
}
int Document::count() const
{
return notes.count();
}
void Document::removeNote(Note *note, bool twoway)
{
this->notes.removeOne(note);
if(twoway)
note->removeFromDocument(this);
this->setModified(true);
}
QString Document::exportNote(const ExportStrategy *es, unsigned int level)
{
if(level){
return es->exportNote(this, level);
}
else{
return es->header()+es->exportNote(this)+es->footer();
}
}
Document::DepthFirstIterator& Document::beginDFIterator()
{
Document::DepthFirstIterator *it = new Document::DepthFirstIterator(this);
return *it;
}
bool Document::DepthFirstIterator::hasNext() const
{
return !fringe.empty();
}
Note * Document::DepthFirstIterator::next(){
if(!hasNext())
throw NotesException("Tree ran out of elements");
Note *node = fringe.takeFirst();
if(node->isDocument()){
for (nListIt it = node->begin(); it!=node->end(); ++it){
if(!explored.contains(*it)){
fringe.push_back(*it);
explored << *it;
}
}
}
return node;
}
Document::DepthFirstIterator::DepthFirstIterator(Document * const doc)
{
if(doc)
fringe.push_back(doc);
}