~faenil/ubuntu-ui-toolkit/flickableBasedScrollView

« back to all changes in this revision

Viewing changes to src/Ubuntu/Components/plugin/ucmathutils.cpp

  • Committer: Andrea Bernabei
  • Date: 2015-11-25 11:50:10 UTC
  • mfrom: (1681.2.44 staging)
  • Revision ID: andrea.bernabei@canonical.com-20151125115010-vpxkm2lze1hav9q9
mergeĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include "ucmathutils.h"
 
19
#include <QDebug>
 
20
 
 
21
/*!
 
22
  \qmltype mathUtils
 
23
  \inqmlmodule Ubuntu.Components
 
24
  \ingroup ubuntu
 
25
  \brief Various mathematical utility functions.
 
26
 */
 
27
 
 
28
UCMathUtils::UCMathUtils(QObject *parent) : QObject(parent)
 
29
{
 
30
 
 
31
}
 
32
 
 
33
/*!
 
34
  \qmlmethod mathUtils::clamp(x, min, max)
 
35
  Ensure the value x is between min and max
 
36
 */
 
37
double UCMathUtils::clamp(double x, double min, double max)
 
38
{
 
39
    if (!(min > max)) {
 
40
        return qBound(min, x, max);
 
41
    } else {
 
42
        // swap min/max if min > max
 
43
        qWarning()<<"MathUtils.clamp, min value should not be bigger than the max value";
 
44
        return qBound(max, x, min);
 
45
    }
 
46
}
 
47
 
 
48
/*!
 
49
  \qmlmethod mathUtils::lerp(delta, from, to)
 
50
  Get the linear interpolation
 
51
 */
 
52
double UCMathUtils::lerp(double delta, double from, double to)
 
53
{
 
54
    return ((1.0 - delta) * from) + (delta * to);
 
55
}
 
56
 
 
57
/*!
 
58
  \qmlmethod mathUtils::projectValue(x, xmin, xmax, ymin, ymax)
 
59
  Linearly project a value x from [xmin, xmax] into [ymin, ymax]
 
60
 */
 
61
double UCMathUtils::projectValue(double x, double xmin, double xmax, double ymin, double ymax)
 
62
{
 
63
    return ((x - xmin) * ymax - (x - xmax) * ymin) / (xmax - xmin);
 
64
}
 
65
 
 
66
/*!
 
67
  \qmlmethod mathUtils::clampAndProject(x, xmin, xmax, ymin, ymax)
 
68
  Linearly project a value x, but in addition to projectValue it's clamped to xmin/xmax first
 
69
 */
 
70
double UCMathUtils::clampAndProject(double x, double xmin, double xmax, double ymin, double ymax)
 
71
{
 
72
    return projectValue(clamp(x, xmin, xmax), xmin, xmax, ymin, ymax);
 
73
}
 
74
 
 
75
QObject *UCMathUtils::qmlRegisterTypeCallback(QQmlEngine *engine, QJSEngine *scriptEngine)
 
76
{
 
77
    Q_UNUSED(engine);
 
78
    Q_UNUSED(scriptEngine);
 
79
    return new UCMathUtils;
 
80
}
 
81