<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 "mpositionindicatorview.h"
#include "mpositionindicatorview_p.h"
#include "qapplication.h"

#include <QString>
#include <QTimer>
#include <QStyleOptionGraphicsItem>
#include <QPropertyAnimation>

#include "mtheme.h"
#include "mviewcreator.h"
#include "mpositionindicator.h"
#include "mscalableimage.h"
#include "mdebug.h"

MPositionIndicatorViewPrivate::MPositionIndicatorViewPrivate()
    : controller(0),
      hideTimer(new QTimer()),
      visible(false),
      onDisplay(false)
{
}

MPositionIndicatorViewPrivate::~MPositionIndicatorViewPrivate()
{
    delete this->hideTimer;
}

void MPositionIndicatorViewPrivate::init(MPositionIndicator *controller)
{
    Q_Q(MPositionIndicatorView);

    this->controller = controller;
    hideTimer->setSingleShot(true);
    fadeAnimation = new QPropertyAnimation(controller, "opacity", q);

    q->connect(hideTimer, SIGNAL(timeout()), SLOT(hide()));
    q->connect(controller, SIGNAL(displayEntered()), SLOT(_q_displayEntered()));
    q->connect(controller, SIGNAL(displayExited()), SLOT(_q_displayExited()));
}

bool MPositionIndicatorViewPrivate::isInSwitcher() const
{
    bool isInSwitcher = false;
    if (controller->scene() && !controller->scene()->views().isEmpty()) {
        MWindow* win = qobject_cast<MWindow*>(controller->scene()->views().at(0));
        if (win) {
            isInSwitcher = win->isInSwitcher();
        }
    }
    return isInSwitcher;
}

bool MPositionIndicatorViewPrivate::contentFullyVisible() const
{
    Q_Q(const MPositionIndicatorView);
    const QSizeF rangeSize = q->model()->range().size();
    return rangeSize.isNull();
}

void MPositionIndicatorViewPrivate::_q_displayEntered()
{
    Q_Q(MPositionIndicatorView);

    onDisplay = true;
    if (!isInSwitcher()) {
        hideTimer->start(q->style()->hideTimeout());
    }
}

void MPositionIndicatorViewPrivate::_q_displayExited()
{
    Q_Q(MPositionIndicatorView);

    /* stop everything and keep indicator visible for next use */
    onDisplay = false;
    hideTimer->stop();
    fadeAnimation->stop();
    controller->setProperty("opacity", 1.0f);
    visible = true;
    q->update();
}

MPositionIndicatorView::MPositionIndicatorView(MPositionIndicator *controller) :
    MWidgetView(* new MPositionIndicatorViewPrivate, controller)
{
    Q_D(MPositionIndicatorView);
    d->init(controller);
}

MPositionIndicatorView::~MPositionIndicatorView()
{
    Q_D(MPositionIndicatorView);
    d->controller = 0;
}

void MPositionIndicatorView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
}

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

    if (d->isInSwitcher()) {
        return;
    }

    QSizeF  vpSize = model()->viewportSize();
    QRectF  pRange = model()->range().adjusted(0, 0, vpSize.width(), vpSize.height());
    QPointF pPos   = model()->position();

    if ((int)pRange.height() > (int)vpSize.height()) {
        const MScalableImage *indicator = style()->indicatorImage();
        if (!indicator) {
            mWarning("MPositionIndicatorView") << "could not get \"indicator-image\"";
            return;
        }

        const MScalableImage *rail = style()->backgroundImage();

        int indicatorPixmapSizeX = indicator->pixmap()->width();

        int indicatorHeight = qMax(style()->minIndicatorSize(), int((vpSize.height()/pRange.height())*size().height()));
        int indicatorPositionY = (pPos.y()/pRange.height())*size().height();

        if (indicatorPositionY + indicatorHeight > size().height()) {
            indicatorHeight = size().height() - indicatorPositionY;
        }

        if (indicatorPositionY < 0) {
            indicatorHeight += indicatorPositionY;
            indicatorPositionY = 0;
        }

        if (rail) {
            int railPixmapSizeX = rail->pixmap()->width();
            int railPositionX = (qApp->layoutDirection() == Qt::RightToLeft ? 0 : size().width() - railPixmapSizeX);

            rail->draw((qreal)railPositionX,
                        0.0,
                        (qreal)railPixmapSizeX,
                        size().height(),
                        painter);
        }
<commit_msg>Fixes: NB#213408 - MPositionIndicator fails for some values<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 "mpositionindicatorview.h"
#include "mpositionindicatorview_p.h"
#include "qapplication.h"

#include <QString>
#include <QTimer>
#include <QStyleOptionGraphicsItem>
#include <QPropertyAnimation>

#include "mtheme.h"
#include "mviewcreator.h"
#include "mpositionindicator.h"
#include "mscalableimage.h"
#include "mdebug.h"

MPositionIndicatorViewPrivate::MPositionIndicatorViewPrivate()
    : controller(0),
      hideTimer(new QTimer()),
      visible(false),
      onDisplay(false)
{
}

MPositionIndicatorViewPrivate::~MPositionIndicatorViewPrivate()
{
    delete this->hideTimer;
}

void MPositionIndicatorViewPrivate::init(MPositionIndicator *controller)
{
    Q_Q(MPositionIndicatorView);

    this->controller = controller;
    hideTimer->setSingleShot(true);
    fadeAnimation = new QPropertyAnimation(controller, "opacity", q);

    q->connect(hideTimer, SIGNAL(timeout()), SLOT(hide()));
    q->connect(controller, SIGNAL(displayEntered()), SLOT(_q_displayEntered()));
    q->connect(controller, SIGNAL(displayExited()), SLOT(_q_displayExited()));
}

bool MPositionIndicatorViewPrivate::isInSwitcher() const
{
    bool isInSwitcher = false;
    if (controller->scene() && !controller->scene()->views().isEmpty()) {
        MWindow* win = qobject_cast<MWindow*>(controller->scene()->views().at(0));
        if (win) {
            isInSwitcher = win->isInSwitcher();
        }
    }
    return isInSwitcher;
}

bool MPositionIndicatorViewPrivate::contentFullyVisible() const
{
    Q_Q(const MPositionIndicatorView);
    const QSizeF rangeSize = q->model()->range().size();
    return rangeSize.isNull();
}

void MPositionIndicatorViewPrivate::_q_displayEntered()
{
    Q_Q(MPositionIndicatorView);

    onDisplay = true;
    if (!isInSwitcher()) {
        hideTimer->start(q->style()->hideTimeout());
    }
}

void MPositionIndicatorViewPrivate::_q_displayExited()
{
    Q_Q(MPositionIndicatorView);

    /* stop everything and keep indicator visible for next use */
    onDisplay = false;
    hideTimer->stop();
    fadeAnimation->stop();
    controller->setProperty("opacity", 1.0f);
    visible = true;
    q->update();
}

MPositionIndicatorView::MPositionIndicatorView(MPositionIndicator *controller) :
    MWidgetView(* new MPositionIndicatorViewPrivate, controller)
{
    Q_D(MPositionIndicatorView);
    d->init(controller);
}

MPositionIndicatorView::~MPositionIndicatorView()
{
    Q_D(MPositionIndicatorView);
    d->controller = 0;
}

void MPositionIndicatorView::drawBackground(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    Q_UNUSED(painter);
    Q_UNUSED(option);
}

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

    if (d->isInSwitcher()) {
        return;
    }

    QSizeF  vpSize = model()->viewportSize();
    QRectF  pRange = model()->range().adjusted(0, 0, vpSize.width(), vpSize.height());
    QPointF pPos   = model()->position();

    if ((int)pRange.height() > (int)vpSize.height()) {
        const MScalableImage *indicator = style()->indicatorImage();
        if (!indicator) {
            mWarning("MPositionIndicatorView") << "could not get \"indicator-image\"";
            return;
        }

        qreal distanceK = pPos.y() / pRange.height();
        qreal sizeK = vpSize.height() / pRange.height();
        qreal railHeight = size().height();

        const MScalableImage *rail = style()->backgroundImage();

        int indicatorPixmapSizeX = indicator->pixmap()->width();

        int indicatorHeight = sizeK * railHeight;
        if (style()->minIndicatorSize() > indicatorHeight) {
            railHeight -= (style()->minIndicatorSize() - indicatorHeight);
            indicatorHeight = style()->minIndicatorSize();
        }

        int indicatorPositionY = distanceK * railHeight;

        if (indicatorPositionY + indicatorHeight >= int(size().height())) {
            indicatorHeight = size().height() - indicatorPositionY;
            indicatorPositionY -= 1;
        }

        if (indicatorPositionY < 0) {
            indicatorHeight += indicatorPositionY;
            indicatorPositionY = 0;
        }

        if (rail) {
            int railPixmapSizeX = rail->pixmap()->width();
            int railPositionX = (qApp->layoutDirection() == Qt::RightToLeft ? 0 : size().width() - railPixmapSizeX);

            rail->draw((qreal)railPositionX,
                        0.0,
                        (qreal)railPixmapSizeX,
                        size().height(),
                        painter);
        }

{'fraction_non_alphanumeric': 0.1577437858508604, 'fraction_numerical': 0.004110898661567878, 'mean_word_length': 3.705802968960864, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 96, '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': '3295083', 'n_tokens_mistral': 3062, 'n_tokens_neox': 2988, 'n_words': 626}