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