File indexing completed on 2024-05-12 15:55:22

0001 // SPDX-FileCopyrightText: 2021 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0002 //
0003 // SPDX-License-Identifier: LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "WildcardCategoryMatcher.h"
0006 
0007 #include <DB/CategoryCollection.h>
0008 #include <DB/ImageDB.h>
0009 #include <kpabase/Logging.h>
0010 
0011 void DB::WildcardCategoryMatcher::debug(int level) const
0012 {
0013     qCDebug(DBCategoryMatcherLog, "%s: %s", qPrintable(spaces(level)), qPrintable(m_re.pattern()));
0014 }
0015 
0016 QRegularExpression DB::WildcardCategoryMatcher::regularExpression() const
0017 {
0018     return m_re;
0019 }
0020 
0021 void DB::WildcardCategoryMatcher::setRegularExpression(const QRegularExpression &re)
0022 {
0023     m_re = re;
0024     m_matchingTags.clear();
0025     for (const auto &category : ImageDB::instance()->categoryCollection()->categories()) {
0026         if (category->isSpecialCategory())
0027             continue;
0028         for (const auto &tag : category->itemsInclCategories()) {
0029             if (m_re.match(tag).hasMatch()) {
0030                 m_matchingTags[category->name()] += tag;
0031             }
0032         }
0033     }
0034 }
0035 
0036 bool DB::WildcardCategoryMatcher::eval(ImageInfoPtr info, QMap<QString, StringSet> &alreadyMatched)
0037 {
0038     Q_UNUSED(alreadyMatched)
0039     return eval(info);
0040 }
0041 
0042 bool DB::WildcardCategoryMatcher::eval(const DB::ImageInfoPtr info) const
0043 {
0044     for (auto it = m_matchingTags.constKeyValueBegin(); it != m_matchingTags.constKeyValueEnd(); ++it) {
0045         const auto categoryName = (*it).first;
0046         const auto tags = (*it).second;
0047         if (info->hasCategoryInfo(categoryName, tags))
0048             return true;
0049     }
0050     return false;
0051 }
0052 
0053 // vi:expandtab:tabstop=4 shiftwidth=4: