]> sigrok.org Git - pulseview.git/blob - pv/views/decoder_output/view.cpp
Add DecoderOutputView save button and action
[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 const char* SaveTypeNames[SaveTypeCount] = {
53         "Binary",
54         "Hex Dump"
55 };
56
57
58 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
59         ViewBase(session, is_main_view, parent),
60
61         // Note: Place defaults in View::reset_view_state(), not here
62         parent_(parent),
63         decoder_selector_(new QComboBox()),
64         format_selector_(new QComboBox()),
65         class_selector_(new QComboBox()),
66         stacked_widget_(new QStackedWidget()),
67         hex_view_(new QHexView()),
68         save_button_(new QToolButton()),
69         save_action_(new QAction(this)),
70         signal_(nullptr)
71 {
72         QVBoxLayout *root_layout = new QVBoxLayout(this);
73         root_layout->setContentsMargins(0, 0, 0, 0);
74
75         // Create toolbar
76         QToolBar* toolbar = new QToolBar();
77         toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
78         parent->addToolBar(toolbar);
79
80         // Populate toolbar
81         toolbar->addWidget(new QLabel(tr("Decoder:")));
82         toolbar->addWidget(decoder_selector_);
83         toolbar->addWidget(class_selector_);
84         toolbar->addSeparator();
85         toolbar->addWidget(new QLabel(tr("Show data as")));
86         toolbar->addWidget(format_selector_);
87         toolbar->addSeparator();
88         toolbar->addWidget(save_button_);
89
90         // Add format types
91         format_selector_->addItem(tr("Hexdump"), qVariantFromValue(QString("text/hexdump")));
92
93         // Add widget stack
94         root_layout->addWidget(stacked_widget_);
95         stacked_widget_->addWidget(hex_view_);
96         stacked_widget_->setCurrentIndex(0);
97
98         connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
99                 this, SLOT(on_selected_decoder_changed(int)));
100         connect(class_selector_, SIGNAL(currentIndexChanged(int)),
101                 this, SLOT(on_selected_class_changed(int)));
102
103         // Configure widgets
104         decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
105         class_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
106
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
128         parent->setSizePolicy(hex_view_->sizePolicy()); // TODO Must be updated when selected widget changes
129
130         reset_view_state();
131 }
132
133 View::~View()
134 {
135 }
136
137 ViewType View::get_type() const
138 {
139         return ViewTypeDecoderOutput;
140 }
141
142 void View::reset_view_state()
143 {
144         ViewBase::reset_view_state();
145
146         decoder_selector_->clear();
147         class_selector_->clear();
148         format_selector_->setCurrentIndex(0);
149         save_button_->setEnabled(false);
150
151         hex_view_->clear();
152 }
153
154 void View::clear_decode_signals()
155 {
156         ViewBase::clear_decode_signals();
157
158         reset_data();
159         reset_view_state();
160 }
161
162 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
163 {
164         ViewBase::add_decode_signal(signal);
165
166         connect(signal.get(), SIGNAL(name_changed(const QString&)),
167                 this, SLOT(on_signal_name_changed(const QString&)));
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) {
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                         }
182         } else
183                 if (!stack.empty()) {
184                         shared_ptr<Decoder>& dec = stack.at(0);
185                         if (dec->get_binary_class_count() > 0)
186                                 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
187                 }
188 }
189
190 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
191 {
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()));
195
196                 if (index != -1)
197                         decoder_selector_->removeItem(index);
198         }
199
200         ViewBase::remove_decode_signal(signal);
201
202         if (signal.get() == signal_) {
203                 reset_data();
204                 update_data();
205                 reset_view_state();
206         }
207 }
208
209 void View::save_settings(QSettings &settings) const
210 {
211         (void)settings;
212 }
213
214 void 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
221 void 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
231 void View::update_data()
232 {
233         if (!signal_)
234                 return;
235
236         if (!binary_data_exists_)
237                 return;
238
239         const DecodeBinaryClass* bin_class =
240                 signal_->get_binary_data_class(current_segment_, decoder_, bin_class_id_);
241
242         hex_view_->setData(bin_class);
243 }
244
245 void View::on_selected_decoder_changed(int index)
246 {
247         if (signal_)
248                 disconnect(signal_, SIGNAL(new_binary_data(unsigned int, void*, unsigned int)));
249
250         reset_data();
251
252         decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
253
254         // Find the signal that contains the selected decoder
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();
259
260         class_selector_->clear();
261
262         if (signal_) {
263                 // Populate binary class selector
264                 uint32_t bin_classes = decoder_->get_binary_class_count();
265                 for (uint32_t i = 0; i < bin_classes; i++) {
266                         const data::decode::DecodeBinaryClassInfo* class_info = decoder_->get_binary_class(i);
267                         class_selector_->addItem(class_info->description, QVariant::fromValue(i));
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)));
272         }
273
274         update_data();
275 }
276
277 void View::on_selected_class_changed(int index)
278 {
279         bin_class_id_ = class_selector_->itemData(index).value<uint32_t>();
280
281         binary_data_exists_ =
282                 signal_->get_binary_data_chunk_count(current_segment_, decoder_, bin_class_id_);
283
284         update_data();
285 }
286
287 void View::on_signal_name_changed(const QString &name)
288 {
289         (void)name;
290
291         SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
292         assert(sb);
293
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                 }
315 }
316
317 void View::on_new_binary_data(unsigned int segment_id, void* decoder, unsigned int bin_class_id)
318 {
319         if ((segment_id == current_segment_) && (decoder == decoder_) && (bin_class_id == bin_class_id_))
320                 if (!delayed_view_updater_.isActive())
321                         delayed_view_updater_.start();
322 }
323
324 void 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
330         // Only add the decoder if it has binary output
331         if (d->get_binary_class_count() == 0)
332                 return;
333
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
349 void 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
360 void View::on_actionSave_triggered(QAction* action)
361 {
362         (void)action;
363 }
364
365 void View::perform_delayed_view_update()
366 {
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
374         update_data();
375 }
376
377
378 } // namespace decoder_output
379 } // namespace views
380 } // namespace pv