From: Miklos Marton Date: Sun, 19 Jan 2020 12:16:15 +0000 (+0100) Subject: TraceView: Add context menu to the cursor X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=79a37ed84b4021736ee7cac75fa3a255b5453dda;p=pulseview.git TraceView: Add context menu to the cursor See https://github.com/sigrokproject/pulseview/pull/15 --- diff --git a/pv/globalsettings.cpp b/pv/globalsettings.cpp index 1c23e04c..1573a9f2 100644 --- a/pv/globalsettings.cpp +++ b/pv/globalsettings.cpp @@ -62,6 +62,8 @@ const QString GlobalSettings::Key_View_DefaultLogicHeight = "View_DefaultLogicHe const QString GlobalSettings::Key_View_ShowHoverMarker = "View_ShowHoverMarker"; const QString GlobalSettings::Key_View_SnapDistance = "View_SnapDistance"; const QString GlobalSettings::Key_View_CursorFillColor = "View_CursorFillColor"; +const QString GlobalSettings::Key_View_CursorShowFrequency = "View_CursorShowFrequency"; +const QString GlobalSettings::Key_View_CursorShowInterval = "View_CursorShowInterval"; const QString GlobalSettings::Key_Dec_InitialStateConfigurable = "Dec_InitialStateConfigurable"; const QString GlobalSettings::Key_Dec_ExportFormat = "Dec_ExportFormat"; const QString GlobalSettings::Key_Dec_AlwaysShowAllRows = "Dec_AlwaysShowAllRows"; @@ -140,6 +142,12 @@ void GlobalSettings::set_defaults_where_needed() if (!contains(Key_View_SnapDistance)) setValue(Key_View_SnapDistance, 15); + if (!contains(Key_View_CursorShowInterval)) + setValue(Key_View_CursorShowInterval, true); + + if (!contains(Key_View_CursorShowFrequency)) + setValue(Key_View_CursorShowFrequency, true); + // %c was used for the row name in the past so we need to transition such users if (!contains(Key_Dec_ExportFormat) || value(Key_Dec_ExportFormat).toString() == "%s %d: %c: %1") diff --git a/pv/globalsettings.hpp b/pv/globalsettings.hpp index 56ca9632..f526285a 100644 --- a/pv/globalsettings.hpp +++ b/pv/globalsettings.hpp @@ -70,6 +70,8 @@ public: static const QString Key_View_ShowHoverMarker; static const QString Key_View_SnapDistance; static const QString Key_View_CursorFillColor; + static const QString Key_View_CursorShowInterval; + static const QString Key_View_CursorShowFrequency; static const QString Key_Dec_InitialStateConfigurable; static const QString Key_Dec_ExportFormat; static const QString Key_Dec_AlwaysShowAllRows; diff --git a/pv/views/trace/cursorpair.cpp b/pv/views/trace/cursorpair.cpp index 3e3a6b8e..bd6814c5 100644 --- a/pv/views/trace/cursorpair.cpp +++ b/pv/views/trace/cursorpair.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include "cursorpair.hpp" @@ -53,6 +54,10 @@ CursorPair::CursorPair(View &view) : GlobalSettings settings; fill_color_ = QColor::fromRgba(settings.value( GlobalSettings::Key_View_CursorFillColor).value()); + show_frequency_ = settings.value( + GlobalSettings::Key_View_CursorShowFrequency).value(); + show_interval_ = settings.value( + GlobalSettings::Key_View_CursorShowInterval).value(); connect(&view_, SIGNAL(hover_point_changed(const QWidget*, QPoint)), this, SLOT(on_hover_point_changed(const QWidget*, QPoint))); @@ -114,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()); + }); + + 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()); + }); + + return menu; +} + QRectF CursorPair::label_rect(const QRectF &rect) const { const QSizeF label_size(text_size_ + LabelPadding * 2); @@ -243,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()); + + if (key == GlobalSettings::Key_View_CursorShowFrequency) + show_frequency_ = value.value(); + + if (key == GlobalSettings::Key_View_CursorShowInterval) + show_interval_ = value.value(); } void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) @@ -261,6 +301,8 @@ void CursorPair::on_hover_point_changed(const QWidget* widget, const QPoint& hp) 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()); @@ -270,14 +312,28 @@ QString CursorPair::format_string_sub(int time_precision, int freq_precision, bo // 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); + int items = 0; + + if (show_frequency_) { + const QString freq = util::format_value_si( + 1 / diff.convert_to(), 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 - return time; + s = time; + + return s; } } // namespace trace diff --git a/pv/views/trace/cursorpair.hpp b/pv/views/trace/cursorpair.hpp index 0a5ff4cb..649875ee 100644 --- a/pv/views/trace/cursorpair.hpp +++ b/pv/views/trace/cursorpair.hpp @@ -86,6 +86,8 @@ public: pv::widgets::Popup* create_popup(QWidget *parent) override; + QMenu* create_header_context_menu(QWidget *parent) override; + QRectF label_rect(const QRectF &rect) const override; /** @@ -126,6 +128,7 @@ private: QSizeF text_size_; QRectF label_area_; bool label_incomplete_; + bool show_interval_, show_frequency_; }; } // namespace trace