From: Joel Holdsworth Date: Sun, 23 Nov 2014 19:53:59 +0000 (+0000) Subject: RowItem: Bundled painting parameters into RowItemPaintParams X-Git-Tag: pulseview-0.3.0~402 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=3eb29afdb641606c2e2e059289d8cc103d027a25;p=pulseview.git RowItem: Bundled painting parameters into RowItemPaintParams --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a66a95bd..40878efa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,6 +175,7 @@ set(pulseview_SOURCES pv/view/logicsignal.cpp pv/view/rowitem.cpp pv/view/rowitemowner.cpp + pv/view/rowitempaintparams.cpp pv/view/ruler.cpp pv/view/selectableitem.cpp pv/view/signal.cpp diff --git a/pv/view/analogsignal.cpp b/pv/view/analogsignal.cpp index 8a19cc21..a7a69218 100644 --- a/pv/view/analogsignal.cpp +++ b/pv/view/analogsignal.cpp @@ -87,16 +87,15 @@ std::pair AnalogSignal::v_extents() const return make_pair(-NominalHeight / 2, NominalHeight / 2); } -void AnalogSignal::paint_back(QPainter &p, int left, int right) +void AnalogSignal::paint_back(QPainter &p, const RowItemPaintParams &pp) { if (channel_->enabled()) - paint_axis(p, get_visual_y(), left, right); + paint_axis(p, get_visual_y(), pp.left(), pp.right()); } -void AnalogSignal::paint_mid(QPainter &p, int left, int right) +void AnalogSignal::paint_mid(QPainter &p, const RowItemPaintParams &pp) { assert(data_); - assert(right >= left); assert(owner_); const int y = get_visual_y(); @@ -126,7 +125,7 @@ void AnalogSignal::paint_mid(QPainter &p, int left, int right) const int64_t last_sample = snapshot->get_sample_count() - 1; const double samples_per_pixel = samplerate * scale; const double start = samplerate * (offset - start_time); - const double end = start + samples_per_pixel * (right - left); + const double end = start + samples_per_pixel * pp.width(); const int64_t start_sample = min(max((int64_t)floor(start), (int64_t)0), last_sample); @@ -134,11 +133,11 @@ void AnalogSignal::paint_mid(QPainter &p, int left, int right) (int64_t)0), last_sample); if (samples_per_pixel < EnvelopeThreshold) - paint_trace(p, snapshot, y, left, + paint_trace(p, snapshot, y, pp.left(), start_sample, end_sample, pixels_offset, samples_per_pixel); else - paint_envelope(p, snapshot, y, left, + paint_envelope(p, snapshot, y, pp.left(), start_sample, end_sample, pixels_offset, samples_per_pixel); } diff --git a/pv/view/analogsignal.hpp b/pv/view/analogsignal.hpp index 5295fe32..c048371e 100644 --- a/pv/view/analogsignal.hpp +++ b/pv/view/analogsignal.hpp @@ -64,18 +64,16 @@ public: /** * Paints the background layer of the signal with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal. - * @param right the x-coordinate of the right edge of the signal. + * @param pp the painting parameters object to paint with.. **/ - void paint_back(QPainter &p, int left, int right); + void paint_back(QPainter &p, const RowItemPaintParams &pp); /** * Paints the mid-layer of the signal with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal. - * @param right the x-coordinate of the right edge of the signal. + * @param pp the painting parameters object to paint with.. **/ - void paint_mid(QPainter &p, int left, int right); + void paint_mid(QPainter &p, const RowItemPaintParams &pp); private: void paint_trace(QPainter &p, diff --git a/pv/view/decodetrace.cpp b/pv/view/decodetrace.cpp index 0fbd16fd..7ed66b3e 100644 --- a/pv/view/decodetrace.cpp +++ b/pv/view/decodetrace.cpp @@ -166,13 +166,13 @@ pair DecodeTrace::v_extents() const return make_pair(-row_height / 2, row_height * 7 / 2); } -void DecodeTrace::paint_back(QPainter &p, int left, int right) +void DecodeTrace::paint_back(QPainter &p, const RowItemPaintParams &pp) { - Trace::paint_back(p, left, right); - paint_axis(p, get_visual_y(), left, right); + Trace::paint_back(p, pp); + paint_axis(p, get_visual_y(), pp.left(), pp.right()); } -void DecodeTrace::paint_mid(QPainter &p, int left, int right) +void DecodeTrace::paint_mid(QPainter &p, const RowItemPaintParams &pp) { using namespace pv::data::decode; @@ -185,14 +185,16 @@ void DecodeTrace::paint_mid(QPainter &p, int left, int right) const QString err = decoder_stack_->error_message(); if (!err.isEmpty()) { - draw_unresolved_period(p, annotation_height, left, right); - draw_error(p, err, left, right); + draw_unresolved_period( + p, annotation_height, pp.left(), pp.right()); + draw_error(p, err, pp); return; } // Iterate through the rows int y = get_visual_y(); - pair sample_range = get_sample_range(left, right); + pair sample_range = get_sample_range( + pp.left(), pp.right()); assert(decoder_stack_); const vector rows(decoder_stack_->get_visible_rows()); @@ -214,8 +216,7 @@ void DecodeTrace::paint_mid(QPainter &p, int left, int right) if (!annotations.empty()) { for (const Annotation &a : annotations) draw_annotation(a, p, get_text_colour(), - annotation_height, left, right, y, - base_colour); + annotation_height, pp, y, base_colour); y += row_height_; visible_rows_.push_back(rows[i]); @@ -223,15 +224,13 @@ void DecodeTrace::paint_mid(QPainter &p, int left, int right) } // Draw the hatching - draw_unresolved_period(p, annotation_height, left, right); + draw_unresolved_period(p, annotation_height, pp.left(), pp.right()); } -void DecodeTrace::paint_fore(QPainter &p, int left, int right) +void DecodeTrace::paint_fore(QPainter &p, const RowItemPaintParams &pp) { using namespace pv::data::decode; - (void)right; - assert(row_height_); for (size_t i = 0; i < visible_rows_.size(); i++) @@ -244,15 +243,15 @@ void DecodeTrace::paint_fore(QPainter &p, int left, int right) if (i != 0) { const QPointF points[] = { - QPointF(left, y - ArrowSize), - QPointF(left + ArrowSize, y), - QPointF(left, y + ArrowSize) + QPointF(pp.left(), y - ArrowSize), + QPointF(pp.left() + ArrowSize, y), + QPointF(pp.left(), y + ArrowSize) }; p.drawPolygon(points, countof(points)); } - const QRect r(left + ArrowSize * 2, y - row_height_ / 2, - right - left, row_height_); + const QRect r(pp.left() + ArrowSize * 2, y - row_height_ / 2, + pp.right() - pp.left(), row_height_); const QString h(visible_rows_[i].title()); const int f = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextDontClip; @@ -337,7 +336,7 @@ QMenu* DecodeTrace::create_context_menu(QWidget *parent) } void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a, - QPainter &p, QColor text_color, int h, int left, int right, int y, + QPainter &p, QColor text_color, int h, const RowItemPaintParams &pp, int y, size_t base_colour) const { double samples_per_pixel, pixels_offset; @@ -353,7 +352,7 @@ void DecodeTrace::draw_annotation(const pv::data::decode::Annotation &a, const QColor &fill = Colours[colour]; const QColor &outline = OutlineColours[colour]; - if (start > right + DrawPadding || end < left - DrawPadding) + if (start > pp.right() + DrawPadding || end < pp.left() - DrawPadding) return; if (a.start_sample() == a.end_sample()) @@ -441,7 +440,7 @@ void DecodeTrace::draw_range(const pv::data::decode::Annotation &a, QPainter &p, } void DecodeTrace::draw_error(QPainter &p, const QString &message, - int left, int right) + const RowItemPaintParams &pp) { const int y = get_visual_y(); @@ -449,7 +448,7 @@ void DecodeTrace::draw_error(QPainter &p, const QString &message, p.setBrush(ErrorBgColour); const QRectF bounding_rect = - QRectF(left, INT_MIN / 2 + y, right - left, INT_MAX); + QRectF(pp.width(), INT_MIN / 2 + y, pp.width(), INT_MAX); const QRectF text_rect = p.boundingRect(bounding_rect, Qt::AlignCenter, message); const float r = text_rect.height() / 4; diff --git a/pv/view/decodetrace.hpp b/pv/view/decodetrace.hpp index e3d8d68c..71876f5c 100644 --- a/pv/view/decodetrace.hpp +++ b/pv/view/decodetrace.hpp @@ -99,26 +99,23 @@ public: /** * Paints the background layer of the trace with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal. - * @param right the x-coordinate of the right edge of the signal. + * @param pp the painting parameters object to paint with.. **/ - void paint_back(QPainter &p, int left, int right); + void paint_back(QPainter &p, const RowItemPaintParams &pp); /** * Paints the mid-layer of the trace with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal - * @param right the x-coordinate of the right edge of the signal + * @param pp the painting parameters object to paint with. **/ - void paint_mid(QPainter &p, int left, int right); + void paint_mid(QPainter &p, const RowItemPaintParams &pp); /** * Paints the foreground layer of the trace with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal - * @param right the x-coordinate of the right edge of the signal + * @param pp the painting parameters object to paint with. **/ - void paint_fore(QPainter &p, int left, int right); + void paint_fore(QPainter &p, const RowItemPaintParams &pp); void populate_popup_form(QWidget *parent, QFormLayout *form); @@ -128,7 +125,7 @@ public: private: void draw_annotation(const pv::data::decode::Annotation &a, QPainter &p, - QColor text_colour, int text_height, int left, int right, int y, + QColor text_colour, int text_height, const RowItemPaintParams &pp, int y, size_t base_colour) const; void draw_instant(const pv::data::decode::Annotation &a, QPainter &p, @@ -140,7 +137,7 @@ private: double end, int y) const; void draw_error(QPainter &p, const QString &message, - int left, int right); + const RowItemPaintParams &pp); void draw_unresolved_period(QPainter &p, int h, int left, int right) const; diff --git a/pv/view/logicsignal.cpp b/pv/view/logicsignal.cpp index fc81a4a9..af992c6b 100644 --- a/pv/view/logicsignal.cpp +++ b/pv/view/logicsignal.cpp @@ -145,13 +145,13 @@ std::pair LogicSignal::v_extents() const return make_pair(-SignalHeight - SignalMargin, SignalMargin); } -void LogicSignal::paint_back(QPainter &p, int left, int right) +void LogicSignal::paint_back(QPainter &p, const RowItemPaintParams &pp) { if (channel_->enabled()) - paint_axis(p, get_visual_y(), left, right); + paint_axis(p, get_visual_y(), pp.left(), pp.right()); } -void LogicSignal::paint_mid(QPainter &p, int left, int right) +void LogicSignal::paint_mid(QPainter &p, const RowItemPaintParams &pp) { using pv::view::View; @@ -161,7 +161,6 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right) assert(channel_); assert(data_); - assert(right >= left); assert(owner_); const int y = get_visual_y(); @@ -199,7 +198,7 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right) const int64_t last_sample = snapshot->get_sample_count() - 1; const double samples_per_pixel = samplerate * scale; const double start = samplerate * (offset - start_time); - const double end = start + samples_per_pixel * (right - left); + const double end = start + samples_per_pixel * pp.width(); snapshot->get_subsampled_edges(edges, min(max((int64_t)floor(start), (int64_t)0), last_sample), @@ -214,7 +213,7 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right) for (auto i = edges.cbegin() + 1; i != edges.cend() - 1; i++) { const float x = ((*i).first / samples_per_pixel - - pixels_offset) + left; + pixels_offset) + pp.left(); *line++ = QLineF(x, high_offset, x, low_offset); } @@ -228,18 +227,16 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right) p.setPen(HighColour); paint_caps(p, cap_lines, edges, true, samples_per_pixel, - pixels_offset, left, high_offset); + pixels_offset, pp.left(), high_offset); p.setPen(LowColour); paint_caps(p, cap_lines, edges, false, samples_per_pixel, - pixels_offset, left, low_offset); + pixels_offset, pp.left(), low_offset); delete[] cap_lines; } -void LogicSignal::paint_fore(QPainter &p, int left, int right) +void LogicSignal::paint_fore(QPainter &p, const RowItemPaintParams &pp) { - (void)left; - // Draw the trigger marker if (!trigger_match_) return; @@ -262,7 +259,7 @@ void LogicSignal::paint_fore(QPainter &p, int left, int right) const int pad = TriggerMarkerPadding; const QSize size = pixmap->size(); const QPoint point( - right - size.width() - pad * 2, + pp.right() - size.width() - pad * 2, y - (SignalHeight + size.height()) / 2); p.setPen(QPen(Qt::NoPen)); diff --git a/pv/view/logicsignal.hpp b/pv/view/logicsignal.hpp index 11f81770..c4588903 100644 --- a/pv/view/logicsignal.hpp +++ b/pv/view/logicsignal.hpp @@ -84,26 +84,23 @@ public: /** * Paints the background layer of the signal with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal. - * @param right the x-coordinate of the right edge of the signal. + * @param pp the painting parameters object to paint with.. **/ - void paint_back(QPainter &p, int left, int right); + void paint_back(QPainter &p, const RowItemPaintParams &pp); /** * Paints the mid-layer of the signal with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal. - * @param right the x-coordinate of the right edge of the signal. + * @param pp the painting parameters object to paint with.. **/ - void paint_mid(QPainter &p, int left, int right); + void paint_mid(QPainter &p, const RowItemPaintParams &pp); /** * Paints the foreground layer of the signal with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal - * @param right the x-coordinate of the right edge of the signal + * @param pp the painting parameters object to paint with. **/ - virtual void paint_fore(QPainter &p, int left, int right); + virtual void paint_fore(QPainter &p, const RowItemPaintParams &pp); private: void paint_caps(QPainter &p, QLineF *const lines, diff --git a/pv/view/rowitem.cpp b/pv/view/rowitem.cpp index 6e87de10..597c4a13 100644 --- a/pv/view/rowitem.cpp +++ b/pv/view/rowitem.cpp @@ -120,25 +120,22 @@ QPoint RowItem::point() const return QPoint(0, visual_v_offset()); } -void RowItem::paint_back(QPainter &p, int left, int right) +void RowItem::paint_back(QPainter &p, const RowItemPaintParams &pp) { (void)p; - (void)left; - (void)right; + (void)pp; } -void RowItem::paint_mid(QPainter &p, int left, int right) +void RowItem::paint_mid(QPainter &p, const RowItemPaintParams &pp) { (void)p; - (void)left; - (void)right; + (void)pp; } -void RowItem::paint_fore(QPainter &p, int left, int right) +void RowItem::paint_fore(QPainter &p, const RowItemPaintParams &pp) { (void)p; - (void)left; - (void)right; + (void)pp; } void RowItem::hover_point_changed() diff --git a/pv/view/rowitem.hpp b/pv/view/rowitem.hpp index d6233e8c..39c6f040 100644 --- a/pv/view/rowitem.hpp +++ b/pv/view/rowitem.hpp @@ -25,6 +25,7 @@ #include +#include "rowitempaintparams.hpp" #include "selectableitem.hpp" namespace pv { @@ -112,26 +113,23 @@ public: /** * Paints the background layer of the trace with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal - * @param right the x-coordinate of the right edge of the signal + * @param pp the painting parameters object to paint with. **/ - virtual void paint_back(QPainter &p, int left, int right); + virtual void paint_back(QPainter &p, const RowItemPaintParams &pp); /** * Paints the mid-layer of the trace with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal - * @param right the x-coordinate of the right edge of the signal + * @param pp the painting parameters object to paint with. **/ - virtual void paint_mid(QPainter &p, int left, int right); + virtual void paint_mid(QPainter &p, const RowItemPaintParams &pp); /** * Paints the foreground layer of the trace with a QPainter * @param p the QPainter to paint into. - * @param left the x-coordinate of the left edge of the signal - * @param right the x-coordinate of the right edge of the signal + * @param pp the painting parameters object to paint with. **/ - virtual void paint_fore(QPainter &p, int left, int right); + virtual void paint_fore(QPainter &p, const RowItemPaintParams &pp); /** * Paints the signal label. diff --git a/pv/view/rowitempaintparams.cpp b/pv/view/rowitempaintparams.cpp new file mode 100644 index 00000000..013c2a23 --- /dev/null +++ b/pv/view/rowitempaintparams.cpp @@ -0,0 +1,35 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "rowitempaintparams.hpp" + +namespace pv { +namespace view { + +RowItemPaintParams::RowItemPaintParams(int left, int right) : + left_(left), + right_(right) { + assert(left <= right); +} + +} // namespace view +} // namespace pv diff --git a/pv/view/rowitempaintparams.hpp b/pv/view/rowitempaintparams.hpp new file mode 100644 index 00000000..062b3cdd --- /dev/null +++ b/pv/view/rowitempaintparams.hpp @@ -0,0 +1,52 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2014 Joel Holdsworth + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef PULSEVIEW_PV_VIEW_ROWITEMPAINTPARAMS_H +#define PULSEVIEW_PV_VIEW_ROWITEMPAINTPARAMS_H + +namespace pv { +namespace view { + +class RowItemPaintParams +{ +public: + RowItemPaintParams(int left, int right); + + int left() const { + return left_; + } + + int right() const { + return right_; + } + + int width() const { + return right_ - left_; + } + +private: + int left_; + int right_; +}; + +} // namespace view +} // namespace pv + +#endif // PULSEVIEW_PV_VIEW_ROWITEMPAINTPARAMS_H diff --git a/pv/view/viewport.cpp b/pv/view/viewport.cpp index a94ada93..bc010b4b 100644 --- a/pv/view/viewport.cpp +++ b/pv/view/viewport.cpp @@ -22,6 +22,7 @@ #include #include +#include "rowitempaintparams.hpp" #include "signal.hpp" #include "view.hpp" #include "viewport.hpp" @@ -69,18 +70,20 @@ void Viewport::paintEvent(QPaintEvent*) if (view_.cursors_shown()) view_.cursors().draw_viewport_background(p, rect()); + const RowItemPaintParams pp(0, width()); + // Plot the signal for (const shared_ptr r : row_items) { assert(r); - r->paint_back(p, 0, width()); + r->paint_back(p, pp); } for (const shared_ptr r : row_items) - r->paint_mid(p, 0, width()); + r->paint_mid(p, pp); for (const shared_ptr r : row_items) - r->paint_fore(p, 0, width()); + r->paint_fore(p, pp); if (view_.cursors_shown()) view_.cursors().draw_viewport_foreground(p, rect()); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 10f26ddc..891d22cb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -47,6 +47,7 @@ set(pulseview_TEST_SOURCES ${PROJECT_SOURCE_DIR}/pv/view/logicsignal.cpp ${PROJECT_SOURCE_DIR}/pv/view/rowitem.cpp ${PROJECT_SOURCE_DIR}/pv/view/rowitemowner.cpp + ${PROJECT_SOURCE_DIR}/pv/view/rowitempaintparams.cpp ${PROJECT_SOURCE_DIR}/pv/view/ruler.cpp ${PROJECT_SOURCE_DIR}/pv/view/selectableitem.cpp ${PROJECT_SOURCE_DIR}/pv/view/signal.cpp