]> sigrok.org Git - pulseview.git/blob - pv/views/tabular_decoder/view.cpp
Add tabular decoder view
[pulseview.git] / pv / views / tabular_decoder / view.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2020 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 <QDebug>
23 #include <QFileDialog>
24 #include <QLabel>
25 #include <QMenu>
26 #include <QMessageBox>
27 #include <QToolBar>
28 #include <QVBoxLayout>
29
30 #include <libsigrokdecode/libsigrokdecode.h>
31
32 #include "view.hpp"
33
34 #include "pv/globalsettings.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::Timestamp;
42
43 using std::shared_ptr;
44
45 namespace pv {
46 namespace views {
47 namespace tabular_decoder {
48
49
50 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
51         ViewBase(session, is_main_view, parent),
52
53         // Note: Place defaults in View::reset_view_state(), not here
54         parent_(parent),
55         decoder_selector_(new QComboBox()),
56         save_button_(new QToolButton()),
57         save_action_(new QAction(this)),
58         table_view_(new QTableView()),
59         model_(new AnnotationCollectionModel()),
60         signal_(nullptr)
61 {
62         QVBoxLayout *root_layout = new QVBoxLayout(this);
63         root_layout->setContentsMargins(0, 0, 0, 0);
64         root_layout->addWidget(table_view_);
65
66         // Create toolbar
67         QToolBar* toolbar = new QToolBar();
68         toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
69         parent->addToolBar(toolbar);
70
71         // Populate toolbar
72         toolbar->addWidget(new QLabel(tr("Decoder:")));
73         toolbar->addWidget(decoder_selector_);
74         toolbar->addSeparator();
75         toolbar->addWidget(save_button_);
76
77         connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
78                 this, SLOT(on_selected_decoder_changed(int)));
79
80         // Configure widgets
81         decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
82
83         // Configure actions
84         save_action_->setText(tr("&Save..."));
85         save_action_->setIcon(QIcon::fromTheme("document-save-as",
86                 QIcon(":/icons/document-save-as.png")));
87         save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
88         connect(save_action_, SIGNAL(triggered(bool)),
89                 this, SLOT(on_actionSave_triggered()));
90
91         QMenu *save_menu = new QMenu();
92         connect(save_menu, SIGNAL(triggered(QAction*)),
93                 this, SLOT(on_actionSave_triggered(QAction*)));
94
95         save_button_->setMenu(save_menu);
96         save_button_->setDefaultAction(save_action_);
97         save_button_->setPopupMode(QToolButton::MenuButtonPopup);
98
99         // Set up the table view
100         table_view_->setModel(model_);
101         table_view_->setSortingEnabled(true);
102         table_view_->sortByColumn(0, Qt::AscendingOrder);
103
104         reset_view_state();
105 }
106
107 ViewType View::get_type() const
108 {
109         return ViewTypeTabularDecoder;
110 }
111
112 void View::reset_view_state()
113 {
114         ViewBase::reset_view_state();
115
116         decoder_selector_->clear();
117 }
118
119 void View::clear_decode_signals()
120 {
121         ViewBase::clear_decode_signals();
122
123         reset_data();
124         reset_view_state();
125 }
126
127 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
128 {
129         ViewBase::add_decode_signal(signal);
130
131         connect(signal.get(), SIGNAL(name_changed(const QString&)),
132                 this, SLOT(on_signal_name_changed(const QString&)));
133         connect(signal.get(), SIGNAL(decoder_stacked(void*)),
134                 this, SLOT(on_decoder_stacked(void*)));
135         connect(signal.get(), SIGNAL(decoder_removed(void*)),
136                 this, SLOT(on_decoder_removed(void*)));
137
138         // Add all decoders provided by this signal
139         auto stack = signal->decoder_stack();
140         if (stack.size() > 1) {
141                 for (const shared_ptr<Decoder>& dec : stack) {
142                         QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
143                         decoder_selector_->addItem(title, QVariant::fromValue((void*)dec.get()));
144                 }
145         } else
146                 if (!stack.empty()) {
147                         shared_ptr<Decoder>& dec = stack.at(0);
148                         decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
149                 }
150 }
151
152 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
153 {
154         // Remove all decoders provided by this signal
155         for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
156                 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
157
158                 if (index != -1)
159                         decoder_selector_->removeItem(index);
160         }
161
162         ViewBase::remove_decode_signal(signal);
163
164         if (signal.get() == signal_) {
165                 reset_data();
166                 update_data();
167                 reset_view_state();
168         }
169 }
170
171 void View::save_settings(QSettings &settings) const
172 {
173         ViewBase::save_settings(settings);
174 }
175
176 void View::restore_settings(QSettings &settings)
177 {
178         // Note: It is assumed that this function is only called once,
179         // immediately after restoring a previous session.
180         ViewBase::restore_settings(settings);
181 }
182
183 void View::reset_data()
184 {
185         signal_ = nullptr;
186         decoder_ = nullptr;
187 }
188
189 void View::update_data()
190 {
191         if (!signal_)
192                 return;
193
194         // TBD
195 }
196
197 void View::save_data() const
198 {
199         assert(decoder_);
200         assert(signal_);
201
202         if (!signal_)
203                 return;
204
205 /*      GlobalSettings settings;
206         const QString dir = settings.value("MainWindow/SaveDirectory").toString();
207
208         const QString file_name = QFileDialog::getSaveFileName(
209                 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
210
211         if (file_name.isEmpty())
212                 return;
213
214         QFile file(file_name);
215         if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
216                 pair<size_t, size_t> selection = hex_view_->get_selection();
217
218                 vector<uint8_t> data;
219                 data.resize(selection.second - selection.first + 1);
220
221                 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
222                         bin_class_id_, selection.first, selection.second, &data);
223
224                 int64_t bytes_written = file.write((const char*)data.data(), data.size());
225
226                 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
227                         QMessageBox msg(parent_);
228                         msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
229                         msg.setStandardButtons(QMessageBox::Ok);
230                         msg.setIcon(QMessageBox::Warning);
231                         msg.exec();
232                         return;
233                 }
234         } */
235 }
236
237 void View::on_selected_decoder_changed(int index)
238 {
239         if (signal_)
240                 disconnect(signal_, SIGNAL(new_annotations()));
241
242         reset_data();
243
244         decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
245
246         // Find the signal that contains the selected decoder
247         for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
248                 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
249                         if (decoder_ == dec.get())
250                                 signal_ = ds.get();
251
252         if (signal_) {
253                 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
254         }
255
256         update_data();
257 }
258
259 void View::on_signal_name_changed(const QString &name)
260 {
261         (void)name;
262
263         SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
264         assert(sb);
265
266         DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
267         assert(signal);
268
269         // Update all decoder entries provided by this signal
270         auto stack = signal->decoder_stack();
271         if (stack.size() > 1) {
272                 for (const shared_ptr<Decoder>& dec : stack) {
273                         QString title = QString("%1 (%2)").arg(signal->name(), dec->name());
274                         int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
275
276                         if (index != -1)
277                                 decoder_selector_->setItemText(index, title);
278                 }
279         } else
280                 if (!stack.empty()) {
281                         shared_ptr<Decoder>& dec = stack.at(0);
282                         int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
283
284                         if (index != -1)
285                                 decoder_selector_->setItemText(index, signal->name());
286                 }
287 }
288
289 void View::on_new_annotations()
290 {
291         if (!delayed_view_updater_.isActive())
292                 delayed_view_updater_.start();
293 }
294
295 void View::on_decoder_stacked(void* decoder)
296 {
297         // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
298
299         Decoder* d = static_cast<Decoder*>(decoder);
300
301         // Find the signal that contains the selected decoder
302         DecodeSignal* signal = nullptr;
303
304         for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
305                 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
306                         if (d == dec.get())
307                                 signal = ds.get();
308
309         assert(signal);
310
311         // Add the decoder to the list
312         QString title = QString("%1 (%2)").arg(signal->name(), d->name());
313         decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
314 }
315
316 void View::on_decoder_removed(void* decoder)
317 {
318         Decoder* d = static_cast<Decoder*>(decoder);
319
320         // Remove the decoder from the list
321         int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
322
323         if (index != -1)
324                 decoder_selector_->removeItem(index);
325 }
326
327 void View::on_actionSave_triggered(QAction* action)
328 {
329         (void)action;
330
331         save_data();
332 }
333
334 void View::perform_delayed_view_update()
335 {
336         update_data();
337 }
338
339
340 } // namespace tabular_decoder
341 } // namespace views
342 } // namespace pv