summaryrefslogtreecommitdiffstats
path: root/sigtools/PluginModel.cpp
blob: 34d283aef1c666f70540d56062e24750f8a21917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2009 Ben Boeckel <MathStuf@gmail.com>
 * 
 * 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 <http://www.gnu.org/licenses/>.
 */

// Header include
#include "PluginModel.h"

// Sigtools includes
#include "ClassModel.h"
#include "PluginTypeModel.h"

// Sigencore includes
#include <sigencore/plugins/ArenaPlugin.h>
#include <sigencore/plugins/CanvasPlugin.h>

// KDE includes
#include <KColorScheme>
#include <KGlobalSettings>
#include <KIcon>
#include <KMessageBox>
#include <KPluginInfo>

// Qt includes
#include <QtCore/QSize>
#include <QtGui/QPainter>
#include <QtGui/QStyleOptionViewItem>

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<ArenaPlugin>(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<CanvasPlugin>(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;
}

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<ClassModel*>(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());
}