]> sigrok.org Git - pulseview.git/blobdiff - pv/views/tabular_decoder/view.cpp
TabularDecView: Implement "hide hidden rows/classes" checkbox
[pulseview.git] / pv / views / tabular_decoder / view.cpp
index b5024d92b531fc3fb3524e8e86fc087cf7ad5c38..0a425e66188119de060ed662914f9a9da6fdb81f 100644 (file)
@@ -51,6 +51,17 @@ namespace pv {
 namespace views {
 namespace tabular_decoder {
 
+const char* SaveTypeNames[SaveTypeCount] = {
+       "CSV, commas escaped",
+       "CSV, fields quoted"
+};
+
+const char* ViewModeNames[ViewModeCount] = {
+       "Show all",
+       "Show all and focus on newest",
+       "Show visible in main view"
+};
+
 QSize QCustomTableView::minimumSizeHint() const
 {
        QSize size(QTableView::sizeHint());
@@ -77,12 +88,13 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        // Note: Place defaults in View::reset_view_state(), not here
        parent_(parent),
        decoder_selector_(new QComboBox()),
+       hide_hidden_cb_(new QCheckBox()),
+       view_mode_selector_(new QComboBox()),
        save_button_(new QToolButton()),
        save_action_(new QAction(this)),
        table_view_(new QCustomTableView()),
        model_(new AnnotationCollectionModel()),
-       signal_(nullptr),
-       updating_data_(false)
+       signal_(nullptr)
 {
        QVBoxLayout *root_layout = new QVBoxLayout(this);
        root_layout->setContentsMargins(0, 0, 0, 0);
@@ -98,13 +110,27 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        toolbar->addWidget(decoder_selector_);
        toolbar->addSeparator();
        toolbar->addWidget(save_button_);
+       toolbar->addSeparator();
+       toolbar->addWidget(view_mode_selector_);
+       toolbar->addSeparator();
+       toolbar->addWidget(hide_hidden_cb_);
 
        connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
                this, SLOT(on_selected_decoder_changed(int)));
+       connect(view_mode_selector_, SIGNAL(currentIndexChanged(int)),
+               this, SLOT(on_view_mode_changed(int)));
+       connect(hide_hidden_cb_, SIGNAL(toggled(bool)),
+               this, SLOT(on_hide_hidden_changed(bool)));
 
        // Configure widgets
        decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
 
+       for (int i = 0; i < ViewModeCount; i++)
+               view_mode_selector_->addItem(ViewModeNames[i], QVariant::fromValue(i));
+
+       hide_hidden_cb_->setText(tr("Hide Hidden Rows/Classes"));
+       hide_hidden_cb_->setChecked(true);
+
        // Configure actions
        save_action_->setText(tr("&Save..."));
        save_action_->setIcon(QIcon::fromTheme("document-save-as",
@@ -117,6 +143,11 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) :
        connect(save_menu, SIGNAL(triggered(QAction*)),
                this, SLOT(on_actionSave_triggered(QAction*)));
 
+       for (int i = 0; i < SaveTypeCount; i++) {
+               QAction *const action = save_menu->addAction(tr(SaveTypeNames[i]));
+               action->setData(qVariantFromValue(i));
+       }
+
        save_button_->setMenu(save_menu);
        save_button_->setDefaultAction(save_action_);
        save_button_->setPopupMode(QToolButton::MenuButtonPopup);
@@ -124,12 +155,13 @@ 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_->setSelectionMode(QAbstractItemView::ContiguousSelection);
        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_->verticalHeader()->setVisible(false);
 
        table_view_->horizontalHeader()->setStretchLastSection(true);
        table_view_->horizontalHeader()->setCascadingSectionResizes(true);
@@ -214,13 +246,20 @@ void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
 void View::save_settings(QSettings &settings) const
 {
        ViewBase::save_settings(settings);
+
+       settings.setValue("view_mode", view_mode_selector_->currentIndex());
+       settings.setValue("hide_hidden", hide_hidden_cb_->isChecked());
 }
 
 void View::restore_settings(QSettings &settings)
 {
-       // Note: It is assumed that this function is only called once,
-       // immediately after restoring a previous session.
        ViewBase::restore_settings(settings);
+
+       if (settings.contains("view_mode"))
+               view_mode_selector_->setCurrentIndex(settings.value("view_mode").toInt());
+
+       if (settings.contains("hide_hidden"))
+               hide_hidden_cb_->setChecked(settings.value("hide_hidden").toBool());
 }
 
 void View::reset_data()
@@ -231,58 +270,97 @@ void View::reset_data()
 
 void View::update_data()
 {
-       if (updating_data_) {
-               if (!delayed_view_updater_.isActive())
-                       delayed_view_updater_.start();
-               return;
-       }
-
-       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
+void View::save_data_as_csv(unsigned int save_type) const
 {
+       // Note: We try to follow RFC 4180 (https://tools.ietf.org/html/rfc4180)
+
        assert(decoder_);
        assert(signal_);
 
        if (!signal_)
                return;
 
-/*     GlobalSettings settings;
+       const bool save_all = !table_view_->selectionModel()->hasSelection();
+
+       GlobalSettings settings;
        const QString dir = settings.value("MainWindow/SaveDirectory").toString();
 
        const QString file_name = QFileDialog::getSaveFileName(
-               parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
+               parent_, tr("Save Annotations as CSV"), dir, tr("CSV Files (*.csv);;Text Files (*.txt);;All Files (*)"));
 
        if (file_name.isEmpty())
                return;
 
        QFile file(file_name);
        if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
-               pair<size_t, size_t> selection = hex_view_->get_selection();
+               QTextStream out_stream(&file);
+
+               if (save_all)
+                       table_view_->selectAll();
+
+               // Write out header columns in visual order, not logical order
+               for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) {
+                       int column = table_view_->horizontalHeader()->logicalIndex(i);
+
+                       if (table_view_->horizontalHeader()->isSectionHidden(column))
+                               continue;
+
+                       const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
+
+                       if (save_type == SaveTypeCSVEscaped)
+                               out_stream << title;
+                       else
+                               out_stream << '"' << title << '"';
 
-               vector<uint8_t> data;
-               data.resize(selection.second - selection.first + 1);
+                       if (i < (table_view_->horizontalHeader()->count() - 1))
+                               out_stream << ",";
+               }
+               out_stream << '\r' << '\n';
+
+
+               QModelIndexList selected_rows = table_view_->selectionModel()->selectedRows();
+
+               for (int i = 0; i < selected_rows.size(); i++) {
+                       const int row = selected_rows.at(i).row();
+
+                       // Write out columns in visual order, not logical order
+                       for (int c = 0; c < table_view_->horizontalHeader()->count(); c++) {
+                               const int column = table_view_->horizontalHeader()->logicalIndex(c);
+
+                               if (table_view_->horizontalHeader()->isSectionHidden(column))
+                                       continue;
 
-               signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
-                       bin_class_id_, selection.first, selection.second, &data);
+                               const QModelIndex idx = model_->index(row, column, QModelIndex());
+                               QString s = model_->data(idx, Qt::DisplayRole).toString();
 
-               int64_t bytes_written = file.write((const char*)data.data(), data.size());
+                               if (save_type == SaveTypeCSVEscaped)
+                                       out_stream << s.replace(",", "\\,");
+                               else
+                                       out_stream << '"' << s.replace("\"", "\"\"") << '"';
+
+                               if (c < (table_view_->horizontalHeader()->count() - 1))
+                                       out_stream << ",";
+                       }
+
+                       out_stream << '\r' << '\n';
+               }
+
+               if (out_stream.status() == QTextStream::Ok) {
+                       if (save_all)
+                               table_view_->clearSelection();
 
-               if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
-                       QMessageBox msg(parent_);
-                       msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
-                       msg.setStandardButtons(QMessageBox::Ok);
-                       msg.setIcon(QMessageBox::Warning);
-                       msg.exec();
                        return;
                }
-       } */
+       }
+
+       QMessageBox msg(parent_);
+       msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
+       msg.setStandardButtons(QMessageBox::Ok);
+       msg.setIcon(QMessageBox::Warning);
+       msg.exec();
 }
 
 void View::on_selected_decoder_changed(int index)
@@ -312,6 +390,19 @@ void View::on_selected_decoder_changed(int index)
        update_data();
 }
 
+void View::on_hide_hidden_changed(bool checked)
+{
+       model_->set_hide_hidden(checked);
+
+       // Force repaint, otherwise the new selection isn't shown for some reason
+       table_view_->viewport()->update();
+}
+
+void View::on_view_mode_changed(int index)
+{
+       (void)index;
+}
+
 void View::on_signal_name_changed(const QString &name)
 {
        (void)name;
@@ -389,9 +480,12 @@ void View::on_decoder_removed(void* decoder)
 
 void View::on_actionSave_triggered(QAction* action)
 {
-       (void)action;
+       int save_type = SaveTypeCSVQuoted;
+
+       if (action)
+               save_type = action->data().toInt();
 
-       save_data();
+       save_data_as_csv(save_type);
 }
 
 void View::on_table_item_clicked(const QModelIndex& index)