- Use the widget style to draw the item background in the delegate.

- Enable alternating row colors in the view instead of drawing a divider line.

Reviewed by Chani.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=784857
This commit is contained in:
Fredrik Höglund 2008-03-12 17:14:15 +00:00
parent e702ebca9a
commit f17c8c6676
2 changed files with 27 additions and 37 deletions

View File

@ -65,6 +65,7 @@ KCategorizedItemsView::KCategorizedItemsView(QWidget * parent, Qt::WindowFlags f
comboFilters->setItemDelegate(new KCategorizedItemsViewFilterDelegate(this)); comboFilters->setItemDelegate(new KCategorizedItemsViewFilterDelegate(this));
itemsView->viewport()->setAttribute(Qt::WA_Hover); itemsView->viewport()->setAttribute(Qt::WA_Hover);
itemsView->setAlternatingRowColors(true);
QAction * find = KStandardAction::find(textSearch, SLOT(setFocus()), this); QAction * find = KStandardAction::find(textSearch, SLOT(setFocus()), this);
addAction(find); addAction(find);

View File

@ -50,17 +50,9 @@ void KCategorizedItemsViewDelegate::paint(QPainter *painter,
getItemByProxyIndex(index); getItemByProxyIndex(index);
if (!item) return; if (!item) return;
// Preparing needed data for painting QStyleOptionViewItemV4 opt(option);
int left = option.rect.left(); QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
int top = option.rect.top(); style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
int width = option.rect.width();
int height = option.rect.height();
QColor backgroundColor = (option.state.testFlag(QStyle::State_Selected))?
option.palette.color(QPalette::Highlight):option.palette.color(QPalette::Base);
// Base Background
painter->fillRect(option.rect, QBrush(backgroundColor));
switch (index.column()) { switch (index.column()) {
case 0: case 0:
@ -75,13 +67,6 @@ void KCategorizedItemsViewDelegate::paint(QPainter *painter,
default: default:
kDebug() << "unexpected column"; kDebug() << "unexpected column";
} }
// Dividing line
backgroundColor = option.palette.color(QPalette::Highlight);
backgroundColor.setAlpha(100);
painter->setPen(backgroundColor);
painter->drawLine(left, top + height - 1, left + width, top + height - 1);
} }
void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter, void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
@ -94,8 +79,6 @@ void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight); bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
QIcon::Mode iconMode = QIcon::Normal; QIcon::Mode iconMode = QIcon::Normal;
QColor backgroundColor = (option.state.testFlag(QStyle::State_Selected))?
option.palette.color(QPalette::Highlight):option.palette.color(QPalette::Base);
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))? QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text); option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
@ -106,6 +89,11 @@ void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
local_option_title.font.setBold(true); local_option_title.font.setBold(true);
local_option_title.font.setPointSize(local_option_title.font.pointSize() + 2); local_option_title.font.setPointSize(local_option_title.font.pointSize() + 2);
QPixmap pixmap(option.rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.translate(-option.rect.topLeft());
QLinearGradient gradient; QLinearGradient gradient;
QString title = item->name(); QString title = item->name();
@ -116,22 +104,22 @@ void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
// Text // Text
int textInner = 2 * UNIVERSAL_PADDING + MAIN_ICON_SIZE; int textInner = 2 * UNIVERSAL_PADDING + MAIN_ICON_SIZE;
painter->setPen(foregroundColor); p.setPen(foregroundColor);
painter->setFont(local_option_title.font); p.setFont(local_option_title.font);
painter->drawText( p.drawText(
left + (leftToRight ? textInner : 0), left + (leftToRight ? textInner : 0),
top + UNIVERSAL_PADDING, top + UNIVERSAL_PADDING,
width - textInner, MAIN_ICON_SIZE / 2, width - textInner, MAIN_ICON_SIZE / 2,
Qt::AlignBottom | Qt::AlignLeft, title); Qt::AlignBottom | Qt::AlignLeft, title);
painter->setFont(local_option_normal.font); p.setFont(local_option_normal.font);
painter->drawText( p.drawText(
left + (leftToRight ? textInner : 0), left + (leftToRight ? textInner : 0),
top + UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2, top + UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2,
width - textInner, MAIN_ICON_SIZE / 2, width - textInner, MAIN_ICON_SIZE / 2,
Qt::AlignTop | Qt::AlignLeft, description); Qt::AlignTop | Qt::AlignLeft, description);
// Main icon // Main icon
item->icon().paint(painter, item->icon().paint(&p,
leftToRight ? left + UNIVERSAL_PADDING : left + width - UNIVERSAL_PADDING - MAIN_ICON_SIZE, leftToRight ? left + UNIVERSAL_PADDING : left + width - UNIVERSAL_PADDING - MAIN_ICON_SIZE,
top + UNIVERSAL_PADDING, top + UNIVERSAL_PADDING,
MAIN_ICON_SIZE, MAIN_ICON_SIZE, Qt::AlignCenter, iconMode); MAIN_ICON_SIZE, MAIN_ICON_SIZE, Qt::AlignCenter, iconMode);
@ -147,21 +135,18 @@ void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
if (leftToRight) { if (leftToRight) {
gradient = QLinearGradient(left + width - UNIVERSAL_PADDING - FADE_LENGTH, 0, gradient = QLinearGradient(left + width - UNIVERSAL_PADDING - FADE_LENGTH, 0,
left + width - UNIVERSAL_PADDING, 0); left + width - UNIVERSAL_PADDING, 0);
gradient.setColorAt(1, backgroundColor); gradient.setColorAt(0, Qt::white);
backgroundColor.setAlpha(0); gradient.setColorAt(1, Qt::transparent);
gradient.setColorAt(0, backgroundColor);
} else { } else {
gradient = QLinearGradient(left + UNIVERSAL_PADDING, 0, gradient = QLinearGradient(left + UNIVERSAL_PADDING, 0,
left + UNIVERSAL_PADDING + FADE_LENGTH, 0); left + UNIVERSAL_PADDING + FADE_LENGTH, 0);
gradient.setColorAt(0, backgroundColor); gradient.setColorAt(0, Qt::transparent);
backgroundColor.setAlpha(0); gradient.setColorAt(1, Qt::white);
gradient.setColorAt(1, backgroundColor);
} }
QRect paintRect = option.rect; QRect paintRect = option.rect;
painter->setCompositionMode(QPainter::CompositionMode_SourceOver); p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
painter->fillRect(paintRect, gradient); p.fillRect(paintRect, gradient);
if (leftToRight) { if (leftToRight) {
gradient.setStart(left + width gradient.setStart(left + width
@ -175,13 +160,14 @@ void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
+ emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) + FADE_LENGTH, 0); + emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) + FADE_LENGTH, 0);
} }
paintRect.setHeight(UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2); paintRect.setHeight(UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2);
painter->fillRect(paintRect, gradient); p.fillRect(paintRect, gradient);
// Emblems icons // Emblems icons
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
int emblemLeft = leftToRight ? (left + width - EMBLEM_ICON_SIZE) : left; // - FAV_ICON_SIZE - 2 * UNIVERSAL_PADDING int emblemLeft = leftToRight ? (left + width - EMBLEM_ICON_SIZE) : left; // - FAV_ICON_SIZE - 2 * UNIVERSAL_PADDING
foreach (emblem, m_parent->m_emblems) { foreach (emblem, m_parent->m_emblems) {
if (item->passesFiltering(emblem.first)) { if (item->passesFiltering(emblem.first)) {
emblem.second->paint(painter, emblem.second->paint(&p,
emblemLeft, top + UNIVERSAL_PADDING, emblemLeft, top + UNIVERSAL_PADDING,
EMBLEM_ICON_SIZE, EMBLEM_ICON_SIZE, Qt::AlignCenter, iconMode); EMBLEM_ICON_SIZE, EMBLEM_ICON_SIZE, Qt::AlignCenter, iconMode);
if (leftToRight) { if (leftToRight) {
@ -191,6 +177,9 @@ void KCategorizedItemsViewDelegate::paintColMain(QPainter *painter,
} }
} }
} }
p.end();
painter->drawPixmap(option.rect.topLeft(), pixmap);
} }
void KCategorizedItemsViewDelegate::paintColFav(QPainter *painter, void KCategorizedItemsViewDelegate::paintColFav(QPainter *painter,