File indexing completed on 2024-04-28 04:21:03

0001 // SPDX-FileCopyrightText: 2003-2020 The KPhotoAlbum Development Team
0002 // SPDX-FileCopyrightText: 2021 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0003 // SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
0004 //
0005 // SPDX-License-Identifier: GPL-2.0-or-later
0006 
0007 #include "TreeView.h"
0008 
0009 #include <kpaexif/Info.h>
0010 
0011 #include <kpabase/StringSet.h>
0012 
0013 #include <qmap.h>
0014 #include <qstringlist.h>
0015 
0016 using Utilities::StringSet;
0017 
0018 Exif::TreeView::TreeView(const QString &title, QWidget *parent)
0019     : QTreeWidget(parent)
0020 {
0021     setHeaderLabel(title);
0022     reload();
0023     connect(this, &TreeView::itemClicked, this, &TreeView::toggleChildren);
0024 }
0025 
0026 void Exif::TreeView::toggleChildren(QTreeWidgetItem *parent)
0027 {
0028     if (!parent)
0029         return;
0030 
0031     bool on = parent->checkState(0) == Qt::Checked;
0032     for (int index = 0; index < parent->childCount(); ++index) {
0033         parent->child(index)->setCheckState(0, on ? Qt::Checked : Qt::Unchecked);
0034         toggleChildren(parent->child(index));
0035     }
0036 }
0037 
0038 StringSet Exif::TreeView::selected()
0039 {
0040     StringSet result;
0041     for (QTreeWidgetItemIterator it(this); *it; ++it) {
0042         if ((*it)->checkState(0) == Qt::Checked)
0043             result.insert((*it)->text(1));
0044     }
0045     return result;
0046 }
0047 
0048 void Exif::TreeView::setSelectedExif(const StringSet &selected)
0049 {
0050     for (QTreeWidgetItemIterator it(this); *it; ++it) {
0051         bool on = selected.contains((*it)->text(1));
0052         (*it)->setCheckState(0, on ? Qt::Checked : Qt::Unchecked);
0053     }
0054 }
0055 
0056 void Exif::TreeView::reload()
0057 {
0058     clear();
0059     setRootIsDecorated(true);
0060     const auto availableKeys = Exif::Info::instance()->availableKeys();
0061     QStringList keys(availableKeys.begin(), availableKeys.end());
0062     keys.sort();
0063 
0064     QMap<QString, QTreeWidgetItem *> tree;
0065 
0066     for (QStringList::const_iterator keysIt = keys.constBegin(); keysIt != keys.constEnd(); ++keysIt) {
0067         const QStringList subKeys = (*keysIt).split(QLatin1String("."));
0068         QTreeWidgetItem *parent = nullptr;
0069         QString path;
0070         for (const QString &subKey : subKeys) {
0071             if (!path.isEmpty())
0072                 path += QString::fromLatin1(".");
0073             path += subKey;
0074             if (tree.contains(path))
0075                 parent = tree[path];
0076             else {
0077                 if (parent == nullptr)
0078                     parent = new QTreeWidgetItem(this, QStringList(subKey));
0079                 else
0080                     parent = new QTreeWidgetItem(parent, QStringList(subKey));
0081                 parent->setText(1, path); // This is simply to make the implementation of selected easier.
0082                 parent->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
0083                 parent->setCheckState(0, Qt::Unchecked);
0084                 tree.insert(path, parent);
0085             }
0086         }
0087     }
0088 
0089     if (QTreeWidgetItem *item = topLevelItem(0))
0090         item->setExpanded(true);
0091 }
0092 
0093 // vi:expandtab:tabstop=4 shiftwidth=4:
0094 
0095 #include "moc_TreeView.cpp"