]> sigrok.org Git - pulseview.git/blob - pv/views/decoder_output/view.cpp
DecoderOutputView: Allow for adaptive size constraints
[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 <climits>
21
22 #include <QByteArray>
23 #include <QDebug>
24 #include <QLabel>
25 #include <QMenu>
26 #include <QToolBar>
27 #include <QVBoxLayout>
28
29 #include <libsigrokdecode/libsigrokdecode.h>
30
31 #include "view.hpp"
32 #include "QHexView.hpp"
33
34 #include "pv/session.hpp"
35 #include "pv/util.hpp"
36 #include "pv/data/decode/decoder.hpp"
37
38 using pv::data::DecodeSignal;
39 using pv::data::SignalBase;
40 using pv::data::decode::Decoder;
41 using pv::util::TimeUnit;
42 using pv::util::Timestamp;
43
44 using std::dynamic_pointer_cast;
45 using std::numeric_limits;
46 using std::shared_ptr;
47
48 namespace pv {
49 namespace views {
50 namespace decoder_output {
51
52 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
53         ViewBase(session, is_main_view, parent),
54
55         // Note: Place defaults in View::reset_view_state(), not here
56         parent_(parent),
57         decoder_selector_(new QComboBox()),
58         format_selector_(new QComboBox()),
59         class_selector_(new QComboBox()),
60         stacked_widget_(new QStackedWidget()),
61         hex_view_(new QHexView()),
62         signal_(nullptr)
63 {
64         QVBoxLayout *root_layout = new QVBoxLayout(this);
65         root_layout->setContentsMargins(0, 0, 0, 0);
66
67         // Create toolbar
68         QToolBar* toolbar = new QToolBar();
69         toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
70         parent->addToolBar(toolbar);
71
72         // Populate toolbar
73         toolbar->addWidget(new QLabel(tr("Decoder:")));
74         toolbar->addWidget(decoder_selector_);
75         toolbar->addWidget(class_selector_);
76         toolbar->addSeparator();
77         toolbar->addWidget(new QLabel(tr("Show data as")));
78         toolbar->addWidget(format_selector_);
79
80         // Add format types
81         format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
82
83         // Add widget stack
84         root_layout->addWidget(stacked_widget_);
85         stacked_widget_->addWidget(hex_view_);
86         stacked_widget_->setCurrentIndex(0);
87
88         connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
89                 this, SLOT(on_selected_decoder_changed(int)));
90         connect(class_selector_, SIGNAL(currentIndexChanged(int)),
91                 this, SLOT(on_selected_class_changed(int)));
92
93         // Configure widgets
94         decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
95         class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
96
97         parent->setSizePolicy(hex_view_->sizePolicy()); // TODO Must be updated when selected widget changes
98
99         reset_view_state();
100 }
101
102 View::~View()
103 {
104 }
105
106 ViewType View::get_type() const
107 {
108         return ViewTypeDecoderOutput;
109 }
110
111 void View::reset_view_state()
112 {
113         ViewBase::reset_view_state();
114 }
115
116 void View::clear_signals()
117 {
118         ViewBase::clear_signalbases();
119         signal_ = nullptr;
120 }
121
122 void View::clear_decode_signals()
123 {
124         ViewBase::clear_decode_signals();
125
126         decoder_selector_->clear();
127         class_selector_->clear();
128         format_selector_->setCurrentIndex(0);
129         signal_ = nullptr;
130 }
131
132 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
133 {
134         ViewBase::add_decode_signal(signal);
135
136         connect(signal.get(), SIGNAL(name_changed(const QString&)),
137                 this, SLOT(on_signal_name_changed(const QString&)));
138         connect(signal.get(), SIGNAL(decoder_stacked(void*)),
139                 this, SLOT(on_decoder_stacked(void*)));
140         connect(signal.get(), SIGNAL(decoder_removed(void*)),
141                 this, SLOT(on_decoder_removed(void*)));
142
143         // Add all decoders provided by this signal
144         auto stack = signal->decoder_stack();
145         if (stack.size() > 1) {
146                 for (const shared_ptr<Decoder>& dec : stack)
147                         // Only add the decoder if it has binary output
148                         if (dec->get_binary_class_count() > 0) {
149                                 QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
150                                 decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
151                         }
152         } else
153                 if (!stack.empty()) {
154                         shared_ptr<Decoder>& dec = stack.at(0);
155                         if (dec->get_binary_class_count() > 0)
156                                 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
157                 }
158 }
159
160 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
161 {
162         // Remove all decoders provided by this signal
163         for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
164                 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
165
166                 if (index != -1)
167                         decoder_selector_->removeItem(index);
168         }
169
170         ViewBase::remove_decode_signal(signal);
171
172         if (signal.get() == signal_) {
173                 signal_ = nullptr;
174                 decoder_ = nullptr;
175                 bin_class_id_ = 0;
176                 update_data();
177         }
178 }
179
180 void View::save_settings(QSettings &settings) const
181 {
182         (void)settings;
183 }
184
185 void View::restore_settings(QSettings &settings)
186 {
187         // Note: It is assumed that this function is only called once,
188         // immediately after restoring a previous session.
189         (void)settings;
190 }
191
192 void View::update_data()
193 {
194         if (!signal_) {
195                 hex_view_->clear();
196                 return;
197         }
198
199         if (signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_) == 0) {
200                 hex_view_->clear();
201                 return;
202         }
203
204         const DecodeBinaryClass* bin_class =
205                 signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
206
207         hex_view_->setData(bin_class);
208 }
209
210 void View::on_selected_decoder_changed(int index)
211 {
212         if (signal_)
213                 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
214
215         decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
216
217         // Find the signal that contains the selected decoder
218         signal_ = nullptr;
219
220         for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
221                 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
222                         if (decoder_ == dec.get())
223                                 signal_ = ds.get();
224
225         class_selector_->clear();
226
227         if (signal_) {
228                 // Populate binary class selector
229                 uint32_t bin_classes = decoder_->get_binary_class_count();
230                 for (uint32_t i = 0; i < bin_classes; i++) {
231                         const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
232                         class_selector_->addItem(class_info->description, QVariant::fromValue(i));
233                 }
234
235                 connect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)),
236                         this, SLOT(on_new_binary_data(unsigned int, void*, unsigned int)));
237         }
238
239         update_data();
240 }
241
242 void View::on_selected_class_changed(int index)
243 {
244         bin_class_id_ = class_selector_->itemData(index).value<uint32_t>();
245
246         update_data();
247 }
248
249 void View::on_signal_name_changed(const QString &name)
250 {
251         (void)name;
252
253         SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
254         assert(sb);
255
256         DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
257         assert(signal);
258
259         // Update all decoder entries provided by this signal
260         auto stack = signal->decoder_stack();
261         if (stack.size() > 1) {
262                 for (const shared_ptr<Decoder>& dec : stack) {
263                         QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
264                         int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
265
266                         if (index != -1)
267                                 decoder_selector_->setItemText(index, title);
268                 }
269         } else
270                 if (!stack.empty()) {
271                         shared_ptr<Decoder>& dec = stack.at(0);
272                         int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
273
274                         if (index != -1)
275                                 decoder_selector_->setItemText(index, signal->name());
276                 }
277 }
278
279 void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
280 {
281         if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
282                 if (!delayed_view_updater_.isActive())
283                         delayed_view_updater_.start();
284 }
285
286 void View::on_decoder_stacked(void* decoder)
287 {
288         // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
289
290         Decoder* d = static_cast<Decoder*>(decoder);
291
292         // Only add the decoder if it has binary output
293         if (d->get_binary_class_count() == 0)
294                 return;
295
296         // Find the signal that contains the selected decoder
297         DecodeSignal* signal = nullptr;
298
299         for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
300                 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
301                         if (d == dec.get())
302                                 signal = ds.get();
303
304         assert(signal);
305
306         // Add the decoder to the list
307         QString title = QString("%1 (%2)").arg(signal->name(), d->name());
308         decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
309 }
310
311 void View::on_decoder_removed(void* decoder)
312 {
313         Decoder* d = static_cast<Decoder*>(decoder);
314
315         // Remove the decoder from the list
316         int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
317
318         if (index != -1)
319                 decoder_selector_->removeItem(index);
320 }
321
322 void View::perform_delayed_view_update()
323 {
324         update_data();
325 }
326
327
328 } // namespace decoder_output
329 } // namespace views
330 } // namespace pv