• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KUtils

kpluginselector.cpp

Go to the documentation of this file.
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             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             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)) // The dependency moved from added to removed or vice-versa
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;
00287 }
00288 
00289 void KPluginSelector::addPlugins(const QString &componentName,
00290                                  const QString &categoryName,
00291                                  const QString &categoryKey,
00292                                  KSharedConfig::Ptr config)
00293 {
00294     QStringList desktopFileNames = KGlobal::dirs()->findAllResources("data",
00295         componentName + "/kpartplugins/*.desktop", KStandardDirs::Recursive);
00296 
00297     QList<KPluginInfo> pluginInfoList = KPluginInfo::fromFiles(desktopFileNames);
00298 
00299     if (pluginInfoList.isEmpty())
00300         return;
00301 
00302     Q_ASSERT(config);
00303     if (!config)
00304         config = KSharedConfig::openConfig(componentName);
00305 
00306     KConfigGroup *cfgGroup = new KConfigGroup(config, "KParts Plugins");
00307     kDebug( 702 ) << "cfgGroup = " << cfgGroup;
00308 
00309     d->pluginModel->addPlugins(pluginInfoList, categoryName, categoryKey, *cfgGroup);
00310 }
00311 
00312 void KPluginSelector::addPlugins(const KComponentData &instance,
00313                                  const QString &categoryName,
00314                                  const QString &categoryKey,
00315                                  const KSharedConfig::Ptr &config)
00316 {
00317     addPlugins(instance.componentName(), categoryName, categoryKey, config);
00318 }
00319 
00320 void KPluginSelector::addPlugins(const QList<KPluginInfo> &pluginInfoList,
00321                                  PluginLoadMethod pluginLoadMethod,
00322                                  const QString &categoryName,
00323                                  const QString &categoryKey,
00324                                  const KSharedConfig::Ptr &config)
00325 {
00326     if (pluginInfoList.isEmpty())
00327         return;
00328 
00329     KConfigGroup *cfgGroup = new KConfigGroup(config ? config : KGlobal::config(), "Plugins");
00330     kDebug( 702 ) << "cfgGroup = " << cfgGroup;
00331 
00332     d->pluginModel->addPlugins(pluginInfoList, categoryName, categoryKey, *cfgGroup, pluginLoadMethod, true /* manually added */);
00333 }
00334 
00335 void KPluginSelector::load()
00336 {
00337     for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00338         QModelIndex index = d->pluginModel->index(i, 0);
00339         PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00340         pluginEntry->pluginInfo.load(pluginEntry->cfgGroup);
00341         d->pluginModel->setData(index, pluginEntry->pluginInfo.isPluginEnabled(), Qt::CheckStateRole);
00342     }
00343 
00344     emit changed(false);
00345 }
00346 
00347 void KPluginSelector::save()
00348 {
00349     for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00350         QModelIndex index = d->pluginModel->index(i, 0);
00351         PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00352         pluginEntry->pluginInfo.setPluginEnabled(pluginEntry->checked);
00353         pluginEntry->pluginInfo.save(pluginEntry->cfgGroup);
00354         pluginEntry->cfgGroup.sync();
00355     }
00356 
00357     emit changed(false);
00358 }
00359 
00360 void KPluginSelector::defaults()
00361 {
00362     for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00363         QModelIndex index = d->pluginModel->index(i, 0);
00364         PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00365         d->pluginModel->setData(index, pluginEntry->pluginInfo.isPluginEnabledByDefault(), Qt::CheckStateRole);
00366     }
00367 
00368     emit changed(true);
00369 }
00370 
00371 void KPluginSelector::updatePluginsState()
00372 {
00373     for (int i = 0; i < d->pluginModel->rowCount(); i++) {
00374         QModelIndex index = d->pluginModel->index(i, 0);
00375         PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00376         if (pluginEntry->manuallyAdded) {
00377             pluginEntry->pluginInfo.setPluginEnabled(pluginEntry->checked);
00378         }
00379     }
00380 }
00381 
00382 KPluginSelector::Private::PluginModel::PluginModel(KPluginSelector::Private *pluginSelector_d, QObject *parent)
00383     : QAbstractListModel(parent)
00384     , pluginSelector_d(pluginSelector_d)
00385 {
00386 }
00387 
00388 KPluginSelector::Private::PluginModel::~PluginModel()
00389 {
00390 }
00391 
00392 void KPluginSelector::Private::PluginModel::addPlugins(const QList<KPluginInfo> &pluginList, const QString &categoryName, const QString &categoryKey, const KConfigGroup &cfgGroup, PluginLoadMethod pluginLoadMethod, bool manuallyAdded)
00393 {
00394     QList<PluginEntry> listToAdd;
00395 
00396     foreach (const KPluginInfo &pluginInfo, pluginList) {
00397         PluginEntry pluginEntry;
00398         pluginEntry.category = categoryName;
00399         pluginEntry.pluginInfo = pluginInfo;
00400         if (pluginLoadMethod == ReadConfigFile) {
00401             pluginEntry.pluginInfo.load(cfgGroup);
00402         }
00403         pluginEntry.checked = pluginInfo.isPluginEnabled();
00404         pluginEntry.manuallyAdded = manuallyAdded;
00405         if (cfgGroup.isValid()) {
00406             pluginEntry.cfgGroup = cfgGroup;
00407         } else {
00408             pluginEntry.cfgGroup = pluginInfo.config();
00409         }
00410 
00411         if (!pluginEntryList.contains(pluginEntry) && !listToAdd.contains(pluginEntry) &&
00412              (!pluginInfo.property("X-KDE-PluginInfo-Category").isValid() ||
00413               !pluginInfo.property("X-KDE-PluginInfo-Category").toString().compare(categoryKey, Qt::CaseInsensitive))) {
00414             listToAdd << pluginEntry;
00415 
00416             if (!pluginSelector_d->showIcons && !pluginInfo.icon().isEmpty()) {
00417                 pluginSelector_d->showIcons = true;
00418             }
00419         }
00420     }
00421 
00422     if (listToAdd.count()) {
00423         beginInsertRows(QModelIndex(), pluginEntryList.count(), pluginEntryList.count() + listToAdd.count() - 1);
00424         pluginEntryList << listToAdd;
00425         endInsertRows();
00426     }
00427 }
00428 
00429 QList<KService::Ptr> KPluginSelector::Private::PluginModel::pluginServices(const QModelIndex &index) const
00430 {
00431     return static_cast<PluginEntry*>(index.internalPointer())->pluginInfo.kcmServices();
00432 }
00433 
00434 QModelIndex KPluginSelector::Private::PluginModel::index(int row, int column, const QModelIndex &parent) const
00435 {
00436     Q_UNUSED(parent)
00437 
00438     return createIndex(row, column, (row < pluginEntryList.count()) ? (void*) &pluginEntryList.at(row)
00439                                                                     : 0);
00440 }
00441 
00442 QVariant KPluginSelector::Private::PluginModel::data(const QModelIndex &index, int role) const
00443 {
00444     if (!index.isValid() || !index.internalPointer()) {
00445         return QVariant();
00446     }
00447 
00448     PluginEntry *pluginEntry = static_cast<PluginEntry*>(index.internalPointer());
00449 
00450     switch (role) {
00451         case Qt::DisplayRole:
00452             return pluginEntry->pluginInfo.name();
00453         case PluginEntryRole:
00454             return QVariant::fromValue(pluginEntry);
00455         case ServicesCountRole:
00456             return pluginEntry->pluginInfo.kcmServices().count();
00457         case NameRole:
00458             return pluginEntry->pluginInfo.name();
00459         case CommentRole:
00460             return pluginEntry->pluginInfo.comment();
00461         case AuthorRole:
00462             return pluginEntry->pluginInfo.author();
00463         case EmailRole:
00464             return pluginEntry->pluginInfo.email();
00465         case WebsiteRole:
00466             return pluginEntry->pluginInfo.website();
00467         case VersionRole:
00468             return pluginEntry->pluginInfo.version();
00469         case LicenseRole:
00470             return pluginEntry->pluginInfo.license();
00471         case DependenciesRole:
00472             return pluginEntry->pluginInfo.dependencies();
00473         case Qt::DecorationRole:
00474             return pluginEntry->pluginInfo.icon();
00475         case Qt::CheckStateRole:
00476             return pluginEntry->checked;
00477         case KCategorizedSortFilterProxyModel::CategoryDisplayRole: // fall through
00478         case KCategorizedSortFilterProxyModel::CategorySortRole:
00479             return pluginEntry->category;
00480         default:
00481             return QVariant();
00482     }
00483 }
00484 
00485 bool KPluginSelector::Private::PluginModel::setData(const QModelIndex &index, const QVariant &value, int role)
00486 {
00487     if (!index.isValid()) {
00488         return false;
00489     }
00490 
00491     bool ret = false;
00492 
00493     if (role == Qt::CheckStateRole) {
00494         static_cast<PluginEntry*>(index.internalPointer())->checked = value.toBool();
00495         ret = true;
00496     }
00497 
00498     if (ret) {
00499         emit dataChanged(index, index);
00500     }
00501 
00502     return ret;
00503 }
00504 
00505 int KPluginSelector::Private::PluginModel::rowCount(const QModelIndex &parent) const
00506 {
00507     if (parent.isValid()) {
00508         return 0;
00509     }
00510 
00511     return pluginEntryList.count();
00512 }
00513 
00514 KPluginSelector::Private::ProxyModel::ProxyModel(KPluginSelector::Private *pluginSelector_d, QObject *parent)
00515     : KCategorizedSortFilterProxyModel(parent)
00516     , pluginSelector_d(pluginSelector_d)
00517 {
00518     sort(0);
00519 }
00520 
00521 KPluginSelector::Private::ProxyModel::~ProxyModel()
00522 {
00523 }
00524 
00525 bool KPluginSelector::Private::ProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
00526 {
00527     Q_UNUSED(sourceParent)
00528 
00529     if (!pluginSelector_d->lineEdit->text().isEmpty()) {
00530         const QModelIndex index = sourceModel()->index(sourceRow, 0);
00531         const KPluginInfo pluginInfo = static_cast<PluginEntry*>(index.internalPointer())->pluginInfo;
00532         return pluginInfo.name().contains(pluginSelector_d->lineEdit->text(), Qt::CaseInsensitive) ||
00533                pluginInfo.comment().contains(pluginSelector_d->lineEdit->text(), Qt::CaseInsensitive);
00534     }
00535 
00536     return true;
00537 }
00538 
00539 bool KPluginSelector::Private::ProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
00540 {
00541     return static_cast<PluginEntry*>(left.internalPointer())->pluginInfo.name().compare(static_cast<PluginEntry*>(right.internalPointer())->pluginInfo.name(), Qt::CaseInsensitive) < 0;
00542 }
00543 
00544 KPluginSelector::Private::PluginDelegate::PluginDelegate(KPluginSelector::Private *pluginSelector_d, QObject *parent)
00545     : KWidgetItemDelegate(pluginSelector_d->listView, parent)
00546     , checkBox(new QCheckBox)
00547     , pushButton(new KPushButton)
00548     , pluginSelector_d(pluginSelector_d)
00549 {
00550     pushButton->setIcon(KIcon("configure")); // only for getting size matters
00551 }
00552 
00553 KPluginSelector::Private::PluginDelegate::~PluginDelegate()
00554 {
00555     delete checkBox;
00556     delete pushButton;
00557 }
00558 
00559 void KPluginSelector::Private::PluginDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
00560 {
00561     if (!index.isValid()) {
00562         return;
00563     }
00564 
00565     int xOffset = checkBox->sizeHint().width();
00566 
00567     painter->save();
00568 
00569     QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
00570 
00571     int iconSize = option.rect.height() - MARGIN * 2;
00572     if (pluginSelector_d->showIcons) {
00573         QPixmap pixmap = KIconLoader::global()->loadIcon(index.model()->data(index, Qt::DecorationRole).toString(),
00574                                                          KIconLoader::Desktop, iconSize);
00575 
00576         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));
00577     } else {
00578         iconSize = -MARGIN;
00579     }
00580 
00581     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);
00582 
00583     int lessHorizontalSpace = MARGIN * 2 + pushButton->sizeHint().width();
00584     if (index.model()->data(index, ServicesCountRole).toBool()) {
00585         lessHorizontalSpace += MARGIN + pushButton->sizeHint().width();
00586     }
00587 
00588     contentsRect.setWidth(contentsRect.width() - lessHorizontalSpace);
00589 
00590     if (option.state & QStyle::State_Selected) {
00591         painter->setPen(option.palette.highlightedText().color());
00592     }
00593 
00594     if (pluginSelector_d->listView->layoutDirection() == Qt::RightToLeft) {
00595         contentsRect.translate(lessHorizontalSpace, 0);
00596     }
00597 
00598     painter->save();
00599     QFont font = titleFont(option.font);
00600     QFontMetrics fmTitle(font);
00601     painter->setFont(font);
00602     painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignTop, fmTitle.elidedText(index.model()->data(index, Qt::DisplayRole).toString(), Qt::ElideRight, contentsRect.width()));
00603     painter->restore();
00604 
00605     painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignBottom, option.fontMetrics.elidedText(index.model()->data(index, CommentRole).toString(), Qt::ElideRight, contentsRect.width()));
00606 
00607     painter->restore();
00608 
00609     KWidgetItemDelegate::paintWidgets(painter, option, index);
00610 }
00611 
00612 QSize KPluginSelector::Private::PluginDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
00613 {
00614     int i = 5;
00615     int j = 1;
00616     if (index.model()->data(index, ServicesCountRole).toBool()) {
00617         i = 6;
00618         j = 2;
00619     }
00620 
00621     if (!pluginSelector_d->showIcons) {
00622         i--;
00623     }
00624 
00625     QFont font = titleFont(option.font);
00626     QFontMetrics fmTitle(font);
00627 
00628     return QSize(qMax(fmTitle.width(index.model()->data(index, Qt::DisplayRole).toString()),
00629                       option.fontMetrics.width(index.model()->data(index, CommentRole).toString())) +
00630                       pluginSelector_d->showIcons ? KIconLoader::SizeMedium : 0 + MARGIN * i + pushButton->sizeHint().width() * j,
00631                  qMax(KIconLoader::SizeMedium + MARGIN * 2, fmTitle.height() + option.fontMetrics.height() + MARGIN * 2));
00632 }
00633 
00634 QList<QWidget*> KPluginSelector::Private::PluginDelegate::createItemWidgets() const
00635 {
00636     QList<QWidget*> widgetList;
00637 
00638     QCheckBox *enabledCheckBox = new QCheckBox;
00639     connect(enabledCheckBox, SIGNAL(clicked(bool)), this, SLOT(slotStateChanged(bool)));
00640     connect(enabledCheckBox, SIGNAL(clicked(bool)), this, SLOT(emitChanged()));
00641 
00642     KPushButton *aboutPushButton = new KPushButton;
00643     aboutPushButton->setIcon(KIcon("dialog-information"));
00644     connect(aboutPushButton, SIGNAL(clicked(bool)), this, SLOT(slotAboutClicked()));
00645 
00646     KPushButton *configurePushButton = new KPushButton;
00647     configurePushButton->setIcon(KIcon("configure"));
00648     connect(configurePushButton, SIGNAL(clicked(bool)), this, SLOT(slotConfigureClicked()));
00649 
00650     setBlockedEventTypes(enabledCheckBox, QList<QEvent::Type>() << QEvent::MouseButtonPress
00651                             << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick);
00652 
00653     setBlockedEventTypes(aboutPushButton, QList<QEvent::Type>() << QEvent::MouseButtonPress
00654                             << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick);
00655 
00656     setBlockedEventTypes(configurePushButton, QList<QEvent::Type>() << QEvent::MouseButtonPress
00657                             << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick);
00658 
00659     widgetList << enabledCheckBox << configurePushButton << aboutPushButton;
00660 
00661     return widgetList;
00662 }
00663 
00664 void KPluginSelector::Private::PluginDelegate::updateItemWidgets(const QList<QWidget*> widgets,
00665                                                                  const QStyleOptionViewItem &option,
00666                                                                  const QPersistentModelIndex &index) const
00667 {
00668     QCheckBox *checkBox = static_cast<QCheckBox*>(widgets[0]);
00669     checkBox->resize(checkBox->sizeHint());
00670     checkBox->move(pluginSelector_d->dependantLayoutValue(MARGIN, checkBox->sizeHint().width(), option.rect.width()), option.rect.height() / 2 - checkBox->sizeHint().height() / 2);
00671 
00672     KPushButton *aboutPushButton = static_cast<KPushButton*>(widgets[2]);
00673     QSize aboutPushButtonSizeHint = aboutPushButton->sizeHint();
00674     aboutPushButton->resize(aboutPushButtonSizeHint);
00675     aboutPushButton->move(pluginSelector_d->dependantLayoutValue(option.rect.width() - MARGIN - aboutPushButtonSizeHint.width(), aboutPushButtonSizeHint.width(), option.rect.width()), option.rect.height() / 2 - aboutPushButtonSizeHint.height() / 2);
00676 
00677     KPushButton *configurePushButton = static_cast<KPushButton*>(widgets[1]);
00678     QSize configurePushButtonSizeHint = configurePushButton->sizeHint();
00679     configurePushButton->resize(configurePushButtonSizeHint);
00680     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);
00681 
00682     if (!index.isValid() || !index.internalPointer()) {
00683         checkBox->setVisible(false);
00684         aboutPushButton->setVisible(false);
00685         configurePushButton->setVisible(false);
00686     } else {
00687         checkBox->setChecked(index.model()->data(index, Qt::CheckStateRole).toBool());
00688         configurePushButton->setVisible(index.model()->data(index, ServicesCountRole).toBool());
00689         configurePushButton->setEnabled(index.model()->data(index, Qt::CheckStateRole).toBool());
00690     }
00691 }
00692 
00693 void KPluginSelector::Private::PluginDelegate::slotStateChanged(bool state)
00694 {
00695     if (!focusedIndex().isValid())
00696         return;
00697 
00698     const QModelIndex index = focusedIndex();
00699 
00700     pluginSelector_d->dependenciesWidget->clearDependencies();
00701 
00702     PluginEntry *pluginEntry = index.model()->data(index, PluginEntryRole).value<PluginEntry*>();
00703     pluginSelector_d->updateDependencies(pluginEntry, state);
00704 
00705     const_cast<QAbstractItemModel*>(index.model())->setData(index, state, Qt::CheckStateRole);
00706 }
00707 
00708 void KPluginSelector::Private::PluginDelegate::emitChanged()
00709 {
00710     emit changed(true);
00711 }
00712 
00713 void KPluginSelector::Private::PluginDelegate::slotAboutClicked()
00714 {
00715     const QModelIndex index = focusedIndex();
00716     const QAbstractItemModel *model = index.model();
00717 
00718     // Try to retrieve the plugin information from the KComponentData object of the plugin.
00719     // If there is no valid information, go and fetch it from the service itself (the .desktop
00720     // file).
00721 
00722     PluginEntry *entry = index.model()->data(index, PluginEntryRole).value<PluginEntry*>();
00723     KService::Ptr entryService = entry->pluginInfo.service();
00724     if (entryService) {
00725         KPluginLoader loader(*entryService);
00726         KPluginFactory *factory = loader.factory();
00727         if (factory) {
00728             const KAboutData *aboutData = factory->componentData().aboutData();
00729             if (!aboutData->programName().isEmpty()) { // Be sure the about data is not completely empty
00730                 KAboutApplicationDialog aboutPlugin(aboutData, itemView());
00731                 aboutPlugin.exec();
00732                 return;
00733             }
00734         }
00735     }
00736 
00737     const QString name = model->data(index, NameRole).toString();
00738     const QString comment = model->data(index, CommentRole).toString();
00739     const QString author = model->data(index, AuthorRole).toString();
00740     const QString email = model->data(index, EmailRole).toString();
00741     const QString website = model->data(index, WebsiteRole).toString();
00742     const QString version = model->data(index, VersionRole).toString();
00743     const QString license = model->data(index, LicenseRole).toString();
00744 
00745     KAboutData aboutData(name.toUtf8(), name.toUtf8(), ki18n(name.toUtf8()), version.toUtf8(), ki18n(comment.toUtf8()), KAboutLicense::byKeyword(license).key(), ki18n(QByteArray()), ki18n(QByteArray()), website.toLatin1());
00746     aboutData.setProgramIconName(index.model()->data(index, Qt::DecorationRole).toString());
00747     const QStringList authors = author.split(',');
00748     const QStringList emails = email.split(',');
00749     int i = 0;
00750     if (authors.count() == emails.count()) {
00751         foreach (const QString &author, authors) {
00752             if (!author.isEmpty()) {
00753                 aboutData.addAuthor(ki18n(author.toUtf8()), ki18n(QByteArray()), emails[i].toUtf8(), 0);
00754             }
00755             i++;
00756         }
00757     }
00758     KAboutApplicationDialog aboutPlugin(&aboutData, itemView());
00759     aboutPlugin.exec();
00760 }
00761 
00762 void KPluginSelector::Private::PluginDelegate::slotConfigureClicked()
00763 {
00764     const QModelIndex index = focusedIndex();
00765     const QAbstractItemModel *model = index.model();
00766 
00767     PluginEntry *pluginEntry = model->data(index, PluginEntryRole).value<PluginEntry*>();
00768     KPluginInfo pluginInfo = pluginEntry->pluginInfo;
00769 
00770     KDialog configDialog(itemView());
00771     configDialog.setWindowTitle(model->data(index, NameRole).toString());
00772     // The number of KCModuleProxies in use determines whether to use a tabwidget
00773     KTabWidget *newTabWidget = 0;
00774     // Widget to use for the setting dialog's main widget,
00775     // either a KTabWidget or a KCModuleProxy
00776     QWidget * mainWidget = 0;
00777     // Widget to use as the KCModuleProxy's parent.
00778     // The first proxy is owned by the dialog itself
00779     QWidget *moduleProxyParentWidget = &configDialog;
00780 
00781     foreach (const KService::Ptr &servicePtr, pluginInfo.kcmServices()) {
00782         if(!servicePtr->noDisplay()) {
00783             KCModuleInfo moduleInfo(servicePtr);
00784             KCModuleProxy *currentModuleProxy = new KCModuleProxy(moduleInfo, moduleProxyParentWidget);
00785             if (currentModuleProxy->realModule()) {
00786                 moduleProxyList << currentModuleProxy;
00787                 if (mainWidget && !newTabWidget) {
00788                     // we already created one KCModuleProxy, so we need a tab widget.
00789                     // Move the first proxy into the tab widget and ensure this and subsequent
00790                     // proxies are in the tab widget
00791                     newTabWidget = new KTabWidget(&configDialog);
00792                     moduleProxyParentWidget = newTabWidget;
00793                     mainWidget->setParent( newTabWidget );
00794                     KCModuleProxy *moduleProxy = qobject_cast<KCModuleProxy*>(mainWidget);
00795                     if (moduleProxy) {
00796                         newTabWidget->addTab(mainWidget, moduleProxy->moduleInfo().moduleName());
00797                         mainWidget = newTabWidget;
00798                     } else {
00799                         delete newTabWidget;
00800                         newTabWidget = 0;
00801                         moduleProxyParentWidget = &configDialog;
00802                         mainWidget->setParent(0);
00803                     }
00804                 }
00805 
00806                 if (newTabWidget) {
00807                     newTabWidget->addTab(currentModuleProxy, servicePtr->name());
00808                 } else {
00809                     mainWidget = currentModuleProxy;
00810                 }
00811             } else {
00812                 delete currentModuleProxy;
00813             }
00814         }
00815     }
00816 
00817     // it could happen that we had services to show, but none of them were real modules.
00818     if (moduleProxyList.count()) {
00819         configDialog.setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Default);
00820 
00821         QWidget *showWidget = new QWidget(&configDialog);
00822         QVBoxLayout *layout = new QVBoxLayout;
00823         showWidget->setLayout(layout);
00824         layout->addWidget(mainWidget);
00825         layout->insertSpacing(-1, KDialog::marginHint());
00826         configDialog.setMainWidget(showWidget);
00827 
00828         connect(&configDialog, SIGNAL(defaultClicked()), this, SLOT(slotDefaultClicked()));
00829 
00830         if (configDialog.exec() == QDialog::Accepted) {
00831             foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
00832                 QStringList parentComponents = moduleProxy->moduleInfo().service()->property("X-KDE-ParentComponents").toStringList();
00833                 moduleProxy->save();
00834                 foreach (const QString &parentComponent, parentComponents) {
00835                     emit configCommitted(parentComponent.toLatin1());
00836                 }
00837             }
00838         } else {
00839             foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
00840                 moduleProxy->load();
00841             }
00842         }
00843 
00844         qDeleteAll(moduleProxyList);
00845         moduleProxyList.clear();
00846     }
00847 }
00848 
00849 void KPluginSelector::Private::PluginDelegate::slotDefaultClicked()
00850 {
00851     foreach (KCModuleProxy *moduleProxy, moduleProxyList) {
00852         moduleProxy->defaults();
00853     }
00854 }
00855 
00856 QFont KPluginSelector::Private::PluginDelegate::titleFont(const QFont &baseFont) const
00857 {
00858     QFont retFont(baseFont);
00859     retFont.setBold(true);
00860 
00861     return retFont;
00862 }
00863 
00864 #include "kpluginselector_p.moc"
00865 #include "kpluginselector.moc"

KUtils

Skip menu "KUtils"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal