File indexing completed on 2024-04-28 16:55:13

0001 /***************************************************************************
0002  *   Copyright (C) 2014 by Nikita Skovoroda <chalkerx@gmail.com>            *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "powerdevilbrightnesslogic.h"
0021 #include "powerdevilbackendinterface.h"
0022 
0023 namespace PowerDevil
0024 {
0025 
0026 BrightnessLogic::BrightnessLogic()
0027 {
0028 }
0029 
0030 void BrightnessLogic::setValue(int value)
0031 {
0032     m_value = value;
0033 }
0034 
0035 void BrightnessLogic::setValueMax(int valueMax)
0036 {
0037     if (valueMax != m_valueMax) {
0038         m_valueMax = valueMax;
0039         m_steps = calculateSteps(valueMax);
0040     }
0041 }
0042 
0043 int BrightnessLogic::action(BrightnessKeyType type) const {
0044     switch (type) {
0045     case Increase:
0046         return increased();
0047     case Decrease:
0048         return decreased();
0049     case Toggle:
0050         return toggled();
0051     }
0052 
0053     return -1; // We shouldn't get here
0054 }
0055 
0056 int BrightnessLogic::increased() const
0057 {
0058     if (m_value == m_valueMax) {
0059         return m_valueMax; // we are at the maximum already
0060     }
0061 
0062     // Add 1 and round upwards to the nearest step
0063     int step = m_steps - (m_valueMax - m_value - 1) * m_steps / m_valueMax;
0064 
0065     if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) <= qRound(percentage(m_value))) {
0066         // When no visible change was made, add 1 step.
0067         // This can happen only if valueMax > 100, else 1 >= 1%.
0068         step++;
0069     }
0070 
0071     return stepToValue(step);
0072 }
0073 
0074 int BrightnessLogic::decreased() const
0075 {
0076     if (m_value == 0) {
0077         return 0; // we are at the minimum already
0078     }
0079 
0080     // Subtract 1 and round downwards to the nearest Step
0081     int step = (m_value - 1) * m_steps / m_valueMax;
0082 
0083     if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) >= qRound(percentage(m_value))) {
0084         // When no visible change was made, subtract 1 step.
0085         // This can happen only if valueMax > 100, else 1 >= 1%.
0086         step--;
0087     }
0088 
0089     return stepToValue(step);
0090 }
0091 
0092 int BrightnessLogic::toggled() const
0093 {
0094     // If it's not minimum, set to minimum, if it's minimum, set to maximum
0095     return m_value > 0 ? 0 : m_valueMax;
0096 }
0097 
0098 int BrightnessLogic::value() const
0099 {
0100     return m_value;
0101 }
0102 
0103 int BrightnessLogic::valueMax() const
0104 {
0105     return m_valueMax;
0106 }
0107 
0108 int BrightnessLogic::steps() const
0109 {
0110     return m_steps;
0111 }
0112 
0113 float BrightnessLogic::percentage(int value) const
0114 {
0115     return value * 100.0 / m_valueMax;
0116 }
0117 
0118 const BrightnessLogic::BrightnessInfo BrightnessLogic::info() const
0119 {
0120     return BrightnessInfo{
0121         m_value, m_valueMax, m_steps
0122     };
0123 }
0124 
0125 int BrightnessLogic::stepToValue(int step) const
0126 {
0127     return qBound(0, qRound(step * 1.0 * m_valueMax / m_steps), m_valueMax);
0128 }
0129 
0130 int BrightnessLogic::valueToStep(int value) const
0131 {
0132     return qBound(0, qRound(value * 1.0 * m_steps / m_valueMax), m_steps);
0133 }
0134 
0135 }