From: Joel Holdsworth Date: Sat, 8 Jun 2013 15:39:10 +0000 (+0100) Subject: Refactored Signal into Trace X-Git-Tag: pulseview-0.2.0~326 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=931f20b0dbd480153611493f51fee68f9d29be74;p=pulseview.git Refactored Signal into Trace This will be used for non-signal traces such as the decode trace. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index dacb9723..01c3660d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,6 +137,7 @@ set(pulseview_SOURCES pv/view/selectableitem.cpp pv/view/signal.cpp pv/view/timemarker.cpp + pv/view/trace.cpp pv/view/view.cpp pv/view/viewport.cpp ) @@ -158,6 +159,7 @@ set(pulseview_HEADERS pv/view/selectableitem.h pv/view/signal.h pv/view/timemarker.h + pv/view/trace.h pv/view/view.h pv/view/viewport.h ) diff --git a/pv/view/signal.cpp b/pv/view/signal.cpp index ab1426e0..9b08336e 100644 --- a/pv/view/signal.cpp +++ b/pv/view/signal.cpp @@ -31,8 +31,6 @@ namespace pv { namespace view { -const int Signal::LabelHitPadding = 2; - const QPen Signal::SignalAxisPen(QColor(128, 128, 128, 64)); const char *const ProbeNames[] = { @@ -53,10 +51,8 @@ const char *const ProbeNames[] = { }; Signal::Signal(pv::SigSession &session, const sr_probe *const probe) : - _session(session), + Trace(session, probe->name), _probe(probe), - _name(probe->name), - _v_offset(0), _name_action(NULL), _name_widget(), _updating_name_widget(false) @@ -74,102 +70,17 @@ Signal::Signal(pv::SigSession &session, const sr_probe *const probe) : this, SLOT(on_text_changed(const QString&))); } -QString Signal::get_name() const -{ - return _name; -} - void Signal::set_name(QString name) { - _name = name; + Trace::set_name(name); _updating_name_widget = true; _name_widget.setEditText(name); _updating_name_widget = false; } -QColor Signal::get_colour() const -{ - return _colour; -} - -void Signal::set_colour(QColor colour) -{ - _colour = colour; -} - -int Signal::get_v_offset() const -{ - return _v_offset; -} - -void Signal::set_v_offset(int v_offset) +bool Signal::enabled() const { - _v_offset = v_offset; -} - -void Signal::paint_label(QPainter &p, int y, int right, bool hover) -{ - p.setBrush(_colour); - - if (!_probe->enabled) - return; - - const QColor colour = get_colour(); - - compute_text_size(p); - const QRectF label_rect = get_label_rect(y, right); - - // Paint the label - const QPointF points[] = { - label_rect.topLeft(), - label_rect.topRight(), - QPointF(right, y), - label_rect.bottomRight(), - label_rect.bottomLeft() - }; - - const QPointF highlight_points[] = { - QPointF(label_rect.left() + 1, label_rect.top() + 1), - QPointF(label_rect.right(), label_rect.top() + 1), - QPointF(right - 1, y), - QPointF(label_rect.right(), label_rect.bottom() - 1), - QPointF(label_rect.left() + 1, label_rect.bottom() - 1) - }; - - if (selected()) { - p.setPen(highlight_pen()); - p.setBrush(Qt::transparent); - p.drawPolygon(points, countof(points)); - } - - p.setPen(Qt::transparent); - p.setBrush(hover ? colour.lighter() : colour); - p.drawPolygon(points, countof(points)); - - p.setPen(colour.lighter()); - p.setBrush(Qt::transparent); - p.drawPolygon(highlight_points, countof(highlight_points)); - - p.setPen(colour.darker()); - p.setBrush(Qt::transparent); - p.drawPolygon(points, countof(points)); - - // Paint the text - p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white); - p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); -} - -bool Signal::pt_in_label_rect(int y, int left, int right, - const QPoint &point) -{ - (void)left; - - const QRectF label = get_label_rect(y, right); - return QRectF( - QPointF(label.left() - LabelHitPadding, - label.top() - LabelHitPadding), - QPointF(right, label.bottom() + LabelHitPadding) - ).contains(point); + return _probe->enabled; } void Signal::paint_axis(QPainter &p, int y, int left, int right) @@ -178,30 +89,9 @@ void Signal::paint_axis(QPainter &p, int y, int left, int right) p.drawLine(QPointF(left, y + 0.5f), QPointF(right, y + 0.5f)); } -void Signal::compute_text_size(QPainter &p) -{ - _text_size = QSize( - p.boundingRect(QRectF(), 0, _name).width(), - p.boundingRect(QRectF(), 0, "Tg").height()); -} - -QRectF Signal::get_label_rect(int y, int right) -{ - using pv::view::View; - - const QSizeF label_size( - _text_size.width() + View::LabelPadding.width() * 2, - ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2); - const float label_arrow_length = label_size.height() / 2; - return QRectF( - right - label_arrow_length - label_size.width() - 0.5, - y + 0.5f - label_size.height() / 2, - label_size.width(), label_size.height()); -} - void Signal::on_text_changed(const QString &text) { - _name = text; + Trace::set_name(text); text_changed(); } diff --git a/pv/view/signal.h b/pv/view/signal.h index eeef5538..81576dd4 100644 --- a/pv/view/signal.h +++ b/pv/view/signal.h @@ -21,110 +21,44 @@ #ifndef PULSEVIEW_PV_SIGNAL_H #define PULSEVIEW_PV_SIGNAL_H -#include - -#include #include -#include #include -#include -#include #include #include #include -#include "selectableitem.h" +#include "trace.h" namespace pv { -class SigSession; - namespace data { class SignalData; } namespace view { -class Signal : public SelectableItem +class Signal : public Trace { Q_OBJECT private: - static const int LabelHitPadding; - static const QPen SignalAxisPen; protected: Signal(pv::SigSession &session, const sr_probe *const probe); public: - /** - * Gets the name of this signal. - */ - QString get_name() const; - /** * Sets the name of the signal. */ void set_name(QString name); /** - * Get the colour of the signal. - */ - QColor get_colour() const; - - /** - * Set the colour of the signal. - */ - void set_colour(QColor colour); - - /** - * Gets the vertical layout offset of this signal. - */ - int get_v_offset() const; - - /** - * Sets the vertical layout offset of this signal. - */ - void set_v_offset(int v_offset); - - /** - * Paints the signal with a QPainter - * @param p the QPainter to paint into. - * @param y the y-coordinate to draw the signal at - * @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 scale the scale in seconds per pixel. - * @param offset the time to show at the left hand edge of - * the view in seconds. - **/ - virtual void paint(QPainter &p, int y, int left, int right, - double scale, double offset) = 0; - - /** - * Paints the signal label into a QGLWidget. - * @param p the QPainter to paint into. - * @param y the y-coordinate of the signal. - * @param right the x-coordinate of the right edge of the header - * area. - * @param hover true if the label is being hovered over by the mouse. + * Returns true if the trace is visible and enabled. */ - virtual void paint_label(QPainter &p, int y, int right, - bool hover); - - /** - * Determines if a point is in the header label rect. - * @param y the y-coordinate of the signal. - * @param left the x-coordinate of the left edge of the header - * area. - * @param right the x-coordinate of the right edge of the header - * area. - * @param point the point to test. - */ - bool pt_in_label_rect(int y, int left, int right, - const QPoint &point); + bool enabled() const; protected: @@ -137,39 +71,12 @@ protected: */ void paint_axis(QPainter &p, int y, int left, int right); -private: - - /** - * Computes an caches the size of the label text. - */ - void compute_text_size(QPainter &p); - - /** - * Computes the outline rectangle of a label. - * @param p the QPainter to lay out text with. - * @param y the y-coordinate of the signal. - * @param right the x-coordinate of the right edge of the header - * area. - * @return Returns the rectangle of the signal label. - */ - QRectF get_label_rect(int y, int right); - private slots: void on_text_changed(const QString &text); -signals: - void text_changed(); - protected: - pv::SigSession &_session; const sr_probe *const _probe; - QString _name; - QColor _colour; - int _v_offset; - - QSizeF _text_size; - QWidgetAction _name_action; QComboBox _name_widget; bool _updating_name_widget; diff --git a/pv/view/trace.cpp b/pv/view/trace.cpp new file mode 100644 index 00000000..4de3f141 --- /dev/null +++ b/pv/view/trace.cpp @@ -0,0 +1,158 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2013 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 +#include + +#include "trace.h" +#include "view.h" + +namespace pv { +namespace view { + +const int Trace::LabelHitPadding = 2; + +Trace::Trace(pv::SigSession &session, QString name) : + _session(session), + _name(name), + _v_offset(0) +{ +} + +QString Trace::get_name() const +{ + return _name; +} + +void Trace::set_name(QString name) +{ + _name = name; +} + +QColor Trace::get_colour() const +{ + return _colour; +} + +void Trace::set_colour(QColor colour) +{ + _colour = colour; +} + +int Trace::get_v_offset() const +{ + return _v_offset; +} + +void Trace::set_v_offset(int v_offset) +{ + _v_offset = v_offset; +} + +void Trace::paint_label(QPainter &p, int y, int right, bool hover) +{ + p.setBrush(_colour); + + if (!enabled()) + return; + + const QColor colour = get_colour(); + + compute_text_size(p); + const QRectF label_rect = get_label_rect(y, right); + + // Paint the label + const QPointF points[] = { + label_rect.topLeft(), + label_rect.topRight(), + QPointF(right, y), + label_rect.bottomRight(), + label_rect.bottomLeft() + }; + + const QPointF highlight_points[] = { + QPointF(label_rect.left() + 1, label_rect.top() + 1), + QPointF(label_rect.right(), label_rect.top() + 1), + QPointF(right - 1, y), + QPointF(label_rect.right(), label_rect.bottom() - 1), + QPointF(label_rect.left() + 1, label_rect.bottom() - 1) + }; + + if (selected()) { + p.setPen(highlight_pen()); + p.setBrush(Qt::transparent); + p.drawPolygon(points, countof(points)); + } + + p.setPen(Qt::transparent); + p.setBrush(hover ? colour.lighter() : colour); + p.drawPolygon(points, countof(points)); + + p.setPen(colour.lighter()); + p.setBrush(Qt::transparent); + p.drawPolygon(highlight_points, countof(highlight_points)); + + p.setPen(colour.darker()); + p.setBrush(Qt::transparent); + p.drawPolygon(points, countof(points)); + + // Paint the text + p.setPen((colour.lightness() > 64) ? Qt::black : Qt::white); + p.drawText(label_rect, Qt::AlignCenter | Qt::AlignVCenter, _name); +} + +bool Trace::pt_in_label_rect(int y, int left, int right, + const QPoint &point) +{ + (void)left; + + const QRectF label = get_label_rect(y, right); + return QRectF( + QPointF(label.left() - LabelHitPadding, + label.top() - LabelHitPadding), + QPointF(right, label.bottom() + LabelHitPadding) + ).contains(point); +} + +void Trace::compute_text_size(QPainter &p) +{ + _text_size = QSize( + p.boundingRect(QRectF(), 0, _name).width(), + p.boundingRect(QRectF(), 0, "Tg").height()); +} + +QRectF Trace::get_label_rect(int y, int right) +{ + using pv::view::View; + + const QSizeF label_size( + _text_size.width() + View::LabelPadding.width() * 2, + ceilf((_text_size.height() + View::LabelPadding.height() * 2) / 2) * 2); + const float label_arrow_length = label_size.height() / 2; + return QRectF( + right - label_arrow_length - label_size.width() - 0.5, + y + 0.5f - label_size.height() / 2, + label_size.width(), label_size.height()); +} + +} // namespace view +} // namespace pv diff --git a/pv/view/trace.h b/pv/view/trace.h new file mode 100644 index 00000000..61433e9c --- /dev/null +++ b/pv/view/trace.h @@ -0,0 +1,154 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2013 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_TRACE_H +#define PULSEVIEW_PV_TRACE_H + +#include +#include +#include +#include + +#include + +#include "selectableitem.h" + +namespace pv { + +class SigSession; + +namespace view { + +class Trace : public SelectableItem +{ + Q_OBJECT + +private: + static const int LabelHitPadding; + +protected: + Trace(SigSession &session, QString name); + +public: + /** + * Gets the name of this signal. + */ + QString get_name() const; + + /** + * Sets the name of the signal. + */ + virtual void set_name(QString name); + + /** + * Get the colour of the signal. + */ + QColor get_colour() const; + + /** + * Set the colour of the signal. + */ + void set_colour(QColor colour); + + /** + * Gets the vertical layout offset of this signal. + */ + int get_v_offset() const; + + /** + * Sets the vertical layout offset of this signal. + */ + void set_v_offset(int v_offset); + + /** + * Returns true if the trace is visible and enabled. + */ + virtual bool enabled() const = 0; + + /** + * Paints the trace with a QPainter + * @param p the QPainter to paint into. + * @param y the y-coordinate to draw the signal at + * @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 scale the scale in seconds per pixel. + * @param offset the time to show at the left hand edge of + * the view in seconds. + **/ + virtual void paint(QPainter &p, int y, int left, int right, + double scale, double offset) = 0; + + /** + * Paints the signal label into a QGLWidget. + * @param p the QPainter to paint into. + * @param y the y-coordinate of the signal. + * @param right the x-coordinate of the right edge of the header + * area. + * @param hover true if the label is being hovered over by the mouse. + */ + virtual void paint_label(QPainter &p, int y, int right, + bool hover); + + /** + * Determines if a point is in the header label rect. + * @param y the y-coordinate of the signal. + * @param left the x-coordinate of the left edge of the header + * area. + * @param right the x-coordinate of the right edge of the header + * area. + * @param point the point to test. + */ + bool pt_in_label_rect(int y, int left, int right, + const QPoint &point); + +private: + + /** + * Computes an caches the size of the label text. + */ + void compute_text_size(QPainter &p); + + /** + * Computes the outline rectangle of a label. + * @param p the QPainter to lay out text with. + * @param y the y-coordinate of the signal. + * @param right the x-coordinate of the right edge of the header + * area. + * @return Returns the rectangle of the signal label. + */ + QRectF get_label_rect(int y, int right); + +signals: + void text_changed(); + +protected: + pv::SigSession &_session; + + QString _name; + QColor _colour; + int _v_offset; + + QSizeF _text_size; +}; + +} // namespace view +} // namespace pv + +#endif // PULSEVIEW_PV_TRACEL_H