diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2009-03-24 23:08:41 -0400 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2009-03-24 23:08:41 -0400 |
commit | 56a35e17ed7c704b964ac5ef24f6deee30efbc5a (patch) | |
tree | 6748a603f4642e22f37a1dc2c6e2519bc0591838 | |
parent | 12b460b7c0058315a3bbb1c0e74cf9951a13e1fb (diff) | |
download | sigen-56a35e17ed7c704b964ac5ef24f6deee30efbc5a.tar.gz sigen-56a35e17ed7c704b964ac5ef24f6deee30efbc5a.tar.xz sigen-56a35e17ed7c704b964ac5ef24f6deee30efbc5a.zip |
Add painting algorithms for PluginModel and spacing variables in BaseModel
-rw-r--r-- | sigtools/BaseModel.cpp | 4 | ||||
-rw-r--r-- | sigtools/BaseModel.h | 4 | ||||
-rw-r--r-- | sigtools/PluginModel.cpp | 80 | ||||
-rw-r--r-- | sigtools/PluginModel.h | 10 |
4 files changed, 97 insertions, 1 deletions
diff --git a/sigtools/BaseModel.cpp b/sigtools/BaseModel.cpp index 7d6f58b7..3ddcccd7 100644 --- a/sigtools/BaseModel.cpp +++ b/sigtools/BaseModel.cpp @@ -20,6 +20,10 @@ using namespace Sigtools; +const int BaseModel::borderWidth = 1; +const int BaseModel::vertSpacing = 1; +const int BaseModel::horizSpacing = 1; + BaseModel::BaseModel(BaseModel* parent) : m_parent(parent) { diff --git a/sigtools/BaseModel.h b/sigtools/BaseModel.h index 4d71fd56..53f34aac 100644 --- a/sigtools/BaseModel.h +++ b/sigtools/BaseModel.h @@ -53,6 +53,10 @@ class SIGTOOLS_NO_EXPORT BaseModel : public QObject protected: virtual int findChild(BaseModel* model) const = 0; + static const int borderWidth; + static const int vertSpacing; + static const int horizSpacing; + BaseModel* m_parent; }; } diff --git a/sigtools/PluginModel.cpp b/sigtools/PluginModel.cpp index 087618a9..a6e49526 100644 --- a/sigtools/PluginModel.cpp +++ b/sigtools/PluginModel.cpp @@ -22,8 +22,16 @@ #include "ClassModel.h" #include "PluginTypeModel.h" +// KDE includes +#include <KColorScheme> +#include <KGlobalSettings> +#include <KIcon> +#include <KPluginInfo> + // Qt includes #include <QtCore/QSize> +#include <QtGui/QPainter> +#include <QtGui/QStyleOptionViewItem> using namespace Sigtools; @@ -45,10 +53,16 @@ Qt::ItemFlags PluginModel::flags() const 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) @@ -62,3 +76,69 @@ 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()); +} diff --git a/sigtools/PluginModel.h b/sigtools/PluginModel.h index a1bca8fb..354ca900 100644 --- a/sigtools/PluginModel.h +++ b/sigtools/PluginModel.h @@ -24,9 +24,14 @@ // KDE includes #include <KService> +// Qt includes +#include <QtGui/QPixmap> + +// Forward declarations +class KPluginInfo; + namespace Sigtools { -// Forward declarations class ClassModel; class PluginTypeModel; @@ -47,8 +52,11 @@ class SIGTOOLS_NO_EXPORT PluginModel : public BaseModel BaseModel* childItem(const int row); protected: int findChild(BaseModel* model) const; + void redraw(const int width) const; const KService::Ptr m_service; + const KPluginInfo* m_info; + mutable QPixmap m_pixmap; QList<ClassModel*> m_classes; }; } |