Substitutes the background svg with a Plasma::roundedRectangle()

this makes the radius of the rounded borders of the background of the icon to look right

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=756763
This commit is contained in:
Marco Martin 2008-01-03 18:38:11 +00:00
parent 9aaedf4ddc
commit 9a0dcafb8d
2 changed files with 44 additions and 136 deletions

View File

@ -57,24 +57,16 @@ namespace Plasma
{ {
Icon::Private::Private() Icon::Private::Private()
: svg("widgets/iconbutton"), : iconSvg(0),
iconSvg(0),
svgElements(0),
iconSize(48, 48), iconSize(48, 48),
states(Private::NoState), states(Private::NoState),
orientation(Qt::Vertical), orientation(Qt::Vertical),
invertLayout(false) invertLayout(false)
{ {
svg.setContentType(Plasma::Svg::ImageSet);
//TODO: recheck when svg changes
checkSvgElements();
textColor = KColorScheme(QPalette::Active, KColorScheme::Window, textColor = KColorScheme(QPalette::Active, KColorScheme::Window,
Plasma::Theme::self()->colors()).foreground().color(); Plasma::Theme::self()->colors()).foreground().color();
shadowColor = KColorScheme(QPalette::Active, KColorScheme::Window, shadowColor = KColorScheme(QPalette::Active, KColorScheme::Window,
Plasma::Theme::self()->colors()).background().color(); Plasma::Theme::self()->colors()).background().color();
shadowColor.setAlphaF(.6);
} }
Icon::Private::~Private() Icon::Private::~Private()
@ -83,47 +75,6 @@ Icon::Private::~Private()
delete iconSvg; delete iconSvg;
} }
void Icon::Private::checkSvgElements()
{
if (svg.elementExists("background")) {
svgElements |= SvgBackground;
}
if (svg.elementExists("background-hover")) {
svgElements |= SvgBackgroundHover;
}
if (svg.elementExists("background-pressed")) {
svgElements |= SvgBackgroundPressed;
}
if (svg.elementExists("foreground")) {
svgElements |= SvgForeground;
}
if (svg.elementExists("foreground-hover")) {
svgElements |= SvgForegroundHover;
}
if (svg.elementExists("foreground-pressed")) {
svgElements |= SvgForegroundPressed;
}
if (svg.elementExists("minibutton")) {
svgElements |= SvgMinibutton;
}
if (svg.elementExists("minibutton-hover")) {
svgElements |= SvgMinibuttonHover;
}
if (svg.elementExists("minibutton-pressed")) {
svgElements |= SvgMinibuttonPressed;
}
}
IconAction::IconAction(Icon* icon, QAction *action) IconAction::IconAction(Icon* icon, QAction *action)
: m_icon(icon), : m_icon(icon),
m_action(action), m_action(action),
@ -207,11 +158,11 @@ void IconAction::rebuildPixmap()
m_pixmap = QPixmap(26, 26); m_pixmap = QPixmap(26, 26);
m_pixmap.fill(Qt::transparent); m_pixmap.fill(Qt::transparent);
int element = Icon::Private::SvgMinibutton; int element = Icon::Private::Minibutton;
if (m_pressed) { if (m_pressed) {
element = Icon::Private::SvgMinibuttonPressed; element = Icon::Private::MinibuttonPressed;
} else if (m_hovered) { } else if (m_hovered) {
element = Icon::Private::SvgMinibuttonHover; element = Icon::Private::MinibuttonHover;
} }
QPainter painter(&m_pixmap); QPainter painter(&m_pixmap);
@ -536,60 +487,34 @@ void Icon::setSvg(const QString &svgFilePath, const QString &elementId)
void Icon::Private::drawBackground(QPainter *painter, IconState state) void Icon::Private::drawBackground(QPainter *painter, IconState state)
{ {
QString element; bool darkShadow = shadowColor.value() < 128;
if (svgElements & Private::SvgBackground) { QColor shadow = shadowColor;
element = "background";
}
switch (state) { shadow.setAlphaF(.35);
switch (state) {
case Private::HoverState: case Private::HoverState:
if (svgElements & Private::SvgBackgroundHover) { shadow.setHsv(shadow.hue(),
element = "background-hover"; shadow.saturation(),
} shadow.value()+(darkShadow?50:-50),
100);
break; break;
case Private::PressedState: case Private::PressedState:
if (svgElements & Private::SvgBackgroundPressed) { shadow.setHsv(shadow.hue(),
element = "background-pressed"; shadow.saturation(),
} else if (svgElements & Private::SvgBackgroundHover) { shadow.value()+(darkShadow?100:-100),
element = "background-hover"; 128);
}
break; break;
default: default:
break; break;
} }
if (!element.isEmpty()) { painter->save();
svg.paint(painter, 0, 0, element); painter->setRenderHint(QPainter::Antialiasing);
} painter->setBrush(shadow);
} painter->setPen(QPen(shadow, 1.0));
painter->drawPath(roundedRectangle(QRectF(QPointF(0.0, 0.0), currentSize), 10.0));
void Icon::Private::drawForeground(QPainter *painter, IconState state) painter->restore();
{
QString element;
if (svgElements & Private::SvgForeground) {
element = "foreground";
}
switch (state) {
case Private::HoverState:
if (svgElements & Private::SvgForegroundHover) {
element = "foreground-hover";
}
break;
case Private::PressedState:
if (svgElements & Private::SvgForegroundPressed) {
element = "foreground-pressed";
} else if (svgElements & Private::SvgForegroundHover) {
element = "foreground-hover";
}
break;
default:
break;
}
if (!element.isEmpty()) {
svg.paint(painter, 0, 0, element);
}
} }
QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect) QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool useHoverEffect)
@ -909,15 +834,11 @@ void Icon::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option
QRectF textBoundingRect; QRectF textBoundingRect;
d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect); d->layoutTextItems(option, icon, &labelLayout, &infoLayout, &textBoundingRect);
//round to the bottom integer; otherwise a pixel could be chopped
d->svg.resize(QSize((int)size().width(), (int)size().height()));
d->drawBackground(painter, state); d->drawBackground(painter, state);
// draw icon // draw icon
painter->drawPixmap(iconPos, icon); painter->drawPixmap(iconPos, icon);
d->drawForeground(painter, state);
// Draw corner actions // Draw corner actions
foreach (IconAction *action, d->cornerActions) { foreach (IconAction *action, d->cornerActions) {
if (action->animationId()) { if (action->animationId()) {
@ -927,32 +848,31 @@ void Icon::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option
// Draw text last because its overlayed // Draw text last because its overlayed
d->drawTextItems(painter, option, labelLayout, infoLayout); d->drawTextItems(painter, option, labelLayout, infoLayout);
} }
void Icon::drawActionButtonBase(QPainter* painter, const QSize &size, int element) void Icon::drawActionButtonBase(QPainter* painter, const QSize &size, int element)
{ {
// Determine proper svg element qreal radius = size.width()/2;
QString id; QRadialGradient gradient(radius, radius, radius, radius, radius);
if (d->svgElements & Private::SvgMinibutton) { int alpha;
id = "minibutton";
}
if (element == Private::SvgMinibuttonPressed) { if (element == Private::MinibuttonPressed) {
if (d->svgElements & Private::SvgMinibuttonPressed) { alpha = 255;
id = "minibutton-pressed"; } else if (element == Private::MinibuttonHover) {
} else if (d->svgElements & Private::SvgMinibuttonHover) { alpha = 200;
id = "minibutton-hover"; } else {
} alpha = 160;
} else if (element == Icon::Private::SvgMinibuttonHover &&
d->svgElements & Private::SvgMinibuttonHover) {
id = "minibutton-hover";
} }
gradient.setColorAt(0, QColor::fromRgb(d->textColor.red(),
d->textColor.green(),
d->textColor.blue(), alpha));
gradient.setColorAt(1, QColor::fromRgb(d->textColor.red(),
d->textColor.green(),
d->textColor.blue(), 0));
if (!id.isEmpty()) { painter->setBrush(gradient);
d->svg.resize(size); painter->setPen(Qt::NoPen);
d->svg.paint(painter, 0, 0, id); painter->drawEllipse(QRectF(QPointF(.0, .0), size));
}
} }

View File

@ -106,7 +106,6 @@ public:
~Private(); ~Private();
void drawBackground(QPainter *painter, IconState state); void drawBackground(QPainter *painter, IconState state);
void drawForeground(QPainter *painter, IconState state);
void drawText(QPainter *painter); void drawText(QPainter *painter);
void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option, void drawTextItems(QPainter *painter, const QStyleOptionGraphicsItem *option,
const QTextLayout &labelLayout, const QTextLayout &infoLayout) const; const QTextLayout &labelLayout, const QTextLayout &infoLayout) const;
@ -146,19 +145,10 @@ public:
inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const; inline QSizeF subtractMargin(const QSizeF &size, MarginType type) const;
void checkSvgElements();
enum { enum {
NoSvg = 0, Minibutton = 64,
SvgBackground = 1, MinibuttonHover = 128,
SvgBackgroundHover = 2, MinibuttonPressed = 256
SvgBackgroundPressed = 4,
SvgForeground = 8,
SvgForegroundHover = 16,
SvgForegroundPressed = 32,
SvgMinibutton = 64,
SvgMinibuttonHover = 128,
SvgMinibuttonPressed = 256
}; };
enum ActionPosition { enum ActionPosition {
@ -171,11 +161,9 @@ public:
QString text; QString text;
QString infoText; QString infoText;
Svg svg;
Svg *iconSvg; Svg *iconSvg;
QString iconSvgElement; QString iconSvgElement;
QPixmap iconSvgPixmap; QPixmap iconSvgPixmap;
int svgElements;
QColor textColor; QColor textColor;
QColor shadowColor; QColor shadowColor;
QSizeF iconSize; QSizeF iconSize;