ChartPlot.cc 2.89 KB
Newer Older
1
#include "ChartPlot.h"
2
#include "QGCApplication.h"
3

4
const QColor ChartPlot::baseColors[numColors] = {
Gus Grubba's avatar
Gus Grubba committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    QColor(242, 255, 128),
    QColor(70, 80, 242),
    QColor(232, 33, 47),
    QColor(116, 251, 110),
    QColor(81, 183, 244),
    QColor(234, 38, 107),
    QColor(92, 247, 217),
    QColor(151, 59, 239),
    QColor(231, 72, 28),
    QColor(236, 48, 221),
    QColor(75, 133, 243),
    QColor(203, 254, 121),
    QColor(104, 64, 240),
    QColor(200, 54, 238),
    QColor(104, 250, 138),
    QColor(235, 43, 165),
    QColor(98, 248, 176),
    QColor(161, 252, 116),
    QColor(87, 231, 246),
    QColor(230, 126, 23)
25
};
26

Gus Grubba's avatar
Gus Grubba committed
27
ChartPlot::ChartPlot(QWidget* parent):
28
    QwtPlot(parent),
Gus Grubba's avatar
Gus Grubba committed
29 30 31 32
    _nextColorIndex(0),
    _symbolWidth(2.0f),
    _curveWidth(2.0f),
    _gridWidth(0.8f)
33
{
34
    // Initialize the list of curves.
Gus Grubba's avatar
Gus Grubba committed
35
    _curves = QMap<QString, QwtPlotCurve*>();
36
    // Set the grid. The colorscheme was already set in generateColorScheme().
Gus Grubba's avatar
Gus Grubba committed
37 38 39 40
    _grid = new QwtPlotGrid;
    _grid->enableXMin(true);
    _grid->attach(this);
    _colors = QList<QColor>();
41 42
    ///> Color map for plots, includes 20 colors
    ///> Map will start from beginning when the first 20 colors are exceeded
Gus Grubba's avatar
Gus Grubba committed
43 44
    for(int i = 0; i < numColors; ++i) {
        _colors.append(baseColors[i]);
45
    }
46
    // Now that all objects have been initialized, color everything.
47
    styleChanged(qgcApp()->styleIsDark());
48 49 50 51 52 53 54 55
}

ChartPlot::~ChartPlot()
{
}

QColor ChartPlot::getNextColor()
{
Gus Grubba's avatar
Gus Grubba committed
56 57
    if(_nextColorIndex >= _colors.count()) {
        _nextColorIndex = 0;
58
    }
Gus Grubba's avatar
Gus Grubba committed
59
    return _colors[_nextColorIndex++];
60 61
}

Gus Grubba's avatar
Gus Grubba committed
62
QColor ChartPlot::getColorForCurve(const QString& id)
63
{
Gus Grubba's avatar
Gus Grubba committed
64
    return _curves.value(id)->pen().color();
65 66 67 68
}

void ChartPlot::shuffleColors()
{
Gus Grubba's avatar
Gus Grubba committed
69 70
    foreach(QwtPlotCurve* curve, _curves) {
        if(curve->isVisible()) {
71 72 73 74
            QPen pen(curve->pen());
            pen.setColor(getNextColor());
            curve->setPen(pen);
        }
75 76 77
    }
}

78
void ChartPlot::styleChanged(bool styleIsDark)
79
{
80
    // Generate a new color list for curves and recolor them.
Gus Grubba's avatar
Gus Grubba committed
81 82
    for(int i = 0; i < numColors; ++i) {
        _colors[i] = styleIsDark ? baseColors[i].lighter(150) : baseColors[i].darker(150);
83
    }
84
    shuffleColors();
85
    // Configure the rest of the UI colors based on the current theme.
Gus Grubba's avatar
Gus Grubba committed
86
    if(styleIsDark) {
87
        // Set canvas background
88
        setCanvasBackground(QColor(0, 0, 0));
89
        // Configure the plot grid.
Gus Grubba's avatar
Gus Grubba committed
90 91 92
        _grid->setMinorPen(QPen(QColor(64, 64, 64), _gridWidth, Qt::SolidLine));
        _grid->setMajorPen(QPen(QColor(96, 96, 96), _gridWidth, Qt::SolidLine));
    } else {
93
        // Set canvas background
94
        setCanvasBackground(QColor(0xFF, 0xFF, 0xFF));
95
        // Configure the plot grid.
Gus Grubba's avatar
Gus Grubba committed
96 97
        _grid->setMinorPen(QPen(QColor(192, 192, 192), _gridWidth, Qt::SolidLine));
        _grid->setMajorPen(QPen(QColor(128, 128, 128), _gridWidth, Qt::SolidLine));
98 99 100 101
    }
    // And finally refresh the widget to make sure all color changes are redrawn.
    replot();
}