-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.cpp
200 lines (159 loc) · 4.59 KB
/
tag.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
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
/* Copyright 2013 Matthew Keller
This file is part of HDDOrganizer.
HDDOrganizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HDDOrganizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HDDOrganizer. If not, see <http://www.gnu.org/licenses/>.
*/
// This is the implementation for the main storage item for the tag tree model
#include "tag.h"
#include <iostream>
#include <QVariant>
#include <QDebug>
#include <QDomElement>
Tag* Tag::addChild(const QString tagName, QDomElement* tagDomNode) {
if (tagName.length() == 0)
return 0;
QList<QVariant> data;
data << tagName << 0;
Tag* tag = new Tag(data, tagDomNode, this);
this->children_.append(tag);
return tag;
}
bool Tag::removeChild(Tag *child)
{
QDomNode childDomNode = *child->domNodePointer();
QDomNode result = this->domNode_->removeChild(childDomNode);
if (result.isNull())
return false;
return this->children_.removeOne(child);
}
Tag::Tag()
{
}
Tag::Tag(const QList<QVariant> &data, QDomElement* domNode, Tag *parent)
{
this->parent_ = parent;
this->data_ = data;
this->domNode_ = domNode;
}
Tag::~Tag()
{
delete domNode_;
for(int i = 0; i < this->children_.size();++i) {
delete children_[i];
}
children_.clear();
}
void Tag::addChild(Tag* child) {
child->setParent(this);
this->children_.append(child);
this->domNode_->appendChild(*child->domNodePointer());
}
void Tag::addChildren(QList<Tag*> tags) {
for(int i = 0; i < tags.size(); ++i) {
this->addChild(tags[i]);
}
}
bool Tag::removeChildren(QList<Tag*> tags) {
bool result = false;
for(int i = 0; i < tags.size(); ++i) {
if (this->removeChild(tags[i])) {
result = true;
}
}
return result;
}
Tag* Tag::child(int row) {
return this->children_.value(row);
}
int Tag::columnCount() const {
return this->data_.count();
}
int Tag::rowCount() const {
return this->children_.count();
}
QVariant Tag::data(int column) const {
return this->data_.value(column);
}
bool Tag::addFiles(const QList<QUrl> files) {
int urlQuantity = files.size();
QString attrName("files");
QString fileString;
if(domNode_)
fileString = domNode_->attribute(attrName);
else
return false;
for (int i = 0; i < urlQuantity; ++i) {
if (files[i].isLocalFile()) {
QString fileName = files[i].toLocalFile();
if (!files_.contains(fileName)) {
this->files_.insert(fileName);
this->setData(1,this->data(1).toInt()+1);
fileString.append(fileName);
fileString.append(QString("|"));
}
}
}
this->domNode_->setAttribute(attrName,fileString);
return true;
}
bool Tag::setFiles(const QList<QString> files) {
int fileQuantity = files.size();
this->files_.clear();
for (int i = 0; i < fileQuantity; ++i) {
QString fileName = files[i];
this->files_.insert(fileName);
this->setData(1,this->data(1).toInt()+1);
}
return true;
}
bool Tag::setData(int column, QVariant value) {
this->data_[column] = value;
return true;
}
Tag* Tag::parent() const {
return this->parent_;
}
void Tag::setParent(Tag *parent)
{
this->parent_ = parent;
}
bool Tag::isExpression()
{
return false;
}
bool Tag::isImplicit()
{
return false;
}
void Tag::setImplicity(bool implicity)
{
}
QDomElement* Tag::domNodePointer() const
{
return this->domNode_;
}
void Tag::setDomNodePointer(QDomElement* domNode)
{
this->domNode_ = domNode;
}
int Tag::row() {
if (parent_)
return parent_->children_.indexOf(const_cast<Tag*>(this));
return 0;
}
QSet<QString> Tag::allFiles() {
int childQuantity = this->children_.size();
QSet<QString> uniqueFiles = this->files_;
for(int i = 0; i < childQuantity; ++i) {
uniqueFiles = uniqueFiles.unite(this->children_[i]->allFiles());
}
return uniqueFiles;
}