From: Joel Holdsworth Date: Sat, 18 May 2013 16:33:51 +0000 (+0100) Subject: Wrapped Cursors in a shared_ptr X-Git-Tag: pulseview-0.2.0~348 X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=58864c5c5dffac4254f199356aaeb5eabd608630 Wrapped Cursors in a shared_ptr --- diff --git a/pv/view/cursor.cpp b/pv/view/cursor.cpp index 7a4272f9..6eb575e3 100644 --- a/pv/view/cursor.cpp +++ b/pv/view/cursor.cpp @@ -33,6 +33,8 @@ #include +using namespace boost; + namespace pv { namespace view { @@ -45,14 +47,16 @@ const int Cursor::Offset = 1; const int Cursor::ArrowSize = 4; -Cursor::Cursor(const View &view, double time, Cursor &other) : - TimeMarker(view, LineColour, time), - _other(other) +Cursor::Cursor(const View &view, double time) : + TimeMarker(view, LineColour, time) { } QRectF Cursor::get_label_rect(const QRect &rect) const { + const shared_ptr other(get_other_cursor()); + assert(other); + const float x = (_time - _view.offset()) / _view.scale(); const QSizeF label_size( @@ -62,7 +66,7 @@ QRectF Cursor::get_label_rect(const QRect &rect) const Cursor::Offset - Cursor::ArrowSize - 0.5f; const float height = label_size.height(); - if (_time > _other.time()) + if (_time > other->time()) return QRectF(x, top, label_size.width(), height); else return QRectF(x - label_size.width(), top, @@ -72,6 +76,9 @@ QRectF Cursor::get_label_rect(const QRect &rect) const void Cursor::paint_label(QPainter &p, const QRect &rect, unsigned int prefix) { + const shared_ptr other(get_other_cursor()); + assert(other); + compute_text_size(p, prefix); const QRectF r(get_label_rect(rect)); @@ -107,9 +114,9 @@ void Cursor::paint_label(QPainter &p, const QRect &rect, QPointF(r.right() - 1, rect.bottom() - 1), }; - const QPointF *const points = (_time > _other.time()) ? + const QPointF *const points = (_time > other->time()) ? left_points : right_points; - const QPointF *const highlight_points = (_time > _other.time()) ? + const QPointF *const highlight_points = (_time > other->time()) ? left_highlight_points : right_highlight_points; if (selected()) { @@ -141,5 +148,12 @@ void Cursor::compute_text_size(QPainter &p, unsigned int prefix) Ruler::format_time(_time, prefix, 2)).size(); } +shared_ptr Cursor::get_other_cursor() const +{ + const CursorPair &cursors = _view.cursors(); + return (cursors.first().get() == this) ? + cursors.second() : cursors.first(); +} + } // namespace view } // namespace pv diff --git a/pv/view/cursor.h b/pv/view/cursor.h index 48bf7a0e..89ea1e71 100644 --- a/pv/view/cursor.h +++ b/pv/view/cursor.h @@ -23,6 +23,8 @@ #include "timemarker.h" +#include + #include class QPainter; @@ -49,9 +51,8 @@ public: * Constructor. * @param view A reference to the view that owns this cursor pair. * @param time The time to set the flag to. - * @param other A reference to the other cursor. */ - Cursor(const View &view, double time, Cursor &other); + Cursor(const View &view, double time); public: /** @@ -73,9 +74,9 @@ public: private: void compute_text_size(QPainter &p, unsigned int prefix); -private: - const Cursor &_other; + boost::shared_ptr get_other_cursor() const; +private: QSizeF _text_size; }; diff --git a/pv/view/cursorpair.cpp b/pv/view/cursorpair.cpp index d8af3d76..70c7bac9 100644 --- a/pv/view/cursorpair.cpp +++ b/pv/view/cursorpair.cpp @@ -25,6 +25,7 @@ #include +using namespace boost; using namespace std; namespace pv { @@ -33,28 +34,18 @@ namespace view { const int CursorPair::DeltaPadding = 8; CursorPair::CursorPair(const View &view) : - _first(view, 0.0, _second), - _second(view, 1.0, _first), + _first(new Cursor(view, 0.0)), + _second(new Cursor(view, 1.0)), _view(view) { } -const Cursor& CursorPair::first() const +shared_ptr CursorPair::first() const { return _first; } -Cursor& CursorPair::first() -{ - return _first; -} - -const Cursor& CursorPair::second() const -{ - return _second; -} - -Cursor& CursorPair::second() +shared_ptr CursorPair::second() const { return _second; } @@ -82,6 +73,9 @@ QRectF CursorPair::get_label_rect(const QRect &rect) const void CursorPair::draw_markers(QPainter &p, const QRect &rect, unsigned int prefix) { + assert(_first); + assert(_second); + compute_text_size(p, prefix); QRectF delta_rect(get_label_rect(rect)); @@ -102,12 +96,12 @@ void CursorPair::draw_markers(QPainter &p, p.setPen(Cursor::TextColour); p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, - Ruler::format_time(_second.time() - _first.time(), prefix, 2)); + Ruler::format_time(_second->time() - _first->time(), prefix, 2)); } // Paint the cursor markers - _first.paint_label(p, rect, prefix); - _second.paint_label(p, rect, prefix); + _first->paint_label(p, rect, prefix); + _second->paint_label(p, rect, prefix); } void CursorPair::draw_viewport_background(QPainter &p, @@ -128,21 +122,30 @@ void CursorPair::draw_viewport_background(QPainter &p, void CursorPair::draw_viewport_foreground(QPainter &p, const QRect &rect) { - _first.paint(p, rect); - _second.paint(p, rect); + assert(_first); + assert(_second); + + _first->paint(p, rect); + _second->paint(p, rect); } void CursorPair::compute_text_size(QPainter &p, unsigned int prefix) { + assert(_first); + assert(_second); + _text_size = p.boundingRect(QRectF(), 0, Ruler::format_time( - _second.time() - _first.time(), prefix, 2)).size(); + _second->time() - _first->time(), prefix, 2)).size(); } pair CursorPair::get_cursor_offsets() const { + assert(_first); + assert(_second); + return pair( - (_first.time() - _view.offset()) / _view.scale(), - (_second.time() - _view.offset()) / _view.scale()); + (_first->time() - _view.offset()) / _view.scale(), + (_second->time() - _view.offset()) / _view.scale()); } } // namespace view diff --git a/pv/view/cursorpair.h b/pv/view/cursorpair.h index 42e6e51f..2e70f7da 100644 --- a/pv/view/cursorpair.h +++ b/pv/view/cursorpair.h @@ -23,6 +23,8 @@ #include "cursor.h" +#include + #include class QPainter; @@ -43,24 +45,14 @@ public: CursorPair(const View &view); /** - * Returns a reference to the first cursor. - */ - Cursor& first(); - - /** - * Returns a reference to the first cursor. - */ - const Cursor& first() const; - - /** - * Returns a reference to the second cursor. + * Returns a pointer to the first cursor. */ - Cursor& second(); + boost::shared_ptr first() const; /** - * Returns a reference to the second cursor. + * Returns a pointer to the second cursor. */ - const Cursor& second() const; + boost::shared_ptr second() const; public: QRectF get_label_rect(const QRect &rect) const; @@ -77,7 +69,7 @@ public: std::pair get_cursor_offsets() const; private: - Cursor _first, _second; + boost::shared_ptr _first, _second; const View &_view; QSizeF _text_size; diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index fa5c488e..74bc1f97 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -34,6 +34,7 @@ #include #include +using namespace boost; using namespace std; namespace pv { @@ -49,8 +50,7 @@ const int Ruler::FirstSIPrefixPower = -15; const int Ruler::HoverArrowSize = 5; Ruler::Ruler(View &parent) : - MarginWidget(parent), - _grabbed_marker(NULL) + MarginWidget(parent) { setMouseTracking(true); @@ -61,8 +61,8 @@ Ruler::Ruler(View &parent) : void Ruler::clear_selection() { CursorPair &cursors = _view.cursors(); - cursors.first().select(false); - cursors.second().select(false); + cursors.first()->select(false); + cursors.second()->select(false); update(); } @@ -181,32 +181,30 @@ void Ruler::paintEvent(QPaintEvent*) void Ruler::mouseMoveEvent(QMouseEvent *e) { - if (!_grabbed_marker) - return; - - _grabbed_marker->set_time(_view.offset() + - ((double)e->x() + 0.5) * _view.scale()); + if (shared_ptr m = _grabbed_marker.lock()) + m->set_time(_view.offset() + + ((double)e->x() + 0.5) * _view.scale()); } void Ruler::mousePressEvent(QMouseEvent *e) { if (e->buttons() & Qt::LeftButton) { - _grabbed_marker = NULL; + _grabbed_marker.reset(); clear_selection(); if (_view.cursors_shown()) { CursorPair &cursors = _view.cursors(); - if (cursors.first().get_label_rect( + if (cursors.first()->get_label_rect( rect()).contains(e->pos())) - _grabbed_marker = &cursors.first(); - else if (cursors.second().get_label_rect( + _grabbed_marker = cursors.first(); + else if (cursors.second()->get_label_rect( rect()).contains(e->pos())) - _grabbed_marker = &cursors.second(); + _grabbed_marker = cursors.second(); } - if(_grabbed_marker) - _grabbed_marker->select(); + if (shared_ptr m = _grabbed_marker.lock()) + m->select(); selection_changed(); } @@ -214,14 +212,14 @@ void Ruler::mousePressEvent(QMouseEvent *e) void Ruler::mouseReleaseEvent(QMouseEvent *) { - _grabbed_marker = NULL; + _grabbed_marker.reset(); } void Ruler::draw_hover_mark(QPainter &p) { const int x = _view.hover_point().x(); - if (x == -1 || _grabbed_marker) + if (x == -1 || !_grabbed_marker.expired()) return; p.setPen(QPen(Qt::NoPen)); diff --git a/pv/view/ruler.h b/pv/view/ruler.h index ba03f312..7514aeca 100644 --- a/pv/view/ruler.h +++ b/pv/view/ruler.h @@ -21,6 +21,8 @@ #ifndef PULSEVIEW_PV_VIEW_RULER_H #define PULSEVIEW_PV_VIEW_RULER_H +#include + #include "marginwidget.h" namespace pv { @@ -67,7 +69,7 @@ private slots: void hover_point_changed(); private: - TimeMarker *_grabbed_marker; + boost::weak_ptr _grabbed_marker; }; } // namespace view diff --git a/pv/view/view.cpp b/pv/view/view.cpp index bc82e0b4..eca6b8ea 100644 --- a/pv/view/view.cpp +++ b/pv/view/view.cpp @@ -85,9 +85,9 @@ View::View(SigSession &session, QWidget *parent) : connect(&_session, SIGNAL(data_updated()), this, SLOT(data_updated())); - connect(&_cursors.first(), SIGNAL(time_changed()), + connect(_cursors.first().get(), SIGNAL(time_changed()), this, SLOT(marker_time_changed())); - connect(&_cursors.second(), SIGNAL(time_changed()), + connect(_cursors.second().get(), SIGNAL(time_changed()), this, SLOT(marker_time_changed())); connect(_header, SIGNAL(signals_moved()), @@ -174,8 +174,8 @@ void View::show_cursors(bool show) void View::centre_cursors() { const double time_width = _scale * _viewport->width(); - _cursors.first().set_time(_offset + time_width * 0.4); - _cursors.second().set_time(_offset + time_width * 0.6); + _cursors.first()->set_time(_offset + time_width * 0.4); + _cursors.second()->set_time(_offset + time_width * 0.6); _ruler->update(); _viewport->update(); } @@ -185,6 +185,11 @@ CursorPair& View::cursors() return _cursors; } +const CursorPair& View::cursors() const +{ + return _cursors; +} + const QPoint& View::hover_point() const { return _hover_point; diff --git a/pv/view/view.h b/pv/view/view.h index 18da7322..155c875b 100644 --- a/pv/view/view.h +++ b/pv/view/view.h @@ -106,6 +106,11 @@ public: */ CursorPair& cursors(); + /** + * Returns a reference to the pair of cursors. + */ + const CursorPair& cursors() const; + const QPoint& hover_point() const; void normalize_layout();