QGCRadioChannelDisplay.cpp 7.03 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 75 76 77 78
        //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));
79
        painter.setBrush(gradientBrush);
80 81 82 83 84 85

        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));
86
        }
87 88 89 90 91
        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));
92

93
            //draw min max indicator bars
94
            painter.setPen(QColor::fromRgb(255,0,0));
95 96 97 98 99 100 101
            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
102
            QString minstr = QString::number(m_min);
103 104
            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);
105 106

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

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

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

        //draw the value string
125 126
        painter.setPen(QColor::fromRgb(255,255,255));
        QString valstr = QString::number(m_value);
127 128 129 130 131
        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));
132
        painter.setBrush(hGradientBrush);
133 134 135 136 137 138 139 140 141 142

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

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

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

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

188 189 190 191 192 193 194 195 196 197 198 199 200

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

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

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