QGCRadioChannelDisplay.cpp 7.31 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
void QGCRadioChannelDisplay::paintEvent(QPaintEvent *event)
{
37 38
    Q_UNUSED(event);
    
39 40 41 42
    //Values range from 0-3000.
    //1500 is the middle, static servo value.
    QPainter painter(this);

43 44 45



46 47 48 49 50 51 52 53 54
    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;
55
    }
56 57
    if (curVal < m_min) {
        curVal = m_min;
58 59
    }

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

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

72 73 74
        //draw the text value of the widget, and its label
        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);
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 111 112
        if (isEnabled()) {
            QString valStr = QString::number(m_value);
            painter.drawText((width()/2.0) - (painter.fontMetrics().width(valStr)/2.0),((height()-3) - (fontHeight * 0)),valStr);

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

            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));
            }
            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));

                //draw min max indicator bars
                painter.setPen(QColor::fromRgb(255,0,0));
                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
                QString minstr = QString::number(m_min);
                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);

                QString maxstr = QString::number(m_max);
                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);

            }
113
        }
114
    }
115
    else //horizontal orientation
116
    {
117
        QLinearGradient hGradientBrush(0, 0, this->width(), this->height());
118 119 120
        hGradientBrush.setColorAt(0.0,DIMMEST_COLOR);
        hGradientBrush.setColorAt(0.5,MIDBRIGHT_COLOR);
        hGradientBrush.setColorAt(1.0, BRIGHTEST_COLOR);
121

122 123
        //draw the value
        painter.drawRect(0,0,width()-1,(height()-1) - twiceFontHeight);
124
        painter.setPen(QColor::fromRgb(50,255,50));
125
        painter.setBrush(hGradientBrush);
126 127

        //draw the value string
128
        painter.setPen(QColor::fromRgb(255,255,255));
129 130
        painter.drawText((width()/2.0) - (painter.fontMetrics().width(m_name)/2.0),((height()-3) - (fontHeight*1)),m_name);

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        if (isEnabled()) {
            QString valstr = QString::number(m_value);
            painter.drawText((width()/2.0) - (painter.fontMetrics().width(valstr)/2.0),((height()-3) - (fontHeight * 0)),valstr);

            painter.setPen(QColor::fromRgb(0,128,0));
            painter.setBrush(hGradientBrush);

            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
                painter.setBrush(Qt::NoBrush);
                painter.setPen(QColor::fromRgb(255,0,0));
                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);

                //draw the min and max strings
                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);
            }
160 161 162 163 164 165
        }
    }
}

void QGCRadioChannelDisplay::setValue(int value)
{
166 167 168 169 170 171 172 173 174 175 176 177
    if (value < 0)
    {
        m_value = 0;
    }
    else if (value > 3000)
    {
        m_value = 3000;
    }
    else
    {
        m_value = value;
    }
178 179 180
    update();
}

181
void QGCRadioChannelDisplay::showMinMax(bool show)
182
{
183
    m_showMinMax = show;
184 185 186 187 188 189 190 191 192
    update();
}

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

193 194 195 196 197 198 199 200 201 202 203 204 205

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);
}

206 207 208
void QGCRadioChannelDisplay::setMin(int value)
{
    m_min = value;
209 210 211 212
    if (m_min == m_max)
    {
        m_min--;
    }
213 214 215 216 217 218
    update();
}

void QGCRadioChannelDisplay::setMax(int value)
{
    m_max = value;
219 220 221 222
    if (m_min == m_max)
    {
        m_max++;
    }
223 224
    update();
}