From: Soeren Apel Date: Thu, 2 Aug 2018 06:56:51 +0000 (+0200) Subject: Allow for a context menu in the view area X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=d9b55cc8ab01e83b392762ed1fa400fdafafb60b;p=pulseview.git Allow for a context menu in the view area --- diff --git a/pv/views/trace/trace.cpp b/pv/views/trace/trace.cpp index 7d850112..3b79dcd9 100644 --- a/pv/views/trace/trace.cpp +++ b/pv/views/trace/trace.cpp @@ -187,6 +187,14 @@ QRectF Trace::label_rect(const QRectF &rect) const label_size.height()); } +QRectF Trace::hit_box_rect(const ViewItemPaintParams &pp) const +{ + pair extents = v_extents(); + const int top = pp.top() + get_visual_y() + extents.first; + const int height = extents.second - extents.first; + return QRectF(pp.left(), top, pp.width(), height); +} + void Trace::set_current_segment(const int segment) { current_segment_ = segment; diff --git a/pv/views/trace/trace.hpp b/pv/views/trace/trace.hpp index d6d53045..bc97736c 100644 --- a/pv/views/trace/trace.hpp +++ b/pv/views/trace/trace.hpp @@ -128,6 +128,14 @@ public: */ QRectF label_rect(const QRectF &rect) const; + /** + * Computes the outline rectangle of the viewport hit-box. + * @param rect the rectangle of the viewport area. + * @return Returns the rectangle of the hit-box. + * @remarks The default implementation returns an empty hit-box. + */ + virtual QRectF hit_box_rect(const ViewItemPaintParams &pp) const; + void set_current_segment(const int segment); int get_current_segment() const; diff --git a/pv/views/trace/view.cpp b/pv/views/trace/view.cpp index 024e3bf4..99e75bfd 100644 --- a/pv/views/trace/view.cpp +++ b/pv/views/trace/view.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -1251,6 +1252,17 @@ bool View::eventFilter(QObject *object, QEvent *event) return QObject::eventFilter(object, event); } +void View::contextMenuEvent(QContextMenuEvent *event) +{ + const shared_ptr r = viewport_->get_mouse_over_item(event->pos()); + if (!r) + return; + + QMenu *menu = r->create_view_context_menu(this); + if (menu) + menu->exec(event->globalPos()); +} + void View::resizeEvent(QResizeEvent* event) { // Only adjust the top margin if we shrunk vertically diff --git a/pv/views/trace/view.hpp b/pv/views/trace/view.hpp index 4f364404..1cff6ab7 100644 --- a/pv/views/trace/view.hpp +++ b/pv/views/trace/view.hpp @@ -394,6 +394,8 @@ private: bool eventFilter(QObject *object, QEvent *event); + virtual void contextMenuEvent(QContextMenuEvent *event); + void resizeEvent(QResizeEvent *event); void update_hover_point(); diff --git a/pv/views/trace/viewitem.cpp b/pv/views/trace/viewitem.cpp index 2141687c..7c1fae73 100644 --- a/pv/views/trace/viewitem.cpp +++ b/pv/views/trace/viewitem.cpp @@ -88,6 +88,12 @@ QMenu* ViewItem::create_context_menu(QWidget *parent) return new QMenu(parent); } +QMenu* ViewItem::create_view_context_menu(QWidget *parent) +{ + (void)parent; + return nullptr; +} + widgets::Popup* ViewItem::create_popup(QWidget *parent) { (void)parent; diff --git a/pv/views/trace/viewitem.hpp b/pv/views/trace/viewitem.hpp index f3e0f9b6..91242f57 100644 --- a/pv/views/trace/viewitem.hpp +++ b/pv/views/trace/viewitem.hpp @@ -157,6 +157,8 @@ public: public: virtual QMenu* create_context_menu(QWidget *parent); + virtual QMenu* create_view_context_menu(QWidget *parent); + virtual pv::widgets::Popup* create_popup(QWidget *parent); virtual void delete_pressed(); diff --git a/pv/views/trace/viewport.hpp b/pv/views/trace/viewport.hpp index ab67f69f..a49084b4 100644 --- a/pv/views/trace/viewport.hpp +++ b/pv/views/trace/viewport.hpp @@ -48,14 +48,6 @@ class Viewport : public ViewWidget public: explicit Viewport(View &parent); -private: - /** - * Indicates when a view item is being hovered over. - * @param item The item that is being hovered over, or @c nullptr - * if no view item is being hovered over. - */ - void item_hover(const shared_ptr &item); - /** * Gets the first view item which has a hit-box that contains @c pt . * @param pt the point to search with. @@ -64,6 +56,14 @@ private: */ shared_ptr get_mouse_over_item(const QPoint &pt); +private: + /** + * Indicates when a view item is being hovered over. + * @param item The item that is being hovered over, or @c nullptr + * if no view item is being hovered over. + */ + void item_hover(const shared_ptr &item); + /** * Sets this item into the dragged state. */