Icons now fade in the background animation. They also learnt to react on color theme

changes. Some codingstyle changes as the icing on the cake.

The fade-in effect can be switched off with Rafaels new feature, btw.

RB:195

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=779804
This commit is contained in:
Sebastian Kügler 2008-02-27 00:50:07 +00:00
parent 758f9ecb0b
commit ae0510db05
3 changed files with 77 additions and 26 deletions

View File

@ -65,9 +65,8 @@ Icon::Private::Private()
invertLayout(false), invertLayout(false),
drawBg(false) drawBg(false)
{ {
//FIXME: update with Theme::change()! m_hoverAnimId = -1;
textColor = Plasma::Theme::self()->textColor(); m_hoverAlpha = 20/255;
shadowColor = Plasma::Theme::self()->backgroundColor();
} }
Icon::Private::~Private() Icon::Private::~Private()
@ -76,6 +75,13 @@ Icon::Private::~Private()
delete iconSvg; delete iconSvg;
} }
void Icon::readColors()
{
d->textColor = Plasma::Theme::self()->textColor();
d->shadowColor = Plasma::Theme::self()->backgroundColor();
}
IconAction::IconAction(Icon* icon, QAction *action) IconAction::IconAction(Icon* icon, QAction *action)
: m_icon(icon), : m_icon(icon),
m_action(action), m_action(action),
@ -279,6 +285,9 @@ Icon::~Icon()
void Icon::init() void Icon::init()
{ {
readColors();
connect(Plasma::Theme::self(), SIGNAL(changed()), SLOT(readColors()));
// setAcceptedMouseButtons(Qt::LeftButton); // setAcceptedMouseButtons(Qt::LeftButton);
setAcceptsHoverEvents(true); setAcceptsHoverEvents(true);
@ -297,6 +306,7 @@ void Icon::init()
d->setActiveMargins(); d->setActiveMargins();
d->currentSize = QSizeF(-1, -1); d->currentSize = QSizeF(-1, -1);
//setDrawStandardBackground(false);
} }
void Icon::addAction(QAction *action) void Icon::addAction(QAction *action)
@ -336,8 +346,7 @@ void Icon::setNumDisplayLines(int numLines)
{ {
if(numLines > d->maxDisplayLines) { if(numLines > d->maxDisplayLines) {
d->numDisplayLines = d->maxDisplayLines; d->numDisplayLines = d->maxDisplayLines;
} } else {
else {
d->numDisplayLines = numLines; d->numDisplayLines = numLines;
} }
} }
@ -487,6 +496,38 @@ void Icon::setSvg(const QString &svgFilePath, const QString &elementId)
d->iconSvgElement = elementId; d->iconSvgElement = elementId;
} }
void Icon::hoverEffect(bool show)
{
d->m_fadeIn = show;
const int FadeInDuration = 150;
if (d->m_hoverAnimId != -1) {
Phase::self()->stopCustomAnimation(d->m_hoverAnimId);
}
d->m_hoverAnimId = Phase::self()->customAnimation(40 / (1000 / FadeInDuration), FadeInDuration,
Phase::EaseOutCurve, this,
"hoverAnimationUpdate");
}
void Icon::hoverAnimationUpdate(qreal progress)
{
if (d->m_fadeIn) {
d->m_hoverAlpha = progress;
} else {
// If we mouse leaves before the fade in is done, fade out from where we were,
// not from fully faded in
qreal new_alpha = d->m_fadeIn ? progress : 1 - progress;
d->m_hoverAlpha = qMin(new_alpha, d->m_hoverAlpha);
}
if (progress == 1) {
d->m_hoverAnimId = -1;
}
if (!d->m_fadeIn && progress == 1 ) {
d->states &= ~Private::HoverState;
}
update();
}
void Icon::Private::drawBackground(QPainter *painter, IconState state) void Icon::Private::drawBackground(QPainter *painter, IconState state)
{ {
if (!drawBg) { if (!drawBg) {
@ -497,20 +538,19 @@ void Icon::Private::drawBackground(QPainter *painter, IconState state)
QColor shadow = shadowColor; QColor shadow = shadowColor;
QColor border = textColor; QColor border = textColor;
shadow.setAlphaF(.60);
border.setAlphaF(.20); border.setAlphaF(.20);
switch (state) { switch (state) {
case Private::HoverState: case Private::HoverState:
shadow.setHsv(shadow.hue(), shadow.setHsv(shadow.hue(),
shadow.saturation(), shadow.saturation(),
shadow.value() + (darkShadow?50:-50), shadow.value() + (int)(darkShadow?50*m_hoverAlpha:-50*m_hoverAlpha),
180); //70% opacity 200+(int)m_hoverAlpha*55); // opacity
break; break;
case Private::PressedState: case Private::PressedState:
shadow.setHsv(shadow.hue(), shadow.setHsv(shadow.hue(),
shadow.saturation(), shadow.saturation(),
shadow.value() + (darkShadow?100:-100), shadow.value() + (darkShadow?(int)(50*m_hoverAlpha):(int)(-50*m_hoverAlpha)),
204); //80% opacity 204); //80% opacity
break; break;
default: default:
@ -520,9 +560,10 @@ void Icon::Private::drawBackground(QPainter *painter, IconState state)
painter->save(); painter->save();
painter->translate(0.5, 0.5); painter->translate(0.5, 0.5);
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
shadow.setAlphaF(.6);
painter->setBrush(shadow); painter->setBrush(shadow);
painter->setPen(QPen(border, 1)); painter->setPen(QPen(border, 1));
painter->drawPath(roundedRectangle(QRectF(QPointF(1, 1), QSize(currentSize.width()-2, currentSize.height()-2)), 5.0)); painter->drawPath(roundedRectangle(QRectF(QPointF(1, 1), QSize((int)currentSize.width()-2, (int)currentSize.height()-2)), 5.0));
painter->restore(); painter->restore();
} }
@ -550,11 +591,12 @@ QPixmap Icon::Private::decoration(const QStyleOptionGraphicsItem *option, bool u
result = icon.pixmap(size, mode, state); result = icon.pixmap(size, mode, state);
} }
if (!result.isNull() && useHoverEffect) { // We disable the iconeffect here since we cannot get it into sync with
// the fade animation. TODO: Enable it when animations are switched off
if (!result.isNull() && useHoverEffect && !drawBg) {
KIconEffect *effect = KIconLoader::global()->iconEffect(); KIconEffect *effect = KIconLoader::global()->iconEffect();
// Note that in KIconLoader terminology, active = hover. // Note that in KIconLoader terminology, active = hover.
// ### We're assuming that the icon group is desktop/filemanager, since this // We're assuming that the icon group is desktop/filemanager, since this
// is KFileItemDelegate. // is KFileItemDelegate.
if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) { if (effect->hasEffect(KIconLoader::Desktop, KIconLoader::ActiveState)) {
result = effect->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState); result = effect->apply(result, KIconLoader::Desktop, KIconLoader::ActiveState);
@ -745,11 +787,11 @@ void Icon::Private::layoutTextItems(const QStyleOptionGraphicsItem *option,
maxInfoSize.rheight() -= labelSize.height(); maxInfoSize.rheight() -= labelSize.height();
// Lay out the info text // Lay out the info text
if (showInformation) if (showInformation) {
infoSize = layoutText(*infoLayout, option, infoText, maxInfoSize); infoSize = layoutText(*infoLayout, option, infoText, maxInfoSize);
else } else {
infoSize = QSizeF(0, 0); infoSize = QSizeF(0, 0);
}
// Compute the bounding rect of the text // Compute the bounding rect of the text
const Qt::Alignment alignment = labelLayout->textOption().alignment(); const Qt::Alignment alignment = labelLayout->textOption().alignment();
const QSizeF size(qMax(labelSize.width(), infoSize.width()), labelSize.height() + infoSize.height()); const QSizeF size(qMax(labelSize.width(), infoSize.width()), labelSize.height() + infoSize.height());
@ -766,9 +808,9 @@ QBrush Icon::Private::foregroundBrush(const QStyleOptionGraphicsItem *option) co
QPalette::Normal : QPalette::Disabled; QPalette::Normal : QPalette::Disabled;
// Always use the highlight color for selected items // Always use the highlight color for selected items
if (option->state & QStyle::State_Selected) if (option->state & QStyle::State_Selected) {
return option->palette.brush(group, QPalette::HighlightedText); return option->palette.brush(group, QPalette::HighlightedText);
}
return option->palette.brush(group, QPalette::Text); return option->palette.brush(group, QPalette::Text);
} }
@ -780,9 +822,9 @@ QBrush Icon::Private::backgroundBrush(const QStyleOptionGraphicsItem *option) co
QBrush background(Qt::NoBrush); QBrush background(Qt::NoBrush);
// Always use the highlight color for selected items // Always use the highlight color for selected items
if (option->state & QStyle::State_Selected) if (option->state & QStyle::State_Selected) {
background = option->palette.brush(group, QPalette::Highlight); background = option->palette.brush(group, QPalette::Highlight);
}
return background; return background;
} }
@ -1037,8 +1079,8 @@ void Icon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
action->show(); action->show();
action->event(event->type(), event->pos()); action->event(event->type(), event->pos());
} }
d->states |= Private::HoverState; d->states |= Private::HoverState;
hoverEffect(true);
update(); update();
Widget::hoverEnterEvent(event); Widget::hoverEnterEvent(event);
@ -1050,8 +1092,8 @@ void Icon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
action->hide(); action->hide();
action->event(event->type(), event->pos()); action->event(event->type(), event->pos());
} }
// d->states &= ~Private::HoverState; // Will be set once progress is zero again ...
d->states &= ~Private::HoverState; hoverEffect(false);
update(); update();
Widget::hoverLeaveEvent(event); Widget::hoverLeaveEvent(event);

View File

@ -255,11 +255,15 @@ public:
private: private:
void init(); void init();
void layoutIcons(const QStyleOptionGraphicsItem *option); void layoutIcons(const QStyleOptionGraphicsItem *option);
void hoverEffect(bool);
Private * const d; Private * const d;
private Q_SLOTS: private Q_SLOTS:
void actionDestroyed(QObject* obj); void actionDestroyed(QObject* obj);
void readColors();
void hoverAnimationUpdate(qreal progress);
}; };
} // namespace Plasma } // namespace Plasma

View File

@ -33,6 +33,7 @@
#include <plasma/svg.h> #include <plasma/svg.h>
#include "icon.h" #include "icon.h"
#include "phase.h"
class QAction; class QAction;
class QPainter; class QPainter;
@ -128,6 +129,7 @@ public:
void layoutTextItems(const QStyleOptionGraphicsItem *option, const QPixmap &icon, void layoutTextItems(const QStyleOptionGraphicsItem *option, const QPixmap &icon,
QTextLayout *labelLayout, QTextLayout *infoLayout, QRectF *textBoundingRect) const; QTextLayout *labelLayout, QTextLayout *infoLayout, QRectF *textBoundingRect) const;
inline void setLayoutOptions(QTextLayout &layout, const QStyleOptionGraphicsItem *options) const; inline void setLayoutOptions(QTextLayout &layout, const QStyleOptionGraphicsItem *options) const;
inline Qt::LayoutDirection iconDirection(const QStyleOptionGraphicsItem *option) const; inline Qt::LayoutDirection iconDirection(const QStyleOptionGraphicsItem *option) const;
@ -168,6 +170,9 @@ public:
QPixmap iconSvgPixmap; QPixmap iconSvgPixmap;
QColor textColor; QColor textColor;
QColor shadowColor; QColor shadowColor;
bool m_fadeIn;
Phase::AnimId m_hoverAnimId;
qreal m_hoverAlpha;
QSizeF iconSize; QSizeF iconSize;
QIcon icon; QIcon icon;
IconStates states; IconStates states;