]> sigrok.org Git - pulseview.git/blobdiff - pv/view/ruler.cpp
Standardize on 'event' as name for all Qt events.
[pulseview.git] / pv / view / ruler.cpp
index 6f8e89a5b69833ad3449167f0599dc87b87794a4..f6ec4ac78d7d142a486d2347287bd4693816ca5c 100644 (file)
@@ -35,10 +35,10 @@ using std::vector;
 namespace pv {
 namespace view {
 
-const float Ruler::RulerHeight = 2.5f;  // x Text Height
+const float Ruler::RulerHeight = 2.5f; // x Text Height
 const int Ruler::MinorTickSubdivision = 4;
 
-const float Ruler::HoverArrowSize = 0.5f;  // x Text Height
+const float Ruler::HoverArrowSize = 0.5f; // x Text Height
 
 Ruler::Ruler(View &parent) :
        MarginWidget(parent)
@@ -77,6 +77,38 @@ QSize Ruler::extended_size_hint() const
                ViewItem::HighlightRadius);
 }
 
+QString Ruler::format_time_with_distance(
+       const pv::util::Timestamp& distance,
+       const pv::util::Timestamp& t,
+       pv::util::SIPrefix prefix,
+       pv::util::TimeUnit unit,
+       unsigned precision,
+       bool sign)
+{
+       const unsigned limit = 60;
+
+       if (t.is_zero())
+               return "0";
+
+       // If we have to use samples then we have no alternative formats
+       if (unit == pv::util::TimeUnit::Samples)
+               return pv::util::format_time_si_adjusted(t, prefix, precision, "sa", sign);
+
+       // View zoomed way out -> low precision (0), big distance (>=60s)
+       // -> DD:HH:MM
+       if ((precision == 0) && (distance >= limit))
+               return pv::util::format_time_minutes(t, 0, sign);
+
+       // View in "normal" range -> medium precision, medium step size
+       // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
+       // View zoomed way in -> high precision (>3), low step size (<1s)
+       // -> HH:MM:SS.mmm... or xxxx (si unit) if less than limit seconds
+       if (abs(t) < limit)
+               return pv::util::format_time_si_adjusted(t, prefix, precision, "s", sign);
+       else
+               return pv::util::format_time_minutes(t, precision, sign);
+}
+
 vector< shared_ptr<ViewItem> > Ruler::items()
 {
        const vector< shared_ptr<TimeItem> > time_items(view_.time_items());
@@ -96,21 +128,21 @@ shared_ptr<ViewItem> Ruler::get_mouse_over_item(const QPoint &pt)
 void Ruler::paintEvent(QPaintEvent*)
 {
        if (!tick_position_cache_) {
-               auto ffunc = [this](const pv::util::Timestamp& t)
-               {
-                       return util::format_time(
+               auto ffunc = [this](const pv::util::Timestamp& t) {
+                       return format_time_with_distance(
+                               this->view_.tick_period(),
                                t,
                                this->view_.tick_prefix(),
                                this->view_.time_unit(),
                                this->view_.tick_precision());
                };
 
-               tick_position_cache_.emplace(calculate_tick_positions(
+               tick_position_cache_ = calculate_tick_positions(
                        view_.tick_period(),
                        view_.offset(),
                        view_.scale(),
                        width(),
-                       ffunc));
+                       ffunc);
        }
 
        const int ValueMargin = 3;
@@ -121,7 +153,6 @@ void Ruler::paintEvent(QPaintEvent*)
        const int minor_tick_y1 = (major_tick_y1 + ruler_height) / 2;
 
        QPainter p(this);
-       p.setRenderHint(QPainter::Antialiasing);
 
        // Draw the tick marks
        p.setPen(palette().color(foregroundRole()));
@@ -141,6 +172,8 @@ void Ruler::paintEvent(QPaintEvent*)
        // Draw the hover mark
        draw_hover_mark(p, text_height);
 
+       p.setRenderHint(QPainter::Antialiasing);
+
        // The cursor labels are not drawn with the arrows exactly on the
        // bottom line of the widget, because then the selection shadow
        // would be clipped away.
@@ -156,7 +189,7 @@ void Ruler::paintEvent(QPaintEvent*)
 }
 
 Ruler::TickPositions Ruler::calculate_tick_positions(
-       const double major_period,
+       const pv::util::Timestamp& major_period,
        const pv::util::Timestamp& offset,
        const double scale,
        const int width,
@@ -164,7 +197,7 @@ Ruler::TickPositions Ruler::calculate_tick_positions(
 {
        TickPositions tp;
 
-       const double minor_period = major_period / MinorTickSubdivision;
+       const pv::util::Timestamp minor_period = major_period / MinorTickSubdivision;
        const pv::util::Timestamp first_major_division = floor(offset / major_period);
        const pv::util::Timestamp first_minor_division = ceil(offset / minor_period);
        const pv::util::Timestamp t0 = first_major_division * major_period;
@@ -175,10 +208,12 @@ Ruler::TickPositions Ruler::calculate_tick_positions(
        double x;
 
        do {
-               const pv::util::Timestamp t = t0 + division * minor_period;
+               pv::util::Timestamp t = t0 + division * minor_period;
                x = ((t - offset) / scale).convert_to<double>();
 
                if (division % MinorTickSubdivision == 0) {
+                       // Recalculate 't' without using 'minor_period' which is a fraction
+                       t = t0 + division / MinorTickSubdivision * major_period;
                        tp.major.emplace_back(x, format_function(t));
                } else {
                        tp.minor.emplace_back(x);
@@ -190,9 +225,9 @@ Ruler::TickPositions Ruler::calculate_tick_positions(
        return tp;
 }
 
-void Ruler::mouseDoubleClickEvent(QMouseEvent *e)
+void Ruler::mouseDoubleClickEvent(QMouseEvent *event)
 {
-       view_.add_flag(view_.offset() + ((double)e->x() + 0.5) * view_.scale());
+       view_.add_flag(view_.offset() + ((double)event->x() + 0.5) * view_.scale());
 }
 
 void Ruler::draw_hover_mark(QPainter &p, int text_height)