From: Soeren Apel Date: Wed, 18 Dec 2019 15:46:31 +0000 (+0100) Subject: Add DecoderOutputView save button and action X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=03408f5f5b62ce74016d91d60bdadce8dbddd46b;p=pulseview.git Add DecoderOutputView save button and action --- diff --git a/pv/toolbars/mainbar.hpp b/pv/toolbars/mainbar.hpp index 008ab628..04c344c5 100644 --- a/pv/toolbars/mainbar.hpp +++ b/pv/toolbars/mainbar.hpp @@ -116,14 +116,6 @@ private: void commit_sample_rate(); void commit_sample_count(); - QAction *const action_new_view_; - QAction *const action_open_; - QAction *const action_save_as_; - QAction *const action_save_selection_as_; - QAction *const action_restore_setup_; - QAction *const action_save_setup_; - QAction *const action_connect_; - private Q_SLOTS: void show_session_error(const QString text, const QString info_text); @@ -162,6 +154,14 @@ Q_SIGNALS: void show_decoder_selector(Session *session); private: + QAction *const action_new_view_; + QAction *const action_open_; + QAction *const action_save_as_; + QAction *const action_save_selection_as_; + QAction *const action_restore_setup_; + QAction *const action_save_setup_; + QAction *const action_connect_; + QToolButton *new_view_button_, *open_button_, *save_button_; pv::widgets::DeviceToolButton device_selector_; diff --git a/pv/views/decoder_output/view.cpp b/pv/views/decoder_output/view.cpp index 18cb3b44..218c5c9a 100644 --- a/pv/views/decoder_output/view.cpp +++ b/pv/views/decoder_output/view.cpp @@ -49,6 +49,12 @@ namespace pv { namespace views { namespace decoder_output { +const char* SaveTypeNames[SaveTypeCount] = { + "Binary", + "Hex Dump" +}; + + View::View(Session &session, bool is_main_view, QMainWindow *parent) : ViewBase(session, is_main_view, parent), @@ -59,6 +65,8 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : class_selector_(new QComboBox()), stacked_widget_(new QStackedWidget()), hex_view_(new QHexView()), + save_button_(new QToolButton()), + save_action_(new QAction(this)), signal_(nullptr) { QVBoxLayout *root_layout = new QVBoxLayout(this); @@ -76,6 +84,8 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : toolbar->addSeparator(); toolbar->addWidget(new QLabel(tr("Show data as"))); toolbar->addWidget(format_selector_); + toolbar->addSeparator(); + toolbar->addWidget(save_button_); // Add format types format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump"))); @@ -94,6 +104,27 @@ View::View(Session &session, bool is_main_view, QMainWindow *parent) : decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); + // Configure actions + save_action_->setText(tr("&Save...")); + save_action_->setIcon(QIcon::fromTheme("document-save-as", + QIcon(":/icons/document-save-as.png"))); + save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); + connect(save_action_, SIGNAL(triggered(bool)), + this, SLOT(on_actionSave_triggered())); + + QMenu *save_menu = new QMenu(); + 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); + parent->setSizePolicy(hex_view_->sizePolicy()); // TODO Must be updated when selected widget changes reset_view_state(); @@ -111,22 +142,21 @@ ViewType View::get_type() const void View::reset_view_state() { ViewBase::reset_view_state(); -} -void View::clear_signals() -{ - ViewBase::clear_signalbases(); - signal_ = nullptr; + decoder_selector_->clear(); + class_selector_->clear(); + format_selector_->setCurrentIndex(0); + save_button_->setEnabled(false); + + hex_view_->clear(); } void View::clear_decode_signals() { ViewBase::clear_decode_signals(); - decoder_selector_->clear(); - class_selector_->clear(); - format_selector_->setCurrentIndex(0); - signal_ = nullptr; + reset_data(); + reset_view_state(); } void View::add_decode_signal(shared_ptr signal) @@ -170,10 +200,9 @@ void View::remove_decode_signal(shared_ptr signal) ViewBase::remove_decode_signal(signal); if (signal.get() == signal_) { - signal_ = nullptr; - decoder_ = nullptr; - bin_class_id_ = 0; + reset_data(); update_data(); + reset_view_state(); } } @@ -189,17 +218,23 @@ void View::restore_settings(QSettings &settings) (void)settings; } +void View::reset_data() +{ + signal_ = nullptr; + decoder_ = nullptr; + bin_class_id_ = 0; + binary_data_exists_ = false; + + hex_view_->clear(); +} + void View::update_data() { - if (!signal_) { - hex_view_->clear(); + if (!signal_) return; - } - if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) { - hex_view_->clear(); + if (!binary_data_exists_) return; - } const DecodeBinaryClass* bin_class = signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_); @@ -212,11 +247,11 @@ void View::on_selected_decoder_changed(int index) if (signal_) disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int))); + reset_data(); + decoder_ = (Decoder*)decoder_selector_->itemData(index).value(); // Find the signal that contains the selected decoder - signal_ = nullptr; - for (const shared_ptr& ds : decode_signals_) for (const shared_ptr& dec : ds->decoder_stack()) if (decoder_ == dec.get()) @@ -243,6 +278,9 @@ void View::on_selected_class_changed(int index) { bin_class_id_ = class_selector_->itemData(index).value(); + binary_data_exists_ = + signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_); + update_data(); } @@ -319,8 +357,20 @@ void View::on_decoder_removed(void* decoder) decoder_selector_->removeItem(index); } +void View::on_actionSave_triggered(QAction* action) +{ + (void)action; +} + void View::perform_delayed_view_update() { + if (!binary_data_exists_) + if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_)) { + binary_data_exists_ = true; + + save_button_->setEnabled(true); + } + update_data(); } diff --git a/pv/views/decoder_output/view.hpp b/pv/views/decoder_output/view.hpp index 39869d51..ece85ad6 100644 --- a/pv/views/decoder_output/view.hpp +++ b/pv/views/decoder_output/view.hpp @@ -20,8 +20,10 @@ #ifndef PULSEVIEW_PV_VIEWS_DECODEROUTPUT_VIEW_HPP #define PULSEVIEW_PV_VIEWS_DECODEROUTPUT_VIEW_HPP +#include #include #include +#include #include #include @@ -36,6 +38,16 @@ namespace views { namespace decoder_output { +// When adding an entry here, don't forget to update SaveTypeNames as well +enum SaveType { + SaveTypeBinary, + SaveTypeHexDump, + SaveTypeCount // Indicates how many save types there are, must always be last +}; + +extern const char* SaveTypeNames[SaveTypeCount]; + + class View : public ViewBase { Q_OBJECT @@ -53,8 +65,6 @@ public: */ virtual void reset_view_state(); - virtual void clear_signals(); - virtual void clear_decode_signals(); virtual void add_decode_signal(shared_ptr signal); virtual void remove_decode_signal(shared_ptr signal); @@ -63,6 +73,7 @@ public: virtual void restore_settings(QSettings &settings); private: + void reset_data(); void update_data(); private Q_SLOTS: @@ -74,6 +85,8 @@ private Q_SLOTS: void on_decoder_stacked(void* decoder); void on_decoder_removed(void* decoder); + void on_actionSave_triggered(QAction* action = nullptr); + virtual void perform_delayed_view_update(); private: @@ -83,9 +96,13 @@ private: QStackedWidget *stacked_widget_; QHexView *hex_view_; + QToolButton* save_button_; + QAction* save_action_; + data::DecodeSignal *signal_; const data::decode::Decoder *decoder_; uint32_t bin_class_id_; + bool binary_data_exists_; }; } // namespace decoder_output diff --git a/pv/views/trace/view.cpp b/pv/views/trace/view.cpp index 80322f9b..9b8e76c5 100644 --- a/pv/views/trace/view.cpp +++ b/pv/views/trace/view.cpp @@ -305,7 +305,7 @@ unordered_set< shared_ptr > View::signals() const void View::clear_signals() { - ViewBase::clear_signalbases(); + ViewBase::clear_signals(); signals_.clear(); } diff --git a/pv/views/viewbase.cpp b/pv/views/viewbase.cpp index b35e27b9..da455ed3 100644 --- a/pv/views/viewbase.cpp +++ b/pv/views/viewbase.cpp @@ -34,9 +34,9 @@ namespace pv { namespace views { const char* ViewTypeNames[ViewTypeCount] = { - "Trace View", + "Trace View", #ifdef ENABLE_DECODE - "Decoder Output View" + "Decoder Output View" #endif }; @@ -80,6 +80,7 @@ const Session& ViewBase::session() const void ViewBase::clear_signals() { + clear_signalbases(); } unordered_set< shared_ptr > ViewBase::signalbases() const