-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkpluginselector.cpp
906 lines (758 loc) · 34.2 KB
/
kpluginselector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/**
* This file is part of the KDE project
* Copyright (C) 2007, 2006 Rafael Fernández López <[email protected]>
* Copyright (C) 2002-2003 Matthias Kretz <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kpluginselector.h"
#include "kpluginselector_p.h"
#include <QDir>
#include <QDirIterator>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
#include <QPainter>
#include <QBoxLayout>
#include <QApplication>
#include <QCheckBox>
#include <QPushButton>
#include <QStandardPaths>
#include <QStyle>
#include <QStyleOptionViewItem>
#include <QDebug>
#include <kurllabel.h>
#include <kcmoduleinfo.h>
#include <kcmoduleproxy.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <klocalizedstring.h>
#include <kcategorydrawer.h>
#include <kcategorizedview.h>
#include <kcategorizedsortfilterproxymodel.h>
#include <kaboutdata.h>
#include <kaboutapplicationdialog.h>
#include <KStandardGuiItem>
#define MARGIN 5
KPluginSelector::Private::Private(KPluginSelector *parent)
: QObject(parent)
, parent(parent)
, listView(nullptr)
, categoryDrawer(nullptr)
, showIcons(false)
{
}
KPluginSelector::Private::~Private()
{
}
void KPluginSelector::Private::updateDependencies(PluginEntry *pluginEntry, bool added)
{
if (added) {
QStringList dependencyList = pluginEntry->pluginInfo.dependencies();
if (!dependencyList.count()) {
return;
}
for (int i = 0; i < pluginModel->rowCount(); i++) {
const QModelIndex index = pluginModel->index(i, 0);
PluginEntry *pe = static_cast<PluginEntry *>(index.internalPointer());
if ((pe->pluginInfo.pluginName() != pluginEntry->pluginInfo.pluginName()) &&
dependencyList.contains(pe->pluginInfo.pluginName()) && !pe->checked) {
dependenciesWidget->addDependency(pe->pluginInfo.name(), pluginEntry->pluginInfo.name(), added);
const_cast<QAbstractItemModel *>(index.model())->setData(index, added, Qt::CheckStateRole);
updateDependencies(pe, added);
}
}
} else {
for (int i = 0; i < pluginModel->rowCount(); i++) {
const QModelIndex index = pluginModel->index(i, 0);
PluginEntry *pe = static_cast<PluginEntry *>(index.internalPointer());
if ((pe->pluginInfo.pluginName() != pluginEntry->pluginInfo.pluginName()) &&
pe->pluginInfo.dependencies().contains(pluginEntry->pluginInfo.pluginName()) && pe->checked) {
dependenciesWidget->addDependency(pe->pluginInfo.name(), pluginEntry->pluginInfo.name(), added);
const_cast<QAbstractItemModel *>(index.model())->setData(index, added, Qt::CheckStateRole);
updateDependencies(pe, added);
}
}
}
}
int KPluginSelector::Private::dependantLayoutValue(int value, int width, int totalWidth) const
{
if (listView->layoutDirection() == Qt::LeftToRight) {
return value;
}
return totalWidth - width - value;
}
KPluginSelector::Private::DependenciesWidget::DependenciesWidget(QWidget *parent)
: QWidget(parent)
, addedByDependencies(0)
, removedByDependencies(0)
{
setVisible(false);
details = new QLabel();
QHBoxLayout *layout = new QHBoxLayout;
QVBoxLayout *dataLayout = new QVBoxLayout;
dataLayout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
layout->setAlignment(Qt::AlignLeft);
QLabel *label = new QLabel();
label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
label->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize)));
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(label);
KUrlLabel *link = new KUrlLabel();
link->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
link->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
link->setGlowEnabled(false);
link->setUnderline(false);
link->setFloatEnabled(true);
link->setUseCursor(true);
link->setHighlightedColor(palette().color(QPalette::Link));
link->setSelectedColor(palette().color(QPalette::Link));
link->setText(i18n("Automatic changes have been performed due to plugin dependencies. Click here for further information"));
dataLayout->addWidget(link);
dataLayout->addWidget(details);
layout->addLayout(dataLayout);
setLayout(layout);
QObject::connect(link, SIGNAL(leftClickedUrl()), this, SLOT(showDependencyDetails()));
}
KPluginSelector::Private::DependenciesWidget::~DependenciesWidget()
{
}
void KPluginSelector::Private::DependenciesWidget::addDependency(const QString &dependency, const QString &pluginCausant, bool added)
{
if (!isVisible()) {
setVisible(true);
}
struct FurtherInfo furtherInfo;
furtherInfo.added = added;
furtherInfo.pluginCausant = pluginCausant;
if (dependencyMap.contains(dependency)) { // The dependency moved from added to removed or vice-versa
if (added && removedByDependencies) {
removedByDependencies--;
} else if (addedByDependencies) {
addedByDependencies--;
}
dependencyMap[dependency] = furtherInfo;
} else {
dependencyMap.insert(dependency, furtherInfo);
}
if (added) {
addedByDependencies++;
} else {
removedByDependencies++;
}
updateDetails();
}
void KPluginSelector::Private::DependenciesWidget::userOverrideDependency(const QString &dependency)
{
if (dependencyMap.contains(dependency)) {
if (addedByDependencies && dependencyMap[dependency].added) {
addedByDependencies--;
} else if (removedByDependencies) {
removedByDependencies--;
}
dependencyMap.remove(dependency);
}
updateDetails();
}
void KPluginSelector::Private::DependenciesWidget::clearDependencies()
{
addedByDependencies = 0;
removedByDependencies = 0;
dependencyMap.clear();
updateDetails();
}
void KPluginSelector::Private::DependenciesWidget::showDependencyDetails()
{
QString message = i18n("Automatic changes have been performed in order to satisfy plugin dependencies:\n");
foreach (const QString &dependency, dependencyMap.keys()) {
if (dependencyMap[dependency].added) {
message += i18n("\n %1 plugin has been automatically checked because of the dependency of %2 plugin", dependency, dependencyMap[dependency].pluginCausant);
} else {
message += i18n("\n %1 plugin has been automatically unchecked because of its dependency on %2 plugin", dependency, dependencyMap[dependency].pluginCausant);
}
}
KMessageBox::information(this, message, i18n("Dependency Check"));
addedByDependencies = 0;
removedByDependencies = 0;
updateDetails();
}
void KPluginSelector::Private::DependenciesWidget::updateDetails()
{
if (!dependencyMap.count()) {
setVisible(false);
return;
}
QString message;
if (addedByDependencies) {
message += i18np("%1 plugin automatically added due to plugin dependencies", "%1 plugins automatically added due to plugin dependencies", addedByDependencies);
}
if (removedByDependencies && !message.isEmpty()) {
message += i18n(", ");
}
if (removedByDependencies) {
message += i18np("%1 plugin automatically removed due to plugin dependencies", "%1 plugins automatically removed due to plugin dependencies", removedByDependencies);
}
if (message.isEmpty()) {
details->setVisible(false);
} else {
details->setVisible(true);
details->setText(message);
}
}
KPluginSelector::KPluginSelector(QWidget *parent)
: QWidget(parent)
, d(new Private(this))
{
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
setLayout(layout);
d->lineEdit = new QLineEdit(this);
d->lineEdit->setClearButtonEnabled(true);
d->lineEdit->setPlaceholderText(i18n("Search Plugins"));
d->listView = new KCategorizedView(this);
d->categoryDrawer = new KCategoryDrawer(d->listView);
d->listView->setVerticalScrollMode(QListView::ScrollPerPixel);
d->listView->setAlternatingRowColors(true);
d->listView->setCategoryDrawer(d->categoryDrawer);
d->dependenciesWidget = new Private::DependenciesWidget(this);
d->pluginModel = new Private::PluginModel(d, this);
d->proxyModel = new Private::ProxyModel(d, this);
d->proxyModel->setCategorizedModel(true);
d->proxyModel->setSourceModel(d->pluginModel);
d->listView->setModel(d->proxyModel);
d->listView->setAlternatingRowColors(true);
Private::PluginDelegate *pluginDelegate = new Private::PluginDelegate(d, this);
d->listView->setItemDelegate(pluginDelegate);
d->listView->setMouseTracking(true);
d->listView->viewport()->setAttribute(Qt::WA_Hover);
connect(d->lineEdit, &QLineEdit::textChanged, d->proxyModel, &QSortFilterProxyModel::invalidate);
connect(pluginDelegate, &Private::PluginDelegate::changed, this, &KPluginSelector::changed);
connect(pluginDelegate, &Private::PluginDelegate::configCommitted, this, &KPluginSelector::configCommitted);
layout->addWidget(d->lineEdit);
layout->addWidget(d->listView);
layout->addWidget(d->dependenciesWidget);
}
KPluginSelector::~KPluginSelector()
{
delete d->listView->itemDelegate();
delete d->listView; // depends on some other things in d, make sure this dies first.
delete d;
}
void KPluginSelector::addPlugins(const QString &componentName,
const QString &categoryName,
const QString &categoryKey,
KSharedConfig::Ptr config)
{
QStringList desktopFileNames;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, componentName + QStringLiteral("/kpartplugins"), QStandardPaths::LocateDirectory);
Q_FOREACH (const QString &dir, dirs) {
QDirIterator it(dir, QStringList() << QStringLiteral("*.desktop"), QDir::NoFilter, QDirIterator::Subdirectories);
while (it.hasNext()) {
desktopFileNames.append(it.next());
}
}
QList<KPluginInfo> pluginInfoList = KPluginInfo::fromFiles(desktopFileNames);
if (pluginInfoList.isEmpty()) {
return;
}
if (!config) {
config = KSharedConfig::openConfig(componentName + QStringLiteral("rc"));
}
Q_ASSERT(config);
KConfigGroup cfgGroup(config, "KParts Plugins");
// qDebug() << "cfgGroup = " << &cfgGroup;
d->pluginModel->addPlugins(pluginInfoList, categoryName, categoryKey, cfgGroup);
d->proxyModel->sort(0);
}
void KPluginSelector::addPlugins(const QList<KPluginInfo> &pluginInfoList,
PluginLoadMethod pluginLoadMethod,
const QString &categoryName,
const QString &categoryKey,
const KSharedConfig::Ptr &config)
{
if (pluginInfoList.isEmpty()) {
return;
}
KConfigGroup cfgGroup(config ? config : KSharedConfig::openConfig(), "Plugins");
// qDebug() << "cfgGroup = " << &cfgGroup;
d->pluginModel->addPlugins(pluginInfoList, categoryName, categoryKey, cfgGroup, pluginLoadMethod, true /* manually added */);
d->proxyModel->sort(0);
}
void KPluginSelector::load()
{
for (int i = 0; i < d->pluginModel->rowCount(); i++) {
const QModelIndex index = d->pluginModel->index(i, 0);
PluginEntry *pluginEntry = static_cast<PluginEntry *>(index.internalPointer());
pluginEntry->pluginInfo.load(pluginEntry->cfgGroup);
d->pluginModel->setData(index, pluginEntry->pluginInfo.isPluginEnabled(), Qt::CheckStateRole);
}
emit changed(false);
}
void KPluginSelector::save()
{
for (int i = 0; i < d->pluginModel->rowCount(); i++) {
const QModelIndex index = d->pluginModel->index(i, 0);
PluginEntry *pluginEntry = static_cast<PluginEntry *>(index.internalPointer());
pluginEntry->pluginInfo.setPluginEnabled(pluginEntry->checked);
pluginEntry->pluginInfo.save(pluginEntry->cfgGroup);
pluginEntry->cfgGroup.sync();
}
emit changed(false);
}
void KPluginSelector::defaults()
{
for (int i = 0; i < d->pluginModel->rowCount(); i++) {
const QModelIndex index = d->pluginModel->index(i, 0);
PluginEntry *pluginEntry = static_cast<PluginEntry *>(index.internalPointer());
d->pluginModel->setData(index, pluginEntry->pluginInfo.isPluginEnabledByDefault(), Qt::CheckStateRole);
}
emit changed(true);
}
bool KPluginSelector::isDefault() const
{
for (int i = 0; i < d->pluginModel->rowCount(); i++) {
const QModelIndex index = d->pluginModel->index(i, 0);
PluginEntry *pluginEntry = static_cast<PluginEntry *>(index.internalPointer());
if (d->pluginModel->data(index, Qt::CheckStateRole).toBool() != pluginEntry->pluginInfo.isPluginEnabledByDefault()) {
return false;
}
}
return true;
}
void KPluginSelector::updatePluginsState()
{
for (int i = 0; i < d->pluginModel->rowCount(); i++) {
const QModelIndex index = d->pluginModel->index(i, 0);
PluginEntry *pluginEntry = static_cast<PluginEntry *>(index.internalPointer());
if (pluginEntry->manuallyAdded) {
pluginEntry->pluginInfo.setPluginEnabled(pluginEntry->checked);
}
}
}
void KPluginSelector::setConfigurationArguments(const QStringList& arguments)
{
d->kcmArguments = arguments;
}
QStringList KPluginSelector::configurationArguments() const
{
return d->kcmArguments;
}
KPluginSelector::Private::PluginModel::PluginModel(KPluginSelector::Private *pluginSelector_d, QObject *parent)
: QAbstractListModel(parent)
, pluginSelector_d(pluginSelector_d)
{
}
KPluginSelector::Private::PluginModel::~PluginModel()
{
}
void KPluginSelector::Private::PluginModel::addPlugins(const QList<KPluginInfo> &pluginList, const QString &categoryName, const QString &categoryKey, const KConfigGroup &cfgGroup, PluginLoadMethod pluginLoadMethod, bool manuallyAdded)
{
QList<PluginEntry> listToAdd;
foreach (const KPluginInfo &pluginInfo, pluginList) {
PluginEntry pluginEntry;
pluginEntry.category = categoryName;
pluginEntry.pluginInfo = pluginInfo;
if (pluginLoadMethod == ReadConfigFile) {
pluginEntry.pluginInfo.load(cfgGroup);
}
pluginEntry.checked = pluginInfo.isPluginEnabled();
pluginEntry.manuallyAdded = manuallyAdded;
if (cfgGroup.isValid()) {
pluginEntry.cfgGroup = cfgGroup;
} else {
pluginEntry.cfgGroup = pluginInfo.config();
}
// this is where kiosk will set if a plugin is checkable or not (pluginName + "Enabled")
pluginEntry.isCheckable = !pluginInfo.isValid() || !pluginEntry.cfgGroup.isEntryImmutable(pluginInfo.pluginName() + QLatin1String("Enabled"));
if (!pluginEntryList.contains(pluginEntry) && !listToAdd.contains(pluginEntry) &&
(categoryKey.isEmpty() || !pluginInfo.category().compare(categoryKey, Qt::CaseInsensitive)) &&
(!pluginInfo.service() || !pluginInfo.service()->noDisplay())) {
listToAdd << pluginEntry;
if (!pluginSelector_d->showIcons && !pluginInfo.icon().isEmpty()) {
pluginSelector_d->showIcons = true;
}
}
}
if (listToAdd.count()) {
beginInsertRows(QModelIndex(), pluginEntryList.count(), pluginEntryList.count() + listToAdd.count() - 1);
pluginEntryList << listToAdd;
endInsertRows();
}
}
QList<KService::Ptr> KPluginSelector::Private::PluginModel::pluginServices(const QModelIndex &index) const
{
return static_cast<PluginEntry *>(index.internalPointer())->pluginInfo.kcmServices();
}
QModelIndex KPluginSelector::Private::PluginModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent)
return createIndex(row, column, (row < pluginEntryList.count()) ? (void *) &pluginEntryList.at(row)
: nullptr);
}
QVariant KPluginSelector::Private::PluginModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !index.internalPointer()) {
return QVariant();
}
PluginEntry *pluginEntry = static_cast<PluginEntry *>(index.internalPointer());
switch (role) {
case Qt::DisplayRole:
return pluginEntry->pluginInfo.name();
case PluginEntryRole:
return QVariant::fromValue(pluginEntry);
case ServicesCountRole:
return pluginEntry->pluginInfo.kcmServices().count();
case NameRole:
return pluginEntry->pluginInfo.name();
case CommentRole:
return pluginEntry->pluginInfo.comment();
case AuthorRole:
return pluginEntry->pluginInfo.author();
case EmailRole:
return pluginEntry->pluginInfo.email();
case WebsiteRole:
return pluginEntry->pluginInfo.website();
case VersionRole:
return pluginEntry->pluginInfo.version();
case LicenseRole:
return pluginEntry->pluginInfo.license();
case DependenciesRole:
return pluginEntry->pluginInfo.dependencies();
case IsCheckableRole:
return pluginEntry->isCheckable;
case Qt::DecorationRole:
return pluginEntry->pluginInfo.icon();
case Qt::CheckStateRole:
return pluginEntry->checked;
case KCategorizedSortFilterProxyModel::CategoryDisplayRole: // fall through
case KCategorizedSortFilterProxyModel::CategorySortRole:
return pluginEntry->category;
default:
return QVariant();
}
}
bool KPluginSelector::Private::PluginModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid()) {
return false;
}
bool ret = false;
if (role == Qt::CheckStateRole) {
static_cast<PluginEntry *>(index.internalPointer())->checked = value.toBool();
ret = true;
}
if (ret) {
emit dataChanged(index, index);
}
return ret;
}
int KPluginSelector::Private::PluginModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return pluginEntryList.count();
}
KPluginSelector::Private::ProxyModel::ProxyModel(KPluginSelector::Private *pluginSelector_d, QObject *parent)
: KCategorizedSortFilterProxyModel(parent)
, pluginSelector_d(pluginSelector_d)
{
sort(0);
}
KPluginSelector::Private::ProxyModel::~ProxyModel()
{
}
bool KPluginSelector::Private::ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
Q_UNUSED(sourceParent)
if (!pluginSelector_d->lineEdit->text().isEmpty()) {
const QModelIndex index = sourceModel()->index(sourceRow, 0);
const KPluginInfo pluginInfo = static_cast<PluginEntry *>(index.internalPointer())->pluginInfo;
return pluginInfo.name().contains(pluginSelector_d->lineEdit->text(), Qt::CaseInsensitive) ||
pluginInfo.comment().contains(pluginSelector_d->lineEdit->text(), Qt::CaseInsensitive);
}
return true;
}
bool KPluginSelector::Private::ProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
{
return static_cast<PluginEntry *>(left.internalPointer())->pluginInfo.name().compare(static_cast<PluginEntry *>(right.internalPointer())->pluginInfo.name(), Qt::CaseInsensitive) < 0;
}
KPluginSelector::Private::PluginDelegate::PluginDelegate(KPluginSelector::Private *pluginSelector_d, QObject *parent)
: KWidgetItemDelegate(pluginSelector_d->listView, parent)
, checkBox(new QCheckBox)
, pushButton(new QPushButton)
, pluginSelector_d(pluginSelector_d)
{
pushButton->setIcon(QIcon::fromTheme(QStringLiteral("configure"))); // only for getting size matters
}
KPluginSelector::Private::PluginDelegate::~PluginDelegate()
{
delete checkBox;
delete pushButton;
}
void KPluginSelector::Private::PluginDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (!index.isValid()) {
return;
}
int xOffset = checkBox->sizeHint().width();
bool disabled = !index.model()->data(index, IsCheckableRole).toBool();
painter->save();
QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, nullptr);
int iconSize = option.rect.height() - MARGIN * 2;
if (pluginSelector_d->showIcons) {
QIcon icon = QIcon::fromTheme(index.model()->data(index, Qt::DecorationRole).toString());
icon.paint(painter, QRect(pluginSelector_d->dependantLayoutValue(MARGIN + option.rect.left() + xOffset, iconSize, option.rect.width()), MARGIN + option.rect.top(), iconSize, iconSize));
} else {
iconSize = -MARGIN;
}
QRect contentsRect(pluginSelector_d->dependantLayoutValue(MARGIN * 2 + iconSize + option.rect.left() + xOffset, option.rect.width() - MARGIN * 3 - iconSize - xOffset, option.rect.width()), MARGIN + option.rect.top(), option.rect.width() - MARGIN * 3 - iconSize - xOffset, option.rect.height() - MARGIN * 2);
int lessHorizontalSpace = MARGIN * 2 + pushButton->sizeHint().width();
if (index.model()->data(index, ServicesCountRole).toBool()) {
lessHorizontalSpace += MARGIN + pushButton->sizeHint().width();
}
contentsRect.setWidth(contentsRect.width() - lessHorizontalSpace);
if (option.state & QStyle::State_Selected) {
painter->setPen(option.palette.highlightedText().color());
}
if (pluginSelector_d->listView->layoutDirection() == Qt::RightToLeft) {
contentsRect.translate(lessHorizontalSpace, 0);
}
painter->save();
if (disabled) {
QPalette pal(option.palette);
pal.setCurrentColorGroup(QPalette::Disabled);
painter->setPen(pal.text().color());
}
painter->save();
QFont font = titleFont(option.font);
QFontMetrics fmTitle(font);
painter->setFont(font);
painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignTop, fmTitle.elidedText(index.model()->data(index, Qt::DisplayRole).toString(), Qt::ElideRight, contentsRect.width()));
painter->restore();
painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignBottom, option.fontMetrics.elidedText(index.model()->data(index, CommentRole).toString(), Qt::ElideRight, contentsRect.width()));
painter->restore();
painter->restore();
}
QSize KPluginSelector::Private::PluginDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int i = 5;
int j = 1;
if (index.model()->data(index, ServicesCountRole).toBool()) {
i = 6;
j = 2;
}
if (!pluginSelector_d->showIcons) {
i--;
}
QFont font = titleFont(option.font);
QFontMetrics fmTitle(font);
return QSize(qMax(fmTitle.width(index.model()->data(index, Qt::DisplayRole).toString()),
option.fontMetrics.width(index.model()->data(index, CommentRole).toString())) +
(pluginSelector_d->showIcons ? KIconLoader::SizeMedium : 0) + MARGIN * i + pushButton->sizeHint().width() * j,
qMax(KIconLoader::SizeMedium + MARGIN * 2, fmTitle.height() + option.fontMetrics.height() + MARGIN * 2));
}
QList<QWidget *> KPluginSelector::Private::PluginDelegate::createItemWidgets(const QModelIndex &index) const
{
Q_UNUSED(index);
QList<QWidget *> widgetList;
QCheckBox *enabledCheckBox = new QCheckBox;
connect(enabledCheckBox, &QAbstractButton::clicked, this, &PluginDelegate::slotStateChanged);
connect(enabledCheckBox, &QAbstractButton::clicked, this, &PluginDelegate::emitChanged);
QPushButton *aboutPushButton = new QPushButton;
aboutPushButton->setIcon(QIcon::fromTheme(QStringLiteral("dialog-information")));
connect(aboutPushButton, &QAbstractButton::clicked, this, &PluginDelegate::slotAboutClicked);
QPushButton *configurePushButton = new QPushButton;
configurePushButton->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
connect(configurePushButton, &QAbstractButton::clicked, this, &PluginDelegate::slotConfigureClicked);
setBlockedEventTypes(enabledCheckBox, QList<QEvent::Type>() << QEvent::MouseButtonPress
<< QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick
<< QEvent::KeyPress << QEvent::KeyRelease);
setBlockedEventTypes(aboutPushButton, QList<QEvent::Type>() << QEvent::MouseButtonPress
<< QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick
<< QEvent::KeyPress << QEvent::KeyRelease);
setBlockedEventTypes(configurePushButton, QList<QEvent::Type>() << QEvent::MouseButtonPress
<< QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick
<< QEvent::KeyPress << QEvent::KeyRelease);
widgetList << enabledCheckBox << configurePushButton << aboutPushButton;
return widgetList;
}
void KPluginSelector::Private::PluginDelegate::updateItemWidgets(const QList<QWidget *> widgets,
const QStyleOptionViewItem &option,
const QPersistentModelIndex &index) const
{
QCheckBox *checkBox = static_cast<QCheckBox *>(widgets[0]);
checkBox->resize(checkBox->sizeHint());
checkBox->move(pluginSelector_d->dependantLayoutValue(MARGIN, checkBox->sizeHint().width(), option.rect.width()), option.rect.height() / 2 - checkBox->sizeHint().height() / 2);
QPushButton *aboutPushButton = static_cast<QPushButton *>(widgets[2]);
QSize aboutPushButtonSizeHint = aboutPushButton->sizeHint();
aboutPushButton->resize(aboutPushButtonSizeHint);
aboutPushButton->move(pluginSelector_d->dependantLayoutValue(option.rect.width() - MARGIN - aboutPushButtonSizeHint.width(), aboutPushButtonSizeHint.width(), option.rect.width()), option.rect.height() / 2 - aboutPushButtonSizeHint.height() / 2);
QPushButton *configurePushButton = static_cast<QPushButton *>(widgets[1]);
QSize configurePushButtonSizeHint = configurePushButton->sizeHint();
configurePushButton->resize(configurePushButtonSizeHint);
configurePushButton->move(pluginSelector_d->dependantLayoutValue(option.rect.width() - MARGIN * 2 - configurePushButtonSizeHint.width() - aboutPushButtonSizeHint.width(), configurePushButtonSizeHint.width(), option.rect.width()), option.rect.height() / 2 - configurePushButtonSizeHint.height() / 2);
if (!index.isValid() || !index.internalPointer()) {
checkBox->setVisible(false);
aboutPushButton->setVisible(false);
configurePushButton->setVisible(false);
} else {
checkBox->setChecked(index.model()->data(index, Qt::CheckStateRole).toBool());
checkBox->setEnabled(index.model()->data(index, IsCheckableRole).toBool());
configurePushButton->setVisible(index.model()->data(index, ServicesCountRole).toBool());
configurePushButton->setEnabled(index.model()->data(index, Qt::CheckStateRole).toBool());
}
}
void KPluginSelector::Private::PluginDelegate::slotStateChanged(bool state)
{
if (!focusedIndex().isValid()) {
return;
}
const QModelIndex index = focusedIndex();
pluginSelector_d->dependenciesWidget->clearDependencies();
PluginEntry *pluginEntry = index.model()->data(index, PluginEntryRole).value<PluginEntry *>();
pluginSelector_d->updateDependencies(pluginEntry, state);
const_cast<QAbstractItemModel *>(index.model())->setData(index, state, Qt::CheckStateRole);
}
void KPluginSelector::Private::PluginDelegate::emitChanged()
{
emit changed(true);
}
void KPluginSelector::Private::PluginDelegate::slotAboutClicked()
{
const QModelIndex index = focusedIndex();
const QAbstractItemModel *model = index.model();
const QString name = model->data(index, NameRole).toString();
const QString comment = model->data(index, CommentRole).toString();
const QString author = model->data(index, AuthorRole).toString();
const QString email = model->data(index, EmailRole).toString();
const QString website = model->data(index, WebsiteRole).toString();
const QString version = model->data(index, VersionRole).toString();
const QString license = model->data(index, LicenseRole).toString();
KAboutData aboutData(name, name, version, comment, KAboutLicense::byKeyword(license).key(), QString(), QString(), website);
aboutData.setProgramIconName(index.model()->data(index, Qt::DecorationRole).toString());
const QStringList authors = author.split(QLatin1Char(','));
const QStringList emails = email.split(QLatin1Char(','));
if (authors.count() == emails.count()) {
int i = 0;
foreach (const QString &author, authors) {
if (!author.isEmpty()) {
aboutData.addAuthor(author, QString(), emails[i]);
}
i++;
}
}
KAboutApplicationDialog aboutPlugin(aboutData, itemView());
aboutPlugin.setWindowTitle(i18nc("Used only for plugins", "About %1", aboutData.displayName()));
aboutPlugin.exec();
}
void KPluginSelector::Private::PluginDelegate::slotConfigureClicked()
{
const QModelIndex index = focusedIndex();
const QAbstractItemModel *model = index.model();
PluginEntry *pluginEntry = model->data(index, PluginEntryRole).value<PluginEntry *>();
KPluginInfo pluginInfo = pluginEntry->pluginInfo;
QDialog configDialog(itemView());
configDialog.setWindowTitle(model->data(index, NameRole).toString());
// The number of KCModuleProxies in use determines whether to use a tabwidget
QTabWidget *newTabWidget = nullptr;
// Widget to use for the setting dialog's main widget,
// either a QTabWidget or a KCModuleProxy
QWidget *mainWidget = nullptr;
// Widget to use as the KCModuleProxy's parent.
// The first proxy is owned by the dialog itself
QWidget *moduleProxyParentWidget = &configDialog;
foreach (const KService::Ptr &servicePtr, pluginInfo.kcmServices()) {
if (!servicePtr->noDisplay()) {
KCModuleInfo moduleInfo(servicePtr);
KCModuleProxy *currentModuleProxy = new KCModuleProxy(moduleInfo, moduleProxyParentWidget, pluginSelector_d->kcmArguments);
if (currentModuleProxy->realModule()) {
moduleProxyList << currentModuleProxy;
if (mainWidget && !newTabWidget) {
// we already created one KCModuleProxy, so we need a tab widget.
// Move the first proxy into the tab widget and ensure this and subsequent
// proxies are in the tab widget
newTabWidget = new QTabWidget(&configDialog);
moduleProxyParentWidget = newTabWidget;
mainWidget->setParent(newTabWidget);
KCModuleProxy *moduleProxy = qobject_cast<KCModuleProxy *>(mainWidget);
if (moduleProxy) {
newTabWidget->addTab(mainWidget, moduleProxy->moduleInfo().moduleName());
mainWidget = newTabWidget;
} else {
delete newTabWidget;
newTabWidget = nullptr;
moduleProxyParentWidget = &configDialog;
mainWidget->setParent(nullptr);
}
}
if (newTabWidget) {
newTabWidget->addTab(currentModuleProxy, servicePtr->name());
} else {
mainWidget = currentModuleProxy;
}
} else {
delete currentModuleProxy;
}
}
}
// it could happen that we had services to show, but none of them were real modules.
if (moduleProxyList.count()) {
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(mainWidget);
const int marginHint = configDialog.style()->pixelMetric(QStyle::PM_DefaultChildMargin);
layout->insertSpacing(-1, marginHint);
QDialogButtonBox *buttonBox = new QDialogButtonBox(&configDialog);
buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults);
KGuiItem::assign(buttonBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
KGuiItem::assign(buttonBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
KGuiItem::assign(buttonBox->button(QDialogButtonBox::RestoreDefaults), KStandardGuiItem::defaults());
connect(buttonBox, &QDialogButtonBox::accepted, &configDialog, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, &configDialog, &QDialog::reject);
connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), &QAbstractButton::clicked, this, &PluginDelegate::slotDefaultClicked);
layout->addWidget(buttonBox);
configDialog.setLayout(layout);
if (configDialog.exec() == QDialog::Accepted) {
foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
QStringList parentComponents = moduleProxy->moduleInfo().service()->property(QStringLiteral("X-KDE-ParentComponents")).toStringList();
moduleProxy->save();
foreach (const QString &parentComponent, parentComponents) {
emit configCommitted(parentComponent.toLatin1());
}
}
} else {
foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
moduleProxy->load();
}
}
qDeleteAll(moduleProxyList);
moduleProxyList.clear();
}
}
void KPluginSelector::Private::PluginDelegate::slotDefaultClicked()
{
foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
moduleProxy->defaults();
}
}
QFont KPluginSelector::Private::PluginDelegate::titleFont(const QFont &baseFont) const
{
QFont retFont(baseFont);
retFont.setBold(true);
return retFont;
}
#include "moc_kpluginselector_p.cpp"
#include "moc_kpluginselector.cpp"