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