File indexing completed on 2024-05-05 04:33:14

0001 /*
0002     SPDX-FileCopyrightText: 2004-2018 Gilles Caulier <caulier dot gilles at gmail dot com>
0003     SPDX-FileCopyrightText: 2012 Victor Dodon <dodonvictor at gmail dot com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "configwidget.h"
0009 
0010 // Qt include
0011 
0012 #include <QList>
0013 #include <QHeaderView>
0014 
0015 // KF includes
0016 
0017 #include <KSharedConfig>
0018 #include <KConfigGroup>
0019 
0020 namespace KIPI
0021 {
0022 
0023 class PluginCheckBox : public QTreeWidgetItem
0024 {
0025 public:
0026 
0027     PluginCheckBox(PluginLoader::Info* const info, QTreeWidget* const parent)
0028         : QTreeWidgetItem(parent),
0029           m_info(info)
0030     {
0031         setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
0032         setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicator);
0033         setDisabled(false);
0034 
0035         // Name + Icon + Selector
0036         setText(0, m_info->name());
0037         setIcon(0, m_info->icon());
0038         setCheckState(0, m_info->shouldLoad() ? Qt::Checked : Qt::Unchecked);
0039 
0040         // Categories
0041         QStringList list = m_info->pluginCategories();
0042         list.removeDuplicates();
0043         list.sort();
0044         setText(1, list.join(QString::fromLatin1(", ")));
0045 
0046         // Description
0047         setText(2, m_info->comment());
0048 
0049         // Author
0050         setText(3, m_info->author().section(QString::fromLatin1(","), 0, 0));
0051     };
0052 
0053     ~PluginCheckBox() override
0054     {
0055     };
0056 
0057     bool contains(const QString& txt, Qt::CaseSensitivity cs) const
0058     {
0059         return (text(0).contains(txt, cs) ||
0060                 text(1).contains(txt, cs) ||
0061                 text(2).contains(txt, cs) ||
0062                 text(3).contains(txt, cs));
0063     };
0064 
0065 public:
0066 
0067     PluginLoader::Info* m_info;
0068 };
0069 
0070 // ---------------------------------------------------------------------
0071 
0072 class Q_DECL_HIDDEN ConfigWidget::Private
0073 {
0074 public:
0075 
0076     Private()
0077     {
0078     };
0079 
0080     QString                filter;
0081     QList<PluginCheckBox*> boxes;
0082 };
0083 
0084 ConfigWidget::ConfigWidget(QWidget* const parent)
0085     : QTreeWidget(parent),
0086       d(new Private)
0087 {
0088     setRootIsDecorated(false);
0089     setSelectionMode(QAbstractItemView::SingleSelection);
0090     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0091     setAllColumnsShowFocus(true);
0092     setSortingEnabled(true);
0093     setColumnCount(4);
0094 
0095     header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0096     header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
0097     header()->setSectionResizeMode(2, QHeaderView::Stretch);
0098     header()->setSectionResizeMode(3, QHeaderView::Interactive);
0099     header()->setSortIndicatorShown(true);
0100 
0101     setAutoFillBackground(false);
0102     viewport()->setAutoFillBackground(false);
0103 
0104     PluginLoader* const loader = PluginLoader::instance();
0105 
0106     if (loader)
0107     {
0108         const auto infos = loader->pluginList();
0109         for (PluginLoader::Info* const info : infos)
0110         {
0111             if (info)
0112             {
0113                 d->boxes.append(new PluginCheckBox(info, this));
0114             }
0115         }
0116     }
0117 
0118     // Sort items by plugin names.
0119     sortItems(0, Qt::AscendingOrder);
0120 }
0121 
0122 ConfigWidget::~ConfigWidget() = default;
0123 
0124 void ConfigWidget::apply()
0125 {
0126     if (PluginLoader::instance())
0127     {
0128         KSharedConfigPtr config = KSharedConfig::openConfig();
0129         KConfigGroup group      = config->group(QString::fromLatin1("KIPI/EnabledPlugin"));
0130 
0131         for (PluginCheckBox* const item : std::as_const(d->boxes))
0132         {
0133             bool orig = item->m_info->shouldLoad();
0134             bool load = (item->checkState(0) == Qt::Checked);
0135 
0136             if (orig != load)
0137             {
0138                 group.writeEntry(item->m_info->uname(), load);
0139                 item->m_info->setShouldLoad(load);
0140 
0141                 // See Bug #289779 - Plugins are not really freed / unplugged when disabled in the kipi setup dialog, always call reload()
0142                 // to reload plugins properly when the replug() signal is send.
0143                 item->m_info->reload();
0144             }
0145         }
0146 
0147         config->sync();
0148 
0149         Q_EMIT PluginLoader::instance()->replug();
0150     }
0151 }
0152 
0153 void ConfigWidget::selectAll()
0154 {
0155     for (PluginCheckBox* const item : std::as_const(d->boxes))
0156     {
0157         item->setCheckState(0, Qt::Checked);
0158     }
0159 }
0160 
0161 void ConfigWidget::clearAll()
0162 {
0163     for (PluginCheckBox* const item : std::as_const(d->boxes))
0164     {
0165         item->setCheckState(0, Qt::Unchecked);
0166     }
0167 }
0168 
0169 int ConfigWidget::count() const
0170 {
0171     return d->boxes.count();
0172 }
0173 
0174 int ConfigWidget::actived() const
0175 {
0176     int actived = 0;
0177 
0178     for (PluginCheckBox* const item : std::as_const(d->boxes))
0179     {
0180         if (item->checkState(0) == Qt::Checked)
0181             actived++;
0182     }
0183 
0184     return actived;
0185 }
0186 
0187 int ConfigWidget::visible() const
0188 {
0189     int visible = 0;
0190 
0191     for (PluginCheckBox* const item : std::as_const(d->boxes))
0192     {
0193         if (!item->isHidden())
0194             visible++;
0195     }
0196 
0197     return visible;
0198 }
0199 
0200 void ConfigWidget::setFilter(const QString& filter, Qt::CaseSensitivity cs)
0201 {
0202     d->filter  = filter;
0203     bool query = false;
0204 
0205     for (PluginCheckBox* const item : std::as_const(d->boxes))
0206     {
0207         if (item->contains(filter, cs))
0208         {
0209             query = true;
0210             item->setHidden(false);
0211         }
0212         else
0213         {
0214             item->setHidden(true);
0215         }
0216     }
0217 
0218     Q_EMIT signalSearchResult(query);
0219 }
0220 
0221 QString ConfigWidget::filter() const
0222 {
0223     return d->filter;
0224 }
0225 
0226 } // namespace KIPI
0227 
0228 #include "moc_configwidget.cpp"