-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagManager.cpp
164 lines (140 loc) · 4.51 KB
/
TagManager.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
#include "TagManager.h"
namespace NM
{
TagManager * TagManager::Instance = 0;
TagManager * TagManager::getInstance(){
if(!Instance){
Instance = new TagManager();
}
return Instance;
}
const Tag * TagManager::addTag(const Tag * T){
NM::NotesManager::getInstance().saveWorkspace();
if(!hash.contains(T)){
QString label = T->getLabel();
QHashIterator<const Tag*, QSet<const Note*> > it(hash);
while(it.hasNext()){
it.next();
if(it.key()->getLabel() == label){
return it.key();
}
}
hash[T].empty();
}
return T;
}
const Tag * TagManager::addTag(const QString & label){
NM::NotesManager::getInstance().saveWorkspace();
QHashIterator<const Tag*, QSet<const Note*> > it(hash);
while(it.hasNext()){
it.next();
if(it.key()->getLabel() == label){
return it.key();
}
}
Tag * NewTag = new Tag(label);
hash[NewTag].empty();
return NewTag;
}
const Tag * TagManager::operator<<(const Tag * T){
return this->addTag(T);
}
const Tag * TagManager::operator<<(const QString & label){
return this->addTag(label);
}
void TagManager::removeTag(const Tag * T){
NM::NotesManager::getInstance().saveWorkspace();
hash.remove(T);
}
void TagManager::removeTag(const QString & label){
NM::NotesManager::getInstance().saveWorkspace();
QHashIterator<const Tag*, QSet<const Note*> > it(hash);
bool found = false;
const Tag * T;
while(it.hasNext() && !found){
it.next();
if(it.key()->getLabel() == label){
found = true;
T = it.key();
}
}
if(found){
hash.remove(T);
}
}
void TagManager::addLink(const Tag * T, const Note * N){
NM::NotesManager::getInstance().saveWorkspace();
if(!hash[T].contains(N)){
hash[T].insert(N);
}
}
void TagManager::addLink(const QString & label, const Note * N){
NM::NotesManager::getInstance().saveWorkspace();
this->addLink(this->addTag(label), N);
}
void TagManager::removeLink(const Tag * T, const Note * N){
if(hash.contains(T)){
hash[T].remove(N);
}
}
void TagManager::removeLink(const QString & label, const Note * N){
NM::NotesManager::getInstance().saveWorkspace();
const Tag * T = this->find(label);
if(T){
hash[T].remove(N);
}
}
const Tag * TagManager::find(const QString & label){
QHashIterator<const Tag*, QSet<const Note*> > it(hash);
while(it.hasNext()){
it.next();
if(it.key()->getLabel() == label){
return it.key();
}
}
return 0;
}
QSet<const Note*> TagManager::Filter(QSet<const Tag*> requestedTags){
QSet<const Tag*>::Iterator it = requestedTags.begin();
QSet<const Note*> rNotes = hash[(*it++)];
for(;it != requestedTags.end(); it++){
rNotes.intersect(hash[*it]);
}
return rNotes;
}
QSet<const Note*> TagManager::Filter(QSet<QString> requestedStringTags){
QSet<QString>::Iterator it = requestedStringTags.begin();
QSet<const Tag*> requestedTags;
QSet<const Note*> rNotes;
for(;it != requestedStringTags.end(); it++){
const Tag * foundTag = this->find(*it);
if(!foundTag){
rNotes.empty();
return rNotes;
}
requestedTags.insert(foundTag);
}
return this->Filter(requestedTags);
}
QSet<const Note*> TagManager::Filter(QString requestedTag) {
QSet<QString> set;
set << requestedTag;
return Filter(set);
}
QSet<const Tag*> TagManager::getLinkedTags(const Note * N) const{
QHashIterator<const Tag*, QSet<const Note*> > it(hash);
QSet<const Tag*> linkedTags;
while(it.hasNext()){
it.next();
const QSet<const Note*> noteSet = it.value();
if(noteSet.contains(N)){
linkedTags.insert(it.key());
}
}
return linkedTags;
}
bool TagManager::isLinked(QString label, Note * N) {
QSet<const Note*> setnote = Filter(label);
return setnote.contains(N);
}
}