]> sigrok.org Git - pulseview.git/blobdiff - pv/views/tabular_decoder/view.cpp
TabularDecView-related bug fixes
[pulseview.git] / pv / views / tabular_decoder / view.cpp
index 8c5859e3bcb17f7f425a0b7ec66787722fd93c1b..b5024d92b531fc3fb3524e8e86fc087cf7ad5c38 100644 (file)
 
 #include <climits>
 
+#include <QApplication>
 #include <QDebug>
 #include <QFileDialog>
+#include <QFontMetrics>
+#include <QHeaderView>
 #include <QLabel>
 #include <QMenu>
 #include <QMessageBox>
@@ -32,6 +35,7 @@
 #include "view.hpp"
 
 #include "pv/globalsettings.hpp"
+#include "pv/session.hpp"
 #include "pv/util.hpp"
 #include "pv/data/decode/decoder.hpp"
 
@@ -40,12 +44,32 @@ using pv::data::SignalBase;
 using pv::data::decode::Decoder;
 using pv::util::Timestamp;
 
+using std::make_shared;
 using std::shared_ptr;
 
 namespace pv {
 namespace views {
 namespace tabular_decoder {
 
+QSize QCustomTableView::minimumSizeHint() const
+{
+       QSize size(QTableView::sizeHint());
+
+       int width = 0;
+       for (int i = 0; i < horizontalHeader()->count(); i++)
+               if (!horizontalHeader()->isSectionHidden(i))
+                       width += horizontalHeader()->sectionSize(i);
+
+       size.setWidth(width + (horizontalHeader()->count() * 1));
+
+       return size;
+}
+
+QSize QCustomTableView::sizeHint() const
+{
+       return minimumSizeHint();
+}
+
 
 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        ViewBase(session, is_main_view, parent),
@@ -55,9 +79,10 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        decoder_selector_(new QComboBox()),
        save_button_(new QToolButton()),
        save_action_(new QAction(this)),
-       table_view_(new QTableView()),
+       table_view_(new QCustomTableView()),
        model_(new AnnotationCollectionModel()),
-       signal_(nullptr)
+       signal_(nullptr),
+       updating_data_(false)
 {
        QVBoxLayout *root_layout = new QVBoxLayout(this);
        root_layout->setContentsMargins(0, 0, 0, 0);
@@ -98,9 +123,29 @@ 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);
 
+       const int font_height = QFontMetrics(QApplication::font()).height();
+       table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4);
+
+       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();
 }
 
@@ -130,23 +175,21 @@ void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
 
        connect(signal.get(), SIGNAL(name_changed(const QString&)),
                this, SLOT(on_signal_name_changed(const QString&)));
+
+       // Note: At time of initial creation, decode signals have no decoders so we
+       // need to watch for decoder stacking events
+
        connect(signal.get(), SIGNAL(decoder_stacked(void*)),
                this, SLOT(on_decoder_stacked(void*)));
        connect(signal.get(), SIGNAL(decoder_removed(void*)),
                this, SLOT(on_decoder_removed(void*)));
 
-       // Add all decoders provided by this signal
+       // Add the top-level decoder provided by an already-existing signal
        auto stack = signal->decoder_stack();
-       if (stack.size() > 1) {
-               for (const shared_ptr<Decoder>& dec : stack) {
-                       QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
-                       decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
-               }
-       } else
-               if (!stack.empty()) {
-                       shared_ptr<Decoder>& dec = stack.at(0);
-                       decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
-               }
+       if (!stack.empty()) {
+               shared_ptr<Decoder>& dec = stack.at(0);
+               decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
+       }
 }
 
 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
@@ -188,10 +231,18 @@ void View::reset_data()
 
 void View::update_data()
 {
-       if (!signal_)
+       if (updating_data_) {
+               if (!delayed_view_updater_.isActive())
+                       delayed_view_updater_.start();
                return;
+       }
 
-       // TBD
+       updating_data_ = true;
+
+       table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
+       model_->set_signal_and_segment(signal_, current_segment_);
+
+       updating_data_ = false;
 }
 
 void View::save_data() const
@@ -236,8 +287,11 @@ void View::save_data() const
 
 void View::on_selected_decoder_changed(int index)
 {
-       if (signal_)
+       if (signal_) {
+               disconnect(signal_, SIGNAL(color_changed(QColor)));
                disconnect(signal_, SIGNAL(new_annotations()));
+               disconnect(signal_, SIGNAL(decode_reset()));
+       }
 
        reset_data();
 
@@ -250,7 +304,9 @@ void View::on_selected_decoder_changed(int index)
                                signal_ = ds.get();
 
        if (signal_) {
+               connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor)));
                connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
+               connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset()));
        }
 
        update_data();
@@ -266,24 +322,22 @@ void View::on_signal_name_changed(const QString &name)
        DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
        assert(signal);
 
-       // Update all decoder entries provided by this signal
+       // Update the top-level decoder provided by this signal
        auto stack = signal->decoder_stack();
-       if (stack.size() > 1) {
-               for (const shared_ptr<Decoder>& dec : stack) {
-                       QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
-                       int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
+       if (!stack.empty()) {
+               shared_ptr<Decoder>& dec = stack.at(0);
+               int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
 
-                       if (index != -1)
-                               decoder_selector_->setItemText(index, title);
-               }
-       } else
-               if (!stack.empty()) {
-                       shared_ptr<Decoder>& dec = stack.at(0);
-                       int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
+               if (index != -1)
+                       decoder_selector_->setItemText(index, signal->name());
+       }
+}
 
-                       if (index != -1)
-                               decoder_selector_->setItemText(index, signal->name());
-               }
+void View::on_signal_color_changed(const QColor &color)
+{
+       (void)color;
+
+       table_view_->update();
 }
 
 void View::on_new_annotations()
@@ -292,10 +346,15 @@ void View::on_new_annotations()
                delayed_view_updater_.start();
 }
 
-void View::on_decoder_stacked(void* decoder)
+void View::on_decoder_reset()
 {
-       // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
+       // Invalidate the model's data connection immediately - otherwise we
+       // will use a stale pointer in model_->index() when called from the table view
+       model_->set_signal_and_segment(signal_, current_segment_);
+}
 
+void View::on_decoder_stacked(void* decoder)
+{
        Decoder* d = static_cast<Decoder*>(decoder);
 
        // Find the signal that contains the selected decoder
@@ -308,9 +367,13 @@ void View::on_decoder_stacked(void* decoder)
 
        assert(signal);
 
-       // Add the decoder to the list
-       QString title = QString("%1 (%2)").arg(signal->name(), d->name());
-       decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
+       const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0);
+       int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
+
+       if (index == -1) {
+               // Add the decoder to the list
+               decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)d));
+       }
 }
 
 void View::on_decoder_removed(void* decoder)
@@ -331,6 +394,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();