00001
00021 #include "kpluginselector.h"
00022 #include "kpluginselector_p.h"
00023
00024 #include <QtGui/QLabel>
00025 #include <QtGui/QPainter>
00026 #include <QtGui/QBoxLayout>
00027 #include <QtGui/QApplication>
00028 #include <QtGui/QCheckBox>
00029 #include <QtGui/QStyleOptionViewItemV4>
00030
00031 #include <kdebug.h>
00032 #include <klineedit.h>
00033 #include <kdialog.h>
00034 #include <kurllabel.h>
00035 #include <ktabwidget.h>
00036 #include <kcmoduleinfo.h>
00037 #include <kcmoduleproxy.h>
00038 #include <kmessagebox.h>
00039 #include <kpushbutton.h>
00040 #include <kiconloader.h>
00041 #include <kstandarddirs.h>
00042 #include <klocalizedstring.h>
00043 #include <kcategorydrawer.h>
00044 #include <kcategorizedview.h>
00045 #include <kcategorizedsortfilterproxymodel.h>
00046 #include <kaboutapplicationdialog.h>
00047
00048 #define MARGIN 5
00049
00050 KPluginSelector::Private::Private(KPluginSelector *parent)
00051 : QObject(parent)
00052 , parent(parent)
00053 , listView(0)
00054 , categoryDrawer(new KCategoryDrawer)
00055 , showIcons(false)
00056 {
00057 }
00058
00059 KPluginSelector::Private::~Private()
00060 {
00061 delete categoryDrawer;
00062 }
00063
00064 void KPluginSelector::Private::updateDependencies(PluginEntry *pluginEntry, bool added)
00065 {
00066 if (added) {
00067 QStringList dependencyList = pluginEntry->pluginInfo.dependencies();
00068
00069 if (!dependencyList.count()) {
00070 return;
00071 }
00072
00073 for (int i = 0; i < pluginModel->rowCount(); i++) {
00074 const QModelIndex index = pluginModel->index(i, 0);
00075 PluginEntry *pe = static_cast<PluginEntry*>(index.internalPointer());
00076
00077 if ((pe->pluginInfo.pluginName() != pluginEntry->pluginInfo.pluginName()) &&
00078 dependencyList.contains(pe->pluginInfo.pluginName()) && !pe->checked) {
00079 dependenciesWidget->addDependency(pe->pluginInfo.name(), pluginEntry->pluginInfo.name(), added);
00080 const_cast<QAbstractItemModel*>(index.model())->setData(index, added, Qt::CheckStateRole);
00081 updateDependencies(pe, added);
00082 }
00083 }
00084 } else {
00085 for (int i = 0; i < pluginModel->rowCount(); i++) {
00086 const QModelIndex index = pluginModel->index(i, 0);
00087 PluginEntry *pe = static_cast<PluginEntry*>(index.internalPointer());
00088
00089 if ((pe->pluginInfo.pluginName() != pluginEntry->pluginInfo.pluginName()) &&
00090 pe->pluginInfo.dependencies().contains(pluginEntry->pluginInfo.pluginName()) && pe->checked) {
00091 dependenciesWidget->addDependency(pe->pluginInfo.name(), pluginEntry->pluginInfo.name(), added);
00092 const_cast<QAbstractItemModel*>(index.model())->setData(index, added, Qt::CheckStateRole);
00093 updateDependencies(pe, added);
00094 }
00095 }
00096 }
00097 }
00098
00099 int KPluginSelector::Private::dependantLayoutValue(int value, int width, int totalWidth) const
00100 {
00101 if (listView->layoutDirection() == Qt::LeftToRight) {
00102 return value;
00103 }
00104
00105 return totalWidth - width - value;
00106 }
00107
00108 KPluginSelector::Private::DependenciesWidget::DependenciesWidget(QWidget *parent)
00109 : QWidget(parent)
00110 , addedByDependencies(0)
00111 , removedByDependencies(0)
00112 {
00113 setVisible(false);
00114
00115 details = new QLabel();
00116
00117 QHBoxLayout *layout = new QHBoxLayout;
00118
00119 QVBoxLayout *dataLayout = new QVBoxLayout;
00120 dataLayout->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
00121 layout->setAlignment(Qt::AlignLeft);
00122 QLabel *label = new QLabel();
00123 label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
00124 label->setPixmap(KIconLoader::global()->loadIcon("dialog-information", KIconLoader::Dialog));
00125 label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00126 layout->addWidget(label);
00127 KUrlLabel *link = new KUrlLabel();
00128 link->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
00129 link->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
00130 link->setGlowEnabled(false);
00131 link->setUnderline(false);
00132 link->setFloatEnabled(true);
00133 link->setUseCursor(true);
00134 link->setHighlightedColor(palette().color(QPalette::Link));
00135 link->setSelectedColor(palette().color(QPalette::Link));
00136 link->setText(i18n("Automatic changes have been performed due to plugin dependencies. Click here for further information"));
00137 dataLayout->addWidget(link);
00138 dataLayout->addWidget(details);
00139 layout->addLayout(dataLayout);
00140 setLayout(layout);
00141
00142 QObject::connect(link, SIGNAL(leftClickedUrl()), this, SLOT(showDependencyDetails()));
00143 }
00144
00145 KPluginSelector::Private::DependenciesWidget::~DependenciesWidget()
00146 {
00147 }
00148
00149 void KPluginSelector::Private::DependenciesWidget::addDependency(const QString &dependency, const QString &pluginCausant, bool added)
00150 {
00151 if (!isVisible())
00152 setVisible(true);
00153
00154 struct FurtherInfo furtherInfo;
00155 furtherInfo.added = added;
00156 furtherInfo.pluginCausant = pluginCausant;
00157
00158 if (dependencyMap.contains(dependency))
00159 {
00160 if (added && removedByDependencies)
00161 removedByDependencies--;
00162 else if (addedByDependencies)
00163 addedByDependencies--;
00164
00165 dependencyMap[dependency] = furtherInfo;
00166 }
00167 else
00168 dependencyMap.insert(dependency, furtherInfo);
00169
00170 if (added)
00171 addedByDependencies++;
00172 else
00173 removedByDependencies++;
00174
00175 updateDetails();
00176 }
00177
00178 void KPluginSelector::Private::DependenciesWidget::userOverrideDependency(const QString &dependency)
00179 {
00180 if (dependencyMap.contains(dependency))
00181 {
00182 if (addedByDependencies && dependencyMap[dependency].added)
00183 addedByDependencies--;
00184 else if (removedByDependencies)
00185 removedByDependencies--;
00186
00187 dependencyMap.remove(dependency);
00188 }
00189
00190 updateDetails();
00191 }
00192
00193 void KPluginSelector::Private::DependenciesWidget::clearDependencies()
00194 {
00195 addedByDependencies = 0;
00196 removedByDependencies = 0;
00197 dependencyMap.clear();
00198 updateDetails();
00199 }
00200
00201 void KPluginSelector::Private::DependenciesWidget::showDependencyDetails()
00202 {
00203 QString message = i18n("Automatic changes have been performed in order to satisfy plugin dependencies:\n");
00204 foreach(const QString &dependency, dependencyMap.keys())
00205 {
00206 if (dependencyMap[dependency].added)
00207 message += i18n("\n %1 plugin has been automatically checked because of the dependency of %2 plugin", dependency, dependencyMap[dependency].pluginCausant);
00208 else
00209 message += i18n("\n %1 plugin has been automatically unchecked because of its dependency on %2 plugin", dependency, dependencyMap[dependency].pluginCausant);
00210 }
00211 KMessageBox::information(this, message, i18n("Dependency Check"));
00212
00213 addedByDependencies = 0;
00214 removedByDependencies = 0;
00215 updateDetails();
00216 }
00217
00218 void KPluginSelector::Private::DependenciesWidget::updateDetails()
00219 {
00220 if (!dependencyMap.count())
00221 {
00222 setVisible(false);
00223 return;
00224 }
00225
00226 QString message;
00227
00228 if (addedByDependencies)
00229 message += i18np("%1 plugin automatically added due to plugin dependencies", "%1 plugins automatically added due to plugin dependencies", addedByDependencies);
00230
00231 if (removedByDependencies && !message.isEmpty())
00232 message += i18n(", ");
00233
00234 if (removedByDependencies)
00235 message += i18np("%1 plugin automatically removed due to plugin dependencies", "%1 plugins automatically removed due to plugin dependencies", removedByDependencies);
00236
00237 if (message.isEmpty())
00238 details->setVisible(false);
00239 else
00240 {
00241 details->setVisible(true);
00242 details->setText(message);
00243 }
00244 }
00245
00246
00247 KPluginSelector::KPluginSelector(QWidget *parent)
00248 : QWidget(parent)
00249 , d(new Private(this))
00250 {
00251 QVBoxLayout *layout = new QVBoxLayout;
00252 layout->setMargin(0);
00253 setLayout(layout);
00254
00255 d->lineEdit = new KLineEdit(this);
00256 d->lineEdit->setClearButtonShown(true);
00257 d->lineEdit->setClickMessage(i18n("Search Plugins"));
00258 d->listView = new KCategorizedView(this);
00259 d->listView->setCategoryDrawer(d->categoryDrawer);
00260 d->dependenciesWidget = new Private::DependenciesWidget(this);
00261
00262 d->pluginModel = new Private::PluginModel(d, this);
00263 d->proxyModel = new Private::ProxyModel(d, this);
00264 d->proxyModel->setCategorizedModel(true);
00265 d->proxyModel->setSourceModel(d->pluginModel);
00266 d->listView->setModel(d->proxyModel);
00267 d->listView->setAlternatingRowColors(true);
00268
00269 Private::PluginDelegate *pluginDelegate = new Private::PluginDelegate(d, this);
00270 d->listView->setItemDelegate(pluginDelegate);
00271
00272 d->listView->setMouseTracking(true);
00273 d->listView->viewport()->setAttribute(Qt::WA_Hover);
00274
00275 connect(d->lineEdit, SIGNAL(textChanged(QString)), d->proxyModel, SLOT(invalidate()));
00276 connect(pluginDelegate, SIGNAL(changed(bool)), this, SIGNAL(changed(bool)));
00277 connect(pluginDelegate, SIGNAL(configCommitted(QByteArray)), this, SIGNAL(configCommitted(QByteArray)));
00278
00279 layout->addWidget(d->lineEdit);
00280 layout->addWidget(d->listView);
00281 layout->addWidget(d->dependenciesWidget);
00282 }
00283
00284 KPluginSelector::~KPluginSelector()
00285 {
00286 delete d->listView;
00287 delete d;
00288 }
00289
00290 void KPluginSelector::addPlugins(const QString &componentName,
00291 const QString &categoryName,
00292 const QString &categoryKey,
00293 KSharedConfig::Ptr config)
00294 {
00295 QStringList desktopFileNames = KGlobal::dirs()->findAllResources("data",
00296 componentName + "/kpartplugins/*.desktop", KStandardDirs::Recursive);
00297
00298 QList<KPluginInfo> pluginInfoList = KPluginInfo::fromFiles(desktopFileNames);
00299
00300 if (pluginInfoList.isEmpty())
00301 return;
00302
00303 Q_ASSERT(config);
00304 if (!config)
00305 config = KSharedConfig::openConfig(componentName);
00306
00307 KConfigGroup cfgGroup(config, "KParts Plugins");
00308 kDebug( 702 ) << "cfgGroup = " << &cfgGroup;
00309
00310 d->pluginModel->addPlugins(pluginInfoList, categoryName, categoryKey, cfgGroup);
00311 }
00312
00313 void KPluginSelector::addPlugins(const KComponentData &instance,
00314 const QString &categoryName,
00315 const QString &categoryKey,
00316 const KSharedConfig::Ptr &config)
00317 {
00318 addPlugins(instance.componentName(), categoryName, categoryKey, config);
00319 }
00320
00321 void KPluginSelector::addPlugins(const QList<KPluginInfo> &pluginInfoList,
00322 PluginLoadMethod pluginLoadMethod,
00323 const QString &categoryName,
00324 const QString &categoryKey,
00325 const KSharedConfig::Ptr &config)
00326 {
00327 if (pluginInfoList.isEmpty())
00328 return;
00329
00330 KConfigGroup cfgGroup(config ? config : KGlobal::config(), "Plugins");
00331 kDebug( 702 ) << "cfgGroup = " << &cfgGroup;
00332
00333 d->pluginModel->addPlugins(pluginInfoList, categoryName, categoryKey, cfgGroup, pluginLoadMethod, true );
00334 }
00335
00336 void KPluginSelector::load()
00337 {
00338 for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00339 const QModelIndex index = d->pluginModel->index(i, 0);
00340 PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00341 pluginEntry->pluginInfo.load(pluginEntry->cfgGroup);
00342 d->pluginModel->setData(index, pluginEntry->pluginInfo.isPluginEnabled(), Qt::CheckStateRole);
00343 }
00344
00345 emit changed(false);
00346 }
00347
00348 void KPluginSelector::save()
00349 {
00350 for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00351 const QModelIndex index = d->pluginModel->index(i, 0);
00352 PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00353 pluginEntry->pluginInfo.setPluginEnabled(pluginEntry->checked);
00354 pluginEntry->pluginInfo.save(pluginEntry->cfgGroup);
00355 pluginEntry->cfgGroup.sync();
00356 }
00357
00358 emit changed(false);
00359 }
00360
00361 void KPluginSelector::defaults()
00362 {
00363 for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00364 const QModelIndex index = d->pluginModel->index(i, 0);
00365 PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00366 d->pluginModel->setData(index, pluginEntry->pluginInfo.isPluginEnabledByDefault(), Qt::CheckStateRole);
00367 }
00368
00369 emit changed(true);
00370 }
00371
00372 bool KPluginSelector::isDefault() const
00373 {
00374 for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00375 const QModelIndex index = d->pluginModel->index(i, 0);
00376 PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00377 if (d->pluginModel->data(index, Qt::CheckStateRole).toBool() != pluginEntry->pluginInfo.isPluginEnabledByDefault()) {
00378 return false;
00379 }
00380 }
00381
00382 return true;
00383 }
00384
00385 void KPluginSelector::updatePluginsState()
00386 {
00387 for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00388 const QModelIndex index = d->pluginModel->index(i, 0);
00389 PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00390 if (pluginEntry->manuallyAdded) {
00391 pluginEntry->pluginInfo.setPluginEnabled(pluginEntry->checked);
00392 }
00393 }
00394 }
00395
00396 KPluginSelector::Private::PluginModel::PluginModel(KPluginSelector::Private *pluginSelector_d, QObject *parent)
00397 : QAbstractListModel(parent)
00398 , pluginSelector_d(pluginSelector_d)
00399 {
00400 }
00401
00402 KPluginSelector::Private::PluginModel::~PluginModel()
00403 {
00404 }
00405
00406 void KPluginSelector::Private::PluginModel::addPlugins(const QList<KPluginInfo> &pluginList, const QString &categoryName, const QString &categoryKey, const KConfigGroup &cfgGroup, PluginLoadMethod pluginLoadMethod, bool manuallyAdded)
00407 {
00408 QList<PluginEntry> listToAdd;
00409
00410 foreach (const KPluginInfo &pluginInfo, pluginList) {
00411 PluginEntry pluginEntry;
00412 pluginEntry.category = categoryName;
00413 pluginEntry.pluginInfo = pluginInfo;
00414 if (pluginLoadMethod == ReadConfigFile) {
00415 pluginEntry.pluginInfo.load(cfgGroup);
00416 }
00417 pluginEntry.checked = pluginInfo.isPluginEnabled();
00418 pluginEntry.manuallyAdded = manuallyAdded;
00419 if (cfgGroup.isValid()) {
00420 pluginEntry.cfgGroup = cfgGroup;
00421 } else {
00422 pluginEntry.cfgGroup = pluginInfo.config();
00423 }
00424
00425
00426 pluginEntry.isCheckable = !pluginInfo.isValid() || !pluginEntry.cfgGroup.isEntryImmutable(pluginInfo.pluginName() + QLatin1String("Enabled"));
00427
00428 if (!pluginEntryList.contains(pluginEntry) && !listToAdd.contains(pluginEntry) &&
00429 (!pluginInfo.property("X-KDE-PluginInfo-Category").isValid() ||
00430 !pluginInfo.property("X-KDE-PluginInfo-Category").toString().compare(categoryKey, Qt::CaseInsensitive)) &&
00431 (pluginInfo.service().isNull() || !pluginInfo.service()->noDisplay())) {
00432 listToAdd << pluginEntry;
00433
00434 if (!pluginSelector_d->showIcons && !pluginInfo.icon().isEmpty()) {
00435 pluginSelector_d->showIcons = true;
00436 }
00437 }
00438 }
00439
00440 if (listToAdd.count()) {
00441 beginInsertRows(QModelIndex(), pluginEntryList.count(), pluginEntryList.count() + listToAdd.count() - 1);
00442 pluginEntryList << listToAdd;
00443 endInsertRows();
00444 }
00445 }
00446
00447 QList<KService::Ptr> KPluginSelector::Private::PluginModel::pluginServices(const QModelIndex &index) const
00448 {
00449 return static_cast<PluginEntry*>(index.internalPointer())->pluginInfo.kcmServices();
00450 }
00451
00452 QModelIndex KPluginSelector::Private::PluginModel::index(int row, int column, const QModelIndex &parent) const
00453 {
00454 Q_UNUSED(parent)
00455
00456 return createIndex(row, column, (row < pluginEntryList.count()) ? (void*) &pluginEntryList.at(row)
00457 : 0);
00458 }
00459
00460 QVariant KPluginSelector::Private::PluginModel::data(const QModelIndex &index, int role) const
00461 {
00462 if (!index.isValid() || !index.internalPointer()) {
00463 return QVariant();
00464 }
00465
00466 PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00467
00468 switch (role) {
00469 case Qt::DisplayRole:
00470 return pluginEntry->pluginInfo.name();
00471 case PluginEntryRole:
00472 return QVariant::fromValue(pluginEntry);
00473 case ServicesCountRole:
00474 return pluginEntry->pluginInfo.kcmServices().count();
00475 case NameRole:
00476 return pluginEntry->pluginInfo.name();
00477 case CommentRole:
00478 return pluginEntry->pluginInfo.comment();
00479 case AuthorRole:
00480 return pluginEntry->pluginInfo.author();
00481 case EmailRole:
00482 return pluginEntry->pluginInfo.email();
00483 case WebsiteRole:
00484 return pluginEntry->pluginInfo.website();
00485 case VersionRole:
00486 return pluginEntry->pluginInfo.version();
00487 case LicenseRole:
00488 return pluginEntry->pluginInfo.license();
00489 case DependenciesRole:
00490 return pluginEntry->pluginInfo.dependencies();
00491 case IsCheckableRole:
00492 return pluginEntry->isCheckable;
00493 case Qt::DecorationRole:
00494 return pluginEntry->pluginInfo.icon();
00495 case Qt::CheckStateRole:
00496 return pluginEntry->checked;
00497 case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
00498 case KCategorizedSortFilterProxyModel::CategorySortRole:
00499 return pluginEntry->category;
00500 default:
00501 return QVariant();
00502 }
00503 }
00504
00505 bool KPluginSelector::Private::PluginModel::setData(const QModelIndex &index, const QVariant &value, int role)
00506 {
00507 if (!index.isValid()) {
00508 return false;
00509 }
00510
00511 bool ret = false;
00512
00513 if (role == Qt::CheckStateRole) {
00514 static_cast<PluginEntry*>(index.internalPointer())->checked = value.toBool();
00515 ret = true;
00516 }
00517
00518 if (ret) {
00519 emit dataChanged(index, index);
00520 }
00521
00522 return ret;
00523 }
00524
00525 int KPluginSelector::Private::PluginModel::rowCount(const QModelIndex &parent) const
00526 {
00527 if (parent.isValid()) {
00528 return 0;
00529 }
00530
00531 return pluginEntryList.count();
00532 }
00533
00534 KPluginSelector::Private::ProxyModel::ProxyModel(KPluginSelector::Private *pluginSelector_d, QObject *parent)
00535 : KCategorizedSortFilterProxyModel(parent)
00536 , pluginSelector_d(pluginSelector_d)
00537 {
00538 sort(0);
00539 }
00540
00541 KPluginSelector::Private::ProxyModel::~ProxyModel()
00542 {
00543 }
00544
00545 bool KPluginSelector::Private::ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
00546 {
00547 Q_UNUSED(sourceParent)
00548
00549 if (!pluginSelector_d->lineEdit->text().isEmpty()) {
00550 const QModelIndex index = sourceModel()->index(sourceRow, 0);
00551 const KPluginInfo pluginInfo = static_cast<PluginEntry*>(index.internalPointer())->pluginInfo;
00552 return pluginInfo.name().contains(pluginSelector_d->lineEdit->text(), Qt::CaseInsensitive) ||
00553 pluginInfo.comment().contains(pluginSelector_d->lineEdit->text(), Qt::CaseInsensitive);
00554 }
00555
00556 return true;
00557 }
00558
00559 bool KPluginSelector::Private::ProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
00560 {
00561 return static_cast<PluginEntry*>(left.internalPointer())->pluginInfo.name().compare(static_cast<PluginEntry*>(right.internalPointer())->pluginInfo.name(), Qt::CaseInsensitive) < 0;
00562 }
00563
00564 KPluginSelector::Private::PluginDelegate::PluginDelegate(KPluginSelector::Private *pluginSelector_d, QObject *parent)
00565 : KWidgetItemDelegate(pluginSelector_d->listView, parent)
00566 , checkBox(new QCheckBox)
00567 , pushButton(new KPushButton)
00568 , pluginSelector_d(pluginSelector_d)
00569 {
00570 pushButton->setIcon(KIcon("configure"));
00571 }
00572
00573 KPluginSelector::Private::PluginDelegate::~PluginDelegate()
00574 {
00575 delete checkBox;
00576 delete pushButton;
00577 }
00578
00579 void KPluginSelector::Private::PluginDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
00580 {
00581 if (!index.isValid()) {
00582 return;
00583 }
00584
00585 int xOffset = checkBox->sizeHint().width();
00586 bool disabled = !index.model()->data(index, IsCheckableRole).toBool();
00587
00588 painter->save();
00589
00590 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
00591
00592 int iconSize = option.rect.height() - MARGIN * 2;
00593 if (pluginSelector_d->showIcons) {
00594 QPixmap pixmap = KIconLoader::global()->loadIcon(index.model()->data(index, Qt::DecorationRole).toString(),
00595 KIconLoader::Desktop, iconSize, disabled ? KIconLoader::DisabledState : KIconLoader::DefaultState);
00596
00597 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));
00598 } else {
00599 iconSize = -MARGIN;
00600 }
00601
00602 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);
00603
00604 int lessHorizontalSpace = MARGIN * 2 + pushButton->sizeHint().width();
00605 if (index.model()->data(index, ServicesCountRole).toBool()) {
00606 lessHorizontalSpace += MARGIN + pushButton->sizeHint().width();
00607 }
00608
00609 contentsRect.setWidth(contentsRect.width() - lessHorizontalSpace);
00610
00611 if (option.state & QStyle::State_Selected) {
00612 painter->setPen(option.palette.highlightedText().color());
00613 }
00614
00615 if (pluginSelector_d->listView->layoutDirection() == Qt::RightToLeft) {
00616 contentsRect.translate(lessHorizontalSpace, 0);
00617 }
00618
00619 painter->save();
00620 if (disabled) {
00621 QPalette pal(option.palette);
00622 pal.setCurrentColorGroup(QPalette::Disabled);
00623 painter->setPen(pal.text().color());
00624 }
00625
00626 painter->save();
00627 QFont font = titleFont(option.font);
00628 QFontMetrics fmTitle(font);
00629 painter->setFont(font);
00630 painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignTop, fmTitle.elidedText(index.model()->data(index, Qt::DisplayRole).toString(), Qt::ElideRight, contentsRect.width()));
00631 painter->restore();
00632
00633 painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignBottom, option.fontMetrics.elidedText(index.model()->data(index, CommentRole).toString(), Qt::ElideRight, contentsRect.width()));
00634
00635 painter->restore();
00636 painter->restore();
00637 }
00638
00639 QSize KPluginSelector::Private::PluginDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
00640 {
00641 int i = 5;
00642 int j = 1;
00643 if (index.model()->data(index, ServicesCountRole).toBool()) {
00644 i = 6;
00645 j = 2;
00646 }
00647
00648 if (!pluginSelector_d->showIcons) {
00649 i--;
00650 }
00651
00652 QFont font = titleFont(option.font);
00653 QFontMetrics fmTitle(font);
00654
00655 return QSize(qMax(fmTitle.width(index.model()->data(index, Qt::DisplayRole).toString()),
00656 option.fontMetrics.width(index.model()->data(index, CommentRole).toString())) +
00657 pluginSelector_d->showIcons ? KIconLoader::SizeMedium : 0 + MARGIN * i + pushButton->sizeHint().width() * j,
00658 qMax(KIconLoader::SizeMedium + MARGIN * 2, fmTitle.height() + option.fontMetrics.height() + MARGIN * 2));
00659 }
00660
00661 QList<QWidget*> KPluginSelector::Private::PluginDelegate::createItemWidgets() const
00662 {
00663 QList<QWidget*> widgetList;
00664
00665 QCheckBox *enabledCheckBox = new QCheckBox;
00666 connect(enabledCheckBox, SIGNAL(clicked(bool)), this, SLOT(slotStateChanged(bool)));
00667 connect(enabledCheckBox, SIGNAL(clicked(bool)), this, SLOT(emitChanged()));
00668
00669 KPushButton *aboutPushButton = new KPushButton;
00670 aboutPushButton->setIcon(KIcon("dialog-information"));
00671 connect(aboutPushButton, SIGNAL(clicked(bool)), this, SLOT(slotAboutClicked()));
00672
00673 KPushButton *configurePushButton = new KPushButton;
00674 configurePushButton->setIcon(KIcon("configure"));
00675 connect(configurePushButton, SIGNAL(clicked(bool)), this, SLOT(slotConfigureClicked()));
00676
00677 setBlockedEventTypes(enabledCheckBox, QList<QEvent::Type>() << QEvent::MouseButtonPress
00678 << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick
00679 << QEvent::KeyPress << QEvent::KeyRelease);
00680
00681 setBlockedEventTypes(aboutPushButton, QList<QEvent::Type>() << QEvent::MouseButtonPress
00682 << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick
00683 << QEvent::KeyPress << QEvent::KeyRelease);
00684
00685 setBlockedEventTypes(configurePushButton, QList<QEvent::Type>() << QEvent::MouseButtonPress
00686 << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick
00687 << QEvent::KeyPress << QEvent::KeyRelease);
00688
00689 widgetList << enabledCheckBox << configurePushButton << aboutPushButton;
00690
00691 return widgetList;
00692 }
00693
00694 void KPluginSelector::Private::PluginDelegate::updateItemWidgets(const QList<QWidget*> widgets,
00695 const QStyleOptionViewItem &option,
00696 const QPersistentModelIndex &index) const
00697 {
00698 QCheckBox *checkBox = static_cast<QCheckBox*>(widgets[0]);
00699 checkBox->resize(checkBox->sizeHint());
00700 checkBox->move(pluginSelector_d->dependantLayoutValue(MARGIN, checkBox->sizeHint().width(), option.rect.width()), option.rect.height() / 2 - checkBox->sizeHint().height() / 2);
00701
00702 KPushButton *aboutPushButton = static_cast<KPushButton*>(widgets[2]);
00703 QSize aboutPushButtonSizeHint = aboutPushButton->sizeHint();
00704 aboutPushButton->resize(aboutPushButtonSizeHint);
00705 aboutPushButton->move(pluginSelector_d->dependantLayoutValue(option.rect.width() - MARGIN - aboutPushButtonSizeHint.width(), aboutPushButtonSizeHint.width(), option.rect.width()), option.rect.height() / 2 - aboutPushButtonSizeHint.height() / 2);
00706
00707 KPushButton *configurePushButton = static_cast<KPushButton*>(widgets[1]);
00708 QSize configurePushButtonSizeHint = configurePushButton->sizeHint();
00709 configurePushButton->resize(configurePushButtonSizeHint);
00710 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);
00711
00712 if (!index.isValid() || !index.internalPointer()) {
00713 checkBox->setVisible(false);
00714 aboutPushButton->setVisible(false);
00715 configurePushButton->setVisible(false);
00716 } else {
00717 checkBox->setChecked(index.model()->data(index, Qt::CheckStateRole).toBool());
00718 checkBox->setEnabled(index.model()->data(index, IsCheckableRole).toBool());
00719 configurePushButton->setVisible(index.model()->data(index, ServicesCountRole).toBool());
00720 configurePushButton->setEnabled(index.model()->data(index, Qt::CheckStateRole).toBool());
00721 }
00722 }
00723
00724 void KPluginSelector::Private::PluginDelegate::slotStateChanged(bool state)
00725 {
00726 if (!focusedIndex().isValid())
00727 return;
00728
00729 const QModelIndex index = focusedIndex();
00730
00731 pluginSelector_d->dependenciesWidget->clearDependencies();
00732
00733 PluginEntry *pluginEntry = index.model()->data(index, PluginEntryRole).value<PluginEntry*>();
00734 pluginSelector_d->updateDependencies(pluginEntry, state);
00735
00736 const_cast<QAbstractItemModel*>(index.model())->setData(index, state, Qt::CheckStateRole);
00737 }
00738
00739 void KPluginSelector::Private::PluginDelegate::emitChanged()
00740 {
00741 emit changed(true);
00742 }
00743
00744 void KPluginSelector::Private::PluginDelegate::slotAboutClicked()
00745 {
00746 const QModelIndex index = focusedIndex();
00747 const QAbstractItemModel *model = index.model();
00748
00749
00750
00751
00752
00753 PluginEntry *entry = index.model()->data(index, PluginEntryRole).value<PluginEntry*>();
00754 KService::Ptr entryService = entry->pluginInfo.service();
00755 if (entryService) {
00756 KPluginLoader loader(*entryService);
00757 KPluginFactory *factory = loader.factory();
00758 if (factory) {
00759 const KAboutData *aboutData = factory->componentData().aboutData();
00760 if (!aboutData->programName().isEmpty()) {
00761 KAboutApplicationDialog aboutPlugin(aboutData, itemView());
00762 aboutPlugin.exec();
00763 return;
00764 }
00765 }
00766 }
00767
00768 const QString name = model->data(index, NameRole).toString();
00769 const QString comment = model->data(index, CommentRole).toString();
00770 const QString author = model->data(index, AuthorRole).toString();
00771 const QString email = model->data(index, EmailRole).toString();
00772 const QString website = model->data(index, WebsiteRole).toString();
00773 const QString version = model->data(index, VersionRole).toString();
00774 const QString license = model->data(index, LicenseRole).toString();
00775
00776 KAboutData aboutData(name.toUtf8(), name.toUtf8(), ki18n(name.toUtf8()), version.toUtf8(), ki18n(comment.toUtf8()), KAboutLicense::byKeyword(license).key(), ki18n(QByteArray()), ki18n(QByteArray()), website.toLatin1());
00777 aboutData.setProgramIconName(index.model()->data(index, Qt::DecorationRole).toString());
00778 const QStringList authors = author.split(',');
00779 const QStringList emails = email.split(',');
00780 int i = 0;
00781 if (authors.count() == emails.count()) {
00782 foreach (const QString &author, authors) {
00783 if (!author.isEmpty()) {
00784 aboutData.addAuthor(ki18n(author.toUtf8()), ki18n(QByteArray()), emails[i].toUtf8(), 0);
00785 }
00786 i++;
00787 }
00788 }
00789 KAboutApplicationDialog aboutPlugin(&aboutData, itemView());
00790 aboutPlugin.exec();
00791 }
00792
00793 void KPluginSelector::Private::PluginDelegate::slotConfigureClicked()
00794 {
00795 const QModelIndex index = focusedIndex();
00796 const QAbstractItemModel *model = index.model();
00797
00798 PluginEntry *pluginEntry = model->data(index, PluginEntryRole).value<PluginEntry*>();
00799 KPluginInfo pluginInfo = pluginEntry->pluginInfo;
00800
00801 KDialog configDialog(itemView());
00802 configDialog.setWindowTitle(model->data(index, NameRole).toString());
00803
00804 KTabWidget *newTabWidget = 0;
00805
00806
00807 QWidget * mainWidget = 0;
00808
00809
00810 QWidget *moduleProxyParentWidget = &configDialog;
00811
00812 foreach (const KService::Ptr &servicePtr, pluginInfo.kcmServices()) {
00813 if(!servicePtr->noDisplay()) {
00814 KCModuleInfo moduleInfo(servicePtr);
00815 KCModuleProxy *currentModuleProxy = new KCModuleProxy(moduleInfo, moduleProxyParentWidget);
00816 if (currentModuleProxy->realModule()) {
00817 moduleProxyList << currentModuleProxy;
00818 if (mainWidget && !newTabWidget) {
00819
00820
00821
00822 newTabWidget = new KTabWidget(&configDialog);
00823 moduleProxyParentWidget = newTabWidget;
00824 mainWidget->setParent( newTabWidget );
00825 KCModuleProxy *moduleProxy = qobject_cast<KCModuleProxy*>(mainWidget);
00826 if (moduleProxy) {
00827 newTabWidget->addTab(mainWidget, moduleProxy->moduleInfo().moduleName());
00828 mainWidget = newTabWidget;
00829 } else {
00830 delete newTabWidget;
00831 newTabWidget = 0;
00832 moduleProxyParentWidget = &configDialog;
00833 mainWidget->setParent(0);
00834 }
00835 }
00836
00837 if (newTabWidget) {
00838 newTabWidget->addTab(currentModuleProxy, servicePtr->name());
00839 } else {
00840 mainWidget = currentModuleProxy;
00841 }
00842 } else {
00843 delete currentModuleProxy;
00844 }
00845 }
00846 }
00847
00848
00849 if (moduleProxyList.count()) {
00850 configDialog.setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Default);
00851
00852 QWidget *showWidget = new QWidget(&configDialog);
00853 QVBoxLayout *layout = new QVBoxLayout;
00854 showWidget->setLayout(layout);
00855 layout->addWidget(mainWidget);
00856 layout->insertSpacing(-1, KDialog::marginHint());
00857 configDialog.setMainWidget(showWidget);
00858
00859 connect(&configDialog, SIGNAL(defaultClicked()), this, SLOT(slotDefaultClicked()));
00860
00861 if (configDialog.exec() == QDialog::Accepted) {
00862 foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
00863 QStringList parentComponents = moduleProxy->moduleInfo().service()->property("X-KDE-ParentComponents").toStringList();
00864 moduleProxy->save();
00865 foreach (const QString &parentComponent, parentComponents) {
00866 emit configCommitted(parentComponent.toLatin1());
00867 }
00868 }
00869 } else {
00870 foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
00871 moduleProxy->load();
00872 }
00873 }
00874
00875 qDeleteAll(moduleProxyList);
00876 moduleProxyList.clear();
00877 }
00878 }
00879
00880 void KPluginSelector::Private::PluginDelegate::slotDefaultClicked()
00881 {
00882 foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
00883 moduleProxy->defaults();
00884 }
00885 }
00886
00887 QFont KPluginSelector::Private::PluginDelegate::titleFont(const QFont &baseFont) const
00888 {
00889 QFont retFont(baseFont);
00890 retFont.setBold(true);
00891
00892 return retFont;
00893 }
00894
00895 #include "kpluginselector_p.moc"
00896 #include "kpluginselector.moc"