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"
8#include "configurepluginstreewidgetdelegate.h"
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 Qt::Literals::StringLiterals;
24using namespace PimCommon;
25ConfigurePluginsListWidget::ConfigurePluginsListWidget(QWidget *parent)
26 : QWidget(parent)
27 , mListWidget(new QTreeWidget(this))
28{
29 auto mainLayout = new QVBoxLayout(this);
30 mainLayout->setObjectName("mainlayout"_L1);
31 mainLayout->setContentsMargins({});
32 mainLayout->setSpacing(0);
33
34 mListWidget->setItemDelegate(new ConfigurePluginsTreeWidgetDelegate(this));
35 mListWidget->setSortingEnabled(true);
36 mListWidget->sortItems(0, Qt::AscendingOrder);
37 mListWidget->setObjectName("listwidget"_L1);
38 mListWidget->setHeaderHidden(true);
39 mListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
40 mListWidget->setColumnCount(2);
41 mListWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
42 mListWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
43 mListWidget->header()->setStretchLastSection(false);
44
45 mTreeWidgetSearchLineEdit = new KTreeWidgetSearchLineWidget(this, mListWidget);
46 mTreeWidgetSearchLineEdit->setObjectName("mTreeWidgetSearchLineEdit"_L1);
47 mTreeWidgetSearchLineEdit->searchLine()->setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags{Qt::BottomEdge}));
48 KLineEditEventHandler::catchReturnKey(mTreeWidgetSearchLineEdit->searchLine());
49
50 connect(mListWidget, &QTreeWidget::itemChanged, this, &ConfigurePluginsListWidget::slotItemChanged);
51
52 mainLayout->addWidget(mTreeWidgetSearchLineEdit);
53 mainLayout->addWidget(mListWidget);
54}
55
56ConfigurePluginsListWidget::~ConfigurePluginsListWidget() = default;
57
58void ConfigurePluginsListWidget::slotItemChanged(QTreeWidgetItem *item, int column)
59{
60 Q_UNUSED(item)
61 if (mInitializeDone) {
62 if (column == 0) {
63 Q_EMIT changed();
64 }
65 }
66}
67
68void ConfigurePluginsListWidget::save()
69{
70}
71
72void ConfigurePluginsListWidget::doLoadFromGlobalSettings()
73{
74 initialize();
75 initializeDone();
76}
77
78void ConfigurePluginsListWidget::doResetToDefaultsOther()
79{
80}
81
82void ConfigurePluginsListWidget::defaults()
83{
84}
85
86void ConfigurePluginsListWidget::initialize()
87{
88}
89
90void ConfigurePluginsListWidget::initializeDone()
91{
92 mInitializeDone = true;
93}
94
95void ConfigurePluginsListWidget::savePlugins(const QString &groupName, const QString &prefixSettingKey, const QList<PluginItem *> &listItems)
96{
97 if (listItems.isEmpty()) {
98 return;
99 }
100 QStringList enabledPlugins;
101 QStringList disabledPlugins;
102 for (PluginItem *item : listItems) {
103 if (item->checkState(0) == Qt::Checked) {
104 enabledPlugins << item->mIdentifier;
105 } else {
106 disabledPlugins << item->mIdentifier;
107 }
108 }
109 PimCommon::PluginUtil::savePluginSettings(groupName, prefixSettingKey, enabledPlugins, disabledPlugins);
110}
111
112void ConfigurePluginsListWidget::fillTopItems(const QList<PimCommon::PluginUtilData> &lst,
113 const QString &topLevelItemName,
114 const QString &groupName,
115 const QString &prefixKey,
116 QList<PluginItem *> &itemsList,
117 const QString &configureGroupName,
118 bool checkable)
119{
120 itemsList.clear();
121 if (!lst.isEmpty()) {
122 auto topLevel = new QTreeWidgetItem(mListWidget, {topLevelItemName});
123 topLevel->setFlags(topLevel->flags() & ~Qt::ItemIsSelectable);
124 const QPair<QStringList, QStringList> pair = PimCommon::PluginUtil::loadPluginSetting(groupName, prefixKey);
125 for (const PimCommon::PluginUtilData &data : lst) {
126 auto subItem = new PluginItem(topLevel);
127 subItem->setData(0, ConfigurePluginsListWidget::PluginItemData::Description, data.mDescription);
128 subItem->setText(0, data.mName);
129 subItem->mIdentifier = data.mIdentifier;
130 subItem->mEnableByDefault = data.mEnableByDefault;
131 subItem->mHasConfigureSupport = data.mHasConfigureDialog;
132 if (checkable) {
133 const bool isPluginActivated = PimCommon::PluginUtil::isPluginActivated(pair.first, pair.second, data.mEnableByDefault, data.mIdentifier);
134 subItem->mEnableFromUserSettings = isPluginActivated;
135 subItem->setCheckState(0, isPluginActivated ? Qt::Checked : Qt::Unchecked);
136 }
137 if (data.mHasConfigureDialog) {
138 auto but = new QToolButton(mListWidget);
139 auto act = new QAction(but);
140 const QStringList actData{configureGroupName, data.mIdentifier};
141 act->setData(actData);
142 but->setDefaultAction(act);
143 but->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
144 but->setText(i18n("..."));
145 but->setFixedWidth(28);
146 but->setToolTip(i18nc("@info:tooltip", "Configure"));
147 but->setAutoFillBackground(true);
148 but->setEnabled(subItem->mHasConfigureSupport);
149 mListWidget->setItemWidget(subItem, 1, but);
150 connect(but, &QToolButton::triggered, this, &ConfigurePluginsListWidget::slotConfigureClicked);
151 }
152 itemsList.append(subItem);
153 }
154 }
155}
156
157void ConfigurePluginsListWidget::slotConfigureClicked(QAction *act)
158{
159 if (act) {
160 const QStringList lst = act->data().toStringList();
161 if (lst.count() == 2) {
162 Q_EMIT configureClicked(lst.at(0), lst.at(1));
163 }
164 }
165}
166
167void ConfigurePluginsListWidget::changeState(const QList<PluginItem *> &items)
168{
169 for (PluginItem *item : items) {
170 item->setCheckState(0, item->mEnableByDefault ? Qt::Checked : Qt::Unchecked);
171 }
172}
173
174void ConfigurePluginsListWidget::resetToUserSettings(const QList<PluginItem *> &items)
175{
176 for (PluginItem *item : items) {
177 item->setCheckState(0, item->mEnableFromUserSettings ? Qt::Checked : Qt::Unchecked);
178 }
179}
180
181#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)
void itemChanged(QTreeWidgetItem *item, int column)
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 Fri Dec 6 2024 12:07:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.