From: Joel Holdsworth Date: Sat, 20 Oct 2012 10:10:54 +0000 (+0100) Subject: Added a context menu for set name X-Git-Tag: pulseview-0.1.0~241 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=49f8ff3fd225c72934eca725eacffb12b6542ebf;p=pulseview.git Added a context menu for set name --- diff --git a/pv/signal.cpp b/pv/signal.cpp index 091dd8d4..aeb693b7 100644 --- a/pv/signal.cpp +++ b/pv/signal.cpp @@ -37,6 +37,11 @@ QString Signal::get_name() const return _name; } +void Signal::set_name(QString name) +{ + _name = name; +} + void Signal::paint_label(QPainter &p, const QRect &rect, bool hover) { p.setBrush(get_colour()); diff --git a/pv/signal.h b/pv/signal.h index b4c4244c..18ff4b4d 100644 --- a/pv/signal.h +++ b/pv/signal.h @@ -40,8 +40,16 @@ protected: Signal(QString name); public: + /** + * Gets the name of this signal. + */ QString get_name() const; + /** + * Sets the name of the signal. + */ + void set_name(QString name); + /** * Paints the signal with a QPainter * @param p the QPainter to paint into. diff --git a/pv/view/header.cpp b/pv/view/header.cpp index 8fe9d485..1c7903db 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -28,6 +28,8 @@ #include +#include +#include #include #include #include @@ -40,9 +42,13 @@ namespace view { Header::Header(View &parent) : QWidget(&parent), - _view(parent) + _view(parent), + _action_set_name(new QAction(tr("Set &Name..."), this)) { setMouseTracking(true); + + connect(_action_set_name, SIGNAL(triggered()), + this, SLOT(on_action_set_name_triggered())); } void Header::paintEvent(QPaintEvent *event) @@ -84,5 +90,47 @@ void Header::leaveEvent(QEvent *event) update(); } +void Header::contextMenuEvent(QContextMenuEvent *event) +{ + const int w = width(); + const vector< shared_ptr > &sigs = + _view.session().get_signals(); + + int offset = -_view.v_offset(); + BOOST_FOREACH(const shared_ptr s, sigs) + { + assert(s); + + const QRect signal_heading_rect( + 0, offset, w, View::SignalHeight); + + if(s->pt_in_label_rect(signal_heading_rect, _mouse_point)) { + QMenu menu(this); + menu.addAction(_action_set_name); + + _context_signal = s; + menu.exec(event->globalPos()); + _context_signal.reset(); + + break; + } + + offset += View::SignalHeight; + } +} + +void Header::on_action_set_name_triggered() +{ + boost::shared_ptr context_signal = _context_signal; + if(!context_signal) + return; + + const QString new_label = QInputDialog::getText(this, tr("Set Name"), + tr("Name"), QLineEdit::Normal, context_signal->get_name()); + + if(!new_label.isEmpty()) + context_signal->set_name(new_label); +} + } // namespace view } // namespace pv diff --git a/pv/view/header.h b/pv/view/header.h index 5693a8a0..a92d1173 100644 --- a/pv/view/header.h +++ b/pv/view/header.h @@ -21,9 +21,14 @@ #ifndef PV_VIEW_HEADER_H #define PV_VIEW_HEADER_H +#include + #include namespace pv { + +class Signal; + namespace view { class View; @@ -43,10 +48,18 @@ private: void leaveEvent(QEvent *event); + void contextMenuEvent(QContextMenuEvent *event); + +private slots: + void on_action_set_name_triggered(); + private: View &_view; QPoint _mouse_point; + + boost::shared_ptr _context_signal; + QAction *_action_set_name; }; } // namespace view