summaryrefslogtreecommitdiffstats
path: root/src/widget/wraterange.cpp
blob: 1d5b0bfea1e597d0c5ad215ed67de9ee52efc86a (plain)
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
#include "widget/wraterange.h"

#include "control/controlobject.h"
#include "control/controlproxy.h"
#include "moc_wraterange.cpp"
#include "util/math.h"

WRateRange::WRateRange(const QString& group, QWidget* parent)
        : WNumber(parent) {
    m_pRateRangeControl = new ControlProxy(
            group, "rateRange", this, ControlFlag::NoAssertIfMissing);
    m_pRateRangeControl->connectValueChanged(this, &WRateRange::setValue);
    m_pRateDirControl = new ControlProxy(
            group, "rate_dir", this, ControlFlag::NoAssertIfMissing);
    m_pRateDirControl->connectValueChanged(this, &WRateRange::slotRateDirChanged);
}

void WRateRange::setup(const QDomNode& node, const SkinContext& context) {
    WNumber::setup(node, context);

    QDomElement RateRangePosition = context.selectElement(node, "Position");
    QDomElement RateRangeType = context.selectElement(node, "Display");
    m_nodePosition = RateRangePosition.text() == "Top"
            ? VerticalPosition::Top
            : VerticalPosition::Bottom;
    if (RateRangeType.text() == "prefix") {
        m_nodeDisplay = DisplayType::Prefix;
    } else if (RateRangeType.text() == "range") {
        m_nodeDisplay = DisplayType::Range;
    } else {
        m_nodeDisplay = DisplayType::Default;
    }

    // Initialize the widget (overrides the base class initial value).
    const double range = m_pRateRangeControl->get();
    setValue(range);
}

void WRateRange::slotRateDirChanged(double dir) {
    Q_UNUSED(dir);

    const double range = m_pRateRangeControl->get();
    setValue(range);
}

void WRateRange::setValue(double range) {
    const double direction = m_pRateDirControl->get();

    QString prefix('-');
    if (m_nodePosition == VerticalPosition::Top && direction > 0) {
        prefix = '+';
    }

    if (m_nodePosition == VerticalPosition::Bottom && direction < 0) {
        prefix = '+';
    }

    if (m_nodeDisplay == DisplayType::Prefix) {
        m_nodeText = prefix;
    } else if (m_nodeDisplay == DisplayType::Range) {
        m_nodeText = QString::number(range * 100);
    } else {
        m_nodeText = prefix.append(QString::number(range * 100));
    }

    setText(m_nodeText);
}