2 * This file is part of the PulseView project.
4 * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net>
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.
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.
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/>.
23 #include <QFileDialog>
26 #include <QMessageBox>
28 #include <QVBoxLayout>
30 #include <libsigrokdecode/libsigrokdecode.h>
34 #include "pv/globalsettings.hpp"
35 #include "pv/util.hpp"
36 #include "pv/data/decode/decoder.hpp"
38 using pv::data::DecodeSignal;
39 using pv::data::SignalBase;
40 using pv::data::decode::Decoder;
41 using pv::util::Timestamp;
43 using std::shared_ptr;
47 namespace tabular_decoder {
50 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
51 ViewBase(session, is_main_view, parent),
53 // Note: Place defaults in View::reset_view_state(), not here
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()),
62 QVBoxLayout *root_layout = new QVBoxLayout(this);
63 root_layout->setContentsMargins(0, 0, 0, 0);
64 root_layout->addWidget(table_view_);
67 QToolBar* toolbar = new QToolBar();
68 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
69 parent->addToolBar(toolbar);
72 toolbar->addWidget(new QLabel(tr("Decoder:")));
73 toolbar->addWidget(decoder_selector_);
74 toolbar->addSeparator();
75 toolbar->addWidget(save_button_);
77 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
78 this, SLOT(on_selected_decoder_changed(int)));
81 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
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()));
91 QMenu *save_menu = new QMenu();
92 connect(save_menu, SIGNAL(triggered(QAction*)),
93 this, SLOT(on_actionSave_triggered(QAction*)));
95 save_button_->setMenu(save_menu);
96 save_button_->setDefaultAction(save_action_);
97 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
99 // Set up the table view
100 table_view_->setModel(model_);
101 table_view_->setSortingEnabled(true);
102 table_view_->sortByColumn(0, Qt::AscendingOrder);
107 ViewType View::get_type() const
109 return ViewTypeTabularDecoder;
112 void View::reset_view_state()
114 ViewBase::reset_view_state();
116 decoder_selector_->clear();
119 void View::clear_decode_signals()
121 ViewBase::clear_decode_signals();
127 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
129 ViewBase::add_decode_signal(signal);
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*)));
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()));
146 if (!stack.empty()) {
147 shared_ptr<Decoder>& dec = stack.at(0);
148 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
152 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
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()));
159 decoder_selector_->removeItem(index);
162 ViewBase::remove_decode_signal(signal);
164 if (signal.get() == signal_) {
171 void View::save_settings(QSettings &settings) const
173 ViewBase::save_settings(settings);
176 void View::restore_settings(QSettings &settings)
178 // Note: It is assumed that this function is only called once,
179 // immediately after restoring a previous session.
180 ViewBase::restore_settings(settings);
183 void View::reset_data()
189 void View::update_data()
197 void View::save_data() const
205 /* GlobalSettings settings;
206 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
208 const QString file_name = QFileDialog::getSaveFileName(
209 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
211 if (file_name.isEmpty())
214 QFile file(file_name);
215 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
216 pair<size_t, size_t> selection = hex_view_->get_selection();
218 vector<uint8_t> data;
219 data.resize(selection.second - selection.first + 1);
221 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
222 bin_class_id_, selection.first, selection.second, &data);
224 int64_t bytes_written = file.write((const char*)data.data(), data.size());
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);
237 void View::on_selected_decoder_changed(int index)
240 disconnect(signal_, SIGNAL(new_annotations()));
244 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
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())
253 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
259 void View::on_signal_name_changed(const QString &name)
263 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
266 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
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()));
277 decoder_selector_->setItemText(index, title);
280 if (!stack.empty()) {
281 shared_ptr<Decoder>& dec = stack.at(0);
282 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
285 decoder_selector_->setItemText(index, signal->name());
289 void View::on_new_annotations()
291 if (!delayed_view_updater_.isActive())
292 delayed_view_updater_.start();
295 void View::on_decoder_stacked(void* decoder)
297 // TODO This doesn't change existing entries for the same signal - but it should as the naming scheme may change
299 Decoder* d = static_cast<Decoder*>(decoder);
301 // Find the signal that contains the selected decoder
302 DecodeSignal* signal = nullptr;
304 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
305 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
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));
316 void View::on_decoder_removed(void* decoder)
318 Decoder* d = static_cast<Decoder*>(decoder);
320 // Remove the decoder from the list
321 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
324 decoder_selector_->removeItem(index);
327 void View::on_actionSave_triggered(QAction* action)
334 void View::perform_delayed_view_update()
340 } // namespace tabular_decoder