/* * Copyright (C) 2016, Mike Walters * * This file is part of inspectrum. * * This program 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. * * This program 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 this program. If not, see . */ #include "samplesource.h" #include "traceplot.h" TracePlot::TracePlot(std::shared_ptr source) : sampleSource(source) { } void TracePlot::paintMid(QPainter &painter, QRect &rect, range_t sampleRange) { painter.save(); painter.setRenderHint(QPainter::Antialiasing, true); auto firstSample = sampleRange.minimum; auto length = sampleRange.length(); // Is it a 2-channel (complex) trace? if (auto src = dynamic_cast>*>(sampleSource.get())) { auto samples = src->getSamples(firstSample, length); if (samples == nullptr) return; painter.setPen(Qt::red); plotTrace(painter, rect, reinterpret_cast(samples.get()), length, 2); painter.setPen(Qt::blue); plotTrace(painter, rect, reinterpret_cast(samples.get())+1, length, 2); // Otherwise is it single channel? } else if (auto src = dynamic_cast*>(sampleSource.get())) { auto samples = src->getSamples(firstSample, length); if (samples == nullptr) return; painter.setPen(Qt::green); plotTrace(painter, rect, samples.get(), length, 1); } else { throw std::runtime_error("TracePlot::paintMid: Unsupported source type"); } painter.restore(); } void TracePlot::plotTrace(QPainter &painter, QRect &rect, float *samples, off_t count, int step = 1) { QPainterPath path; range_t xRange{0, rect.width() - 2.f}; range_t yRange{0, rect.height() - 2.f}; const float xStep = 1.0 / count * rect.width(); for (off_t i = 0; i < count; i++) { float sample = samples[i*step]; float x = i * xStep; float y = rect.height() - ((sample * rect.height()/2) + rect.height()/2); x = xRange.clip(x); y = yRange.clip(y); path.lineTo(QPointF{x + rect.x(), y + rect.y()}); } painter.drawPath(path); }