QGCRadioChannelDisplay.cpp 6.7 KB
Newer Older
1 2 3 4
#include "QGCRadioChannelDisplay.h"
#include <QPainter>
QGCRadioChannelDisplay::QGCRadioChannelDisplay(QWidget *parent) : QWidget(parent)
{
5 6
    m_showMinMax = false;
    m_min = 0;
7
    m_max = 1;
8 9 10
    m_value = 1500;
    m_orientation = Qt::Vertical;
    m_name = "Yaw";
11 12 13 14

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


15 16


17
}
18 19 20 21 22
void QGCRadioChannelDisplay::setName(QString name)
{
    m_name = name;
    update();
}
23 24 25 26 27 28

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

30 31 32 33 34 35
void QGCRadioChannelDisplay::paintEvent(QPaintEvent *event)
{
    //Values range from 0-3000.
    //1500 is the middle, static servo value.
    QPainter painter(this);

36 37 38



39 40 41 42 43 44 45 46 47
    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;
48
    }
49 50
    if (curVal < m_min) {
        curVal = m_min;
51 52
    }

53 54
    if (m_orientation == Qt::Vertical)
    {
55 56 57 58 59
        QLinearGradient gradientBrush(0, 0, this->width(), this->height());
        gradientBrush.setColorAt(1.0,QColor::fromRgb(0,128,0));
        gradientBrush.setColorAt(0.5,QColor::fromRgb(0,200,0));
        gradientBrush.setColorAt(0.0, QColor::fromRgb(64,255,0));

60 61
        //draw border
        painter.drawRect(0,0,width()-1,(height()-1) - twiceFontHeight);
62
        painter.setPen(QColor::fromRgb(50,255,50));
63
        painter.setBrush(Qt::SolidPattern);
64

65 66 67 68 69 70 71
        //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));
72
        painter.setBrush(gradientBrush);
73 74 75 76 77 78

        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));
79
        }
80 81 82 83 84
        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));
85

86
            //draw min max indicator bars
87
            painter.setPen(QColor::fromRgb(255,0,0));
88 89 90 91 92 93 94
            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
95
            QString minstr = QString::number(m_min);
96 97
            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);
98 99

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

        }
104
    }
105
    else //horizontal orientation
106
    {
107 108 109 110 111
        QLinearGradient hGradientBrush(0, 0, this->width(), this->height());
        hGradientBrush.setColorAt(0.0,QColor::fromRgb(0,128,0));
        hGradientBrush.setColorAt(0.5,QColor::fromRgb(0,200,0));
        hGradientBrush.setColorAt(1.0, QColor::fromRgb(64,255,0));

112 113
        //draw the value
        painter.drawRect(0,0,width()-1,(height()-1) - twiceFontHeight);
114
        painter.setPen(QColor::fromRgb(50,255,50));
115
        painter.setBrush(hGradientBrush);
116 117

        //draw the value string
118 119
        painter.setPen(QColor::fromRgb(255,255,255));
        QString valstr = QString::number(m_value);
120 121 122 123 124
        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));
125
        painter.setBrush(hGradientBrush);
126 127 128 129 130 131 132 133 134 135

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

140
            //draw the min and max strings
141 142 143 144 145 146 147 148 149 150 151 152 153
            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)
{
154 155 156 157 158 159 160 161 162 163 164 165
    if (value < 0)
    {
        m_value = 0;
    }
    else if (value > 3000)
    {
        m_value = 3000;
    }
    else
    {
        m_value = value;
    }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    update();
}

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

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

void QGCRadioChannelDisplay::setMin(int value)
{
    m_min = value;
184 185 186 187
    if (m_min == m_max)
    {
        m_min--;
    }
188 189 190 191 192 193
    update();
}

void QGCRadioChannelDisplay::setMax(int value)
{
    m_max = value;
194 195 196 197
    if (m_min == m_max)
    {
        m_max++;
    }
198 199
    update();
}