MauiKit File Browsing

tagslist.cpp
1#include "tagslist.h"
2#include "tagging.h"
3
4TagsList::TagsList(QObject *parent)
5 : MauiList(parent) {}
6
7void TagsList::setList()
8{
10
11 if (this->m_urls.isEmpty()) {
12 this->list = FMH::toModelList(Tagging::getInstance()->getAllTags(this->strict));
13
14 } else if(this->m_urls.size() > 1) {
15 this->list.clear();
16
17 } else {
18 this->list.clear();
19 this->list = std::accumulate(this->m_urls.constBegin(), this->m_urls.constEnd(), FMH::MODEL_LIST(), [this](FMH::MODEL_LIST &list, const QString &url) {
20 list << FMH::toModelList(Tagging::getInstance()->getUrlTags(url, this->strict));
21 return list;
22 });
23 }
24
25 Q_EMIT this->tagsChanged();
26 Q_EMIT this->postListChanged();
27}
28
30{
31 this->setList();
32}
33
34bool TagsList::insert(const QString &tag)
35{
36 if (Tagging::getInstance()->tag(tag.trimmed()))
37 return true;
38
39 return false;
40}
41
43{
44 if (m_urls.isEmpty())
45 return;
46
47 for (const auto &url : std::as_const(this->m_urls))
48 Tagging::getInstance()->tagUrl(url, tag);
49
50 this->refresh();
51}
52
53void TagsList::updateToUrls(const QStringList &tags) //if there is only one url update the tags if there are more than one url then add the new tags
54{
55 if (this->m_urls.isEmpty())
56 return;
57
58 if(this->m_urls.size() == 1)
59 {
60 Tagging::getInstance()->updateUrlTags(this->m_urls.first(), tags);
61 }else
62 {
63 for (const auto &url : std::as_const(this->m_urls))
64 {
65 for(const auto &tag : tags)
66 {
67 Tagging::getInstance()->tagUrl(url, tag);
68 }
69 }
70 }
71
72 this->refresh();
73}
74
75void TagsList::removeFromUrls(const int &index)
76{
77 if (index >= this->list.size() || index < 0)
78 return;
79
80 if (this->m_urls.isEmpty())
81 return;
82
83 const auto tag = this->list[index][FMH::MODEL_KEY::TAG];
84
85 for (const auto &url : std::as_const(m_urls))
86 {
88 }
89
90 this->remove(index);
91}
92
94{
95 const auto index = indexOf(FMH::MODEL_KEY::TAG, tag);
96 removeFromUrls(index);
97}
98
99bool TagsList::remove(const int &index)
100{
101 if (index >= this->list.size() || index < 0)
102 return false;
103
104 Q_EMIT this->preItemRemoved(index);
105 this->list.removeAt(index);
106 Q_EMIT this->tagsChanged();
107 Q_EMIT this->postItemRemoved();
108
109 return true;
110}
111
112void TagsList::removeFrom(const int &index, const QString &url)
113{
114 if (index >= this->list.size() || index < 0)
115 return;
116
117 if (Tagging::getInstance()->removeUrlTag(url, this->list[index][FMH::MODEL_KEY::TAG]))
118 this->remove(index);
119}
120
121void TagsList::erase(const int &index)
122{
123 Q_UNUSED(index)
124}
125
126const FMH::MODEL_LIST &TagsList::items() const
127{
128 return this->list;
129}
130
131bool TagsList::getStrict() const
132{
133 return this->strict;
134}
135
136void TagsList::setStrict(const bool &value)
137{
138 if (this->strict == value)
139 return;
140
141 this->strict = value;
142 Q_EMIT this->strictChanged();
143}
144
145QStringList TagsList::getTags() const
146{
147 return FMH::modelToList(this->list, FMH::MODEL_KEY::TAG);
148}
149
150QStringList TagsList::getUrls() const
151{
152 return this->m_urls;
153}
154
155void TagsList::setUrls(const QStringList &value)
156{
157 if (this->m_urls == value)
158 return;
159
160 this->m_urls = value;
161 Q_EMIT this->urlsChanged();
162}
163
164void TagsList::append(const QString &tag)
165{
166 this->append(FMH::MODEL {{FMH::MODEL_KEY::TAG, tag}});
167}
168
169void TagsList::appendItem(const QVariantMap &tag)
170{
171 this->append(FMH::toModel(tag));
172}
173
174void TagsList::append(const FMH::MODEL &tag)
175{
176 if (this->exists(FMH::MODEL_KEY::TAG, tag[FMH::MODEL_KEY::TAG]))
177 return;
178
179 Q_EMIT this->preItemAppended();
180 this->list << tag;
181 Q_EMIT this->postItemAppended();
182 Q_EMIT this->tagsChanged();
183}
184
185void TagsList::append(const QStringList &tags)
186{
187 for (const auto &tag : std::as_const(tags))
188 {
189 this->append(tag);
190 }
191}
192
194{
195 return this->exists(FMH::MODEL_KEY::TAG, tag);
196}
197
198void TagsList::componentComplete()
199{
200 connect(Tagging::getInstance(), &Tagging::tagged, this, &TagsList::appendItem);
201 connect(Tagging::getInstance(), &Tagging::tagRemoved, this, &TagsList::refresh);
202
203 connect(this, &TagsList::urlsChanged, this, &TagsList::setList);
204 connect(this, &TagsList::strictChanged, this, &TagsList::setList);
205
206 this->setList();
207}
void preItemAppended()
void postItemAppended()
void preItemRemoved(int index)
void preListChanged()
void postListChanged()
void postItemRemoved()
int indexOf(const FMH::MODEL_KEY &key, const QString &value) const
bool exists(const FMH::MODEL_KEY &key, const QString &value) const
bool tagUrl(const QString &url, const QString &tag, const QString &color=QString(), const QString &comment=QString())
Adds a tag to a given file URL, if the given tag doesn't exists then it gets created.
Definition tagging.cpp:164
static Tagging * getInstance()
Returns an instance to the tagging object.
Definition tagging.cpp:70
bool updateUrlTags(const QString &url, const QStringList &tags, const bool &strict=false)
Updates the tags associated to a file URL.
Definition tagging.cpp:193
bool removeUrlTag(const QString &url, const QString &tag)
Removes a given tag associated to a given file URL.
Definition tagging.cpp:257
QStringList tags
The resulting list of tag names that were found.
Definition tagslist.h:42
QML_ELEMENTbool strict
Whether the retrieved tags should be only associated to the current application or to any other app.
Definition tagslist.h:32
void updateToUrls(const QStringList &tags)
Updates a list of tags associated to the current file URLs.
Definition tagslist.cpp:53
bool insert(const QString &tag)
Inserts a tag to the tagging data base.
Definition tagslist.cpp:34
bool remove(const int &index)
Removes a tag from the model at a given index.
Definition tagslist.cpp:99
void appendItem(const QVariantMap &tag)
Adds a given tag map to the model, if the tag map already exists in the model then nothing happens.
Definition tagslist.cpp:169
void erase(const int &index)
Removes a tag from the tagging data base.
Definition tagslist.cpp:121
void removeFromUrls(const int &index)
Removes a tag at a given index in the model from the all the file URLs currently set.
Definition tagslist.cpp:75
bool contains(const QString &tag)
Checks whether a given tag name is already in the model list.
Definition tagslist.cpp:193
void removeFrom(const int &index, const QString &url)
Removes a tag at the given index in the model from the given file URL.
Definition tagslist.cpp:112
void insertToUrls(const QString &tag)
Associates a given tag to the current file URLs set to the URLs property.
Definition tagslist.cpp:42
void refresh()
Reloads the model, checking the tags from the given list of file URLs.
Definition tagslist.cpp:29
const FMH::MODEL_LIST toModelList(const QVariantList &list)
const QStringList modelToList(const MODEL_LIST &list, const MODEL_KEY &key)
const FMH::MODEL toModel(const QVariantMap &map)
void clear()
const_iterator constBegin() const const
T & first()
bool isEmpty() const const
void removeAt(qsizetype i)
qsizetype size() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:51:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.