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/>.
22 #include <QApplication>
24 #include <QFileDialog>
25 #include <QFontMetrics>
26 #include <QHeaderView>
29 #include <QMessageBox>
31 #include <QVBoxLayout>
33 #include <libsigrokdecode/libsigrokdecode.h>
37 #include "pv/globalsettings.hpp"
38 #include "pv/util.hpp"
39 #include "pv/data/decode/decoder.hpp"
41 using pv::data::DecodeSignal;
42 using pv::data::SignalBase;
43 using pv::data::decode::Decoder;
44 using pv::util::Timestamp;
46 using std::make_shared;
47 using std::shared_ptr;
51 namespace tabular_decoder {
53 QSize QCustomTableView::minimumSizeHint() const
55 QSize size(QTableView::sizeHint());
58 for (int i = 0; i < horizontalHeader()->count(); i++)
59 if (!horizontalHeader()->isSectionHidden(i))
60 width += horizontalHeader()->sectionSizeHint(i);
62 size.setWidth(width + (horizontalHeader()->count() * 1));
67 QSize QCustomTableView::sizeHint() const
69 return minimumSizeHint();
73 View::View(Session &session, bool is_main_view, QMainWindow *parent) :
74 ViewBase(session, is_main_view, parent),
76 // Note: Place defaults in View::reset_view_state(), not here
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()),
86 QVBoxLayout *root_layout = new QVBoxLayout(this);
87 root_layout->setContentsMargins(0, 0, 0, 0);
88 root_layout->addWidget(table_view_);
91 QToolBar* toolbar = new QToolBar();
92 toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
93 parent->addToolBar(toolbar);
96 toolbar->addWidget(new QLabel(tr("Decoder:")));
97 toolbar->addWidget(decoder_selector_);
98 toolbar->addSeparator();
99 toolbar->addWidget(save_button_);
101 connect(decoder_selector_, SIGNAL(currentIndexChanged(int)),
102 this, SLOT(on_selected_decoder_changed(int)));
105 decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
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()));
115 QMenu *save_menu = new QMenu();
116 connect(save_menu, SIGNAL(triggered(QAction*)),
117 this, SLOT(on_actionSave_triggered(QAction*)));
119 save_button_->setMenu(save_menu);
120 save_button_->setDefaultAction(save_action_);
121 save_button_->setPopupMode(QToolButton::MenuButtonPopup);
123 // Set up the table view
124 table_view_->setModel(model_);
125 table_view_->setSortingEnabled(true);
126 table_view_->sortByColumn(0, Qt::AscendingOrder);
128 const int font_height = QFontMetrics(QApplication::font()).height();
129 table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4);
131 table_view_->horizontalHeader()->setSectionResizeMode(model_->columnCount() - 1, QHeaderView::Stretch);
133 table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
134 parent->setSizePolicy(table_view_->sizePolicy());
139 ViewType View::get_type() const
141 return ViewTypeTabularDecoder;
144 void View::reset_view_state()
146 ViewBase::reset_view_state();
148 decoder_selector_->clear();
151 void View::clear_decode_signals()
153 ViewBase::clear_decode_signals();
159 void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
161 ViewBase::add_decode_signal(signal);
163 connect(signal.get(), SIGNAL(name_changed(const QString&)),
164 this, SLOT(on_signal_name_changed(const QString&)));
165 connect(signal.get(), SIGNAL(decoder_stacked(void*)),
166 this, SLOT(on_decoder_stacked(void*)));
167 connect(signal.get(), SIGNAL(decoder_removed(void*)),
168 this, SLOT(on_decoder_removed(void*)));
170 // Add the top-level decoder provided by this signal
171 auto stack = signal->decoder_stack();
172 if (!stack.empty()) {
173 shared_ptr<Decoder>& dec = stack.at(0);
174 decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get()));
178 void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
180 // Remove all decoders provided by this signal
181 for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) {
182 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
185 decoder_selector_->removeItem(index);
188 ViewBase::remove_decode_signal(signal);
190 if (signal.get() == signal_) {
197 void View::save_settings(QSettings &settings) const
199 ViewBase::save_settings(settings);
202 void View::restore_settings(QSettings &settings)
204 // Note: It is assumed that this function is only called once,
205 // immediately after restoring a previous session.
206 ViewBase::restore_settings(settings);
209 void View::reset_data()
215 void View::update_data()
220 if (updating_data_) {
221 if (!delayed_view_updater_.isActive())
222 delayed_view_updater_.start();
226 updating_data_ = true;
228 table_view_->setRootIndex(model_->index(1, 0, QModelIndex()));
229 model_->set_signal_and_segment(signal_, current_segment_);
231 updating_data_ = false;
234 void View::save_data() const
242 /* GlobalSettings settings;
243 const QString dir = settings.value("MainWindow/SaveDirectory").toString();
245 const QString file_name = QFileDialog::getSaveFileName(
246 parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)"));
248 if (file_name.isEmpty())
251 QFile file(file_name);
252 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
253 pair<size_t, size_t> selection = hex_view_->get_selection();
255 vector<uint8_t> data;
256 data.resize(selection.second - selection.first + 1);
258 signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_,
259 bin_class_id_, selection.first, selection.second, &data);
261 int64_t bytes_written = file.write((const char*)data.data(), data.size());
263 if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) {
264 QMessageBox msg(parent_);
265 msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name));
266 msg.setStandardButtons(QMessageBox::Ok);
267 msg.setIcon(QMessageBox::Warning);
274 void View::on_selected_decoder_changed(int index)
277 disconnect(signal_, SIGNAL(new_annotations()));
281 decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>();
283 // Find the signal that contains the selected decoder
284 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
285 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
286 if (decoder_ == dec.get())
290 connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations()));
295 void View::on_signal_name_changed(const QString &name)
299 SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender());
302 DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb);
305 // Update the top-level decoder provided by this signal
306 auto stack = signal->decoder_stack();
307 if (!stack.empty()) {
308 shared_ptr<Decoder>& dec = stack.at(0);
309 int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get()));
312 decoder_selector_->setItemText(index, signal->name());
316 void View::on_new_annotations()
318 if (!delayed_view_updater_.isActive())
319 delayed_view_updater_.start();
322 void View::on_decoder_stacked(void* decoder)
324 Decoder* d = static_cast<Decoder*>(decoder);
326 // Find the signal that contains the selected decoder
327 DecodeSignal* signal = nullptr;
329 for (const shared_ptr<DecodeSignal>& ds : decode_signals_)
330 for (const shared_ptr<Decoder>& dec : ds->decoder_stack())
336 if (signal == signal_)
340 void View::on_decoder_removed(void* decoder)
342 Decoder* d = static_cast<Decoder*>(decoder);
344 // Remove the decoder from the list
345 int index = decoder_selector_->findData(QVariant::fromValue((void*)d));
348 decoder_selector_->removeItem(index);
351 void View::on_actionSave_triggered(QAction* action)
358 void View::perform_delayed_view_update()
364 } // namespace tabular_decoder