From c85460780f94a01fb885f237efdf8f78e4202ec1 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 26 Mar 2009 07:04:03 -0400 Subject: Drawing routines (adapted from KPluginSelector) --- sigtools/PluginTreeDelegate.cpp | 94 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) (limited to 'sigtools/PluginTreeDelegate.cpp') diff --git a/sigtools/PluginTreeDelegate.cpp b/sigtools/PluginTreeDelegate.cpp index bf039bc8..41216b02 100644 --- a/sigtools/PluginTreeDelegate.cpp +++ b/sigtools/PluginTreeDelegate.cpp @@ -15,38 +15,130 @@ * with this program. If not, see . */ +/* + * Drawing code adapted from KPluginSelector (2009-03-26) + * + * Copyright (C) 2007, 2006 Rafael Fernández López + * Copyright (C) 2002-2003 Matthias Kretz + */ + // Header include #include "PluginTreeDelegate.h" // Sigtools includes +#include "PluginLoader.h" #include "PluginTree.h" // KDE includes +#include +#include #include +#include +#include +#include +#include + +// Qt includes +#include + +static const int margins = 3; using namespace Sigtools; PluginTreeDelegate::PluginTreeDelegate(KCategorizedView* view, PluginTree* tree) : - KWidgetItemDelegate(view, tree) + KWidgetItemDelegate(view, tree), + m_about(new KPushButton) +{ + m_about->setIcon(KIcon("dialog-information")); +} + +PluginTreeDelegate::~PluginTreeDelegate() { + delete m_about; } void PluginTreeDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { + if (!index.isValid()) + return; + + KApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0); + + int iconSize = option.rect.height() - 2 * margins; + painter->drawPixmap(QRect(margins + option.rect.left(), margins + option.rect.top(), iconSize, iconSize), KIconLoader::global()->loadIcon(index.model()->data(index, Qt::DecorationRole).toString(), KIconLoader::Desktop, iconSize)); + + QRect contentsRect(2 * margins + iconSize + option.rect.left(), margins + option.rect.top(), option.rect.width() - 5 * margins - iconSize - m_about->sizeHint().width(), option.rect.height() - 2 * margins); + + painter->save(); + + if (option.state & QStyle::State_Selected) + painter->setPen(option.palette.highlightedText().color()); + + QFont font = option.font; + font.setBold(true); + QFontMetrics fmTitle(font); + painter->setFont(font); + painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignTop, fmTitle.elidedText(index.model()->data(index, Qt::DisplayRole).toString(), Qt::ElideRight, contentsRect.width())); + + painter->setFont(option.font); + painter->drawText(contentsRect, Qt::AlignLeft | Qt::AlignBottom, option.fontMetrics.elidedText(index.model()->data(index, Qt::UserRole).toString(), Qt::ElideRight, contentsRect.width())); + + painter->restore(); } QSize PluginTreeDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { + QFont font = option.font; + font.setBold(true); + QFontMetrics fmTitle(font); + const QSize nameSize = fmTitle.size(0, index.model()->data(index, Qt::DisplayRole).toString()); + const QSize descSize = option.fontMetrics.size(0, index.model()->data(index, Qt::UserRole).toString()); + + return QSize(qMax(nameSize.width(), descSize.width()) + KIconLoader::SizeMedium, qMax(int(KIconLoader::SizeMedium), nameSize.height() + descSize.height()) + 2 * margins); } QList PluginTreeDelegate::createItemWidgets() const { + QList widgetList; + KPushButton *aboutPushButton = new KPushButton; + aboutPushButton->setIcon(KIcon("dialog-information")); + connect(aboutPushButton, SIGNAL(clicked(bool)), this, SLOT(aboutClicked())); + + setBlockedEventTypes(aboutPushButton, QList() << QEvent::MouseButtonPress << QEvent::MouseButtonRelease << QEvent::MouseButtonDblClick); + + widgetList << aboutPushButton; + return widgetList; } void PluginTreeDelegate::updateItemWidgets(const QList widgets, const QStyleOptionViewItem& option, const QPersistentModelIndex& index) const { + Q_UNUSED(index) + QSize aboutSize = widgets[0]->sizeHint(); + widgets[0]->resize(aboutSize); + widgets[0]->move(option.rect.width() - margins - aboutSize.width(), (option.rect.height() - aboutSize.height()) / 2); } void PluginTreeDelegate::aboutClicked() { + const QModelIndex index = focusedIndex(); + const QAbstractItemModel* model = index.model(); + const QString type = model->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); + const QString cname = model->data(index, Qt::DisplayRole).toString(); + KService::Ptr service = PluginLoader::service(type, cname); + KPluginInfo info(service); + const QString name = info.name(); + const QStringList authors = info.author().split(','); + const QStringList emails = info.email().split(','); + KAboutData aboutData(name.toUtf8(), name.toUtf8(), ki18n(name.toUtf8()), info.version().toUtf8(), ki18n(info.comment().toUtf8()), KAboutLicense::byKeyword(info.license()).key(), ki18n(QByteArray()), ki18n(QByteArray()), info.website().toLatin1()); + aboutData.setProgramIconName(info.icon()); + if (authors.count() == emails.count()) + { + for (int i = 0; i < authors.count(); ++i) + { + if (!authors[i].isEmpty()) + aboutData.addAuthor(ki18n(authors[i].toUtf8()), ki18n(QByteArray()), emails[i].toUtf8(), 0); + } + } + KAboutApplicationDialog aboutPlugin(&aboutData, itemView()); + aboutPlugin.exec(); } -- cgit