From ec2dec4fafeeb97801f9ed3a82e5279575b9bb98 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Fri, 26 Aug 2011 19:47:11 -0500 Subject: [PATCH] Handle our own Arrow Drawing. Adds AntiAliasing and uses our own colors. --- src/libtomahawk/utils/stylehelper.cpp | 90 ++++++++++++++++++++++++++- src/libtomahawk/utils/stylehelper.h | 2 + src/libtomahawk/widgets/combobox.cpp | 3 +- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/libtomahawk/utils/stylehelper.cpp b/src/libtomahawk/utils/stylehelper.cpp index 2681facc7..7583c5b76 100644 --- a/src/libtomahawk/utils/stylehelper.cpp +++ b/src/libtomahawk/utils/stylehelper.cpp @@ -19,7 +19,8 @@ #include "stylehelper.h" #include - +#include +#include QColor StyleHelper::headerUpperColor() { @@ -61,3 +62,90 @@ QColor StyleHelper::headerTextColor() { return Qt::white; } + + +/* + * This implementation is from QWindowsStyle (Qt 7.2) + * + * It is licensed under the GPL 3: + * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). + * Contact: Nokia Corporation (qt-info@nokia.com) + */ +void StyleHelper::drawArrow(QStyle::PrimitiveElement element, QPainter *p, const QStyleOption *opt) +{ + + if (opt->rect.width() <= 1 || opt->rect.height() <= 1) + return; + QRect r = opt->rect; + int size = qMin(r.height(), r.width()); + QPixmap pixmap; + QString pixmapName; + pixmapName.sprintf("arrow-%s-%d-%d-%d-%lld", + "$qt_ia", + uint(opt->state), element, + size, opt->palette.cacheKey()); + if (!QPixmapCache::find(pixmapName, pixmap)) { + int border = size/5; + int sqsize = 2*(size/2); + QImage image(sqsize, sqsize, QImage::Format_ARGB32); + image.fill(0); + QPainter imagePainter(&image); + imagePainter.setRenderHint(QPainter::Antialiasing, true); + QPolygon a; + switch (element) { + case QStyle::PE_IndicatorArrowUp: + a.setPoints(3, border, sqsize/2, sqsize/2, border, sqsize - border, sqsize/2); + break; + case QStyle::PE_IndicatorArrowDown: + a.setPoints(3, border, sqsize/2, sqsize/2, sqsize - border, sqsize - border, sqsize/2); + break; + case QStyle::PE_IndicatorArrowRight: + a.setPoints(3, sqsize - border, sqsize/2, sqsize/2, border, sqsize/2, sqsize - border); + break; + case QStyle::PE_IndicatorArrowLeft: + a.setPoints(3, border, sqsize/2, sqsize/2, border, sqsize/2, sqsize - border); + break; + default: + break; + } + + int bsx = 0; + int bsy = 0; + + if (opt->state & QStyle::State_Sunken) { + bsx = qApp->style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal); + bsy = qApp->style()->pixelMetric(QStyle::PM_ButtonShiftVertical); + } + + QRect bounds = a.boundingRect(); + int sx = sqsize / 2 - bounds.center().x() - 1; + int sy = sqsize / 2 - bounds.center().y() - 1; + imagePainter.translate(sx + bsx, sy + bsy); + imagePainter.setPen(opt->palette.buttonText().color()); + imagePainter.setBrush(opt->palette.buttonText()); + + if (!(opt->state & QStyle::State_Enabled)) { + QColor foreGround(150, 150, 150, 150); + imagePainter.setBrush(opt->palette.mid().color()); + imagePainter.setPen(opt->palette.mid().color()); + } else { + QColor shadow(0, 0, 0, 100); + imagePainter.translate(0, 1); + imagePainter.setPen(shadow); + imagePainter.setBrush(shadow); + QColor foreGround(255, 255, 255, 210); + imagePainter.drawPolygon(a); + imagePainter.translate(0, -1); + imagePainter.setPen(foreGround); + imagePainter.setBrush(foreGround); + } + + imagePainter.drawPolygon(a); + imagePainter.end(); + pixmap = QPixmap::fromImage(image); + QPixmapCache::insert(pixmapName, pixmap); + } + int xOffset = r.x() + (r.width() - size)/2; + int yOffset = r.y() + (r.height() - size)/2; + p->drawPixmap(xOffset, yOffset, pixmap); +} diff --git a/src/libtomahawk/utils/stylehelper.h b/src/libtomahawk/utils/stylehelper.h index 7881f5974..2fa371725 100644 --- a/src/libtomahawk/utils/stylehelper.h +++ b/src/libtomahawk/utils/stylehelper.h @@ -22,6 +22,7 @@ #include #include +#include class QPalette; class QPainter; @@ -36,6 +37,7 @@ public: static QColor headerHighlightColor(); static void horizontalHeader(QPainter *painter, const QRect &rect); + static void drawArrow(QStyle::PrimitiveElement, QPainter *painter, const QStyleOption *opt); private: static QColor m_baseColor; static QColor m_requestedBaseColor; diff --git a/src/libtomahawk/widgets/combobox.cpp b/src/libtomahawk/widgets/combobox.cpp index 29be3bac2..6a53b8fb5 100644 --- a/src/libtomahawk/widgets/combobox.cpp +++ b/src/libtomahawk/widgets/combobox.cpp @@ -74,7 +74,6 @@ void ComboBox::paintEvent(QPaintEvent *) QStyleOption arrowOpt = cb; arrowOpt.rect = arrowRect; - //p.drawPrimitive(QStyle::PE_IndicatorArrowDown, arrowOpt); - //Utils::StyleHelper::drawArrow(QStyle::PE_IndicatorArrowDown, &p, &arrowOpt); + StyleHelper::drawArrow(QStyle::PE_IndicatorArrowDown, &p, &arrowOpt); }