]> sigrok.org Git - pulseview.git/blobdiff - pv/views/trace/cursorpair.cpp
TraceView: Add context menu to the cursor
[pulseview.git] / pv / views / trace / cursorpair.cpp
index 933eb8fff33e0c3aaf2ed255dc2a6e00bc5b8754..bd6814c5c8f1a86258d2dcda6eb4d4af5b922450 100644 (file)
@@ -21,6 +21,7 @@
 #include <cassert>
 
 #include <QColor>
+#include <QMenu>
 #include <QToolTip>
 
 #include "cursorpair.hpp"
@@ -45,13 +46,18 @@ const int CursorPair::DeltaPadding = 8;
 CursorPair::CursorPair(View &view) :
        TimeItem(view),
        first_(new Cursor(view, 0.0)),
-       second_(new Cursor(view, 1.0))
+       second_(new Cursor(view, 1.0)),
+       label_incomplete_(true)
 {
        GlobalSettings::add_change_handler(this);
 
        GlobalSettings settings;
        fill_color_ = QColor::fromRgba(settings.value(
                GlobalSettings::Key_View_CursorFillColor).value<uint32_t>());
+       show_frequency_ = settings.value(
+               GlobalSettings::Key_View_CursorShowFrequency).value<bool>();
+       show_interval_ = settings.value(
+               GlobalSettings::Key_View_CursorShowInterval).value<bool>();
 
        connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)),
                this, SLOT(on_hover_point_changed(const QWidget*, QPoint)));
@@ -84,11 +90,24 @@ void CursorPair::set_time(const pv::util::Timestamp& time)
        second_->set_time(time + delta);
 }
 
+const pv::util::Timestamp CursorPair::time() const
+{
+       return 0;
+}
+
 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);
@@ -100,6 +119,35 @@ pv::widgets::Popup* CursorPair::create_popup(QWidget *parent)
        return nullptr;
 }
 
+QMenu *CursorPair::create_header_context_menu(QWidget *parent)
+{
+       QMenu *menu = new QMenu(parent);
+
+       QAction *displayIntervalAction = new QAction(tr("Display interval"));
+       displayIntervalAction->setCheckable(true);
+       displayIntervalAction->setChecked(show_interval_);
+       menu->addAction(displayIntervalAction);
+
+       connect(displayIntervalAction, &QAction::toggled, [=]{
+               GlobalSettings settings;
+               settings.setValue(GlobalSettings::Key_View_CursorShowInterval,
+                       !settings.value(GlobalSettings::Key_View_CursorShowInterval).value<bool>());
+               });
+
+       QAction *displayFrequencyAction = new QAction(tr("Display frequency"));
+       displayFrequencyAction->setCheckable(true);
+       displayFrequencyAction->setChecked(show_frequency_);
+       menu->addAction(displayFrequencyAction);
+
+       connect(displayFrequencyAction, &QAction::toggled, [=]{
+               GlobalSettings settings;
+               settings.setValue(GlobalSettings::Key_View_CursorShowFrequency,
+                       !settings.value(GlobalSettings::Key_View_CursorShowFrequency).value<bool>());
+               });
+
+       return menu;
+}
+
 QRectF CursorPair::label_rect(const QRectF &rect) const
 {
        const QSizeF label_size(text_size_ + LabelPadding * 2);
@@ -129,19 +177,14 @@ void CursorPair::paint_label(QPainter &p, const QRect &rect, bool hover)
        const QColor text_color = ViewItem::select_text_color(Cursor::FillColor);
        p.setPen(text_color);
 
-       QString text = format_string();
-       text_size_ = p.boundingRect(QRectF(), 0, text).size();
-
        QRectF delta_rect(label_rect(rect));
        const int radius = delta_rect.height() / 2;
        QRectF text_rect(delta_rect.intersected(rect).adjusted(radius, 0, -radius, 0));
 
-       if (text_rect.width() < text_size_.width()) {
-               text = "...";
-               text_size_ = p.boundingRect(QRectF(), 0, text).size();
-               label_incomplete_ = true;
-       } else
-               label_incomplete_ = false;
+       QString text = format_string(text_rect.width(),
+               [&p](const QString& s) -> double { return p.boundingRect(QRectF(), 0, s).width(); });
+
+       text_size_ = p.boundingRect(QRectF(), 0, text).size();
 
        if (selected()) {
                p.setBrush(Qt::transparent);
@@ -178,17 +221,48 @@ void CursorPair::paint_back(QPainter &p, ViewItemPaintParams &pp)
        p.drawRect(l, pp.top(), r - l, pp.height());
 }
 
-QString CursorPair::format_string()
+QString CursorPair::format_string(int max_width, std::function<double(const QString&)> 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;
+
+       QString s = format_string_sub(time_precision, freq_precision);
 
-       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);
+       // Try full "{time} s / {freq} Hz" format
+       if ((max_width <= 0) || (query_size(s) <= max_width)) {
+               label_incomplete_ = false;
+               return s;
+       }
 
-       return QString("%1 / %2").arg(s1, s2);
+       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--;
+
+               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<float, float> CursorPair::get_cursor_offsets() const
@@ -203,6 +277,12 @@ void CursorPair::on_setting_changed(const QString &key, const QVariant &value)
 {
        if (key == GlobalSettings::Key_View_CursorFillColor)
                fill_color_ = QColor::fromRgba(value.value<uint32_t>());
+
+       if (key == GlobalSettings::Key_View_CursorShowFrequency)
+               show_frequency_ = value.value<bool>();
+
+       if (key == GlobalSettings::Key_View_CursorShowInterval)
+               show_interval_ = value.value<bool>();
 }
 
 void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
@@ -219,6 +299,43 @@ void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp)
                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)
+{
+       QString s = " ";
+
+       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) {
+               int items = 0;
+
+               if (show_frequency_) {
+                       const QString freq = util::format_value_si(
+                               1 / diff.convert_to<double>(), pv::util::SIPrefix::unspecified,
+                               freq_precision, (show_unit ? "Hz" : nullptr), false);
+                       s = QString("%1").arg(freq);
+                       items++;
+               }
+
+               if (show_interval_) {
+                       if (items > 0)
+                               s = QString("%1 / %2").arg(s, time);
+                       else
+                               s = QString("%1").arg(time);
+                       items++;
+               }
+       } else
+               // In this case, we return the number of samples, really
+               s = time;
+
+       return s;
+}
+
 } // namespace trace
 } // namespace views
 } // namespace pv