]> sigrok.org Git - pulseview.git/commitdiff
TraceView: Add context menu to the cursor
authorMiklos Marton <redacted>
Sun, 19 Jan 2020 12:16:15 +0000 (13:16 +0100)
committerUwe Hermann <redacted>
Wed, 22 Jan 2020 21:56:04 +0000 (22:56 +0100)
See https://github.com/sigrokproject/pulseview/pull/15

pv/globalsettings.cpp
pv/globalsettings.hpp
pv/views/trace/cursorpair.cpp
pv/views/trace/cursorpair.hpp

index 1c23e04c39a85929ed17de14d4e23878caeaca58..1573a9f2ba165b23c2582de93aa0aba05ef794c1 100644 (file)
@@ -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")
index 56ca9632e45a3bdcbb757018bd72a177f493791a..f526285a13d322f364d34d069459e839a7eb4780 100644 (file)
@@ -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;
index 3e3a6b8e467e3b3fe1e7087f7c2b9e681b7ecedb..bd6814c5c8f1a86258d2dcda6eb4d4af5b922450 100644 (file)
@@ -21,6 +21,7 @@
 #include <cassert>
 
 #include <QColor>
+#include <QMenu>
 #include <QToolTip>
 
 #include "cursorpair.hpp"
@@ -53,6 +54,10 @@ CursorPair::CursorPair(View &view) :
        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)));
@@ -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<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);
@@ -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<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)
@@ -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<double>(), 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<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
-               return time;
+               s = time;
+
+       return s;
 }
 
 } // namespace trace
index 0a5ff4cb782e7bca07607b90b112587fc3a955e7..649875ee4f1e98ae5dde96bd24aaa011c505f8b3 100644 (file)
@@ -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