#include "gtl_gui_scr_specs_series.h" namespace gtl { namespace gui { scr_specs_series::scr_specs_series(gtl::scr::spec* spec, ::chart::axis_horz* axis_x, ::chart::axis_vert* axis_y) : spec_series(true, gtl::math::spec::undef, spec->ad(), axis_x, axis_y) { delete _spec; _spec = spec; set_color(spec->color()); connect(_spec, >l::math::spec::changed, this, &spec_series::update); connect(_spec, >l::math::spec::initialized, this, &spec_series::update); connect(spec, >l::scr::spec::color_changed, this, &scr_specs_series::spec_color_changed); connect(spec, >l::scr::spec::harms_set_visible_changed, this, &scr_specs_series::update); update(); } QString scr_specs_series::name() const { if(static_cast(_spec)->name().isEmpty()) return spec_series::name(); return static_cast(_spec)->name(); } gtl::scr::spec *scr_specs_series::spec() const { return static_cast(_spec); } void scr_specs_series::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { spec_series::paint(painter, option, widget); _harms.clear(); static_cast(_spec)->get_harms(std::back_inserter(_harms)); std::vector peaks; static_cast(_spec)->get_peaks(std::back_inserter(peaks)); qreal rx = qAbs(_axis_horz->map_from_widget(0) - _axis_horz->map_from_widget(5)); qreal ry = qAbs(_axis_vert->map_from_widget(0) - _axis_vert->map_from_widget(5)); for(auto peak_idx : peaks) { auto it = std::find_if(_harms.begin(), _harms.end(), [=](gtl::scr::spec_harm* harm){return harm->peak_freq() == peak_idx*_spec->resolution();}); if(it == _harms.end()) painter->setBrush(Qt::NoBrush); else painter->setBrush(painter->pen().color()); painter->drawEllipse( QPointF(peak_idx*_spec->resolution(), _spec->at(peak_idx)), rx, ry ); } std::vector smoothed_line; static_cast(_spec)->get_smoothed_line(std::back_inserter(smoothed_line)); if(smoothed_line.empty()) return; std::vector polyline; for(int i = 0; i< smoothed_line.size(); i++) polyline.push_back( QPointF( i*_spec->resolution(), smoothed_line[i] ) ); QPen pen(QColor(static_cast(_spec)->smoothed_line_color()), 0); painter->setPen(pen); painter->drawPolyline(&polyline[0], polyline.size()); painter->resetTransform(); QRectF rect = boundingRect(); for(auto harm : _harms) { QPen pen(QColor(harm->color()), harm->weight()); painter->setPen(pen); qreal x = _axis_horz->map_to_widget(harm->freq()); qreal y = _axis_vert->map_to_widget(static_cast(_spec)->get_amplitude(harm->freq())); painter->drawLine(x, y, x, rect.bottom()); if(harm->is_present()) { painter->setBrush(QBrush(harm->color())); painter->drawEllipse(QPointF(x, y), 3, 3); } } std::vector humps; static_cast(_spec)->get_humps(std::back_inserter(humps)); for(auto hump : humps) { QPen pen(QColor(hump->color())); QColor bruhs_color(hump->color()); bruhs_color.setAlpha(32); painter->setBrush(QBrush(bruhs_color)); painter->setPen(pen); qreal x0 = hump->x0(); qreal dx = hump->dx(); std::vector line; hump->get_line(std::back_inserter(line)); QPolygonF polyline; polyline.push_back(QPointF(_axis_horz->map_to_widget(x0), rect.bottom())); for(int i = 0; i < line.size(); i++) polyline.push_back(QPointF(_axis_horz->map_to_widget(x0 + i*dx), _axis_vert->map_to_widget(line[i]))); polyline.push_back(QPointF(_axis_horz->map_to_widget(x0 + line.size()*dx), rect.bottom())); QPainterPath path; path.addPolygon(polyline); painter->drawPath(path); bruhs_color.setAlpha(96); painter->setBrush(QBrush(bruhs_color)); for(int idx = 0; idx < hump->count(); idx++) { QPainterPath hump_path; QPolygonF hump_polygon; for(int i = (*hump)[idx].left_idx; i <= (*hump)[idx].right_idx; i++) { hump_polygon.push_back(QPointF(_axis_horz->map_to_widget(x0 + i*dx), _axis_vert->map_to_widget(line[i]))); } hump_path.addPolygon(hump_polygon); painter->drawPath(hump_path); } } } void scr_specs_series::set_tool_tip(QPoint pos) { QRectF rect = boundingRect(); for(auto harm : _harms) { qreal x = _axis_horz->map_to_widget(harm->freq()); qreal y = _axis_vert->map_to_widget(static_cast(_spec)->get_amplitude(harm->freq())); QRectF harm_rect(x - 1, y, 2, rect.bottom() - y); if(harm_rect.contains(pos)) { QString text = tr("peak freq") + ": " + (harm->peak_freq() < 0 ? "-" : QString::number(harm->peak_freq(), 'f', 3)) + "\n" + tr("freq") + ": " + QString::number(harm->freq(), 'f', 3) + "\n" + tr("base") + ": " + QString::number(harm->base(), 'f', 3) + "\n" + tr("level") + ": " + QString::number(harm->level(), 'f', 3) + "\n" + tr("amplitude") + ": " + QString::number(harm->amplitude(), 'f', 3) + "\n" + tr("integral index") + ": " + QString::number(harm->integral_index(), 'f', 3) ; setToolTip(text); return; } } setToolTip(""); } void scr_specs_series::spec_color_changed() { set_color(QColor(static_cast(_spec)->color())); } } }