File indexing completed on 2024-05-12 04:19:48

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "fakesemanticinfobackend.h"
0023 
0024 // Qt
0025 #include <QStringList>
0026 
0027 // KF
0028 
0029 // Local
0030 
0031 namespace Gwenview
0032 {
0033 FakeSemanticInfoBackEnd::FakeSemanticInfoBackEnd(QObject *parent, InitializeMode mode)
0034     : AbstractSemanticInfoBackEnd(parent)
0035     , mInitializeMode(mode)
0036 {
0037     mAllTags << tagForLabel("beach") << tagForLabel("mountains") << tagForLabel("wallpaper");
0038 }
0039 
0040 void FakeSemanticInfoBackEnd::storeSemanticInfo(const QUrl &url, const SemanticInfo &semanticInfo)
0041 {
0042     mSemanticInfoForUrl[url] = semanticInfo;
0043     mergeTagsWithAllTags(semanticInfo.mTags);
0044 }
0045 
0046 void FakeSemanticInfoBackEnd::mergeTagsWithAllTags(const TagSet &set)
0047 {
0048     int size = mAllTags.size();
0049     mAllTags |= set;
0050     if (mAllTags.size() > size) {
0051         // Q_EMIT allTagsUpdated();
0052     }
0053 }
0054 
0055 TagSet FakeSemanticInfoBackEnd::allTags() const
0056 {
0057     return mAllTags;
0058 }
0059 
0060 void FakeSemanticInfoBackEnd::refreshAllTags()
0061 {
0062 }
0063 
0064 void FakeSemanticInfoBackEnd::retrieveSemanticInfo(const QUrl &url)
0065 {
0066     if (!mSemanticInfoForUrl.contains(url)) {
0067         QString urlString = url.url();
0068         SemanticInfo semanticInfo;
0069         if (mInitializeMode == InitializeRandom) {
0070             semanticInfo.mRating = int(urlString.length()) % 6;
0071             semanticInfo.mDescription = url.fileName();
0072             const QStringList lst = url.path().split('/');
0073             for (const QString &token : lst) {
0074                 if (!token.isEmpty()) {
0075                     semanticInfo.mTags << '#' + token.toLower();
0076                 }
0077             }
0078             semanticInfo.mTags << QString("#length-%1").arg(url.fileName().length());
0079 
0080             mergeTagsWithAllTags(semanticInfo.mTags);
0081         } else {
0082             semanticInfo.mRating = 0;
0083         }
0084         mSemanticInfoForUrl[url] = semanticInfo;
0085     }
0086     Q_EMIT semanticInfoRetrieved(url, mSemanticInfoForUrl.value(url));
0087 }
0088 
0089 QString FakeSemanticInfoBackEnd::labelForTag(const SemanticInfoTag &tag) const
0090 {
0091     return tag[1].toUpper() + tag.mid(2);
0092 }
0093 
0094 SemanticInfoTag FakeSemanticInfoBackEnd::tagForLabel(const QString &label)
0095 {
0096     return '#' + label.toLower();
0097 }
0098 
0099 } // namespace
0100 
0101 #include "moc_fakesemanticinfobackend.cpp"