[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: kdebase/workspace/kicker/kicker/ui/appletitemdelegate
From: "=?UTF-8?Q?Rafael_Fern=C3=A1ndez_L=C3=B3pez?=" <ereslibre () gmail ! com>
Date: 2006-12-30 11:30:49
Message-ID: 93f85fee0612300330r6383386bke0cf0b64d225dc83 () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
[Attachment #4 (text/plain)]
Hi again,
This is the patch with the improvements Aaron suggested ;)
Yeah use it wherever you want. If you need some change on the behaviour of
the delegate or some improvement in it, just tell me and I can spend some
time on it.
Bye,
Rafael Fernández López.
[Attachment #5 (text/html)]
Hi again,<br><br>This is the patch with the improvements Aaron suggested \
;)<br><br>Yeah use it wherever you want. If you need some change on the behaviour of \
the delegate or some improvement in it, just tell me and I can spend some time on it. \
<br><br>Bye,<br>Rafael Fernández López.<br>
["patch.diff" (text/plain)]
Index: workspace/kicker/kicker/ui/appletitemdelegate.cpp
===================================================================
--- workspace/kicker/kicker/ui/appletitemdelegate.cpp (revisión: 617737)
+++ workspace/kicker/kicker/ui/appletitemdelegate.cpp (copia de trabajo)
@@ -19,18 +19,79 @@
#include "appletitemdelegate.h"
+class AppletItemDelegate::Private
+{
+public:
+ QString title(const QModelIndex &index) const;
+ QString description(const QModelIndex &index) const;
+ QPixmap icon(const QModelIndex &index, int width, int height) const;
+ int calculateCenter(const QRect &rect, int pixmapHeight) const;
+
+ int iconWidth;
+ int iconHeight;
+ int minimumItemWidth;
+ int leftMargin;
+ int rightMargin;
+ int separatorPixels;
+};
+
+QString AppletItemDelegate::Private::title(const QModelIndex &index) const
+{
+ return index.model()->data(index, Qt::DisplayRole).toString();
+}
+
+QString AppletItemDelegate::Private::description(const QModelIndex &index) const
+{
+ return index.model()->data(index, SecondaryDisplayRole).toString();
+}
+
+QPixmap AppletItemDelegate::Private::icon(const QModelIndex &index, int width, int \
height) const +{
+ QVariant icon = index.model()->data(index, Qt::DecorationRole);
+
+ return icon.value<QIcon>().pixmap(width, height);
+}
+
+int AppletItemDelegate::Private::calculateCenter(const QRect &rect, int \
pixmapHeight) const +{
+ return (rect.height() / 2) - (pixmapHeight / 2);
+}
+
AppletItemDelegate::AppletItemDelegate(QObject *parent)
: QItemDelegate(parent)
+ , d(new Private)
+{
+}
+
+AppletItemDelegate::~AppletItemDelegate()
{
+ delete d;
}
void AppletItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem \
&option, const QModelIndex &index) const {
+ QFontMetrics fontMetrics = painter->fontMetrics();
+
+ QColor unselectedTextColor = option.palette.text().color();
+ QColor selectedTextColor = option.palette.highlightedText().color();
+ QPen currentPen = painter->pen();
+ QPen unselectedPen = QPen(currentPen);
+ QPen selectedPen = QPen(currentPen);
+
+ unselectedPen.setColor(unselectedTextColor);
+ selectedPen.setColor(selectedTextColor);
+
if (option.state & QStyle::State_Selected)
+ {
painter->fillRect(option.rect, option.palette.highlight());
+ painter->setPen(selectedPen);
+ }
+ else
+ {
+ painter->setPen(unselectedPen);
+ }
- QVariant icon = index.model()->data(index, Qt::DecorationRole);
- QPixmap iconPixmap = icon.value<QIcon>().pixmap(48, 48);
+ QPixmap iconPixmap = d->icon(index, 48, 48);
QFont title(painter->font());
QFont previousFont(painter->font());
@@ -39,19 +100,59 @@ void AppletItemDelegate::paint(QPainter
title.setWeight(QFont::Bold);
painter->save();
+
painter->setRenderHint(QPainter::Antialiasing, true);
+
painter->setFont(title);
- painter->drawText(40 + iconPixmap.width(), 30 + option.rect.top(), \
index.model()->data(index, Qt::DisplayRole).toString()); +
+ QString display = painter->fontMetrics().elidedText(d->title(index), \
Qt::ElideRight, option.rect.width() - d->leftMargin - d->rightMargin - \
iconPixmap.width() - d->separatorPixels); +
+ QString secondaryDisplay = fontMetrics.elidedText(d->description(index), \
Qt::ElideRight, option.rect.width() - d->leftMargin - d->rightMargin - \
iconPixmap.width() - d->separatorPixels); +
+ painter->drawText(d->leftMargin + d->separatorPixels + iconPixmap.width(), \
d->separatorPixels + option.rect.top(), painter->fontMetrics().width(display), \
painter->fontMetrics().height(), Qt::AlignLeft, display); +
painter->setFont(previousFont);
- painter->drawText(40 + iconPixmap.width(), 60 + option.rect.top(), \
index.model()->data(index, SecondaryDisplayRole).toString());
- painter->drawPixmap(20, (option.rect.height() / 2) + option.rect.top() - \
(iconPixmap.height() / 2), iconPixmap); +
+ painter->drawText(d->leftMargin + d->separatorPixels + iconPixmap.width(), \
option.rect.height() - d->separatorPixels - fontMetrics.height() + option.rect.top(), \
painter->fontMetrics().width(secondaryDisplay), painter->fontMetrics().height(), \
Qt::AlignLeft, secondaryDisplay); +
+ painter->drawPixmap(d->leftMargin, d->calculateCenter(option.rect, \
iconPixmap.height()) + option.rect.top(), iconPixmap); +
painter->restore();
}
QSize AppletItemDelegate::sizeHint(const QStyleOptionViewItem &option, const \
QModelIndex &index) const {
- return QSize(78, 78);
+ int itemWidth = qMax(option.rect.width(), d->minimumItemWidth);
+ int itemHeight = (d->separatorPixels * 2) + d->iconHeight;
+
+ return QSize(itemWidth, itemHeight);
+}
+
+void AppletItemDelegate::setIconSize(int width, int height)
+{
+ d->iconWidth = width;
+ d->iconHeight = height;
+}
+
+void AppletItemDelegate::setMinimumItemWidth(int minimumItemWidth)
+{
+ d->minimumItemWidth = minimumItemWidth;
+}
+
+void AppletItemDelegate::setLeftMargin(int leftMargin)
+{
+ d->leftMargin = leftMargin;
+}
+
+void AppletItemDelegate::setRightMargin(int rightMargin)
+{
+ d->rightMargin = rightMargin;
+}
+
+void AppletItemDelegate::setSeparatorPixels(int separatorPixels)
+{
+ d->separatorPixels = separatorPixels;
}
#include "appletitemdelegate.moc"
Index: workspace/kicker/kicker/ui/addapplet.cpp
===================================================================
--- workspace/kicker/kicker/ui/addapplet.cpp (revisión: 617737)
+++ workspace/kicker/kicker/ui/addapplet.cpp (copia de trabajo)
@@ -146,6 +146,11 @@ void AddAppletDialog::populateApplets()
m_mainWidgetView->appletListView->setModel(m_listModel);
AppletItemDelegate *appletItemDelegate = new AppletItemDelegate(this);
+ appletItemDelegate->setIconSize(48, 48);
+ appletItemDelegate->setMinimumItemWidth(200);
+ appletItemDelegate->setLeftMargin(20);
+ appletItemDelegate->setRightMargin(0);
+ appletItemDelegate->setSeparatorPixels(20);
m_mainWidgetView->appletListView->setItemDelegate(appletItemDelegate);
}
Index: workspace/kicker/kicker/ui/appletitemdelegate.h
===================================================================
--- workspace/kicker/kicker/ui/appletitemdelegate.h (revisión: 617737)
+++ workspace/kicker/kicker/ui/appletitemdelegate.h (copia de trabajo)
@@ -38,6 +38,11 @@ public:
*/
AppletItemDelegate(QObject *parent = 0);
+ /**
+ * @brief Destructor.
+ */
+ ~AppletItemDelegate();
+
/**
* @brief Paints the item delegate.
@@ -49,11 +54,47 @@ public:
*/
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
+ /**
+ * Sets the icon size to @p width x @p height
+ * All icons will have @p width x @p height size
+ * @param width the width of the icon
+ * @param height the height of the icon
+ */
+ void setIconSize(int width, int height);
+
+ /**
+ * Sets the minimum row width to @p minimumItemWidth
+ * @param minimumItemWidth the minimum width you want to set the delegate to
+ */
+ void setMinimumItemWidth(int minimumItemWidth);
+
+ /**
+ * Sets the left margin of the delegate to @p leftMargin
+ * @param leftMargin the left margin that will be set
+ */
+ void setLeftMargin(int leftMargin);
+
+ /**
+ * Sets the right margin of the delegate to @p rightMargin
+ * @param rightMargin the right margin that will be set
+ */
+ void setRightMargin(int rightMargin);
+
+ /**
+ * Sets the number of pixels that will be drawn blank on top and bottom
+ * of the row to @p separatorPixels
+ * @param separatorPixels the number of pixels you wanto to set as separator
+ */
+ void setSeparatorPixels(int separatorPixels);
enum AppletRole
{
SecondaryDisplayRole = 33
};
+
+private:
+ class Private;
+ Private *d;
};
#endif
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic