]> sigrok.org Git - pulseview.git/commitdiff
Moved context menu functionality into SelectableItem family
authorJoel Holdsworth <redacted>
Sun, 8 Sep 2013 13:06:24 +0000 (14:06 +0100)
committerJoel Holdsworth <redacted>
Sun, 29 Sep 2013 14:56:13 +0000 (23:56 +0900)
pv/view/header.cpp
pv/view/header.h
pv/view/selectableitem.cpp
pv/view/selectableitem.h
pv/view/trace.cpp
pv/view/trace.h

index b85b1bb1c0a03cc5ed11249d0e4dd96df2865d26..e084ecc94e55fe4a7390a0bc3f76aae2002e63e7 100644 (file)
@@ -29,8 +29,6 @@
 #include <boost/foreach.hpp>
 
 #include <QApplication>
 #include <boost/foreach.hpp>
 
 #include <QApplication>
-#include <QColorDialog>
-#include <QInputDialog>
 #include <QMenu>
 #include <QMouseEvent>
 #include <QPainter>
 #include <QMenu>
 #include <QMouseEvent>
 #include <QPainter>
@@ -43,17 +41,10 @@ namespace pv {
 namespace view {
 
 Header::Header(View &parent) :
 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);
 
 {
        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()));
 
        connect(&_view.session(), SIGNAL(signals_changed()),
                this, SLOT(on_signals_changed()));
 
@@ -209,44 +200,8 @@ void Header::contextMenuEvent(QContextMenuEvent *event)
 {
        const shared_ptr<Trace> t = get_mouse_over_trace(_mouse_point);
 
 {
        const shared_ptr<Trace> 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<view::Trace> 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<view::Trace> 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()
 }
 
 void Header::on_signals_changed()
index 7a180359539f7f6f9d03590b4042648804d0c72c..35d5d20f49f7b5bcf89f1ca1c91a127188d07abb 100644 (file)
@@ -65,10 +65,6 @@ private:
 private slots:
        void on_signals_changed();
 
 private slots:
        void on_signals_changed();
 
-       void on_action_set_name_triggered();
-
-       void on_action_set_colour_triggered();
-
        void on_signals_moved();
 
 signals:
        void on_signals_moved();
 
 signals:
@@ -80,10 +76,6 @@ private:
 
        std::list<std::pair<boost::weak_ptr<Trace>, int> >
                _drag_traces;
 
        std::list<std::pair<boost::weak_ptr<Trace>, int> >
                _drag_traces;
-
-       boost::shared_ptr<Trace> _context_trace;
-       QAction *_action_set_name;
-       QAction *_action_set_colour;
 };
 
 } // namespace view
 };
 
 } // namespace view
index 8dfdd8aa33d36c9c200486cb6ff317da13500989..31c45ef3b166fa9ff4f85894edc304d761b1ad72 100644 (file)
@@ -21,6 +21,7 @@
 #include "selectableitem.h"
 
 #include <QApplication>
 #include "selectableitem.h"
 
 #include <QApplication>
+#include <QMenu>
 #include <QPalette>
 
 namespace pv {
 #include <QPalette>
 
 namespace pv {
@@ -29,6 +30,7 @@ namespace view {
 const int SelectableItem::HighlightRadius = 6;
 
 SelectableItem::SelectableItem() :
 const int SelectableItem::HighlightRadius = 6;
 
 SelectableItem::SelectableItem() :
+       _context_parent(NULL),
        _selected(false)
 {
 }
        _selected(false)
 {
 }
@@ -43,6 +45,12 @@ void SelectableItem::select(bool select)
        _selected = 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(
 QPen SelectableItem::highlight_pen()
 {
        return QPen(QApplication::palette().brush(
index 467da0f2cbf0b7140edf87fb9e3be0b34b3c79e6..323c87e18743ff894255137af83cbc86c6fce5c3 100644 (file)
@@ -26,6 +26,8 @@
 #include <QPen>
 
 class QAction;
 #include <QPen>
 
 class QAction;
+class QMenu;
+class QWidget;
 
 namespace pv {
 namespace view {
 
 namespace pv {
 namespace view {
@@ -56,9 +58,14 @@ public:
 
        virtual const std::list<QAction*> get_context_bar_actions() = 0;
 
 
        virtual const std::list<QAction*> get_context_bar_actions() = 0;
 
+       virtual QMenu* create_context_menu(QWidget *parent);
+
 protected:
        static QPen highlight_pen();
 
 protected:
        static QPen highlight_pen();
 
+protected:
+       QWidget *_context_parent;
+
 private:
        bool _selected;
 };
 private:
        bool _selected;
 };
index b3ebd95b4a7e1928b1c33370f39a1cf8506a3f86..67ea3a02788416c670a6c1d3f12d591d402df84d 100644 (file)
@@ -23,6 +23,9 @@
 #include <assert.h>
 #include <math.h>
 
 #include <assert.h>
 #include <math.h>
 
+#include <QColorDialog>
+#include <QInputDialog>
+
 #include "trace.h"
 #include "view.h"
 
 #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);
 }
 
                        ).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();
 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());
 }
 
                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
 } // namespace view
 } // namespace pv
index 415a7dc28c70c4e273f2bcde3bd5d037306d10f2..631c8d9cb6a7435343ef806d2ed1eb6566cb7e02 100644 (file)
@@ -131,6 +131,8 @@ public:
         */
        bool pt_in_label_rect(int left, int right, const QPoint &point);
 
         */
        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.
 protected:
        /**
         * Gets the y-offset of the axis.
@@ -170,6 +172,11 @@ private:
         */
        QRectF get_label_rect(int right);
 
         */
        QRectF get_label_rect(int right);
 
+private slots:
+       void on_action_set_name_triggered();
+
+       void on_action_set_colour_triggered();
+
 signals:
        void text_changed();    
 
 signals:
        void text_changed();