/* * Copyright 2009 Ben Boeckel * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ // Header include #include "PluginModel.h" // Sigtools includes #include "ClassModel.h" #include "PluginTypeModel.h" // KDE includes #include #include #include #include // Qt includes #include #include #include using namespace Sigtools; PluginModel::PluginModel(KService::Ptr service, PluginTypeModel* parent) : BaseModel(parent), m_service(service) { } int PluginModel::rowCount() const { return m_classes.size(); } Qt::ItemFlags PluginModel::flags() const { return Qt::ItemIsEnabled; } void PluginModel::paint(QPainter* painter, const QStyleOptionViewItem& option) const { if (option.rect.width() != m_pixmap.width()) redraw(option.rect.width()); painter->drawPixmap(option.rect.topLeft(), m_pixmap); } QSize PluginModel::sizeHint(const QStyleOptionViewItem& option) const { if (option.rect.width() != m_pixmap.width()) redraw(option.rect.width()); return m_pixmap.size(); } BaseModel* PluginModel::childItem(const int row) { if ((0 <= row) && (row < m_classes.size())) return m_classes[row]; return NULL; } int PluginModel::findChild(BaseModel* model) const { return m_classes.indexOf(qobject_cast(model)); } void PluginModel::redraw(const int width) const { QFont regFont = KGlobalSettings::generalFont(); QFontMetrics regMetrics(regFont); QFont smallFont = KGlobalSettings::smallestReadableFont(); smallFont.setItalic(true); QFontMetrics smallMetrics(smallFont); QSize nameSize = regMetrics.size(0, m_info->name()); QSize authorSize = smallMetrics.size(0, QString("by %1").arg(m_info->author())); QSize versionSize = regMetrics.size(0, m_info->version()); int row1width; int row1height; row1height = qMax(nameSize.height(), qMax(authorSize.height(), versionSize.height())); row1width = row1height + nameSize.width() + authorSize.width() + versionSize.width(); QSize websiteSize = smallMetrics.size(0, m_info->website()); int maxWidth = qMax(qMax(row1width + 4 * horizSpacing, websiteSize.width()), width - 2 * borderWidth); QSize commentSize = smallMetrics.boundingRect(QRect(0, 0, maxWidth, 0), Qt::TextWordWrap, m_info->comment()).size(); int maxHeight = row1height + websiteSize.height() + commentSize.height() + 2 * vertSpacing; int h = borderWidth; int w = borderWidth; m_pixmap = QPixmap(maxWidth + 2 * borderWidth, maxHeight + 2 * borderWidth); m_pixmap.fill(KStatefulBrush(KColorScheme::View, KColorScheme::NormalBackground).brush(QPalette::Normal).color()); QPainter painter(&m_pixmap); QPixmap icon = KIcon(m_info->icon()).pixmap(row1height, row1height); painter.drawPixmap(w, h, icon); w += row1height; w += horizSpacing; painter.drawText(QRect(QPoint(w, h), QSize(row1width - w, row1height)), Qt::AlignBottom | Qt::AlignLeft, m_info->name()); w += nameSize.width(); w += 2 * horizSpacing; painter.setFont(smallFont); painter.drawText(QRect(QPoint(w, h), QSize(row1width - w, row1height)), Qt::AlignBottom | Qt::AlignLeft, QString("by %1").arg(m_info->author())); w += authorSize.width(); w += horizSpacing; painter.setFont(regFont); painter.drawText(QRect(QPoint(w, h), QSize(maxWidth - w, row1height)), Qt::AlignBottom | Qt::AlignRight, m_info->version()); h += row1height; h += vertSpacing; w = borderWidth; painter.setFont(smallFont); painter.drawText(QRect(QPoint(w, h), websiteSize), Qt::AlignBottom | Qt::AlignLeft, m_info->website()); h += websiteSize.height(); h += vertSpacing; painter.drawText(QRect(QPoint(w, h), commentSize), Qt::AlignTop | Qt::AlignLeft | Qt::TextWordWrap, m_info->comment()); }