File: powerdevilbrightnesslogic.cpp

package info (click to toggle)
powerdevil 4%3A6.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: cpp: 13,284; xml: 1,911; python: 1,204; sh: 19; makefile: 10
file content (125 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *   SPDX-FileCopyrightText: 2014 Nikita Skovoroda <chalkerx@gmail.com>
 *
 *   SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "powerdevilbrightnesslogic.h"

#include <QDebug>

namespace PowerDevil
{
BrightnessLogic::BrightnessLogic()
{
}

void BrightnessLogic::setValue(int value)
{
    m_current.value = value;
}

double BrightnessLogic::valueAsRatio() const
{
    return m_current.value / static_cast<double>(m_current.valueMax);
}

int BrightnessLogic::valueFromRatio(double ratio) const
{
    return std::round(std::clamp(ratio, 0.0, 1.0) * m_current.valueMax);
}

void BrightnessLogic::setValueRange(int valueMin, int valueMax)
{
    if (valueMax != m_current.valueMax || valueMin != m_current.valueMin) {
        m_current.valueMax = valueMax;
        m_current.valueMin = valueMin;
        m_current.steps = calculateSteps(valueMax);
    }
}

int BrightnessLogic::adjusted(StepAdjustmentAction adjustment) const
{
    switch (adjustment) {
    case Increase:
        return increased();
    case Decrease:
        return decreased();
    case IncreaseSmall:
        return increasedSmall();
    case DecreaseSmall:
        return decreasedSmall();
    }

    return -1; // We shouldn't get here
}

int BrightnessLogic::increased() const
{
    if (m_current.value == m_current.valueMax) {
        return m_current.valueMax; // we are at the maximum already
    }

    // Add 1 and round upwards to the nearest step
    int step = m_current.steps - (m_current.valueMax - m_current.value - 1) * m_current.steps / m_current.valueMax;

    if (m_current.valueMax > 100 && qRound(ratio(stepToValue(step)) * 100.0) <= qRound(ratio(m_current.value) * 100.0)) {
        // When no visible change was made, add 1 step.
        // This can happen only if valueMax > 100, else 1 >= 1%.
        step++;
    }

    return stepToValue(step);
}

int BrightnessLogic::increasedSmall() const
{
    const double newPercent = (std::round(m_current.value * 100 / double(m_current.valueMax)) + 1) / 100.0;
    return std::min<int>(m_current.valueMax, std::round(m_current.valueMax * newPercent));
}

int BrightnessLogic::decreased() const
{
    if (m_current.value == m_current.valueMin) {
        return m_current.valueMin; // we are at the minimum already
    }

    // Subtract 1 and round downwards to the nearest Step
    int step = (m_current.value - 1) * m_current.steps / m_current.valueMax;

    if (m_current.valueMax > 100 && qRound(ratio(stepToValue(step)) * 100.0) >= qRound(ratio(m_current.value) * 100.0)) {
        // When no visible change was made, subtract 1 step.
        // This can happen only if valueMax > 100, else 1 >= 1%.
        step--;
    }

    return stepToValue(step);
}

int BrightnessLogic::decreasedSmall() const
{
    const double newPercent = (std::round(m_current.value * 100 / double(m_current.valueMax)) - 1) / 100.0;
    return std::max<int>(m_current.valueMin, std::round(m_current.valueMax * newPercent));
}

int BrightnessLogic::steps() const
{
    return m_current.steps;
}

double BrightnessLogic::ratio(int value) const
{
    return value / static_cast<double>(m_current.valueMax);
}

const BrightnessLogic::BrightnessInfo BrightnessLogic::info() const
{
    return m_current;
}

int BrightnessLogic::stepToValue(int step) const
{
    return qBound(m_current.valueMin, qRound(step * 1.0 * m_current.valueMax / m_current.steps), m_current.valueMax);
}

}