X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fviews%2Ftrace%2Fcursorpair.cpp;h=f58db8fafd4ebf97b0221d1ea87d2911188e3508;hp=b67609dacdce624b8c19dbce09625a272ffb735a;hb=710c2a1896fbac968c82f2d1257aaabd10a48cc8;hpb=076ce70c06de8bb05a8e8970788ecfd73b63346d diff --git a/pv/views/trace/cursorpair.cpp b/pv/views/trace/cursorpair.cpp index b67609da..f58db8fa 100644 --- a/pv/views/trace/cursorpair.cpp +++ b/pv/views/trace/cursorpair.cpp @@ -17,15 +17,19 @@ * along with this program; if not, see . */ +#include +#include + +#include +#include + #include "cursorpair.hpp" +#include "pv/globalsettings.hpp" #include "pv/util.hpp" #include "ruler.hpp" #include "view.hpp" -#include -#include - using std::max; using std::make_pair; using std::min; @@ -37,13 +41,25 @@ namespace views { namespace trace { const int CursorPair::DeltaPadding = 8; -const QColor CursorPair::ViewportFillColor(220, 231, 243); CursorPair::CursorPair(View &view) : TimeItem(view), first_(new Cursor(view, 0.0)), second_(new Cursor(view, 1.0)) { + GlobalSettings::add_change_handler(this); + + GlobalSettings settings; + fill_color_ = QColor::fromRgba(settings.value( + GlobalSettings::Key_View_CursorFillColor).value()); + + connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)), + this, SLOT(on_hover_point_changed(const QWidget*, QPoint))); +} + +CursorPair::~CursorPair() +{ + GlobalSettings::remove_change_handler(this); } bool CursorPair::enabled() const @@ -68,11 +84,24 @@ void CursorPair::set_time(const pv::util::Timestamp& time) second_->set_time(time + delta); } +const pv::util::Timestamp CursorPair::time() const +{ + return (first_->time() + second_->time()) / 2.0f; +} + float CursorPair::get_x() const { return (first_->get_x() + second_->get_x()) / 2.0f; } +const pv::util::Timestamp CursorPair::delta(const pv::util::Timestamp& other) const +{ + if (other < second_->time()) + return other - first_->time(); + else + return other - second_->time(); +} + QPoint CursorPair::drag_point(const QRect &rect) const { return first_->drag_point(rect); @@ -110,38 +139,36 @@ void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover) if (!enabled()) return; - const QColor text_color = - ViewItem::select_text_color(Cursor::FillColor); - + const QColor text_color = ViewItem::select_text_color(Cursor::FillColor); p.setPen(text_color); - compute_text_size(p); - QRectF delta_rect(label_rect(rect)); + 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()) { - const int highlight_radius = delta_rect.height() / 2 - 2; - - if (selected()) { - p.setBrush(Qt::transparent); - p.setPen(highlight_pen()); - p.drawRoundedRect(delta_rect, radius, radius); - } - - p.setBrush(hover ? Cursor::FillColor.lighter() : - Cursor::FillColor); - p.setPen(Cursor::FillColor.darker()); - p.drawRoundedRect(delta_rect, radius, radius); + QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0)); - delta_rect.adjust(1, 1, -1, -1); - p.setPen(Cursor::FillColor.lighter()); - p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); + QString text = format_string(text_rect.width(), + [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); }); - p.setPen(text_color); - p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, - format_string()); + text_size_ = p.boundingRect(QRectF(), 0, text).size(); + + if (selected()) { + p.setBrush(Qt::transparent); + p.setPen(highlight_pen()); + p.drawRoundedRect(delta_rect, radius, radius); } + + p.setBrush(hover ? Cursor::FillColor.lighter() : Cursor::FillColor); + p.setPen(Cursor::FillColor.darker()); + p.drawRoundedRect(delta_rect, radius, radius); + + delta_rect.adjust(1, 1, -1, -1); + p.setPen(Cursor::FillColor.lighter()); + const int highlight_radius = delta_rect.height() / 2 - 2; + p.drawRoundedRect(delta_rect, highlight_radius, highlight_radius); + label_area_ = delta_rect; + + p.setPen(text_color); + p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter, text); } void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp) @@ -150,36 +177,57 @@ void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp) return; p.setPen(Qt::NoPen); - p.setBrush(QBrush(ViewportFillColor)); + p.setBrush(fill_color_); const pair offsets(get_cursor_offsets()); - const int l = (int)max(min( - offsets.first, offsets.second), 0.0f); - const int r = (int)min(max( - offsets.first, offsets.second), (float)pp.width()); + const int l = (int)max(min(offsets.first, offsets.second), 0.0f); + const int r = (int)min(max(offsets.first, offsets.second), (float)pp.width()); p.drawRect(l, pp.top(), r - l, pp.height()); } -QString CursorPair::format_string() +QString CursorPair::format_string(int max_width, std::function query_size) { - const pv::util::SIPrefix prefix = view_.tick_prefix(); - const pv::util::Timestamp diff = abs(second_->time() - first_->time()); + int time_precision = 12; + int freq_precision = 12; - 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); + QString s = format_string_sub(time_precision, freq_precision); - return QString("%1 / %2").arg(s1, s2); -} + // Try full "{time} s / {freq} Hz" format + if ((max_width <= 0) || (query_size(s) <= max_width)) { + label_incomplete_ = false; + return s; + } -void CursorPair::compute_text_size(QPainter &p) -{ - assert(first_); - assert(second_); + label_incomplete_ = true; + + // Gradually reduce time precision to match frequency precision + while (time_precision > freq_precision) { + time_precision--; + + s = format_string_sub(time_precision, freq_precision); + if (query_size(s) <= max_width) + return s; + } + + // Gradually reduce both precisions down to zero + while (time_precision > 0) { + time_precision--; + freq_precision--; - text_size_ = p.boundingRect(QRectF(), 0, format_string()).size(); + s = format_string_sub(time_precision, freq_precision); + if (query_size(s) <= max_width) + return s; + } + + // Try no trailing digits and drop the unit to at least display something + s = format_string_sub(0, 0, false); + + if (query_size(s) <= max_width) + return s; + + // Give up + return "..."; } pair CursorPair::get_cursor_offsets() const @@ -190,6 +238,47 @@ pair CursorPair::get_cursor_offsets() const return pair(first_->get_x(), second_->get_x()); } +void CursorPair::on_setting_changed(const QString &key, const QVariant &value) +{ + if (key == GlobalSettings::Key_View_CursorFillColor) + fill_color_ = QColor::fromRgba(value.value()); +} + +void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) +{ + if (widget != view_.ruler()) + return; + + if (!label_incomplete_) + return; + + if (label_area_.contains(hp)) + QToolTip::showText(view_.mapToGlobal(hp), format_string()); + else + QToolTip::hideText(); // TODO Will break other tooltips when there can be others +} + +QString CursorPair::format_string_sub(int time_precision, int freq_precision, bool show_unit) +{ + const pv::util::SIPrefix prefix = view_.tick_prefix(); + const pv::util::Timestamp diff = abs(second_->time() - first_->time()); + + const QString time = Ruler::format_time_with_distance( + diff, diff, prefix, (show_unit ? view_.time_unit() : pv::util::TimeUnit::None), + time_precision, false); + + // We can only show a frequency when there's a time base + if (view_.time_unit() == pv::util::TimeUnit::Time) { + const QString freq = util::format_value_si( + 1 / diff.convert_to(), pv::util::SIPrefix::unspecified, + freq_precision, (show_unit ? "Hz" : nullptr), false); + + return QString("%1 / %2").arg(time, freq); + } else + // In this case, we return the number of samples, really + return time; +} + } // namespace trace } // namespace views } // namespace pv