<commit_before>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mbuttoniconview.h"
#include "mbuttoniconview_p.h"

#include <QPainter>
#include <QTimeLine>

#include "mviewcreator.h"
#include "mbutton.h"
#include "mbutton_p.h"
#include "mlabel.h"
#include "mtheme.h"


MButtonIconViewPrivate::MButtonIconViewPrivate()
    : timelineShrink(new QTimeLine())
    , timelineGlow(new QTimeLine())
    , glowCacheKey( 0 )
    , glowRadius( 0 )
{
    timelineShrink->setCurveShape(QTimeLine::EaseInCurve);
    timelineGlow->setCurveShape(QTimeLine::SineCurve);
}

MButtonIconViewPrivate::~MButtonIconViewPrivate()
{
    delete timelineShrink;
    delete timelineGlow;
}

void MButtonIconViewPrivate::drawGlowIcon(QPainter *painter, const QRectF &iconRect) const
{
    Q_Q(const MButtonIconView);

    if (!icon)
        return;

    QRectF glowRect = iconRect;
    QPointF offset(-q->style()->glowRadius(),
                   -q->style()->glowRadius());
    glowRect.translate(offset);
    glowRect.setSize(QSizeF(icon->pixmap->width() + 2 * q->style()->glowRadius(), icon->pixmap->height() + 2 * q->style()->glowRadius()));

    painter->setOpacity(controller->effectiveOpacity() * timelineGlow->currentValue());
    painter->drawImage(glowRect, const_cast<MButtonIconViewPrivate*>(this)->glowIcon(icon->pixmap, q->style()->glowRadius(), q->style()->glowColor()));
}

MButtonIconView::MButtonIconView(MButton *controller) :
    MButtonView(* new MButtonIconViewPrivate, controller)
{
    Q_D(MButtonIconView);

    connect(d->timelineShrink, SIGNAL(valueChanged(qreal)),
            this, SLOT(scaleValueChanged(qreal)));

    connect(d->timelineGlow, SIGNAL(valueChanged(qreal)),
            this, SLOT(glowValueChanged(qreal)));
}

MButtonIconView::~MButtonIconView()
{
}

void MButtonIconView::drawContents(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    Q_D(const MButtonIconView);

    bool scaling = (d->timelineShrink->state() == QTimeLine::Running) || (model()->down());
    bool glowing = (!model()->down()) && (d->timelineGlow->state() == QTimeLine::Running) &&
                   model()->iconVisible();

    if (!scaling && !glowing) {
        MButtonView::drawContents(painter, option);
    } else {
        QRectF iconRect = d->iconRect;

        if (scaling) {
            int w = size().width() / 2;
            int h = size().height() / 2;
            painter->translate(QPoint(w, h));

            // update iconRect and textRect for translate
            QPointF offset(-w, -h);
            iconRect.translate(offset);

            // Scales the painting if zoom timeline running or button pressed
            float value = d->timelineShrink->currentValue();
            float s = 1.0f - style()->shrinkFactor() * value;
            painter->setRenderHint(QPainter::SmoothPixmapTransform);
            painter->scale(s, s);
        }

        // draw glow icon
        if (glowing)
            d->drawGlowIcon(painter, iconRect);

        painter->setOpacity(d->controller->effectiveOpacity());

        drawIcon(painter, iconRect);
    }
}

void MButtonIconView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    MButtonView::drawBackground(painter, option);
}

void MButtonIconView::applyStyle()
{
    Q_D(MButtonIconView);

    MButtonView::applyStyle();

    d->timelineShrink->setDuration(style()->shrinkDuration());
    d->timelineGlow->setDuration(style()->glowDuration());
}

void MButtonIconView::scaleValueChanged(qreal value)
{
    Q_UNUSED(value);
    Q_D(MButtonIconView);

    float s = 1.0f - style()->shrinkFactor() * value;
    if (s != 1.0f)
        d->label->setTransformOriginPoint(d->label->size().width() / 2.0f, d->label->size().height() / 2.0f);
    else
        d->label->setTransformOriginPoint(0.0f, 0.0f);
    d->label->setScale(s);

    update();
}<commit_msg>Changes: checking before to apply the style that duration for QTimeLine::setDuration is >0 RevBy: <NAME> Details: QTimeLine::setDuration() needs that the parameter is >0. Some styles come with 0 as nothing/default and a warning is printed when the app is executed. This fix avoid to invoke the function with a wrong value (<=0)<commit_after>/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at <EMAIL>.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mbuttoniconview.h"
#include "mbuttoniconview_p.h"

#include <QPainter>
#include <QTimeLine>

#include "mviewcreator.h"
#include "mbutton.h"
#include "mbutton_p.h"
#include "mlabel.h"
#include "mtheme.h"


MButtonIconViewPrivate::MButtonIconViewPrivate()
    : timelineShrink(new QTimeLine())
    , timelineGlow(new QTimeLine())
    , glowCacheKey( 0 )
    , glowRadius( 0 )
{
    timelineShrink->setCurveShape(QTimeLine::EaseInCurve);
    timelineGlow->setCurveShape(QTimeLine::SineCurve);
}

MButtonIconViewPrivate::~MButtonIconViewPrivate()
{
    delete timelineShrink;
    delete timelineGlow;
}

void MButtonIconViewPrivate::drawGlowIcon(QPainter *painter, const QRectF &iconRect) const
{
    Q_Q(const MButtonIconView);

    if (!icon)
        return;

    QRectF glowRect = iconRect;
    QPointF offset(-q->style()->glowRadius(),
                   -q->style()->glowRadius());
    glowRect.translate(offset);
    glowRect.setSize(QSizeF(icon->pixmap->width() + 2 * q->style()->glowRadius(), icon->pixmap->height() + 2 * q->style()->glowRadius()));

    painter->setOpacity(controller->effectiveOpacity() * timelineGlow->currentValue());
    painter->drawImage(glowRect, const_cast<MButtonIconViewPrivate*>(this)->glowIcon(icon->pixmap, q->style()->glowRadius(), q->style()->glowColor()));
}

MButtonIconView::MButtonIconView(MButton *controller) :
    MButtonView(* new MButtonIconViewPrivate, controller)
{
    Q_D(MButtonIconView);

    connect(d->timelineShrink, SIGNAL(valueChanged(qreal)),
            this, SLOT(scaleValueChanged(qreal)));

    connect(d->timelineGlow, SIGNAL(valueChanged(qreal)),
            this, SLOT(glowValueChanged(qreal)));
}

MButtonIconView::~MButtonIconView()
{
}

void MButtonIconView::drawContents(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    Q_D(const MButtonIconView);

    bool scaling = (d->timelineShrink->state() == QTimeLine::Running) || (model()->down());
    bool glowing = (!model()->down()) && (d->timelineGlow->state() == QTimeLine::Running) &&
                   model()->iconVisible();

    if (!scaling && !glowing) {
        MButtonView::drawContents(painter, option);
    } else {
        QRectF iconRect = d->iconRect;

        if (scaling) {
            int w = size().width() / 2;
            int h = size().height() / 2;
            painter->translate(QPoint(w, h));

            // update iconRect and textRect for translate
            QPointF offset(-w, -h);
            iconRect.translate(offset);

            // Scales the painting if zoom timeline running or button pressed
            float value = d->timelineShrink->currentValue();
            float s = 1.0f - style()->shrinkFactor() * value;
            painter->setRenderHint(QPainter::SmoothPixmapTransform);
            painter->scale(s, s);
        }

        // draw glow icon
        if (glowing)
            d->drawGlowIcon(painter, iconRect);

        painter->setOpacity(d->controller->effectiveOpacity());

        drawIcon(painter, iconRect);
    }
}

void MButtonIconView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    MButtonView::drawBackground(painter, option);
}

void MButtonIconView::applyStyle()
{
    Q_D(MButtonIconView);

    MButtonView::applyStyle();

    if (style()->shrinkDuration() > 0)
        d->timelineShrink->setDuration(style()->shrinkDuration());

    if (style()->glowDuration() > 0)
        d->timelineGlow->setDuration(style()->glowDuration());
}

void MButtonIconView::scaleValueChanged(qreal value)
{
    Q_UNUSED(value);
    Q_D(MButtonIconView);

    float s = 1.0f - style()->shrinkFactor() * value;
    if (s != 1.0f)
        d->label->setTransformOriginPoint(d->label->size().width() / 2.0f, d->label->size().height() / 2.0f);
    else
        d->label->setTransformOriginPoint(0.0f, 0.0f);
    d->label->setScale(s);

    update();
}
{'fraction_non_alphanumeric': 0.17224274406332454, 'fraction_numerical': 0.006121372031662269, 'mean_word_length': 3.800405268490375, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 142, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3371379', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2865, 'n_words': 619}