]> sigrok.org Git - pulseview.git/blob - pv/views/tabular_decoder/view.cpp
Add row colors and some fixes
[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 <QApplication>
23 #include <QDebug>
24 #include <QFileDialog>
25 #include <QFontMetrics>
26 #include <QHeaderView>
27 #include <QLabel>
28 #include <QMenu>
29 #include <QMessageBox>
30 #include <QToolBar>
31 #include <QVBoxLayout>
32
33 #include <libsigrokdecode/libsigrokdecode.h>
34
35 #include "view.hpp"
36
37 #include "pv/globalsettings.hpp"
38 #include "pv/util.hpp"
39 #include "pv/data/decode/decoder.hpp"
40
41 using pv::data::DecodeSignal;
42 using pv::data::SignalBase;
43 using pv::data::decode::Decoder;
44 using pv::util::Timestamp;
45
46 using std::make_shared;
47 using std::shared_ptr;
48
49 namespace pv {
50 namespace views {
51 namespace tabular_decoder {
52
53 QSize QCustomTableView::minimumSizeHint() const
54 {
55         QSize size(QTableView::sizeHint());
56
57         int width = 0;
58         for (int i = 0; i < horizontalHeader()->count(); i++)
59                 if (!horizontalHeader()->isSectionHidden(i))
60                         width += horizontalHeader()->sectionSize(i);
61
62         size.setWidth(width + (horizontalHeader()->count() * 1));
63
64         return size;
65 }
66
67 QSize QCustomTableView::sizeHint() const
68 {
69         return minimumSizeHint();
70 }
71
72
73 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
74         ViewBase(session, is_main_view, parent),
75
76         // Note: Place defaults in View::reset_view_state(), not here
77         parent_(parent),
78         decoder_selector_(new QComboBox()),
79         save_button_(new QToolButton()),
80         save_action_(new QAction(this)),
81         table_view_(new QCustomTableView()),
82         model_(new AnnotationCollectionModel()),
83         signal_(nullptr),
84         updating_data_(false)
85 {
86         QVBoxLayout *root_layout = new QVBoxLayout(this);
87         root_layout->setContentsMargins(0, 0, 0, 0);
88         root_layout->addWidget(table_view_);
89
90         // Create toolbar
91         QToolBar* toolbar = new QToolBar();
92         toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
93         parent->addToolBar(toolbar);
94
95         // Populate toolbar
96         toolbar->addWidget(new QLabel(tr("Decoder:")));
97         toolbar->addWidget(decoder_selector_);
98         toolbar->addSeparator();
99         toolbar->addWidget(save_button_);
100
101         connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
102                 this, SLOT(on_selected_decoder_changed(int)));
103
104         // Configure widgets
105         decoder_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         save_button_->setMenu(save_menu);
120         save_button_->setDefaultAction(save_action_);
121         save_button_->setPopupMode(QToolButton::MenuButtonPopup);
122
123         // Set up the table view
124         table_view_->setModel(model_);
125         table_view_->setSortingEnabled(true);
126         table_view_->sortByColumn(0, Qt::AscendingOrder);
127
128         const int font_height = QFontMetrics(QApplication::font()).height();
129         table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4);
130
131         table_view_->horizontalHeader()->setStretchLastSection(true);
132         table_view_->horizontalHeader()->setCascadingSectionResizes(true);
133         table_view_->horizontalHeader()->setSectionsMovable(true);
134
135         table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
136         parent->setSizePolicy(table_view_->sizePolicy());
137
138         reset_view_state();
139 }
140
141 ViewType View::get_type() const
142 {
143         return ViewTypeTabularDecoder;
144 }
145
146 void View::reset_view_state()
147 {
148         ViewBase::reset_view_state();
149
150         decoder_selector_->clear();
151 }
152
153 void View::clear_decode_signals()
154 {
155         ViewBase::clear_decode_signals();
156
157         reset_data();
158         reset_view_state();
159 }
160
161 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
162 {
163         ViewBase::add_decode_signal(signal);
164
165         connect(signal.get(), SIGNAL(name_changed(const QString&)),
166                 this, SLOT(on_signal_name_changed(const QString&)));
167
168         // Note: At time of initial creation, decode signals have no decoders so we
169         // need to watch for decoder stacking events
170
171         connect(signal.get(), SIGNAL(decoder_stacked(void*)),
172                 this, SLOT(on_decoder_stacked(void*)));
173         connect(signal.get(), SIGNAL(decoder_removed(void*)),
174                 this, SLOT(on_decoder_removed(void*)));
175
176         // Add the top-level decoder provided by an already-existing signal
177         auto stack = signal->decoder_stack();
178         if (!stack.empty()) {
179                 shared_ptr<Decoder>& dec = stack.at(0);
180                 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
181         }
182 }
183
184 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
185 {
186         // Remove all decoders provided by this signal
187         for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
188                 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
189
190                 if (index != -1)
191                         decoder_selector_->removeItem(index);
192         }
193
194         ViewBase::remove_decode_signal(signal);
195
196         if (signal.get() == signal_) {
197                 reset_data();
198                 update_data();
199                 reset_view_state();
200         }
201 }
202
203 void View::save_settings(QSettings &settings) const
204 {
205         ViewBase::save_settings(settings);
206 }
207
208 void View::restore_settings(QSettings &settings)
209 {
210         // Note: It is assumed that this function is only called once,
211         // immediately after restoring a previous session.
212         ViewBase::restore_settings(settings);
213 }
214
215 void View::reset_data()
216 {
217         signal_ = nullptr;
218         decoder_ = nullptr;
219 }
220
221 void View::update_data()
222 {
223         if (!signal_)
224                 return;
225
226         if (updating_data_) {
227                 if (!delayed_view_updater_.isActive())
228                         delayed_view_updater_.start();
229                 return;
230         }
231
232         updating_data_ = true;
233
234         table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
235         model_->set_signal_and_segment(signal_, current_segment_);
236
237         updating_data_ = false;
238 }
239
240 void View::save_data() const
241 {
242         assert(decoder_);
243         assert(signal_);
244
245         if (!signal_)
246                 return;
247
248 /*      GlobalSettings settings;
249         const QString dir = settings.value("MainWindow/SaveDirectory").toString();
250
251         const QString file_name = QFileDialog::getSaveFileName(
252                 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
253
254         if (file_name.isEmpty())
255                 return;
256
257         QFile file(file_name);
258         if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
259                 pair<size_t, size_t> selection = hex_view_->get_selection();
260
261                 vector<uint8_t> data;
262                 data.resize(selection.second - selection.first + 1);
263
264                 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
265                         bin_class_id_, selection.first, selection.second, &data);
266
267                 int64_t bytes_written = file.write((const char*)data.data(), data.size());
268
269                 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
270                         QMessageBox msg(parent_);
271                         msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
272                         msg.setStandardButtons(QMessageBox::Ok);
273                         msg.setIcon(QMessageBox::Warning);
274                         msg.exec();
275                         return;
276                 }
277         } */
278 }
279
280 void View::on_selected_decoder_changed(int index)
281 {
282         if (signal_) {
283                 disconnect(signal_, SIGNAL(signal_color_changed()));
284                 disconnect(signal_, SIGNAL(new_annotations()));
285                 disconnect(signal_, SIGNAL(decode_reset()));
286         }
287
288         reset_data();
289
290         decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
291
292         // Find the signal that contains the selected decoder
293         for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
294                 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
295                         if (decoder_ == dec.get())
296                                 signal_ = ds.get();
297
298         if (signal_) {
299                 connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor)));
300                 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
301                 connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset()));
302         }
303
304         update_data();
305 }
306
307 void View::on_signal_name_changed(const QString &name)
308 {
309         (void)name;
310
311         SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
312         assert(sb);
313
314         DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
315         assert(signal);
316
317         // Update the top-level decoder provided by this signal
318         auto stack = signal->decoder_stack();
319         if (!stack.empty()) {
320                 shared_ptr<Decoder>& dec = stack.at(0);
321                 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
322
323                 if (index != -1)
324                         decoder_selector_->setItemText(index, signal->name());
325         }
326 }
327
328 void View::on_signal_color_changed(const QColor &color)
329 {
330         (void)color;
331
332         table_view_->update();
333 }
334
335 void View::on_new_annotations()
336 {
337         if (!delayed_view_updater_.isActive())
338                 delayed_view_updater_.start();
339 }
340
341 void View::on_decoder_reset()
342 {
343         // Invalidate the model's data connection immediately - otherwise we
344         // will use a stale pointer in model_->index() when called from the table view
345         model_->set_signal_and_segment(signal_, current_segment_);
346 }
347
348 void View::on_decoder_stacked(void* decoder)
349 {
350         Decoder* d = static_cast<Decoder*>(decoder);
351
352         // Find the signal that contains the selected decoder
353         DecodeSignal* signal = nullptr;
354
355         for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
356                 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
357                         if (d == dec.get())
358                                 signal = ds.get();
359
360         assert(signal);
361
362         const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0);
363         int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
364
365         if (index == -1) {
366                 // Add the decoder to the list
367                 QString title = QString("%1 (%2)").arg(signal->name(), d->name());
368                 decoder_selector_->addItem(title, QVariant::fromValue((void*)d));
369         }
370 }
371
372 void View::on_decoder_removed(void* decoder)
373 {
374         Decoder* d = static_cast<Decoder*>(decoder);
375
376         // Remove the decoder from the list
377         int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
378
379         if (index != -1)
380                 decoder_selector_->removeItem(index);
381 }
382
383 void View::on_actionSave_triggered(QAction* action)
384 {
385         (void)action;
386
387         save_data();
388 }
389
390 void View::perform_delayed_view_update()
391 {
392         update_data();
393 }
394
395
396 } // namespace tabular_decoder
397 } // namespace views
398 } // namespace pv