QGCRadioChannelDisplay.cpp 7 KB
Newer Older
1 2
#include "QGCRadioChannelDisplay.h"
#include <QPainter>
3 4 5 6 7

#define DIMMEST_COLOR   QColor::fromRgb(0,100,0)
#define MIDBRIGHT_COLOR   QColor::fromRgb(0,180,0)
#define BRIGHTEST_COLOR   QColor::fromRgb(64,255,0)

8 9
QGCRadioChannelDisplay::QGCRadioChannelDisplay(QWidget *parent) : QWidget(parent)
{
10 11
    m_showMinMax = false;
    m_min = 0;
12
    m_max = 1;
13 14 15
    m_value = 1500;
    m_orientation = Qt::Vertical;
    m_name = "Yaw";
16 17 18 19

    m_fillBrush = QBrush(Qt::green, Qt::SolidPattern);


20 21


22
}
23 24 25 26 27
void QGCRadioChannelDisplay::setName(QString name)
{
    m_name = name;
    update();
}
28 29 30 31 32 33

void QGCRadioChannelDisplay::setOrientation(Qt::Orientation orient)
{
    m_orientation = orient;
    update();
}
34

35 36 37 38 39 40
void QGCRadioChannelDisplay::paintEvent(QPaintEvent *event)
{
    //Values range from 0-3000.
    //1500 is the middle, static servo value.
    QPainter painter(this);

41 42 43



44 45 46 47 48 49 50 51 52
    int fontHeight = painter.fontMetrics().height();
    int twiceFontHeight = fontHeight * 2;

    painter.setBrush(Qt::Dense4Pattern);
    painter.setPen(QColor::fromRgb(128,128,64));

    int curVal = m_value;
    if (curVal > m_max)  {
        curVal = m_max;
53
    }
54 55
    if (curVal < m_min) {
        curVal = m_min;
56 57
    }

58 59
    if (m_orientation == Qt::Vertical)
    {
60
        QLinearGradient gradientBrush(0, 0, this->width(), this->height());
61 62 63
        gradientBrush.setColorAt(1.0,DIMMEST_COLOR);
        gradientBrush.setColorAt(0.5,MIDBRIGHT_COLOR);
        gradientBrush.setColorAt(0.0, BRIGHTEST_COLOR);
64

65 66
        //draw border
        painter.drawRect(0,0,width()-1,(height()-1) - twiceFontHeight);
67
        painter.setPen(QColor::fromRgb(50,255,50));
68
        painter.setBrush(Qt::SolidPattern);
69

70 71 72 73 74 75 76
        //draw the text value of the widget, and its label
        QString valStr = QString::number(m_value);
        painter.setPen(QColor::fromRgb(255,255,255));
        painter.drawText((width()/2.0) - (painter.fontMetrics().width(m_name)/2.0),((height()-3) - (fontHeight*1)),m_name);
        painter.drawText((width()/2.0) - (painter.fontMetrics().width(valStr)/2.0),((height()-3) - (fontHeight * 0)),valStr);

        painter.setPen(QColor::fromRgb(128,128,64));
77
        painter.setBrush(gradientBrush);
78 79 80 81 82 83

        if (!m_showMinMax) {
            //draw just the value
            int newval = (height()-2-twiceFontHeight) * ((float)(curVal - m_min) / ((m_max-m_min)+1));
            int yVal = (height()-2-twiceFontHeight) - newval;
            painter.drawRect(1,yVal,width()-3,((height()-2) - yVal - twiceFontHeight));
84
        }
85 86 87 88 89
        else {
            //draw the value
            int newval = (height()-2-twiceFontHeight) * ((float)(curVal / 3001.0));
            int yVal = (height()-2-twiceFontHeight) - newval;
            painter.drawRect(1,yVal,width()-3,((height()-2) - yVal - twiceFontHeight));
90

91
            //draw min max indicator bars
92
            painter.setPen(QColor::fromRgb(255,0,0));
93 94 95 96 97 98 99
            painter.setBrush(Qt::NoBrush);

            int yMax = (height()-3 - twiceFontHeight) - (((height()-3-twiceFontHeight) * ((float)m_max / 3000.0)));
            int yMin = (height()-3 - twiceFontHeight) - (((height()-3-twiceFontHeight) * ((float)m_min / 3000.0)));
            painter.drawRect(2,yMax,width()-3,yMin - yMax);

            //draw min and max labels
100
            QString minstr = QString::number(m_min);
101 102
            painter.drawText((width() / 2.0) - (painter.fontMetrics().width("min")/2.0),yMin,"min");
            painter.drawText((width() / 2.0) - (painter.fontMetrics().width(minstr)/2.0),yMin + fontHeight,minstr);
103 104

            QString maxstr = QString::number(m_max);
105 106
            painter.drawText((width() / 2.0) - (painter.fontMetrics().width("max")/2.0),yMax,"max");
            painter.drawText((width() / 2.0) - (painter.fontMetrics().width(maxstr)/2.0),yMax + fontHeight,maxstr);
107 108

        }
109
    }
110
    else //horizontal orientation
111
    {
112
        QLinearGradient hGradientBrush(0, 0, this->width(), this->height());
113 114 115
        hGradientBrush.setColorAt(0.0,DIMMEST_COLOR);
        hGradientBrush.setColorAt(0.5,MIDBRIGHT_COLOR);
        hGradientBrush.setColorAt(1.0, BRIGHTEST_COLOR);
116

117 118
        //draw the value
        painter.drawRect(0,0,width()-1,(height()-1) - twiceFontHeight);
119
        painter.setPen(QColor::fromRgb(50,255,50));
120
        painter.setBrush(hGradientBrush);
121 122

        //draw the value string
123 124
        painter.setPen(QColor::fromRgb(255,255,255));
        QString valstr = QString::number(m_value);
125 126 127 128 129
        painter.drawText((width()/2.0) - (painter.fontMetrics().width(m_name)/2.0),((height()-3) - (fontHeight*1)),m_name);
        painter.drawText((width()/2.0) - (painter.fontMetrics().width(valstr)/2.0),((height()-3) - (fontHeight * 0)),valstr);


        painter.setPen(QColor::fromRgb(0,128,0));
130
        painter.setBrush(hGradientBrush);
131 132 133 134 135 136 137 138 139 140

        if (!m_showMinMax) {
            //draw just the value
            painter.drawRect(1,1,(width()-3) * ((float)(curVal-m_min) / (m_max-m_min)),(height()-3) - twiceFontHeight);
        }
        else {
            //draw the value
            painter.drawRect(1,1,(width()-3) * ((float)curVal / 3000.0),(height()-3) - twiceFontHeight);

             //draw the min and max bars
141 142
            painter.setBrush(Qt::NoBrush);
            painter.setPen(QColor::fromRgb(255,0,0));
143
            painter.drawRect(width() * ((float)m_min / 3000.0),2,((width()-1) * ((float)m_max / 3000.0)) - (width() * ((float)m_min / 3000.0)),(height()-5) - twiceFontHeight);
144

145
            //draw the min and max strings
146 147 148 149 150 151 152 153 154 155 156 157 158
            QString minstr = QString::number(m_min);
            painter.drawText((width() * ((float)m_min / 3000.0)) - (painter.fontMetrics().width("min")/2.0),((height()-3) - (painter.fontMetrics().height()*1)),"min");
            painter.drawText((width() * ((float)m_min / 3000.0)) - (painter.fontMetrics().width(minstr)/2.0),((height()-3) - (painter.fontMetrics().height() * 0)),minstr);

            QString maxstr = QString::number(m_max);
            painter.drawText((width() * ((float)m_max / 3000.0)) - (painter.fontMetrics().width("max")/2.0),((height()-3) - (painter.fontMetrics().height()*1)),"max");
            painter.drawText((width() * ((float)m_max / 3000.0)) - (painter.fontMetrics().width(maxstr)/2.0),((height()-3) - (painter.fontMetrics().height() * 0)),maxstr);
        }
    }
}

void QGCRadioChannelDisplay::setValue(int value)
{
159 160 161 162 163 164 165 166 167 168 169 170
    if (value < 0)
    {
        m_value = 0;
    }
    else if (value > 3000)
    {
        m_value = 3000;
    }
    else
    {
        m_value = value;
    }
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    update();
}

void QGCRadioChannelDisplay::showMinMax()
{
    m_showMinMax = true;
    update();
}

void QGCRadioChannelDisplay::hideMinMax()
{
    m_showMinMax = false;
    update();
}

186 187 188 189 190 191 192 193 194 195 196 197 198

void QGCRadioChannelDisplay::setValueAndRange(int val, int min, int max)
{
    setValue(val);
    setMinMax(min,max);
}

void QGCRadioChannelDisplay::setMinMax(int min, int max)
{
    setMin(min);
    setMax(max);
}

199 200 201
void QGCRadioChannelDisplay::setMin(int value)
{
    m_min = value;
202 203 204 205
    if (m_min == m_max)
    {
        m_min--;
    }
206 207 208 209 210 211
    update();
}

void QGCRadioChannelDisplay::setMax(int value)
{
    m_max = value;
212 213 214 215
    if (m_min == m_max)
    {
        m_max++;
    }
216 217
    update();
}