]> sigrok.org Git - pulseview.git/commitdiff
TabularDecView: Column header context menu and focus-on-double-click
authorSoeren Apel <redacted>
Thu, 23 Apr 2020 18:20:45 +0000 (20:20 +0200)
committerUwe Hermann <redacted>
Sun, 3 May 2020 15:20:55 +0000 (17:20 +0200)
pv/views/tabular_decoder/model.cpp
pv/views/tabular_decoder/view.cpp
pv/views/tabular_decoder/view.hpp
pv/views/trace/view.cpp
pv/views/trace/view.hpp
pv/views/viewbase.cpp
pv/views/viewbase.hpp

index b502395c3dd907a8d0d2c2f89b07458cc3547d6c..bd9e174c26ba482237c7bdc750e002c1f52880c7 100644 (file)
@@ -90,9 +90,9 @@ QVariant AnnotationCollectionModel::data(const QModelIndex& index, int role) con
 Qt::ItemFlags AnnotationCollectionModel::flags(const QModelIndex& index) const
 {
        if (!index.isValid())
-               return 0;
+               return Qt::NoItemFlags;
 
-       return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+       return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
 }
 
 QVariant AnnotationCollectionModel::headerData(int section, Qt::Orientation orientation,
index 1f79be033bf1432ca472f63b0cbe19d1ceb67bc8..99da2c35d53ad60f75dc7ec1a309affee27bf73f 100644 (file)
@@ -35,6 +35,7 @@
 #include "view.hpp"
 
 #include "pv/globalsettings.hpp"
+#include "pv/session.hpp"
 #include "pv/util.hpp"
 #include "pv/data/decode/decoder.hpp"
 
@@ -122,6 +123,8 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
 
        // Set up the table view
        table_view_->setModel(model_);
+       table_view_->setSelectionBehavior(QAbstractItemView::SelectRows);
+       table_view_->setSelectionMode(QAbstractItemView::SingleSelection);
        table_view_->setSortingEnabled(true);
        table_view_->sortByColumn(0, Qt::AscendingOrder);
 
@@ -131,10 +134,18 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        table_view_->horizontalHeader()->setStretchLastSection(true);
        table_view_->horizontalHeader()->setCascadingSectionResizes(true);
        table_view_->horizontalHeader()->setSectionsMovable(true);
+       table_view_->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu);
 
        table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        parent->setSizePolicy(table_view_->sizePolicy());
 
+       connect(table_view_, SIGNAL(clicked(const QModelIndex&)),
+               this, SLOT(on_table_item_clicked(const QModelIndex&)));
+       connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)),
+               this, SLOT(on_table_item_double_clicked(const QModelIndex&)));
+       connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)),
+               this, SLOT(on_table_header_requested(const QPoint&)));
+
        reset_view_state();
 }
 
@@ -387,6 +398,55 @@ void View::on_actionSave_triggered(QAction* action)
        save_data();
 }
 
+void View::on_table_item_clicked(const QModelIndex& index)
+{
+       (void)index;
+
+       // Force repaint, otherwise the new selection isn't shown for some reason
+       table_view_->viewport()->update();
+}
+
+void View::on_table_item_double_clicked(const QModelIndex& index)
+{
+       const Annotation* ann = static_cast<const Annotation*>(index.internalPointer());
+
+       shared_ptr<views::ViewBase> main_view = session_.main_view();
+
+       main_view->focus_on_range(ann->start_sample(), ann->end_sample());
+}
+
+void View::on_table_header_requested(const QPoint& pos)
+{
+       QMenu* menu = new QMenu(this);
+
+       for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) {
+               int column = table_view_->horizontalHeader()->logicalIndex(i);
+
+               const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
+               QAction* action = new QAction(title, this);
+
+               action->setCheckable(true);
+               action->setChecked(!table_view_->horizontalHeader()->isSectionHidden(column));
+               action->setData(column);
+
+               connect(action, SIGNAL(toggled(bool)), this, SLOT(on_table_header_toggled(bool)));
+
+               menu->addAction(action);
+       }
+
+       menu->popup(table_view_->horizontalHeader()->viewport()->mapToGlobal(pos));
+}
+
+void View::on_table_header_toggled(bool checked)
+{
+       QAction* action = qobject_cast<QAction*>(QObject::sender());
+       assert(action);
+
+       const int column = action->data().toInt();
+
+       table_view_->horizontalHeader()->setSectionHidden(column, !checked);
+}
+
 void View::perform_delayed_view_update()
 {
        update_data();
index 461cc7cd2c460092e0aa436ea6851e6652311c34..a7215f7b85008ad4cf5f0bb9e52bbae68dda7308 100644 (file)
@@ -120,6 +120,11 @@ private Q_SLOTS:
 
        void on_actionSave_triggered(QAction* action = nullptr);
 
+       void on_table_item_clicked(const QModelIndex& index);
+       void on_table_item_double_clicked(const QModelIndex& index);
+       void on_table_header_requested(const QPoint& pos);
+       void on_table_header_toggled(bool checked);
+
        virtual void perform_delayed_view_update();
 
 private:
index 227fadfdb4197c334d084dd9a19edb37e4728181..33122047745048e341c155956e6f99076b73e136 100644 (file)
@@ -783,6 +783,26 @@ void View::zoom_fit(bool gui_state)
        set_scale_offset(scale.convert_to<double>(), extents.first);
 }
 
+void View::focus_on_range(uint64_t start_sample, uint64_t end_sample)
+{
+       assert(viewport_);
+       const int w = viewport_->width();
+       if (w <= 0)
+               return;
+
+       const double samplerate = session_.get_samplerate();
+
+       const uint64_t sample_delta = (end_sample - start_sample);
+
+       // Note: We add 20% margin on the left and 5% on the right
+       const Timestamp delta = (sample_delta * 1.25) / samplerate;
+
+       const Timestamp scale = max(min(delta / w, MaxScale), MinScale);
+       const Timestamp offset = (start_sample - sample_delta * 0.20) / samplerate;
+
+       set_scale_offset(scale.convert_to<double>(), offset);
+}
+
 void View::set_scale_offset(double scale, const Timestamp& offset)
 {
        // Disable sticky scrolling / always zoom to fit when acquisition runs
index d57acd914bdaa8872a95f930e7e8b9b374fc3d4d..5863bc9d976caf6a2eb43d392879aa26b74a0a2b 100644 (file)
@@ -154,7 +154,6 @@ public:
        const Ruler* ruler() const;
 
        virtual void save_settings(QSettings &settings) const;
-
        virtual void restore_settings(QSettings &settings);
 
        /**
@@ -254,6 +253,8 @@ public:
 
        void zoom_fit(bool gui_state);
 
+       virtual void focus_on_range(uint64_t start_sample, uint64_t end_sample);
+
        /**
         * Sets the scale and offset.
         * @param scale The new view scale in seconds per pixel.
index 9e5a2887c8af2cdcd3bf384810ed976858fdb214..7be8822263c12111ec1bb0a6bb2e78ef7c88e607 100644 (file)
@@ -146,6 +146,12 @@ void ViewBase::restore_settings(QSettings &settings)
        (void)settings;
 }
 
+void ViewBase::focus_on_range(uint64_t start_sample, uint64_t end_sample)
+{
+       (void)start_sample;
+       (void)end_sample;
+}
+
 void ViewBase::trigger_event(int segment_id, util::Timestamp location)
 {
        (void)segment_id;
index e3a3c6a404925ee6d03f4a10fba7d89b89fb71b0..73d7a179c5a4e78fa446b1ec8a040fed98e6820b 100644 (file)
@@ -105,9 +105,10 @@ public:
 #endif
 
        virtual void save_settings(QSettings &settings) const;
-
        virtual void restore_settings(QSettings &settings);
 
+       virtual void focus_on_range(uint64_t start_sample, uint64_t end_sample);
+
 public Q_SLOTS:
        virtual void trigger_event(int segment_id, util::Timestamp location);
        virtual void signals_changed();