ChartPlot.cc 3.63 KB
Newer Older
1 2 3
#include "ChartPlot.h"
#include "MainWindow.h"

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
const QColor ChartPlot::baseColors[numColors] = {
    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)
};
26 27

ChartPlot::ChartPlot(QWidget *parent):
28
    QwtPlot(parent),
29
    nextColorIndex(0),
30
    symbolWidth(2.0f),
31
    curveWidth(2.0f),
32
    gridWidth(0.8f),
33
    zoomerWidth(2.0f)
34
{
35 36 37
    // Initialize the list of curves.
    curves = QMap<QString, QwtPlotCurve*>();

38 39 40 41 42
    // Set the grid. The colorscheme was already set in generateColorScheme().
    grid = new QwtPlotGrid;
    grid->enableXMin(true);
    grid->attach(this);

43
    // Enable zooming
44 45
    QwtPlotCanvas *c = static_cast<QwtPlotCanvas*>(canvas());
    zoomer = new ScrollZoomer(c);
46 47 48 49 50

    colors = QList<QColor>();

    ///> Color map for plots, includes 20 colors
    ///> Map will start from beginning when the first 20 colors are exceeded
51 52 53 54
    for (int i = 0; i < numColors; ++i)
    {
        colors.append(baseColors[i]);
    }
55 56

    // Now that all objects have been initialized, color everything.
57
    styleChanged(MainWindow::instance()->getStyle());
58 59 60 61 62 63 64 65 66
}

ChartPlot::~ChartPlot()
{

}

QColor ChartPlot::getNextColor()
{
67 68 69 70 71
    if(nextColorIndex >= colors.count())
    {
        nextColorIndex = 0;
    }
    return colors[nextColorIndex++];
72 73
}

74
QColor ChartPlot::getColorForCurve(const QString &id)
75 76 77 78 79 80 81 82
{
    return curves.value(id)->pen().color();
}

void ChartPlot::shuffleColors()
{
    foreach (QwtPlotCurve* curve, curves)
    {
83 84 85 86 87
        if (curve->isVisible()) {
            QPen pen(curve->pen());
            pen.setColor(getNextColor());
            curve->setPen(pen);
        }
88 89 90
    }
}

91
void ChartPlot::styleChanged(MainWindow::QGC_MAINWINDOW_STYLE style)
92
{
93
    // Generate a new color list for curves and recolor them.
94
    for (int i = 0; i < numColors; ++i)
95 96
    {
        if (style == MainWindow::QGC_MAINWINDOW_STYLE_LIGHT) {
97
            colors[i] = baseColors[i].darker(150);
98 99 100
        }
        else
        {
101
            colors[i] = baseColors[i].lighter(150);
102 103
        }
    }
104
    shuffleColors();
105 106 107 108 109

    // Configure the rest of the UI colors based on the current theme.
    if (style == MainWindow::QGC_MAINWINDOW_STYLE_LIGHT)
    {
        // Set the coloring of the area selector for zooming.
110 111
        zoomer->setRubberBandPen(QPen(QColor(0x37, 0x9A, 0xC3), zoomerWidth, Qt::DotLine));
        zoomer->setTrackerPen(QPen(QColor(0x37, 0x9A, 0xC3)));
112 113 114 115 116

        // Set canvas background
        setCanvasBackground(QColor(0xFF, 0xFF, 0xFF));

        // Configure the plot grid.
117 118
        grid->setMinorPen(QPen(QColor(0x55, 0x55, 0x55), gridWidth, Qt::DotLine));
        grid->setMajorPen(QPen(QColor(0x22, 0x22, 0x22), gridWidth, Qt::DotLine));
119 120 121 122
    }
    else
    {
        // Set the coloring of the area selector for zooming.
123 124
        zoomer->setRubberBandPen(QPen(QColor(0xB8, 0xD3, 0xE6), zoomerWidth, Qt::DotLine));
        zoomer->setTrackerPen(QPen(QColor(0xB8, 0xD3, 0xE6)));
125 126 127 128 129

        // Set canvas background
        setCanvasBackground(QColor(0, 0, 0));

        // Configure the plot grid.
130 131
        grid->setMinorPen(QPen(QColor(0xAA, 0xAA, 0xAA), gridWidth, Qt::DotLine));
        grid->setMajorPen(QPen(QColor(0xDD, 0xDD, 0xDD), gridWidth, Qt::DotLine));
132 133 134 135 136
    }

    // And finally refresh the widget to make sure all color changes are redrawn.
    replot();
}