Pimcommon

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

KDE's Doxygen guidelines are available online.