- const QString s1 = Ruler::format_time_with_distance(
- diff, diff, prefix, view_.time_unit(), 12, false); /* Always use 12 precision digits */
- const QString s2 = util::format_time_si(
- 1 / diff, pv::util::SIPrefix::unspecified, 4, "Hz", false);
+ const QString time = Ruler::format_time_with_distance(
+ diff, diff, prefix, view_.time_unit(), time_precision, false);
+ const QString freq = util::format_time_si(
+ 1 / diff, pv::util::SIPrefix::unspecified, freq_precision, "Hz", false);
+ const QString out = QString("%1 / %2").arg(time, freq);
+
+ // Try full "{time} ms / {freq} Hz" format
+ if (max_width <= 0 || query_size(out) <= max_width) {
+ label_incomplete_ = false;
+ return out;
+ }
+
+ label_incomplete_ = true;
+
+ // Try just "{time}ms" format and gradually reduce time precision down to zero
+ for (int shrinkage=0; shrinkage <= time_precision; shrinkage++) {
+ int prec = time_precision - shrinkage ;
+
+ const QString time = Ruler::format_time_with_distance(
+ diff, diff, prefix, view_.time_unit(),
+ prec, false);
+
+ if (query_size(time) <= max_width)
+ return time;
+ }
+
+ // Try no trailing digits and drop the unit to at least display something. The unit should be obvious from the ruler
+ // anyway.
+ const QString bare_number = Ruler::format_time_with_distance(
+ diff, diff, prefix, view_.time_unit(),
+ 0, false, false);
+ if (query_size(bare_number) <= max_width)
+ return bare_number;