]> sigrok.org Git - pulseview.git/blobdiff - pv/view/cursorpair.cpp
Change namespace for the trace view and implement ViewBase
[pulseview.git] / pv / view / cursorpair.cpp
index 0395d03446d03abb72d6b5e8a92917e8b1f73bcd..e2c80833f2503631814a5ee1888f1088af13da69 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "cursorpair.hpp"
 
+#include "ruler.hpp"
 #include "view.hpp"
 #include "pv/util.hpp"
 
@@ -33,7 +34,8 @@ using std::shared_ptr;
 using std::pair;
 
 namespace pv {
-namespace view {
+namespace views {
+namespace TraceView {
 
 const int CursorPair::DeltaPadding = 8;
 const QColor CursorPair::ViewportFillColour(220, 231, 243);
@@ -60,8 +62,9 @@ shared_ptr<Cursor> CursorPair::second() const
        return second_;
 }
 
-void CursorPair::set_time(double time) {
-       const double delta = second_->time() - first_->time();
+void CursorPair::set_time(const pv::util::Timestamp& time)
+{
+       const pv::util::Timestamp delta = second_->time() - first_->time();
        first_->set_time(time);
        second_->set_time(time + delta);
 }
@@ -71,9 +74,9 @@ float CursorPair::get_x() const
        return (first_->get_x() + second_->get_x()) / 2.0f;
 }
 
-QPoint CursorPair::point() const
+QPoint CursorPair::point(const QRect &rect) const
 {
-       return first_->point();
+       return first_->point(rect);
 }
 
 pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
@@ -84,9 +87,7 @@ pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
 
 QRectF CursorPair::label_rect(const QRectF &rect) const
 {
-       const QSizeF label_size(
-               text_size_.width() + View::LabelPadding.width() * 2,
-               text_size_.height() + View::LabelPadding.height() * 2);
+       const QSizeF label_size(text_size_ + LabelPadding * 2);
        const pair<float, float> offsets(get_cursor_offsets());
        const pair<float, float> normal_offsets(
                (offsets.first < offsets.second) ? offsets :
@@ -110,19 +111,17 @@ void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
        if (!enabled())
                return;
 
-       const unsigned int prefix = view_.tick_prefix();
        const QColor text_colour =
                ViewItem::select_text_colour(Cursor::FillColour);
 
        p.setPen(text_colour);
-       compute_text_size(p, prefix);
+       compute_text_size(p);
        QRectF delta_rect(label_rect(rect));
 
        const int radius = delta_rect.height() / 2;
        const QRectF text_rect(delta_rect.intersected(
                rect).adjusted(radius, 0, -radius, 0));
-       if(text_rect.width() >= text_size_.width())
-       {
+       if (text_rect.width() >= text_size_.width()) {
                const int highlight_radius = delta_rect.height() / 2 - 2;
 
                if (selected()) {
@@ -142,11 +141,12 @@ void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
 
                p.setPen(text_colour);
                p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
-                       pv::util::format_time(second_->time() - first_->time(), prefix, 2));
+                       format_string());
        }
 }
 
-void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) {
+void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp)
+{
        if (!enabled())
                return;
 
@@ -162,13 +162,25 @@ void CursorPair::paint_back(QPainter &p, const ViewItemPaintParams &pp) {
        p.drawRect(l, pp.top(), r - l, pp.height());
 }
 
-void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
+QString CursorPair::format_string()
+{
+       const pv::util::SIPrefix prefix = view_.tick_prefix();
+       const pv::util::Timestamp diff = abs(second_->time() - first_->time());
+
+       const QString s1 = Ruler::format_time_with_distance(
+               diff, diff, prefix, view_.time_unit(), view_.tick_precision(), false);
+       const QString s2 = util::format_time_si(
+               1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false);
+
+       return QString("%1 / %2").arg(s1).arg(s2);
+}
+
+void CursorPair::compute_text_size(QPainter &p)
 {
        assert(first_);
        assert(second_);
 
-       text_size_ = p.boundingRect(QRectF(), 0, pv::util::format_time(
-               second_->time() - first_->time(), prefix, 2)).size();
+       text_size_ = p.boundingRect(QRectF(), 0, format_string()).size();
 }
 
 pair<float, float> CursorPair::get_cursor_offsets() const
@@ -177,9 +189,10 @@ pair<float, float> CursorPair::get_cursor_offsets() const
        assert(second_);
 
        return pair<float, float>(
-               (first_->time() - view_.offset()) / view_.scale(),
-               (second_->time() - view_.offset()) / view_.scale());
+               ((first_->time() - view_.offset()) / view_.scale()).convert_to<float>(),
+               ((second_->time() - view_.offset()) / view_.scale()).convert_to<float>());
 }
 
-} // namespace view
+} // namespace TraceView
+} // namespace views
 } // namespace pv