File indexing completed on 2025-01-19 03:55:36

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2018-12-31
0007  * Description : configuration view for external plugin
0008  *
0009  * SPDX-FileCopyrightText: 2018-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dpluginconfview.h"
0016 
0017 // Qt include
0018 
0019 #include <QList>
0020 #include <QHeaderView>
0021 
0022 // KDE includes
0023 
0024 #include <ksharedconfig.h>
0025 #include <kconfiggroup.h>
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "dplugin.h"
0031 
0032 namespace Digikam
0033 {
0034 
0035 class Q_DECL_HIDDEN DPluginCB : public QTreeWidgetItem
0036 {
0037 public:
0038 
0039     explicit DPluginCB(DPlugin* const plugin, QTreeWidget* const parent)
0040         : QTreeWidgetItem(parent),
0041           m_plugin       (plugin)
0042     {
0043         setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
0044         setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
0045         setDisabled(false);
0046 
0047         // Name + Icon + Selector
0048 
0049         setText(0, m_plugin->name());
0050         setIcon(0, m_plugin->icon());
0051 
0052         if (m_plugin->hasVisibilityProperty())
0053         {
0054             setCheckState(0, m_plugin->shouldLoaded() ? Qt::Checked : Qt::Unchecked);
0055         }
0056 
0057         setToolTip(0, m_plugin->description());
0058 
0059         // Categories
0060 
0061         QStringList list = m_plugin->categories();
0062         setText(1, list.join(QString::fromLatin1(", ")));
0063 
0064         // Number of tools
0065 
0066         setText(2, QString::number(m_plugin->count()));
0067 
0068         // Description
0069 
0070         setText(3, m_plugin->description());
0071 
0072         // Authors
0073 
0074         QStringList auth = m_plugin->pluginAuthors();
0075         setText(4, auth.join(QString::fromLatin1(", ")));
0076     };
0077 
0078     ~DPluginCB() override
0079     {
0080     };
0081 
0082     bool contains(const QString& txt, Qt::CaseSensitivity cs) const
0083     {
0084         return (text(0).contains(txt, cs) ||
0085                 text(1).contains(txt, cs) ||
0086                 text(2).contains(txt, cs) ||
0087                 text(3).contains(txt, cs) ||
0088                 text(4).contains(txt, cs));
0089     };
0090 
0091 public:
0092 
0093     DPlugin* m_plugin;
0094 
0095 private:
0096 
0097     Q_DISABLE_COPY(DPluginCB)
0098 };
0099 
0100 // ---------------------------------------------------------------------
0101 
0102 class Q_DECL_HIDDEN DPluginConfView::Private
0103 {
0104 public:
0105 
0106     explicit Private()
0107     {
0108     }
0109 
0110     QString           filter;
0111     QList<DPluginCB*> plugBoxes;
0112 };
0113 
0114 DPluginConfView::DPluginConfView(QWidget* const parent)
0115     : QTreeWidget(parent),
0116       d          (new Private)
0117 {
0118     setRootIsDecorated(false);
0119     setUniformRowHeights(true);
0120     setSelectionMode(QAbstractItemView::SingleSelection);
0121     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0122     setAllColumnsShowFocus(true);
0123     setSortingEnabled(true);
0124     setColumnCount(5);
0125 
0126     header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0127     header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
0128     header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
0129     header()->setSectionResizeMode(3, QHeaderView::Stretch);
0130     header()->setSectionResizeMode(4, QHeaderView::Interactive);
0131     header()->setSortIndicatorShown(true);
0132 
0133     QStringList labels;
0134     labels.append(i18nc("@title: Dplugin property", "Name"));
0135     labels.append(i18nc("@title: Dplugin property", "Categories"));
0136     labels.append(i18nc("@title: Dplugin property", "Tools"));
0137     labels.append(i18nc("@title: Dplugin property", "Description"));
0138     labels.append(i18nc("@title: Dplugin property", "Authors"));
0139     setHeaderLabels(labels);
0140 
0141     setAutoFillBackground(false);
0142     viewport()->setAutoFillBackground(false);
0143 }
0144 
0145 DPluginConfView::~DPluginConfView()
0146 {
0147     delete d;
0148 }
0149 
0150 QTreeWidgetItem* DPluginConfView::appendPlugin(DPlugin* const plugin)
0151 {
0152     DPluginCB* const cb = new DPluginCB(plugin, this);
0153     d->plugBoxes.append(cb);
0154 
0155     return cb;
0156 }
0157 
0158 DPlugin* DPluginConfView::plugin(QTreeWidgetItem* const item) const
0159 {
0160     if (item)
0161     {
0162         DPluginCB* const cb = dynamic_cast<DPluginCB*>(item);
0163 
0164         if (cb)
0165         {
0166             return cb->m_plugin;
0167         }
0168     }
0169 
0170     return nullptr;
0171 }
0172 
0173 void DPluginConfView::apply()
0174 {
0175     DPluginLoader* const loader = DPluginLoader::instance();
0176 
0177     if (loader)
0178     {
0179         KSharedConfigPtr config = KSharedConfig::openConfig();
0180         KConfigGroup group      = config->group(loader->configGroupName());
0181 
0182         Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0183         {
0184             if (item->m_plugin->hasVisibilityProperty())
0185             {
0186                 bool load = (item->checkState(0) == Qt::Checked);
0187                 group.writeEntry(item->m_plugin->iid(), load);
0188                 item->m_plugin->setVisible(load);
0189                 item->m_plugin->setShouldLoaded(load);
0190             }
0191         }
0192 
0193         config->sync();
0194     }
0195 }
0196 
0197 void DPluginConfView::selectAll()
0198 {
0199     Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0200     {
0201         item->setCheckState(0, Qt::Checked);
0202     }
0203 }
0204 
0205 void DPluginConfView::clearAll()
0206 {
0207     Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0208     {
0209         item->setCheckState(0, Qt::Unchecked);
0210     }
0211 }
0212 
0213 int DPluginConfView::count() const
0214 {
0215     return d->plugBoxes.count();
0216 }
0217 
0218 int DPluginConfView::actived() const
0219 {
0220     int actived = 0;
0221 
0222     Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0223     {
0224         if (item->checkState(0) == Qt::Checked)
0225         {
0226             ++actived; // cppcheck-suppress useStlAlgorithm
0227         }
0228     }
0229 
0230     return actived;
0231 }
0232 
0233 int DPluginConfView::itemsVisible() const
0234 {
0235     int visible = 0;
0236 
0237     Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0238     {
0239         if (!item->isHidden())
0240         {
0241             ++visible; // cppcheck-suppress useStlAlgorithm
0242         }
0243     }
0244 
0245     return visible;
0246 }
0247 
0248 int DPluginConfView::itemsWithVisiblyProperty() const
0249 {
0250     int vp = 0;
0251 
0252     Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0253     {
0254         if (!item->isHidden() && item->m_plugin->hasVisibilityProperty())
0255         {
0256             ++vp; // cppcheck-suppress useStlAlgorithm
0257         }
0258     }
0259 
0260     return vp;
0261 }
0262 
0263 void DPluginConfView::setFilter(const QString& filter, Qt::CaseSensitivity cs)
0264 {
0265     d->filter = filter;
0266     int found = 0;
0267 
0268     Q_FOREACH (DPluginCB* const item, d->plugBoxes)
0269     {
0270         if (item->contains(filter, cs))
0271         {
0272             found++;
0273             item->setHidden(false);
0274         }
0275         else
0276         {
0277             item->setHidden(true);
0278         }
0279     }
0280 
0281     Q_EMIT signalSearchResult(found);
0282 }
0283 
0284 QString DPluginConfView::filter() const
0285 {
0286     return d->filter;
0287 }
0288 
0289 } // namespace Digikam
0290 
0291 #include "moc_dpluginconfview.cpp"