]> sigrok.org Git - pulseview.git/commitdiff
Add DecoderOutputView save button and action
authorSoeren Apel <redacted>
Wed, 18 Dec 2019 15:46:31 +0000 (16:46 +0100)
committerSoeren Apel <redacted>
Fri, 20 Dec 2019 00:44:21 +0000 (01:44 +0100)
pv/toolbars/mainbar.hpp
pv/views/decoder_output/view.cpp
pv/views/decoder_output/view.hpp
pv/views/trace/view.cpp
pv/views/viewbase.cpp

index 008ab628f701ca714cfca02391f07ed90d438335..04c344c5351088e8e636e50159adb1c536d3dc08 100644 (file)
@@ -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_;
index 18cb3b449726ad19e953624d334d3f3e4fb17460..218c5c9a6e84fbf07e09c043915c8ad6130dff4f 100644 (file)
@@ -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<data::DecodeSignal> signal)
@@ -170,10 +200,9 @@ void View::remove_decode_signal(shared_ptr<data::DecodeSignal> 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<void*>();
 
        // Find the signal that contains the selected decoder
-       signal_ = nullptr;
-
        for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
                for (const shared_ptr<Decoder>& 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<uint32_t>();
 
+       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();
 }
 
index 39869d514b289b8a80d53cd11fd2b743e27b0065..ece85ad65835f45a5d83a29ece14f8eb97515ae6 100644 (file)
 #ifndef PULSEVIEW_PV_VIEWS_DECODEROUTPUT_VIEW_HPP
 #define PULSEVIEW_PV_VIEWS_DECODEROUTPUT_VIEW_HPP
 
+#include <QAction>
 #include <QComboBox>
 #include <QStackedWidget>
+#include <QToolButton>
 
 #include <pv/views/viewbase.hpp>
 #include <pv/data/decodesignal.hpp>
@@ -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<data::DecodeSignal> signal);
        virtual void remove_decode_signal(shared_ptr<data::DecodeSignal> 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
index 80322f9b3a4258db05e9b53a7fc4cb3549cf9e76..9b8e76c5696cc6559bd450fbee614bbb5f2c04dc 100644 (file)
@@ -305,7 +305,7 @@ unordered_set< shared_ptr<Signal> > View::signals() const
 
 void View::clear_signals()
 {
-       ViewBase::clear_signalbases();
+       ViewBase::clear_signals();
        signals_.clear();
 }
 
index b35e27b9798604cfc5b1647282bacfaccfb0035c..da455ed30ca4f8db3e4651baf5748df6ce1d8863 100644 (file)
@@ -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<data::SignalBase> > ViewBase::signalbases() const