File: powerdevilbrightnesslogic.cpp

package info (click to toggle)
powerdevil 4%3A5.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,712 kB
  • ctags: 1,017
  • sloc: cpp: 7,945; xml: 1,205; sh: 13; makefile: 5
file content (135 lines) | stat: -rw-r--r-- 3,939 bytes parent folder | download
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
126
127
128
129
130
131
132
133
134
135
/***************************************************************************
 *   Copyright (C) 2014 by Nikita Skovoroda <chalkerx@gmail.com>            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#include "powerdevilbrightnesslogic.h"
#include "powerdevilbackendinterface.h"

namespace PowerDevil
{

BrightnessLogic::BrightnessLogic()
{
}

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

void BrightnessLogic::setValueMax(int valueMax)
{
    if (valueMax != m_valueMax) {
        m_valueMax = valueMax;
        m_steps = calculateSteps(valueMax);
    }
}

int BrightnessLogic::action(BrightnessKeyType type) const {
    switch (type) {
    case Increase:
        return increased();
    case Decrease:
        return decreased();
    case Toggle:
        return toggled();
    }

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

int BrightnessLogic::increased() const
{
    if (m_value == m_valueMax) {
        return -1; // ignore, we are at the maximum
    }

    // Add 1 and round upwards to the nearest step
    int step = m_steps - (m_valueMax - m_value - 1) * m_steps / m_valueMax;

    if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) <= qRound(percentage(m_value))) {
        // 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::decreased() const
{
    if (m_value == 0) {
        return -1; // ignore, we are at the minimum
    }

    // Subtract 1 and round downwards to the nearest Step
    int step = (m_value - 1) * m_steps / m_valueMax;

    if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) >= qRound(percentage(m_value))) {
        // 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::toggled() const
{
    // If it's not minimum, set to minimum, if it's minimum, set to maximum
    return m_value > 0 ? 0 : m_valueMax;
}

int BrightnessLogic::value() const
{
    return m_value;
}

int BrightnessLogic::valueMax() const
{
    return m_valueMax;
}

int BrightnessLogic::steps() const
{
    return m_steps;
}

float BrightnessLogic::percentage(int value) const
{
    return value * 100.0 / m_valueMax;
}

const BrightnessLogic::BrightnessInfo BrightnessLogic::info() const
{
    return BrightnessInfo{
        m_value, m_valueMax, m_steps
    };
}

int BrightnessLogic::stepToValue(int step) const
{
    return qBound(0, qRound(step * 1.0 * m_valueMax / m_steps), m_valueMax);
}

int BrightnessLogic::valueToStep(int value) const
{
    return qBound(0, qRound(value * 1.0 * m_steps / m_valueMax), m_steps);
}

}