From e3374498baf2b2a05889cab370442ff326b390b3 Mon Sep 17 00:00:00 2001 From: Joel Holdsworth Date: Sat, 17 Nov 2012 08:13:31 +0000 Subject: [PATCH] Added signal (multi) selection --- pv/view/header.cpp | 78 +++++++++++++++++++++++++++++++++------------- pv/view/header.h | 6 ++++ pv/view/signal.cpp | 24 +++++++++++++- pv/view/signal.h | 13 ++++++++ 4 files changed, 98 insertions(+), 23 deletions(-) diff --git a/pv/view/header.cpp b/pv/view/header.cpp index 125d3c24..8434e2c8 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -28,6 +28,7 @@ #include +#include #include #include #include @@ -55,6 +56,29 @@ Header::Header(View &parent) : this, SLOT(on_action_set_colour_triggered())); } +boost::shared_ptr Header::get_mouse_over_signal( + const QPoint &pt) +{ + const int w = width(); + const vector< shared_ptr > &sigs = + _view.session().get_signals(); + + const int v_offset = _view.v_offset(); + BOOST_FOREACH(const shared_ptr s, sigs) + { + assert(s); + + const QRect signal_heading_rect( + 0, s->get_v_offset() - v_offset, + w, View::SignalHeight); + + if(s->pt_in_label_rect(signal_heading_rect, pt)) + return s; + } + + return shared_ptr(); +} + void Header::paintEvent(QPaintEvent *event) { const int w = width(); @@ -80,6 +104,29 @@ void Header::paintEvent(QPaintEvent *event) painter.end(); } +void Header::mousePressEvent(QMouseEvent *event) +{ + assert(event); + + const vector< shared_ptr > &sigs = + _view.session().get_signals(); + + if(~QApplication::keyboardModifiers() & Qt::ControlModifier) { + // Unselect all other signals because the Ctrl is not + // pressed + BOOST_FOREACH(const shared_ptr s, sigs) + s->select(false); + } + + // Select the signal if it has been clicked + const shared_ptr mouse_over_signal = + get_mouse_over_signal(event->pos()); + if(mouse_over_signal) + mouse_over_signal->select(!mouse_over_signal->selected()); + + update(); +} + void Header::mouseMoveEvent(QMouseEvent *event) { assert(event); @@ -95,31 +142,18 @@ void Header::leaveEvent(QEvent *event) void Header::contextMenuEvent(QContextMenuEvent *event) { - const int w = width(); - const vector< shared_ptr > &sigs = - _view.session().get_signals(); + const shared_ptr s = get_mouse_over_signal(_mouse_point); - const int v_offset = _view.v_offset(); - BOOST_FOREACH(const shared_ptr s, sigs) - { - assert(s); - - const QRect signal_heading_rect( - 0, s->get_v_offset() - v_offset, - w, View::SignalHeight); - - if(s->pt_in_label_rect(signal_heading_rect, _mouse_point)) { - QMenu menu(this); - menu.addAction(_action_set_name); - menu.addAction(_action_set_colour); + if(!s) + return; - _context_signal = s; - menu.exec(event->globalPos()); - _context_signal.reset(); + QMenu menu(this); + menu.addAction(_action_set_name); + menu.addAction(_action_set_colour); - break; - } - } + _context_signal = s; + menu.exec(event->globalPos()); + _context_signal.reset(); } void Header::on_action_set_name_triggered() diff --git a/pv/view/header.h b/pv/view/header.h index efc126e9..e5cd241f 100644 --- a/pv/view/header.h +++ b/pv/view/header.h @@ -38,10 +38,16 @@ class Header : public QWidget public: Header(View &parent); +private: + boost::shared_ptr get_mouse_over_signal( + const QPoint &pt); + private: void paintEvent(QPaintEvent *event); private: + void mousePressEvent(QMouseEvent * event); + void mouseMoveEvent(QMouseEvent *event); void leaveEvent(QEvent *event); diff --git a/pv/view/signal.cpp b/pv/view/signal.cpp index 02732ffc..8fed4273 100644 --- a/pv/view/signal.cpp +++ b/pv/view/signal.cpp @@ -20,6 +20,8 @@ #include +#include + #include "signal.h" #include "view.h" @@ -27,10 +29,12 @@ namespace pv { namespace view { const int Signal::LabelHitPadding = 2; +const int Signal::LabelHighlightRadius = 6; Signal::Signal(QString name) : _name(name), - _v_offset(0) + _v_offset(0), + _selected(false) { } @@ -64,6 +68,16 @@ void Signal::set_v_offset(int v_offset) _v_offset = v_offset; } +bool Signal::selected() const +{ + return _selected; +} + +void Signal::select(bool select) +{ + _selected = select; +} + void Signal::paint_label(QPainter &p, const QRect &rect, bool hover) { p.setBrush(_colour); @@ -91,6 +105,14 @@ void Signal::paint_label(QPainter &p, const QRect &rect, bool hover) QPointF(label_rect.left() + 1, label_rect.bottom() - 1) }; + if(_selected) { + p.setPen(QPen(QApplication::palette().brush( + QPalette::Highlight), LabelHighlightRadius, + Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); + p.setBrush(Qt::transparent); + p.drawPolygon(points, countof(points)); + } + p.setPen(Qt::transparent); p.setBrush(hover ? colour.lighter() : colour); p.drawPolygon(points, countof(points)); diff --git a/pv/view/signal.h b/pv/view/signal.h index b3212394..72d42cc2 100644 --- a/pv/view/signal.h +++ b/pv/view/signal.h @@ -40,6 +40,7 @@ class Signal { private: static const int LabelHitPadding; + static const int LabelHighlightRadius; protected: Signal(QString name); @@ -75,6 +76,16 @@ public: */ void set_v_offset(int v_offset); + /** + * Returns true if the signal has been selected by the user. + */ + bool selected() const; + + /** + * Selects or deselects the signal. + */ + void select(bool select = true); + /** * Paints the signal with a QPainter * @param p the QPainter to paint into. @@ -130,6 +141,8 @@ protected: QColor _colour; int _v_offset; + bool _selected; + QSizeF _text_size; }; -- 2.30.2