From: Joel Holdsworth Date: Sun, 8 Sep 2013 13:06:24 +0000 (+0100) Subject: Moved context menu functionality into SelectableItem family X-Git-Tag: pulseview-0.2.0~283 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=9b6378f147671e62f57887d51d01864d20e01475;p=pulseview.git Moved context menu functionality into SelectableItem family --- diff --git a/pv/view/header.cpp b/pv/view/header.cpp index b85b1bb1..e084ecc9 100644 --- a/pv/view/header.cpp +++ b/pv/view/header.cpp @@ -29,8 +29,6 @@ #include #include -#include -#include #include #include #include @@ -43,17 +41,10 @@ namespace pv { namespace view { Header::Header(View &parent) : - MarginWidget(parent), - _action_set_name(new QAction(tr("Set &Name..."), this)), - _action_set_colour(new QAction(tr("Set &Colour..."), this)) + MarginWidget(parent) { setMouseTracking(true); - connect(_action_set_name, SIGNAL(triggered()), - this, SLOT(on_action_set_name_triggered())); - connect(_action_set_colour, SIGNAL(triggered()), - this, SLOT(on_action_set_colour_triggered())); - connect(&_view.session(), SIGNAL(signals_changed()), this, SLOT(on_signals_changed())); @@ -209,44 +200,8 @@ void Header::contextMenuEvent(QContextMenuEvent *event) { const shared_ptr t = get_mouse_over_trace(_mouse_point); - if (!t) - return; - - QMenu menu(this); - menu.addAction(_action_set_name); - menu.addAction(_action_set_colour); - - _context_trace = t; - menu.exec(event->globalPos()); - _context_trace.reset(); -} - -void Header::on_action_set_name_triggered() -{ - bool ok = false; - - shared_ptr context_trace = _context_trace; - if (!context_trace) - return; - - const QString new_label = QInputDialog::getText(this, tr("Set Name"), - tr("Name"), QLineEdit::Normal, context_trace->get_name(), &ok); - - if (ok) - context_trace->set_name(new_label); -} - -void Header::on_action_set_colour_triggered() -{ - shared_ptr context_trace = _context_trace; - if (!context_trace) - return; - - const QColor new_colour = QColorDialog::getColor( - context_trace->get_colour(), this, tr("Set Colour")); - - if (new_colour.isValid()) - context_trace->set_colour(new_colour); + if (t) + t->create_context_menu(this)->exec(event->globalPos()); } void Header::on_signals_changed() diff --git a/pv/view/header.h b/pv/view/header.h index 7a180359..35d5d20f 100644 --- a/pv/view/header.h +++ b/pv/view/header.h @@ -65,10 +65,6 @@ private: private slots: void on_signals_changed(); - void on_action_set_name_triggered(); - - void on_action_set_colour_triggered(); - void on_signals_moved(); signals: @@ -80,10 +76,6 @@ private: std::list, int> > _drag_traces; - - boost::shared_ptr _context_trace; - QAction *_action_set_name; - QAction *_action_set_colour; }; } // namespace view diff --git a/pv/view/selectableitem.cpp b/pv/view/selectableitem.cpp index 8dfdd8aa..31c45ef3 100644 --- a/pv/view/selectableitem.cpp +++ b/pv/view/selectableitem.cpp @@ -21,6 +21,7 @@ #include "selectableitem.h" #include +#include #include namespace pv { @@ -29,6 +30,7 @@ namespace view { const int SelectableItem::HighlightRadius = 6; SelectableItem::SelectableItem() : + _context_parent(NULL), _selected(false) { } @@ -43,6 +45,12 @@ void SelectableItem::select(bool select) _selected = select; } +QMenu* SelectableItem::create_context_menu(QWidget *parent) +{ + _context_parent = parent; + return new QMenu(parent); +} + QPen SelectableItem::highlight_pen() { return QPen(QApplication::palette().brush( diff --git a/pv/view/selectableitem.h b/pv/view/selectableitem.h index 467da0f2..323c87e1 100644 --- a/pv/view/selectableitem.h +++ b/pv/view/selectableitem.h @@ -26,6 +26,8 @@ #include class QAction; +class QMenu; +class QWidget; namespace pv { namespace view { @@ -56,9 +58,14 @@ public: virtual const std::list get_context_bar_actions() = 0; + virtual QMenu* create_context_menu(QWidget *parent); + protected: static QPen highlight_pen(); +protected: + QWidget *_context_parent; + private: bool _selected; }; diff --git a/pv/view/trace.cpp b/pv/view/trace.cpp index b3ebd95b..67ea3a02 100644 --- a/pv/view/trace.cpp +++ b/pv/view/trace.cpp @@ -23,6 +23,9 @@ #include #include +#include +#include + #include "trace.h" #include "view.h" @@ -163,6 +166,23 @@ bool Trace::pt_in_label_rect(int left, int right, const QPoint &point) ).contains(point); } +QMenu* Trace::create_context_menu(QWidget *parent) +{ + QMenu *const menu = SelectableItem::create_context_menu(parent); + + QAction *const set_name = new QAction(tr("Set &Name..."), this); + connect(set_name, SIGNAL(triggered()), + this, SLOT(on_action_set_name_triggered())); + menu->addAction(set_name); + + QAction *const set_colour = new QAction(tr("Set &Colour..."), this); + connect(set_colour, SIGNAL(triggered()), + this, SLOT(on_action_set_colour_triggered())); + menu->addAction(set_colour); + + return menu; +} + int Trace::get_y() const { return _v_offset - _view->v_offset(); @@ -203,5 +223,27 @@ QRectF Trace::get_label_rect(int right) label_size.width(), label_size.height()); } +void Trace::on_action_set_name_triggered() +{ + bool ok = false; + + const QString new_label = QInputDialog::getText(_context_parent, + tr("Set Name"), tr("Name"), QLineEdit::Normal, get_name(), + &ok); + + if (ok) + set_name(new_label); +} + +void Trace::on_action_set_colour_triggered() +{ + const QColor new_colour = QColorDialog::getColor( + get_colour(), _context_parent, tr("Set Colour")); + + if (new_colour.isValid()) + set_colour(new_colour); +} + + } // namespace view } // namespace pv diff --git a/pv/view/trace.h b/pv/view/trace.h index 415a7dc2..631c8d9c 100644 --- a/pv/view/trace.h +++ b/pv/view/trace.h @@ -131,6 +131,8 @@ public: */ bool pt_in_label_rect(int left, int right, const QPoint &point); + virtual QMenu* create_context_menu(QWidget *parent); + protected: /** * Gets the y-offset of the axis. @@ -170,6 +172,11 @@ private: */ QRectF get_label_rect(int right); +private slots: + void on_action_set_name_triggered(); + + void on_action_set_colour_triggered(); + signals: void text_changed();