File indexing completed on 2024-05-12 09:41:26

0001 /*
0002  *   SPDX-FileCopyrightText: 2014 Nikita Skovoroda <chalkerx@gmail.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "powerdevilbrightnesslogic.h"
0008 #include "powerdevilbackendinterface.h"
0009 
0010 #include <QDebug>
0011 
0012 namespace PowerDevil
0013 {
0014 BrightnessLogic::BrightnessLogic()
0015 {
0016 }
0017 
0018 void BrightnessLogic::setValue(int value)
0019 {
0020     m_value = value;
0021 }
0022 
0023 void BrightnessLogic::setValueMax(int valueMax)
0024 {
0025     if (valueMax != m_valueMax) {
0026         m_valueMax = valueMax;
0027         m_steps = calculateSteps(valueMax);
0028     }
0029 }
0030 
0031 void BrightnessLogic::setValueBeforeTogglingOff(int valueBeforeTogglingOff)
0032 {
0033     m_valueBeforeTogglingOff = valueBeforeTogglingOff;
0034 }
0035 
0036 int BrightnessLogic::action(BrightnessKeyType type) const
0037 {
0038     switch (type) {
0039     case Increase:
0040         return increased();
0041     case Decrease:
0042         return decreased();
0043     case Toggle:
0044         return toggled();
0045     case IncreaseSmall:
0046         return increasedSmall();
0047     case DecreaseSmall:
0048         return decreasedSmall();
0049     }
0050 
0051     return -1; // We shouldn't get here
0052 }
0053 
0054 int BrightnessLogic::increased() const
0055 {
0056     if (m_value == m_valueMax) {
0057         return m_valueMax; // we are at the maximum already
0058     }
0059 
0060     // Add 1 and round upwards to the nearest step
0061     int step = m_steps - (m_valueMax - m_value - 1) * m_steps / m_valueMax;
0062 
0063     if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) <= qRound(percentage(m_value))) {
0064         // When no visible change was made, add 1 step.
0065         // This can happen only if valueMax > 100, else 1 >= 1%.
0066         step++;
0067     }
0068 
0069     return stepToValue(step);
0070 }
0071 
0072 int BrightnessLogic::increasedSmall() const
0073 {
0074     const double newPercent = (std::round(m_value * 100 / double(m_valueMax)) + 1) / 100.0;
0075     return std::min<int>(m_valueMax, std::round(m_valueMax * newPercent));
0076 }
0077 
0078 int BrightnessLogic::decreased() const
0079 {
0080     if (m_value == valueMin()) {
0081         return valueMin(); // we are at the minimum already
0082     }
0083 
0084     // Subtract 1 and round downwards to the nearest Step
0085     int step = (m_value - 1) * m_steps / m_valueMax;
0086 
0087     if (m_valueMax > 100 && qRound(percentage(stepToValue(step))) >= qRound(percentage(m_value))) {
0088         // When no visible change was made, subtract 1 step.
0089         // This can happen only if valueMax > 100, else 1 >= 1%.
0090         step--;
0091     }
0092 
0093     return stepToValue(step);
0094 }
0095 
0096 int BrightnessLogic::decreasedSmall() const
0097 {
0098     const double newPercent = (std::round(m_value * 100 / double(m_valueMax)) - 1) / 100.0;
0099     return std::max<int>(valueMin(), std::round(m_valueMax * newPercent));
0100 }
0101 
0102 int BrightnessLogic::toggled() const
0103 {
0104     if (m_value > 0) {
0105         return 0; // currently on: toggle off
0106     } else if (m_valueBeforeTogglingOff > 0) {
0107         return m_valueBeforeTogglingOff; // currently off and was on before toggling: restore
0108     } else {
0109         return m_valueMax; // currently off and would stay off if restoring: toggle to max
0110     }
0111 }
0112 
0113 int BrightnessLogic::steps() const
0114 {
0115     return m_steps;
0116 }
0117 
0118 float BrightnessLogic::percentage(int value) const
0119 {
0120     return value * 100.0 / m_valueMax;
0121 }
0122 
0123 const BrightnessLogic::BrightnessInfo BrightnessLogic::info() const
0124 {
0125     return BrightnessInfo{m_value, m_valueMax, m_valueBeforeTogglingOff, m_steps};
0126 }
0127 
0128 int BrightnessLogic::stepToValue(int step) const
0129 {
0130     return qBound(valueMin(), qRound(step * 1.0 * m_valueMax / m_steps), m_valueMax);
0131 }
0132 
0133 }