]> sigrok.org Git - pulseview.git/blob - pv/views/decoder_output/view.cpp
DecoderOutput: Add selector box and fix signal handling
[pulseview.git] / pv / views / decoder_output / view.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2019 Soeren Apel <soeren@apelpie.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <libsigrokdecode/libsigrokdecode.h>
21
22 #include <QLabel>
23 #include <QMenu>
24 #include <QToolBar>
25 #include <QVBoxLayout>
26
27 #include "view.hpp"
28
29 #include "pv/session.hpp"
30 #include "pv/util.hpp"
31
32 using pv::data::SignalBase;
33 using pv::util::TimeUnit;
34 using pv::util::Timestamp;
35
36 using std::shared_ptr;
37
38 namespace pv {
39 namespace views {
40 namespace decoder_output {
41
42 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
43         ViewBase(session, is_main_view, parent),
44
45         // Note: Place defaults in View::reset_view_state(), not here
46         signal_selector_(new QComboBox())
47 {
48         QVBoxLayout *root_layout = new QVBoxLayout(this);
49         root_layout->setContentsMargins(0, 0, 0, 0);
50
51         // Create toolbar
52         QToolBar* toolbar = new QToolBar();
53         toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
54         parent->addToolBar(toolbar);
55
56         // Populate toolbar
57         toolbar->addWidget(new QLabel(tr("Decoder:")));
58         toolbar->addWidget(signal_selector_);
59
60         reset_view_state();
61 }
62
63 View::~View()
64 {
65 }
66
67 ViewType View::get_type() const
68 {
69         return ViewTypeDecoderOutput;
70 }
71
72 void View::reset_view_state()
73 {
74         ViewBase::reset_view_state();
75 }
76
77 void View::clear_signals()
78 {
79         ViewBase::clear_signalbases();
80 }
81
82 void View::clear_decode_signals()
83 {
84         signal_selector_->clear();
85 }
86
87 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
88 {
89         connect(signal.get(), SIGNAL(name_changed(const QString&)),
90                 this, SLOT(on_signal_name_changed(const QString&)));
91
92         signal_selector_->addItem(signal->name(), qVariantFromValue(signal.get()));
93 }
94
95 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
96 {
97         int index = signal_selector_->findData(qVariantFromValue(signal.get()));
98
99         if (index != -1)
100                 signal_selector_->removeItem(index);
101 }
102
103 void View::save_settings(QSettings &settings) const
104 {
105         (void)settings;
106 }
107
108 void View::restore_settings(QSettings &settings)
109 {
110         // Note: It is assumed that this function is only called once,
111         // immediately after restoring a previous session.
112         (void)settings;
113 }
114
115 void View::on_signal_name_changed(const QString &name)
116 {
117         SignalBase *sb = qobject_cast<SignalBase*>(QObject::sender());
118         assert(sb);
119
120         int index = signal_selector_->findData(qVariantFromValue(sb));
121         if (index != -1)
122                 signal_selector_->setItemText(index, name);
123 }
124
125 } // namespace decoder_output
126 } // namespace views
127 } // namespace pv