]> sigrok.org Git - pulseview.git/commitdiff
Ruler: Add context menu with "Create marker here" entry
authorSoeren Apel <redacted>
Wed, 10 Oct 2018 16:08:40 +0000 (18:08 +0200)
committerSoeren Apel <redacted>
Sat, 13 Oct 2018 13:37:04 +0000 (15:37 +0200)
manual/analysis.txt
pv/views/trace/ruler.cpp
pv/views/trace/ruler.hpp
test/view/ruler.cpp

index deb31d720db4e17910b14f709785129471f4af87..034f969a81029c48fffd954531dee77a584965c6 100644 (file)
@@ -43,8 +43,9 @@ image::pv_cursors_markers.png[]
 
 Markers are movable indicators that you can create wherever you like on the
 time scale - just double-click on it and it'll create one for you where your
-mouse cursor is at the time. You can click on its label and you'll have the
-option to change its name, or drag it to reposition it.
+mouse cursor is at the time, or use the context menu when right-clicking.
+You can click on its label and you'll have the option to change its name, or
+drag it to reposition it.
 
 [NOTE]
 For timing comparison purposes, you can also enable a vertical marker line that
index 63d3d23fcb946536f173ee0e184aacd1084d0039..7688f2b7e1c0262fc12a89e82e2ee6c75b5605d2 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <QApplication>
 #include <QFontMetrics>
+#include <QMenu>
 #include <QMouseEvent>
 
 #include "ruler.hpp"
@@ -111,6 +112,30 @@ QString Ruler::format_time_with_distance(
                return pv::util::format_time_minutes(t, precision, sign);
 }
 
+pv::util::Timestamp Ruler::get_time_from_x_pos(uint32_t x)
+{
+       return view_.ruler_offset() + ((double)x + 0.5) * view_.scale();
+}
+
+void Ruler::contextMenuEvent(QContextMenuEvent *event)
+{
+       context_menu_x_pos_ = event->pos().x();
+
+       QMenu *const menu = new QMenu(this);
+
+       QAction *const create_marker = new QAction(tr("Create marker here"), this);
+       connect(create_marker, SIGNAL(triggered()), this, SLOT(on_createMarker()));
+       menu->addAction(create_marker);
+
+       menu->popup(event->globalPos());
+}
+
+void Ruler::resizeEvent(QResizeEvent*)
+{
+       // the tick calculation depends on the width of this widget
+       invalidate_tick_position_cache();
+}
+
 vector< shared_ptr<ViewItem> > Ruler::items()
 {
        const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
@@ -127,6 +152,11 @@ shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
        return nullptr;
 }
 
+void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
+{
+       view_.add_flag(get_time_from_x_pos(event->x()));
+}
+
 void Ruler::paintEvent(QPaintEvent*)
 {
        if (!tick_position_cache_) {
@@ -199,7 +229,32 @@ void Ruler::paintEvent(QPaintEvent*)
        }
 }
 
-Ruler::TickPositions Ruler::calculate_tick_positions(
+void Ruler::draw_hover_mark(QPainter &p, int text_height)
+{
+       const int x = view_.hover_point().x();
+
+       if (x == -1)
+               return;
+
+       p.setPen(QPen(Qt::NoPen));
+       p.setBrush(QBrush(palette().color(foregroundRole())));
+
+       const int b = RulerHeight * text_height;
+       const float hover_arrow_size = HoverArrowSize * text_height;
+       const QPointF points[] = {
+               QPointF(x, b),
+               QPointF(x - hover_arrow_size, b - hover_arrow_size),
+               QPointF(x + hover_arrow_size, b - hover_arrow_size)
+       };
+       p.drawPolygon(points, countof(points));
+}
+
+int Ruler::calculate_text_height() const
+{
+       return QFontMetrics(font()).ascent();
+}
+
+TickPositions Ruler::calculate_tick_positions(
        const pv::util::Timestamp& major_period,
        const pv::util::Timestamp& offset,
        const double scale,
@@ -237,36 +292,6 @@ Ruler::TickPositions Ruler::calculate_tick_positions(
        return tp;
 }
 
-void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
-{
-       view_.add_flag(view_.ruler_offset() + ((double)event->x() + 0.5) * view_.scale());
-}
-
-void Ruler::draw_hover_mark(QPainter &p, int text_height)
-{
-       const int x = view_.hover_point().x();
-
-       if (x == -1)
-               return;
-
-       p.setPen(QPen(Qt::NoPen));
-       p.setBrush(QBrush(palette().color(foregroundRole())));
-
-       const int b = RulerHeight * text_height;
-       const float hover_arrow_size = HoverArrowSize * text_height;
-       const QPointF points[] = {
-               QPointF(x, b),
-               QPointF(x - hover_arrow_size, b - hover_arrow_size),
-               QPointF(x + hover_arrow_size, b - hover_arrow_size)
-       };
-       p.drawPolygon(points, countof(points));
-}
-
-int Ruler::calculate_text_height() const
-{
-       return QFontMetrics(font()).ascent();
-}
-
 void Ruler::hover_point_changed(const QPoint &hp)
 {
        (void)hp;
@@ -279,10 +304,9 @@ void Ruler::invalidate_tick_position_cache()
        tick_position_cache_ = boost::none;
 }
 
-void Ruler::resizeEvent(QResizeEvent*)
+void Ruler::on_createMarker()
 {
-       // the tick calculation depends on the width of this widget
-       invalidate_tick_position_cache();
+       view_.add_flag(get_time_from_x_pos(context_menu_x_pos_));
 }
 
 } // namespace trace
index 448204b2410410bb7e78c7f635c2392d49937603..61e622ac7afa350bf7ea1cfd1852fa465f7cf4b8 100644 (file)
@@ -46,6 +46,12 @@ namespace trace {
 class TimeItem;
 class ViewItem;
 
+struct TickPositions
+{
+       vector<pair<double, QString>> major;
+       vector<double> minor;
+};
+
 /**
  * The Ruler class manages and displays the time scale above the trace canvas.
  * It may also contain @ref TimeItem instances used to identify or highlight
@@ -69,7 +75,6 @@ private:
 public:
        Ruler(View &parent);
 
-public:
        QSize sizeHint() const override;
 
        /**
@@ -112,6 +117,12 @@ public:
                unsigned precision = 0,
                bool sign = true);
 
+       pv::util::Timestamp get_time_from_x_pos(uint32_t x);
+
+protected:
+       virtual void contextMenuEvent(QContextMenuEvent *event) override;
+       void resizeEvent(QResizeEvent*) override;
+
 private:
        /**
         * Gets the time items.
@@ -126,10 +137,10 @@ private:
         */
        shared_ptr<ViewItem> get_mouse_over_item(const QPoint &pt) override;
 
-       void paintEvent(QPaintEvent *event) override;
-
        void mouseDoubleClickEvent(QMouseEvent *event) override;
 
+       void paintEvent(QPaintEvent *event) override;
+
        /**
         * Draw a hover arrow under the cursor position.
         * @param p The painter to draw into.
@@ -139,18 +150,6 @@ private:
 
        int calculate_text_height() const;
 
-       struct TickPositions
-       {
-               vector<pair<double, QString>> major;
-               vector<double> minor;
-       };
-
-       /**
-        * Holds the tick positions so that they don't have to be recalculated on
-        * every redraw. Set by 'paintEvent()' when needed.
-        */
-       boost::optional<TickPositions> tick_position_cache_;
-
        /**
         * Calculates the major and minor tick positions.
         *
@@ -171,14 +170,21 @@ private:
                const unsigned int minor_tick_count,
                function<QString(const pv::util::Timestamp&)> format_function);
 
-protected:
-       void resizeEvent(QResizeEvent*) override;
-
 private Q_SLOTS:
        void hover_point_changed(const QPoint &hp);
 
-       // Resets the 'tick_position_cache_'.
        void invalidate_tick_position_cache();
+
+       void on_createMarker();
+
+private:
+       /**
+        * Holds the tick positions so that they don't have to be recalculated on
+        * every redraw. Set by 'paintEvent()' when needed.
+        */
+       boost::optional<TickPositions> tick_position_cache_;
+
+       uint32_t context_menu_x_pos_;
 };
 
 } // namespace trace
index 3984e973aa026af34fc8a3cfa1b66ce85eea3b50..5cef38d5ce20211c1ca8e329ca8e05f0a62629a1 100644 (file)
@@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE(tick_position_test_0)
        const int width(500);
        const unsigned int minor_tick_count(4);
 
-       const Ruler::TickPositions ts = Ruler::calculate_tick_positions(
+       const TickPositions ts = Ruler::calculate_tick_positions(
                major_period, offset, scale, width, minor_tick_count, format);
 
        BOOST_REQUIRE_EQUAL(ts.major.size(), 6);
@@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE(tick_position_test_1)
        const int width(500);
        const unsigned int minor_tick_count(4);
 
-       const Ruler::TickPositions ts = Ruler::calculate_tick_positions(
+       const TickPositions ts = Ruler::calculate_tick_positions(
                major_period, offset, scale, width, minor_tick_count, format);
 
        BOOST_REQUIRE_EQUAL(ts.major.size(), 5);
@@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(tick_position_test_2)
        const int width(580);
        const unsigned int minor_tick_count(4);
 
-       const Ruler::TickPositions ts = Ruler::calculate_tick_positions(
+       const TickPositions ts = Ruler::calculate_tick_positions(
                major_period, offset, scale, width, minor_tick_count, format);
 
        const double mp = 5;