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
21 if (this->m_urls.isEmpty())
22 {
23 this->list = FMH::toModelList(Tagging::getInstance()->getAllTags(this->strict));
24
25 }else
26 {
27 this->list = std::accumulate(this->m_urls.constBegin(), this->m_urls.constEnd(), FMH::MODEL_LIST(), [this](FMH::MODEL_LIST &list, const QString &url) {
28 list << FMH::toModelList(Tagging::getInstance()->getUrlTags(url, this->strict));
29 return list;
30 });
31 }
32
33 Q_EMIT this->tagsChanged();
34 Q_EMIT this->postListChanged();
35}
36
38{
39 m_refreshTimer->start();
40}
41
42bool TagsList::insert(const QString &tag)
43{
44 if (Tagging::getInstance()->tag(tag.trimmed()))
45 return true;
46
47 return false;
48}
49
51{
52 if (m_urls.isEmpty())
53 return;
54
55 for (const auto &url : std::as_const(this->m_urls))
56 Tagging::getInstance()->tagUrl(url, tag);
57}
58
59void 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
60{
61 if (this->m_urls.isEmpty())
62 return;
63
64 if(this->m_urls.size() == 1)
65 {
66 Tagging::getInstance()->updateUrlTags(this->m_urls.first(), tags);
67 }else
68 {
69 for (const auto &url : std::as_const(this->m_urls))
70 {
71 for(const auto &tag : tags)
72 {
73 Tagging::getInstance()->tagUrl(url, tag);
74 }
75 }
76 }
77}
78
79void TagsList::removeFromUrls(const int &index)
80{
81 if (index >= this->list.size() || index < 0)
82 return;
83
84 if (this->m_urls.isEmpty())
85 return;
86
87 const auto tag = this->list[index][FMH::MODEL_KEY::TAG];
88
89 for (const auto &url : std::as_const(m_urls))
90 {
92 }
93
94 this->remove(index);
95}
96
98{
99 const auto index = indexOf(FMH::MODEL_KEY::TAG, tag);
100 removeFromUrls(index);
101}
102
103bool TagsList::remove(const int &index)
104{
105 if (index >= this->list.size() || index < 0)
106 return false;
107
108 Q_EMIT this->preItemRemoved(index);
109 this->list.removeAt(index);
110 Q_EMIT this->tagsChanged();
111 Q_EMIT this->postItemRemoved();
112
113 return true;
114}
115
116void TagsList::removeFrom(const int &index, const QString &url)
117{
118 if (index >= this->list.size() || index < 0)
119 return;
120
121 if (Tagging::getInstance()->removeUrlTag(url, this->list[index][FMH::MODEL_KEY::TAG]))
122 this->remove(index);
123}
124
125void TagsList::erase(const int &index)
126{
127 Q_UNUSED(index)
128}
129
130const FMH::MODEL_LIST &TagsList::items() const
131{
132 return this->list;
133}
134
135bool TagsList::getStrict() const
136{
137 return this->strict;
138}
139
140void TagsList::setStrict(const bool &value)
141{
142 if (this->strict == value)
143 return;
144
145 this->strict = value;
146 Q_EMIT this->strictChanged();
147}
148
149QStringList TagsList::getTags() const
150{
151 return FMH::modelToList(this->list, FMH::MODEL_KEY::TAG);
152}
153
154QStringList TagsList::getUrls() const
155{
156 return this->m_urls;
157}
158
159void TagsList::setUrls(const QStringList &value)
160{
161 if (this->m_urls == value)
162 return;
163
164 this->m_urls = value;
165 Q_EMIT this->urlsChanged();
166}
167
168void TagsList::append(const QString &tag)
169{
170 this->append(FMH::MODEL {{FMH::MODEL_KEY::TAG, tag}, {FMH::MODEL_KEY::ICON, QStringLiteral("tag")}});
171}
172
173void TagsList::appendItem(const QVariantMap &tag)
174{
175 this->append(FMH::toModel(tag));
176}
177
178void TagsList::append(const FMH::MODEL &tag)
179{
180 if (this->exists(FMH::MODEL_KEY::TAG, tag[FMH::MODEL_KEY::TAG]))
181 return;
182
183 Q_EMIT this->preItemAppended();
184 this->list << tag;
185 Q_EMIT this->postItemAppended();
186 Q_EMIT this->tagsChanged();
187}
188
189void TagsList::append(const QStringList &tags)
190{
191 for (const auto &tag : std::as_const(tags))
192 {
193 this->append(tag);
194 }
195}
196
198{
199 return this->exists(FMH::MODEL_KEY::TAG, tag);
200}
201
202void TagsList::componentComplete()
203{
204 connect(Tagging::getInstance(), &Tagging::tagged, [this](QVariantMap)
205 {
206 if(this->m_urls.isEmpty())
207 {
208 this->refresh();
209 }
210 });
211
213
214
216 {
217 if(m_urls.contains(url))
218 this->refresh();
219 });
220
222 {
223 if(m_urls.contains(url))
224 this->refresh();
225 });
226
227 connect(this, &TagsList::urlsChanged, this, &TagsList::setList);
228 connect(this, &TagsList::strictChanged, this, &TagsList::setList);
229
230 this->setList();
231}
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:59
bool insert(const QString &tag)
Inserts a tag to the tagging data base.
Definition tagslist.cpp:42
bool remove(const int &index)
Removes a tag from the model at a given index.
Definition tagslist.cpp:103
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:173
void erase(const int &index)
Removes a tag from the tagging data base.
Definition tagslist.cpp:125
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:79
bool contains(const QString &tag)
Checks whether a given tag name is already in the model list.
Definition tagslist.cpp:197
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:116
void insertToUrls(const QString &tag)
Associates a given tag to the current file URLs set to the URLs property.
Definition tagslist.cpp:50
void refresh()
Reloads the model, checking the tags from the given list of file URLs.
Definition tagslist.cpp:37
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
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void start()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:50:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.