]> sigrok.org Git - pulseview.git/blobdiff - pv/view/ruler.cpp
Added cursor dragging
[pulseview.git] / pv / view / ruler.cpp
index 97c0be6822506ffa1c3056493a629d14428dc6e2..bbafdb0f22cdce854fb52322a7a666b82bdac039 100644 (file)
  */
 
 #include "ruler.h"
+
+#include "cursor.h"
 #include "view.h"
+#include "viewport.h"
 
 #include <extdef.h>
 
 #include <assert.h>
 #include <math.h>
 
+#include <QMouseEvent>
 #include <QPainter>
 #include <QTextStream>
 
+using namespace std;
+
 namespace pv {
 namespace view {
 
@@ -39,10 +45,17 @@ const QString Ruler::SIPrefixes[9] =
        {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
 const int Ruler::FirstSIPrefixPower = -15;
 
+const int Ruler::HoverArrowSize = 5;
+
 Ruler::Ruler(View &parent) :
        QWidget(&parent),
-       _view(parent)
+       _view(parent),
+       _grabbed_marker(NULL)
 {
+       setMouseTracking(true);
+
+       connect(&_view, SIGNAL(hover_point_changed()),
+               this, SLOT(hover_point_changed()));
 }
 
 void Ruler::paintEvent(QPaintEvent *event)
@@ -115,8 +128,80 @@ void Ruler::paintEvent(QPaintEvent *event)
                division++;
        }
 
+       // Draw the cursors
+       draw_cursors(p);
+
+       // Draw the hover mark
+       draw_hover_mark(p);
+
        p.end();
 }
 
+void Ruler::mouseMoveEvent(QMouseEvent *e)
+{
+       if(!_grabbed_marker)
+               return;
+
+       _grabbed_marker->set_time(_view.offset() +
+               ((double)e->x() + 0.5) * _view.scale());
+}
+
+void Ruler::mousePressEvent(QMouseEvent *e)
+{
+       if(e->buttons() & Qt::LeftButton) {
+               _grabbed_marker = NULL;
+
+               if(_view.cursors_shown()) {
+                       std::pair<Cursor, Cursor> &cursors =
+                               _view.cursors();
+                       if(cursors.first.get_label_rect(
+                               rect()).contains(e->pos()))
+                               _grabbed_marker = &cursors.first;
+                       else if(cursors.second.get_label_rect(
+                               rect()).contains(e->pos()))
+                               _grabbed_marker = &cursors.second;
+               }
+       }
+}
+
+void Ruler::mouseReleaseEvent(QMouseEvent *)
+{
+       _grabbed_marker = NULL;
+}
+
+void Ruler::draw_cursors(QPainter &p)
+{
+       if(!_view.cursors_shown())
+               return;
+
+       const QRect r = rect();
+       pair<Cursor, Cursor> &cursors = _view.cursors();
+       cursors.first.paint_label(p, r);
+       cursors.second.paint_label(p, r);
+}
+
+void Ruler::draw_hover_mark(QPainter &p)
+{
+       const int x = _view.hover_point().x();
+       if(x == -1 || _grabbed_marker)
+               return;
+
+       p.setPen(QPen(Qt::NoPen));
+       p.setBrush(QBrush(QColor(Qt::black)));
+
+       const int b = height() - 1;
+       const QPointF points[] = {
+               QPointF(x, b),
+               QPointF(x - HoverArrowSize, b - HoverArrowSize),
+               QPointF(x + HoverArrowSize, b - HoverArrowSize)
+       };
+       p.drawPolygon(points, countof(points));
+}
+
+void Ruler::hover_point_changed()
+{
+       update();
+}
+
 } // namespace view
 } // namespace pv