File: qledlabel.cpp

package info (click to toggle)
wfview 2.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,256 kB
  • sloc: cpp: 43,386; ansic: 3,196; sh: 32; xml: 29; makefile: 11
file content (110 lines) | stat: -rw-r--r-- 3,959 bytes parent folder | download | duplicates (3)
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
#include "qledlabel.h"
#include <QDebug>

static const int SIZE = 16;
static const QString greenSS = QString("color: white;border-radius: %1;background-color: qlineargradient(spread:pad, x1:0.145, y1:0.16, x2:1, y2:1, stop:0 rgba(20, 252, 7, 255), stop:1 rgba(25, 134, 5, 255));").arg(SIZE / 2);
static const QString redSS = QString("color: white;border-radius: %1;background-color: qlineargradient(spread:pad, x1:0.145, y1:0.16, x2:0.92, y2:0.988636, stop:0 rgba(255, 12, 12, 255), stop:0.869347 rgba(103, 0, 0, 255));").arg(SIZE / 2);
static const QString rgSplitSS = QString("color: white;border-radius: %1;background-color: qlineargradient(spread:pad, x1:0.4, y1:0.5, x2:0.6, y2:0.5, stop:0 rgba(255, 0, 0, 255), stop:1.0 rgba(0, 255, 0, 255));").arg(SIZE / 2);
static const QString orangeSS = QString("color: white;border-radius: %1;background-color: qlineargradient(spread:pad, x1:0.232, y1:0.272, x2:0.98, y2:0.959773, stop:0 rgba(255, 113, 4, 255), stop:1 rgba(91, 41, 7, 255))").arg(SIZE / 2);
static const QString blueSS = QString("color: white;border-radius: %1;background-color: qlineargradient(spread:pad, x1:0.04, y1:0.0565909, x2:0.799, y2:0.795, stop:0 rgba(203, 220, 255, 255), stop:0.41206 rgba(0, 115, 255, 255), stop:1 rgba(0, 49, 109, 255));").arg(SIZE / 2);
static const QString blankSS = QString("color: white;border-radius: %1;background-color: qlineargradient(spread:pad, x1:0.04, y1:0.0565909, x2:0.799, y2:0.795, stop:0 rgba(203, 220, 255, 0), stop:0.41206 rgba(0, 115, 255, 0), stop:1 rgba(0, 49, 109, 0));").arg(SIZE / 2);

QLedLabel::QLedLabel(QWidget* parent) :
    QLabel(parent)
{
    //Set to ok by default
    setState(StateOkBlue);
    baseColor = QColor(0, 115, 255, 255);
    setFixedSize(SIZE, SIZE);
}

void QLedLabel::setState(State state)
{
    //Debug() << "LED: setState" << state;
    switch (state) {
    case StateOk:
        setStyleSheet(greenSS);
        break;
    case StateWarning:
        setStyleSheet(orangeSS);
        break;
    case StateError:
        setStyleSheet(redSS);
        break;
    case StateOkBlue:
        setStyleSheet(blueSS);
        break;
    case StateBlank:
        setStyleSheet(blankSS);
        break;
    case StateSplitErrorOk:
        setStyleSheet(rgSplitSS);
        break;
    default:
        setStyleSheet(blueSS);
        break;
    }
}

void QLedLabel::setState(bool state)
{
    setState(state ? StateOk : StateError);
}

void QLedLabel::setColor(QColor customColor, bool applyGradient=true)
{
    QColor top,middle,bottom;
    this->baseColor = customColor;

    if(applyGradient)
    {
        top = customColor.lighter(200);
        middle = customColor;
        bottom = customColor.darker(200);
    } else {
        top = customColor;
        middle = customColor;
        bottom = customColor;
    }

    // Stop 0 is the upper corner color, white-ish
    // Stop 0.4 is the middle color
    // Stop 1 is the bottom-right corner, darker.

    QString colorSS = QString("color: white;\
            border-radius: %1;background-color: \
            qlineargradient(spread:pad, x1:0.04, \
            y1:0.0565909, x2:0.799, y2:0.795, \
            stop:0 \
            rgba(%2, %3, %4, %5), \
            stop:0.41206 \
            rgba(%6, %7, %8, %9), \
            stop:1 \
            rgba(%10, %11, %12, %13));").arg(SIZE / 2)
            .arg(top.red()).arg(top.green()).arg(top.blue()).arg(top.alpha())
            .arg(middle.red()).arg(middle.green()).arg(middle.blue()).arg(middle.alpha())
            .arg(bottom.red()).arg(bottom.green()).arg(bottom.blue()).arg(bottom.alpha());

    setStyleSheet(colorSS);
}

void QLedLabel::setColor(QString colorString, bool applyGradient=true)
{
    QColor c = QColor(colorString);
    setColor(c, applyGradient);
}

void QLedLabel::setColor(QColor c)
{
    this->setColor(c, true);
}

void QLedLabel::setColor(QString s)
{
    this->setColor(s, true);
}

QColor QLedLabel::getColor()
{
    return baseColor;
}