MauiKit File Browsing

tagslist.cpp
1
2#include "tagslist.h"
3#include "tagging.h"
4#include <QTimer>
5
6TagsList::TagsList(QObject *parent)
7 : MauiList(parent)
8 ,m_refreshTimer(new QTimer(this))
9{
10 m_refreshTimer->setInterval(1000);
11 m_refreshTimer->setSingleShot(true);
12
13 connect(m_refreshTimer, &QTimer::timeout, this, &TagsList::setList);
14}
15
16void TagsList::setList()
17{
18 Q_EMIT this->preListChanged();
19 this->list.clear();
20 this->list = getDBTags();
21
22 Q_EMIT this->tagsChanged();
23 Q_EMIT this->postListChanged();
24}
25
26FMH::MODEL_LIST TagsList::getDBTags() const
27{
28
29 if (this->m_urls.isEmpty())
30 {
31 return FMH::toModelList(Tagging::getInstance()->getAllTags(this->strict));
32
33 }else
34 {
35 return std::accumulate(this->m_urls.constBegin(), this->m_urls.constEnd(), FMH::MODEL_LIST(), [this](FMH::MODEL_LIST &list, const QString &url) {
36 list << FMH::toModelList(Tagging::getInstance()->getUrlTags(url, this->strict));
37 return list;
38 });
39 }
40}
41
43{
44 m_refreshTimer->start();
45}
46
47bool TagsList::insert(const QString &tag)
48{
49 if (Tagging::getInstance()->tag(tag.trimmed()))
50 return true;
51
52 return false;
53}
54
56{
57 if (m_urls.isEmpty())
58 return;
59
60 for (const auto &url : std::as_const(this->m_urls))
61 Tagging::getInstance()->tagUrl(url, tag);
62}
63
64void 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
65{
66 if (this->m_urls.isEmpty())
67 return;
68
69 if(this->m_urls.size() == 1)
70 {
71 Tagging::getInstance()->updateUrlTags(this->m_urls.first(), tags);
72 }else
73 {
74 for (const auto &url : std::as_const(this->m_urls))
75 {
76 for(const auto &tag : tags)
77 {
78 Tagging::getInstance()->tagUrl(url, tag);
79 }
80 }
81 }
82}
83
84void TagsList::removeFromUrls(const int &index)
85{
86 if (index >= this->list.size() || index < 0)
87 return;
88
89 if (this->m_urls.isEmpty())
90 return;
91
92 const auto tag = this->list[index][FMH::MODEL_KEY::TAG];
93
94 for (const auto &url : std::as_const(m_urls))
95 {
97 }
98
99 this->remove(index);
100}
101
103{
104 const auto index = indexOf(FMH::MODEL_KEY::TAG, tag);
105 removeFromUrls(index);
106}
107
108bool TagsList::remove(const int &index)
109{
110 if (index >= this->list.size() || index < 0)
111 return false;
112
113 Q_EMIT this->preItemRemoved(index);
114 this->list.removeAt(index);
115 Q_EMIT this->tagsChanged();
116 Q_EMIT this->postItemRemoved();
117
118 return true;
119}
120
121void TagsList::removeFrom(const int &index, const QString &url)
122{
123 if (index >= this->list.size() || index < 0)
124 return;
125
126 if (Tagging::getInstance()->removeUrlTag(url, this->list[index][FMH::MODEL_KEY::TAG]))
127 this->remove(index);
128}
129
130void TagsList::erase(const int &index)
131{
132 Q_UNUSED(index)
133}
134
135const FMH::MODEL_LIST &TagsList::items() const
136{
137 return this->list;
138}
139
140bool TagsList::getStrict() const
141{
142 return this->strict;
143}
144
145void TagsList::setStrict(const bool &value)
146{
147 if (this->strict == value)
148 return;
149
150 this->strict = value;
151 Q_EMIT this->strictChanged();
152}
153
154QStringList TagsList::getTags() const
155{
156 return FMH::modelToList(this->list, FMH::MODEL_KEY::TAG);
157}
158
159QStringList TagsList::getNewTags() const
160{
161 auto allTags = getTags();
162 auto existingTags = FMH::modelToList(getDBTags(), FMH::MODEL_KEY::TAG);
163
164 QStringListIterator i(existingTags);
165 while(i.hasNext())
166 {
167 allTags.removeAll(i.next());
168 }
169
170 return allTags;
171}
172
173QStringList TagsList::getUrls() const
174{
175 return this->m_urls;
176}
177
178void TagsList::setUrls(const QStringList &value)
179{
180 if (this->m_urls == value)
181 return;
182
183 this->m_urls = value;
184 Q_EMIT this->urlsChanged();
185}
186
187void TagsList::append(const QString &tag)
188{
189 this->append(FMH::MODEL {{FMH::MODEL_KEY::TAG, tag}, {FMH::MODEL_KEY::ICON, QStringLiteral("tag")}});
190}
191
192void TagsList::appendItem(const QVariantMap &tag)
193{
194 this->append(FMH::toModel(tag));
195}
196
197void TagsList::append(const FMH::MODEL &tag)
198{
199 if (this->exists(FMH::MODEL_KEY::TAG, tag[FMH::MODEL_KEY::TAG]))
200 return;
201
202 Q_EMIT this->preItemAppended();
203 this->list << tag;
204 Q_EMIT this->postItemAppended();
205 Q_EMIT this->tagsChanged();
206}
207
208void TagsList::append(const QStringList &tags)
209{
210 for (const auto &tag : std::as_const(tags))
211 {
212 this->append(tag);
213 }
214}
215
217{
218 return this->exists(FMH::MODEL_KEY::TAG, tag);
219}
220
221void TagsList::componentComplete()
222{
223 connect(Tagging::getInstance(), &Tagging::tagged, [this](QVariantMap)
224 {
225 if(this->m_urls.isEmpty())
226 {
227 this->refresh();
228 }
229 });
230
232
233
234 connect(Tagging::getInstance(), &Tagging::urlTagged, [this](QString url, QString)
235 {
236 if(m_urls.contains(url))
237 this->refresh();
238 });
239
240 connect(Tagging::getInstance(), &Tagging::urlTagRemoved, [this](QString, QString url)
241 {
242 if(m_urls.contains(url))
243 this->refresh();
244 });
245
246 connect(this, &TagsList::urlsChanged, this, &TagsList::setList);
247 connect(this, &TagsList::strictChanged, this, &TagsList::setList);
248
249 this->setList();
250}
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
void urlTagged(QString url, QString tag)
Emitted when a tag has been assigened to a file URL.
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
void tagRemoved(QString tag)
Emitted when a tag has been removed.
void tagged(QVariantMap tag)
Emitted when a new tag has been created.
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
void urlTagRemoved(QString tag, QString url)
Emitted when a tag has been dissociated from a file URL.
QStringList tags
The resulting list of tag names that were found.
Definition tagslist.h:39
QML_ELEMENTbool strict
Whether the retrieved tags should be only associated to the current application or to any other app.
Definition tagslist.h:29
void updateToUrls(const QStringList &tags)
Updates a list of tags associated to the current file URLs.
Definition tagslist.cpp:64
bool insert(const QString &tag)
Inserts a tag to the tagging data base.
Definition tagslist.cpp:47
bool remove(const int &index)
Removes a tag from the model at a given index.
Definition tagslist.cpp:108
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:192
void erase(const int &index)
Removes a tag from the tagging data base.
Definition tagslist.cpp:130
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:84
bool contains(const QString &tag)
Checks whether a given tag name is already in the model list.
Definition tagslist.cpp:216
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:121
void insertToUrls(const QString &tag)
Associates a given tag to the current file URLs set to the URLs property.
Definition tagslist.cpp:55
void refresh()
Reloads the model, checking the tags from the given list of file URLs.
Definition tagslist.cpp:42
const FMH::MODEL_LIST toModelList(const QVariantList &list)
const QStringList modelToList(const MODEL_LIST &list, const MODEL_KEY &key)
QVector< MODEL > MODEL_LIST
QHash< MODEL_KEY, QString > MODEL
const FMH::MODEL toModel(const QVariantMap &map)
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString trimmed() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 11:55:53 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.