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