RCValueWidget.cc 10.9 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009, 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "RCValueWidget.h"

#include <QPainter>
#include <QDebug>

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

RCValueWidget::RCValueWidget(QWidget *parent) :
    QWidget(parent),
    _smallMode(false),
    _value(_centerValue),
    _min(_centerValue),
    _max(_centerValue),
    _trim(_centerValue),
    _minValid(false),
    _maxValid(false),
    _showMinMax(false),
    _showTrim(false),
    _fgColor(255, 255, 255)
{
    setAutoFillBackground(true);
}

/// @brief Draws the control
void RCValueWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);
    
    Q_UNUSED(event);
    
    int curVal = _value;
    if (curVal > _maxRange)  {
        curVal = _maxRange;
    }
    if (curVal < _minRange) {
        curVal = _minRange;
    }
    
    QPen pen(_fgColor);
    pen.setWidth(1);
    painter.setPen(pen);
    
    if (_smallMode) {
        painter.drawLine(0, height() / 2, width(), height() / 2);
    } else {
        // The value bar itself it centered in the control
        QRect rectValueBar(0, (rect().height() - _barHeight)/2, rect().width() - 1, _barHeight);
        
        painter.drawRoundedRect(rectValueBar, _barHeight/2, _barHeight/2);
    }
    
    // Draw the RC value circle
    int radius = _smallMode ? 4 : _barHeight;
    int curValNormalized;
    if (_reversed) {
        curValNormalized = _centerValue - (curVal - _centerValue);
    } else {
        curValNormalized = curVal;
    }
    QPoint  ptCenter(width() * ((float)(curValNormalized-_minRange) / (_maxRange-_minRange)), height() / 2);
    QBrush brush(QColor(128, 128, 128));
    painter.setBrush(brush);
    painter.drawEllipse(ptCenter, radius, radius);
    
#if 0
    int fontHeight = painter.fontMetrics().height();
    int rowHeigth = fontHeight + 2;

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

    int curVal = _value;
    if (curVal > _maxRange)  {
        curVal = _maxRange;
    }
    if (curVal < _minRange) {
        curVal = _minRange;
    }

    if (isEnabled()) {
        QLinearGradient hGradientBrush(0, 0, this->width(), this->height());
        hGradientBrush.setColorAt(0.0,DIMMEST_COLOR);
        hGradientBrush.setColorAt(0.5,MIDBRIGHT_COLOR);
        hGradientBrush.setColorAt(1.0, BRIGHTEST_COLOR);
        
        // Calculate how much horizontal space we need to show a min/max value. We must be able to display four numeric
        // digits for the rc value, plus we add another digit for spacing.
        int minMaxDisplayWidth = painter.fontMetrics().width("00000");

        // Draw the value axis line with center and end point tick marks. We need to leave enough space on the left and the right
        // for the Min/Max value displays.
        QRect rcValueAxis(minMaxDisplayWidth, rowHeigth * 2, width() - (minMaxDisplayWidth * 2), rowHeigth);
        int yLine = rcValueAxis.y() + rcValueAxis.height() / 2;
        painter.drawLine(rcValueAxis.left(), yLine, rcValueAxis.right(), yLine);
        painter.drawLine(rcValueAxis.left(), rcValueAxis.top(), rcValueAxis.left(), rcValueAxis.bottom());
        painter.drawLine(rcValueAxis.right(), rcValueAxis.top(), rcValueAxis.right(), rcValueAxis.bottom());
        painter.drawLine(rcValueAxis.left() + rcValueAxis.width() / 2, rcValueAxis.top(), rcValueAxis.left() + rcValueAxis.width() / 2, rcValueAxis.bottom());
        
        painter.setPen(QColor::fromRgb(50,255,50));
        painter.setBrush(hGradientBrush);

        if (_showMinMax) {
            QString text;
            
            // Draw the Min numeric value display to the left
            painter.drawText(0, rowHeigth, minMaxDisplayWidth, fontHeight, Qt::AlignHCenter | Qt::AlignBottom, "Min");
            if (_minValid) {
                text = QString::number(_min);
            } else {
                text = "----";
            }
            painter.drawText(0, rowHeigth * 2, minMaxDisplayWidth, fontHeight, Qt::AlignHCenter | Qt::AlignBottom, text);

            // Draw the Max numeric value display to the right
            painter.drawText(width() - minMaxDisplayWidth, rowHeigth, minMaxDisplayWidth, fontHeight, Qt::AlignHCenter | Qt::AlignBottom, "Max");
            if (_maxValid) {
                text = QString::number(_max);
            } else {
                text = QString::number(_max);
            }
            painter.drawText(width() - minMaxDisplayWidth, rowHeigth * 2, minMaxDisplayWidth, fontHeight, Qt::AlignHCenter | Qt::AlignBottom, text);
            
            // Draw the Min/Max tick marks on the axis
            int xTick;
            if (_minValid) {
                int xTick = rcValueAxis.left() + (rcValueAxis.width() * ((float)(_min-_minRange) / (_maxRange-_minRange)));
                painter.drawLine(xTick, rcValueAxis.top(), xTick, rcValueAxis.bottom());
            }
            if (_maxValid) {
                xTick = rcValueAxis.left() + (rcValueAxis.width() * ((float)(_max-_minRange) / (_maxRange-_minRange)));
                painter.drawLine(xTick, rcValueAxis.top(), xTick, rcValueAxis.bottom());
            }
        }
        
        if (_showTrim) {
            // Draw the Trim value pointer
            _drawValuePointer(&painter,
                              rcValueAxis.left() + (rcValueAxis.width() * ((float)(_trim-_minRange) / (_maxRange-_minRange))),      // x position for tip of triangle
                              (rowHeigth * 2) + (rowHeigth / 2) - 1,                                                                // y position for tip of triangle
                              rowHeigth / 2,                                                                                        // heigth of triangle
                              false);                                                                                               // draw upside down
            
            // Draw the Trim value label
            QString trimStr = tr("Trim %1").arg(QString::number(_trim));
            
            int trimTextWidth = painter.fontMetrics().width(trimStr);
            
            painter.drawText(rcValueAxis.left() + (rcValueAxis.width() * ((float)(_trim-_minRange) / (_maxRange-_minRange))) - (trimTextWidth / 2),
                             rowHeigth,
                             trimTextWidth,
                             fontHeight,
                             Qt::AlignLeft | Qt::AlignBottom,
                             trimStr);
        }
        
        // Draw the RC value pointer
        _drawValuePointer(&painter,
                          rcValueAxis.left() + (rcValueAxis.width() * ((float)(curVal-_minRange) / (_maxRange-_minRange))),     // x position for tip of triangle
                          (rowHeigth * 2) + (rowHeigth / 2) + 1,                                                                // y position for tip of triangle
                          rowHeigth / 2,                                                                                        // heigth of triangle
                          true);                                                                                                // draw right side up

        // Draw the control value
        QString valueStr = QString::number(_value);
        
        int valueTextWidth = painter.fontMetrics().width(valueStr);
        
        painter.drawText(rcValueAxis.left() + (rcValueAxis.width() * ((float)(_value-_minRange) / (_maxRange-_minRange))) - (valueTextWidth / 2),
                         rowHeigth * 3,
                         valueTextWidth,
                         fontHeight,
                         Qt::AlignLeft | Qt::AlignBottom,
                         valueStr);

        painter.setPen(QColor::fromRgb(0,128,0));
        painter.setBrush(hGradientBrush);
    } else {
        painter.setPen(QColor(Qt::gray));
        painter.drawText(0, 0, width(), height(), Qt::AlignHCenter | Qt::AlignVCenter, tr("not available"));
    }
#endif
}

void RCValueWidget::setValue(int value)
{
    _value = value;
    update();
}

void RCValueWidget::showMinMax(bool show)
{
    _showMinMax = show;
    update();
}

void RCValueWidget::showTrim(bool show)
{
    _showTrim = show;
    update();
}

void RCValueWidget::setValueAndMinMax(int val, int min, int max)
{
    setValue(val);
    setMinMax(min,max);
}

void RCValueWidget::setMinMax(int min, int max)
{
    _min = min;
    _max = max;
    update();
}

void RCValueWidget::setMin(int min)
{
    _min = min;
    update();
}

void RCValueWidget::setMax(int max)
{
    _max = max;
    update();
}

void RCValueWidget::setTrim(int value)
{
    _trim = value;
    update();
}

/// @brief Draw rc value pointer triangle.
///     @param painter Draw using this painter
///     @param x X position for tip of triangle
///     @param y Y position for tip of triangle
///     @param heigth Height of triangle
///     @param rightSideUp true: draw triangle right side up, false: draw triangle upside down
void RCValueWidget::_drawValuePointer(QPainter* painter, int xTip, int yTip, int height, bool rightSideUp)
{
    QPointF trianglePoints[3];
    
    // Topmost tip of triangle points to value
    trianglePoints[0].setX(xTip);
    trianglePoints[0].setY(yTip);
    
    int yBottom;
    if (rightSideUp) {
        yBottom = yTip + height;
    } else {
        yBottom = yTip - height;
    }
    
    // Right bottom tip of triangle
    trianglePoints[1].setX(xTip + (height * 0.75));
    trianglePoints[1].setY(yBottom);
    
    // Left bottom tip of triangle
    trianglePoints[2].setX(xTip - (height * 0.75));
    trianglePoints[2].setY(yBottom);
    
    painter->drawPolygon (trianglePoints, 3);
}

/// @brief Set whether the Min range value is valid or not.
void RCValueWidget::setMinValid(bool valid)
{
    _minValid = valid;
    update();
}

/// @brief Set whether the Max range value is valid or not.
void RCValueWidget::setMaxValid(bool valid)
{
    _maxValid = valid;
    update();
}