Pimcommon

configurepluginslistwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "configurepluginslistwidget.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include <KLineEditEventHandler>
11#include <KLocalizedString>
12#include <KSharedConfig>
13#include <KTreeWidgetSearchLine>
14#include <KTreeWidgetSearchLineWidget>
15
16#include <QAction>
17#include <QHeaderView>
18#include <QPushButton>
19#include <QToolButton>
20#include <QTreeWidget>
21#include <QVBoxLayout>
22
23using namespace PimCommon;
24ConfigurePluginsListWidget::ConfigurePluginsListWidget(QWidget *parent)
25 : QWidget(parent)
26 , mListWidget(new QTreeWidget(this))
27{
28 auto mainLayout = new QVBoxLayout(this);
29 mainLayout->setObjectName("mainlayout"_L1);
30 mainLayout->setContentsMargins({});
31 mainLayout->setSpacing(0);
32
33 mListWidget->setSortingEnabled(true);
34 mListWidget->sortItems(0, Qt::AscendingOrder);
35 mListWidget->setObjectName("listwidget"_L1);
36 mListWidget->setHeaderHidden(true);
37 mListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
38 mListWidget->setColumnCount(2);
39 mListWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
40 mListWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
41 mListWidget->header()->setStretchLastSection(false);
42
43 mTreeWidgetSearchLineEdit = new KTreeWidgetSearchLineWidget(this, mListWidget);
44 mTreeWidgetSearchLineEdit->setObjectName("mTreeWidgetSearchLineEdit"_L1);
45 mTreeWidgetSearchLineEdit->searchLine()->setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags{Qt::BottomEdge}));
46 KLineEditEventHandler::catchReturnKey(mTreeWidgetSearchLineEdit->searchLine());
47
48 connect(mListWidget, &QTreeWidget::itemSelectionChanged, this, &ConfigurePluginsListWidget::slotItemSelectionChanged);
49 connect(mListWidget, &QTreeWidget::itemChanged, this, &ConfigurePluginsListWidget::slotItemChanged);
50
51 mainLayout->addWidget(mTreeWidgetSearchLineEdit);
52 mainLayout->addWidget(mListWidget);
53}
54
55ConfigurePluginsListWidget::~ConfigurePluginsListWidget() = default;
56
57void ConfigurePluginsListWidget::slotItemChanged(QTreeWidgetItem *item, int column)
58{
59 Q_UNUSED(item)
60 if (mInitializeDone) {
61 if (column == 0) {
62 Q_EMIT changed();
63 }
64 }
65}
66
67void ConfigurePluginsListWidget::slotItemSelectionChanged()
68{
69 QTreeWidgetItem *item = mListWidget->currentItem();
70 if (auto pluginItem = dynamic_cast<PluginItem *>(item)) {
71 Q_EMIT descriptionChanged(pluginItem->mDescription);
72 }
73}
74
75void ConfigurePluginsListWidget::save()
76{
77}
78
79void ConfigurePluginsListWidget::doLoadFromGlobalSettings()
80{
81 initialize();
82 initializeDone();
83}
84
85void ConfigurePluginsListWidget::doResetToDefaultsOther()
86{
87}
88
89void ConfigurePluginsListWidget::defaults()
90{
91}
92
93void ConfigurePluginsListWidget::initialize()
94{
95}
96
97void ConfigurePluginsListWidget::initializeDone()
98{
99 mInitializeDone = true;
100}
101
102void ConfigurePluginsListWidget::savePlugins(const QString &groupName, const QString &prefixSettingKey, const QList<PluginItem *> &listItems)
103{
104 if (listItems.isEmpty()) {
105 return;
106 }
107 QStringList enabledPlugins;
108 QStringList disabledPlugins;
109 for (PluginItem *item : listItems) {
110 if (item->checkState(0) == Qt::Checked) {
111 enabledPlugins << item->mIdentifier;
112 } else {
113 disabledPlugins << item->mIdentifier;
114 }
115 }
116 PimCommon::PluginUtil::savePluginSettings(groupName, prefixSettingKey, enabledPlugins, disabledPlugins);
117}
118
119void ConfigurePluginsListWidget::fillTopItems(const QList<PimCommon::PluginUtilData> &lst,
120 const QString &topLevelItemName,
121 const QString &groupName,
122 const QString &prefixKey,
123 QList<PluginItem *> &itemsList,
124 const QString &configureGroupName,
125 bool checkable)
126{
127 itemsList.clear();
128 if (!lst.isEmpty()) {
129 auto topLevel = new QTreeWidgetItem(mListWidget, {topLevelItemName});
130 topLevel->setFlags(topLevel->flags() & ~Qt::ItemIsSelectable);
131 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(groupName, prefixKey);
132 for (const PimCommon::PluginUtilData &data : lst) {
133 auto subItem = new PluginItem(topLevel);
134 subItem->setText(0, data.mName);
135 subItem->mIdentifier = data.mIdentifier;
136 subItem->mDescription = data.mDescription;
137 subItem->mEnableByDefault = data.mEnableByDefault;
138 subItem->mHasConfigureSupport = data.mHasConfigureDialog;
139 if (checkable) {
140 const bool isPluginActivated = PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, data.mEnableByDefault, data.mIdentifier);
141 subItem->mEnableFromUserSettings = isPluginActivated;
142 subItem->setCheckState(0, isPluginActivated ? Qt::Checked : Qt::Unchecked);
143 }
144 if (data.mHasConfigureDialog) {
145 auto but = new QToolButton(mListWidget);
146 auto act = new QAction(but);
147 const QStringList actData{configureGroupName, data.mIdentifier};
148 act->setData(actData);
149 but->setDefaultAction(act);
150 but->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
151 but->setText(i18n("..."));
152 but->setFixedWidth(28);
153 but->setToolTip(i18nc("@info:tooltip", "Configure"));
154 but->setAutoFillBackground(true);
155 but->setEnabled(subItem->mHasConfigureSupport);
156 mListWidget->setItemWidget(subItem, 1, but);
157 connect(but, &QToolButton::triggered, this, &ConfigurePluginsListWidget::slotConfigureClicked);
158 }
159 itemsList.append(subItem);
160 }
161 }
162}
163
164void ConfigurePluginsListWidget::slotConfigureClicked(QAction *act)
165{
166 if (act) {
167 const QStringList lst = act->data().toStringList();
168 if (lst.count() == 2) {
169 Q_EMIT configureClicked(lst.at(0), lst.at(1));
170 }
171 }
172}
173
174void ConfigurePluginsListWidget::changeState(const QList<PluginItem *> &items)
175{
176 for (PluginItem *item : items) {
177 item->setCheckState(0, item->mEnableByDefault ? Qt::Checked : Qt::Unchecked);
178 }
179}
180
181void ConfigurePluginsListWidget::resetToUserSettings(const QList<PluginItem *> &items)
182{
183 for (PluginItem *item : items) {
184 item->setCheckState(0, item->mEnableFromUserSettings ? Qt::Checked : Qt::Unchecked);
185 }
186}
187
188#include "moc_configurepluginslistwidget.cpp"
The PluginUtilData class.
Definition pluginutil.h:23
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
folderdialogacltab.h
QVariant data() const const
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
void clear()
qsizetype count() const const
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
BottomEdge
ItemIsSelectable
AscendingOrder
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void triggered(QAction *action)
QTreeWidgetItem * currentItem() const const
void itemChanged(QTreeWidgetItem *item, int column)
void itemSelectionChanged()
void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget)
Qt::CheckState checkState(int column) const const
void setCheckState(int column, Qt::CheckState state)
void setFlags(Qt::ItemFlags flags)
QVariant fromValue(T &&value)
QStringList toStringList() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.