/* * 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" // Sigencore includes #include #include // KDE includes #include #include #include #include #include // Qt includes #include using namespace Sigencore::Interfaces; using namespace Sigtools; PluginModel::PluginModel(KService::Ptr service, PluginTypeModel* parent) : BaseModel(parent), m_service(service), m_info(new KPluginInfo(m_service)) { KPluginLoader loader(m_service->library()); KPluginFactory *factory = loader.factory(); if (!factory) { KMessageBox::error(NULL, QString("The plugin of type \"%1\" with name \"%2\" is not a valid Sigen plugin. The error was:\n%3").arg(m_service->serviceTypes()[0]).arg(m_service->name()).arg(loader.errorString()), "Plugin loading error"); return; } const QStringList types = m_service->serviceTypes(); foreach (const QString& type, types) { if (type == "Sigen/Arena") { ArenaPlugin* plugin = factory->create(this); if (plugin) { QStringList arenas = plugin->arenas(); foreach (const QString& arena, arenas) m_classes.append(new ClassModel(plugin->icon(arena), arena, plugin->description(arena), this)); } } else if (type == "Sigen/Canvas") { CanvasPlugin* plugin = factory->create(this); if (plugin) { QStringList canvases = plugin->canvases(); foreach (const QString& canvas, canvases) m_classes.append(new ClassModel(plugin->icon(canvas), canvas, plugin->description(canvas), this)); } } else KMessageBox::information(NULL, QString("The plugin type \"%1\" is not supported.").arg(m_service->type()), "Unsupported plugin type"); } } int PluginModel::rowCount() const { return m_classes.size(); } Qt::ItemFlags PluginModel::flags() const { return Qt::ItemIsEnabled; } 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); painter.drawPixmap(w, h, KIcon(m_info->icon()).pixmap(row1height, row1height)); 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()); }