]>
Commit | Line | Data |
---|---|---|
24d69d27 SA |
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 | ||
f54e68b0 | 22 | #include <QApplication> |
24d69d27 SA |
23 | #include <QDebug> |
24 | #include <QFileDialog> | |
f54e68b0 SA |
25 | #include <QFontMetrics> |
26 | #include <QHeaderView> | |
24d69d27 SA |
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" | |
9a35b05d | 38 | #include "pv/session.hpp" |
24d69d27 SA |
39 | #include "pv/util.hpp" |
40 | #include "pv/data/decode/decoder.hpp" | |
41 | ||
42 | using pv::data::DecodeSignal; | |
43 | using pv::data::SignalBase; | |
44 | using pv::data::decode::Decoder; | |
45 | using pv::util::Timestamp; | |
46 | ||
f54e68b0 | 47 | using std::make_shared; |
24d69d27 SA |
48 | using std::shared_ptr; |
49 | ||
50 | namespace pv { | |
51 | namespace views { | |
52 | namespace tabular_decoder { | |
53 | ||
f54e68b0 SA |
54 | QSize QCustomTableView::minimumSizeHint() const |
55 | { | |
56 | QSize size(QTableView::sizeHint()); | |
57 | ||
58 | int width = 0; | |
59 | for (int i = 0; i < horizontalHeader()->count(); i++) | |
60 | if (!horizontalHeader()->isSectionHidden(i)) | |
88a25978 | 61 | width += horizontalHeader()->sectionSize(i); |
f54e68b0 SA |
62 | |
63 | size.setWidth(width + (horizontalHeader()->count() * 1)); | |
64 | ||
65 | return size; | |
66 | } | |
67 | ||
68 | QSize QCustomTableView::sizeHint() const | |
69 | { | |
70 | return minimumSizeHint(); | |
71 | } | |
72 | ||
24d69d27 SA |
73 | |
74 | View::View(Session &session, bool is_main_view, QMainWindow *parent) : | |
75 | ViewBase(session, is_main_view, parent), | |
76 | ||
77 | // Note: Place defaults in View::reset_view_state(), not here | |
78 | parent_(parent), | |
79 | decoder_selector_(new QComboBox()), | |
80 | save_button_(new QToolButton()), | |
81 | save_action_(new QAction(this)), | |
f54e68b0 | 82 | table_view_(new QCustomTableView()), |
24d69d27 | 83 | model_(new AnnotationCollectionModel()), |
f54e68b0 SA |
84 | signal_(nullptr), |
85 | updating_data_(false) | |
24d69d27 SA |
86 | { |
87 | QVBoxLayout *root_layout = new QVBoxLayout(this); | |
88 | root_layout->setContentsMargins(0, 0, 0, 0); | |
89 | root_layout->addWidget(table_view_); | |
90 | ||
91 | // Create toolbar | |
92 | QToolBar* toolbar = new QToolBar(); | |
93 | toolbar->setContextMenuPolicy(Qt::PreventContextMenu); | |
94 | parent->addToolBar(toolbar); | |
95 | ||
96 | // Populate toolbar | |
97 | toolbar->addWidget(new QLabel(tr("Decoder:"))); | |
98 | toolbar->addWidget(decoder_selector_); | |
99 | toolbar->addSeparator(); | |
100 | toolbar->addWidget(save_button_); | |
101 | ||
102 | connect(decoder_selector_, SIGNAL(currentIndexChanged(int)), | |
103 | this, SLOT(on_selected_decoder_changed(int))); | |
104 | ||
105 | // Configure widgets | |
106 | decoder_selector_->setSizeAdjustPolicy(QComboBox::AdjustToContents); | |
107 | ||
108 | // Configure actions | |
109 | save_action_->setText(tr("&Save...")); | |
110 | save_action_->setIcon(QIcon::fromTheme("document-save-as", | |
111 | QIcon(":/icons/document-save-as.png"))); | |
112 | save_action_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S)); | |
113 | connect(save_action_, SIGNAL(triggered(bool)), | |
114 | this, SLOT(on_actionSave_triggered())); | |
115 | ||
116 | QMenu *save_menu = new QMenu(); | |
117 | connect(save_menu, SIGNAL(triggered(QAction*)), | |
118 | this, SLOT(on_actionSave_triggered(QAction*))); | |
119 | ||
120 | save_button_->setMenu(save_menu); | |
121 | save_button_->setDefaultAction(save_action_); | |
122 | save_button_->setPopupMode(QToolButton::MenuButtonPopup); | |
123 | ||
124 | // Set up the table view | |
125 | table_view_->setModel(model_); | |
9a35b05d SA |
126 | table_view_->setSelectionBehavior(QAbstractItemView::SelectRows); |
127 | table_view_->setSelectionMode(QAbstractItemView::SingleSelection); | |
24d69d27 SA |
128 | table_view_->setSortingEnabled(true); |
129 | table_view_->sortByColumn(0, Qt::AscendingOrder); | |
130 | ||
f54e68b0 SA |
131 | const int font_height = QFontMetrics(QApplication::font()).height(); |
132 | table_view_->verticalHeader()->setDefaultSectionSize((font_height * 5) / 4); | |
133 | ||
88a25978 SA |
134 | table_view_->horizontalHeader()->setStretchLastSection(true); |
135 | table_view_->horizontalHeader()->setCascadingSectionResizes(true); | |
136 | table_view_->horizontalHeader()->setSectionsMovable(true); | |
9a35b05d | 137 | table_view_->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu); |
f54e68b0 SA |
138 | |
139 | table_view_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
140 | parent->setSizePolicy(table_view_->sizePolicy()); | |
141 | ||
9a35b05d SA |
142 | connect(table_view_, SIGNAL(clicked(const QModelIndex&)), |
143 | this, SLOT(on_table_item_clicked(const QModelIndex&))); | |
144 | connect(table_view_, SIGNAL(doubleClicked(const QModelIndex&)), | |
145 | this, SLOT(on_table_item_double_clicked(const QModelIndex&))); | |
146 | connect(table_view_->horizontalHeader(), SIGNAL(customContextMenuRequested(const QPoint&)), | |
147 | this, SLOT(on_table_header_requested(const QPoint&))); | |
148 | ||
24d69d27 SA |
149 | reset_view_state(); |
150 | } | |
151 | ||
152 | ViewType View::get_type() const | |
153 | { | |
154 | return ViewTypeTabularDecoder; | |
155 | } | |
156 | ||
157 | void View::reset_view_state() | |
158 | { | |
159 | ViewBase::reset_view_state(); | |
160 | ||
161 | decoder_selector_->clear(); | |
162 | } | |
163 | ||
164 | void View::clear_decode_signals() | |
165 | { | |
166 | ViewBase::clear_decode_signals(); | |
167 | ||
168 | reset_data(); | |
169 | reset_view_state(); | |
170 | } | |
171 | ||
172 | void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
173 | { | |
174 | ViewBase::add_decode_signal(signal); | |
175 | ||
176 | connect(signal.get(), SIGNAL(name_changed(const QString&)), | |
177 | this, SLOT(on_signal_name_changed(const QString&))); | |
88a25978 SA |
178 | |
179 | // Note: At time of initial creation, decode signals have no decoders so we | |
180 | // need to watch for decoder stacking events | |
181 | ||
24d69d27 SA |
182 | connect(signal.get(), SIGNAL(decoder_stacked(void*)), |
183 | this, SLOT(on_decoder_stacked(void*))); | |
184 | connect(signal.get(), SIGNAL(decoder_removed(void*)), | |
185 | this, SLOT(on_decoder_removed(void*))); | |
186 | ||
88a25978 | 187 | // Add the top-level decoder provided by an already-existing signal |
24d69d27 | 188 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
189 | if (!stack.empty()) { |
190 | shared_ptr<Decoder>& dec = stack.at(0); | |
191 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)dec.get())); | |
192 | } | |
24d69d27 SA |
193 | } |
194 | ||
195 | void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal) | |
196 | { | |
197 | // Remove all decoders provided by this signal | |
198 | for (const shared_ptr<Decoder>& dec : signal->decoder_stack()) { | |
199 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
200 | ||
201 | if (index != -1) | |
202 | decoder_selector_->removeItem(index); | |
203 | } | |
204 | ||
205 | ViewBase::remove_decode_signal(signal); | |
206 | ||
207 | if (signal.get() == signal_) { | |
208 | reset_data(); | |
209 | update_data(); | |
210 | reset_view_state(); | |
211 | } | |
212 | } | |
213 | ||
214 | void View::save_settings(QSettings &settings) const | |
215 | { | |
216 | ViewBase::save_settings(settings); | |
217 | } | |
218 | ||
219 | void View::restore_settings(QSettings &settings) | |
220 | { | |
221 | // Note: It is assumed that this function is only called once, | |
222 | // immediately after restoring a previous session. | |
223 | ViewBase::restore_settings(settings); | |
224 | } | |
225 | ||
226 | void View::reset_data() | |
227 | { | |
228 | signal_ = nullptr; | |
229 | decoder_ = nullptr; | |
230 | } | |
231 | ||
232 | void View::update_data() | |
233 | { | |
f54e68b0 SA |
234 | if (updating_data_) { |
235 | if (!delayed_view_updater_.isActive()) | |
236 | delayed_view_updater_.start(); | |
237 | return; | |
238 | } | |
239 | ||
240 | updating_data_ = true; | |
241 | ||
242 | table_view_->setRootIndex(model_->index(1, 0, QModelIndex())); | |
243 | model_->set_signal_and_segment(signal_, current_segment_); | |
244 | ||
245 | updating_data_ = false; | |
24d69d27 SA |
246 | } |
247 | ||
248 | void View::save_data() const | |
249 | { | |
250 | assert(decoder_); | |
251 | assert(signal_); | |
252 | ||
253 | if (!signal_) | |
254 | return; | |
255 | ||
256 | /* GlobalSettings settings; | |
257 | const QString dir = settings.value("MainWindow/SaveDirectory").toString(); | |
258 | ||
259 | const QString file_name = QFileDialog::getSaveFileName( | |
260 | parent_, tr("Save Binary Data"), dir, tr("Binary Data Files (*.bin);;All Files (*)")); | |
261 | ||
262 | if (file_name.isEmpty()) | |
263 | return; | |
264 | ||
265 | QFile file(file_name); | |
266 | if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) { | |
267 | pair<size_t, size_t> selection = hex_view_->get_selection(); | |
268 | ||
269 | vector<uint8_t> data; | |
270 | data.resize(selection.second - selection.first + 1); | |
271 | ||
272 | signal_->get_merged_binary_data_chunks_by_offset(current_segment_, decoder_, | |
273 | bin_class_id_, selection.first, selection.second, &data); | |
274 | ||
275 | int64_t bytes_written = file.write((const char*)data.data(), data.size()); | |
276 | ||
277 | if ((bytes_written == -1) || ((uint64_t)bytes_written != data.size())) { | |
278 | QMessageBox msg(parent_); | |
279 | msg.setText(tr("Error") + "\n\n" + tr("File %1 could not be written to.").arg(file_name)); | |
280 | msg.setStandardButtons(QMessageBox::Ok); | |
281 | msg.setIcon(QMessageBox::Warning); | |
282 | msg.exec(); | |
283 | return; | |
284 | } | |
285 | } */ | |
286 | } | |
287 | ||
288 | void View::on_selected_decoder_changed(int index) | |
289 | { | |
02c87df7 | 290 | if (signal_) { |
85125b0f | 291 | disconnect(signal_, SIGNAL(color_changed(QColor))); |
24d69d27 | 292 | disconnect(signal_, SIGNAL(new_annotations())); |
02c87df7 SA |
293 | disconnect(signal_, SIGNAL(decode_reset())); |
294 | } | |
24d69d27 SA |
295 | |
296 | reset_data(); | |
297 | ||
298 | decoder_ = (Decoder*)decoder_selector_->itemData(index).value<void*>(); | |
299 | ||
300 | // Find the signal that contains the selected decoder | |
301 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
302 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
303 | if (decoder_ == dec.get()) | |
304 | signal_ = ds.get(); | |
305 | ||
02c87df7 | 306 | if (signal_) { |
88a25978 | 307 | connect(signal_, SIGNAL(color_changed(QColor)), this, SLOT(on_signal_color_changed(QColor))); |
24d69d27 | 308 | connect(signal_, SIGNAL(new_annotations()), this, SLOT(on_new_annotations())); |
02c87df7 SA |
309 | connect(signal_, SIGNAL(decode_reset()), this, SLOT(on_decoder_reset())); |
310 | } | |
24d69d27 SA |
311 | |
312 | update_data(); | |
313 | } | |
314 | ||
315 | void View::on_signal_name_changed(const QString &name) | |
316 | { | |
317 | (void)name; | |
318 | ||
319 | SignalBase* sb = qobject_cast<SignalBase*>(QObject::sender()); | |
320 | assert(sb); | |
321 | ||
322 | DecodeSignal* signal = dynamic_cast<DecodeSignal*>(sb); | |
323 | assert(signal); | |
324 | ||
f54e68b0 | 325 | // Update the top-level decoder provided by this signal |
24d69d27 | 326 | auto stack = signal->decoder_stack(); |
f54e68b0 SA |
327 | if (!stack.empty()) { |
328 | shared_ptr<Decoder>& dec = stack.at(0); | |
329 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
24d69d27 | 330 | |
f54e68b0 SA |
331 | if (index != -1) |
332 | decoder_selector_->setItemText(index, signal->name()); | |
333 | } | |
24d69d27 SA |
334 | } |
335 | ||
88a25978 SA |
336 | void View::on_signal_color_changed(const QColor &color) |
337 | { | |
338 | (void)color; | |
339 | ||
340 | table_view_->update(); | |
341 | } | |
342 | ||
24d69d27 SA |
343 | void View::on_new_annotations() |
344 | { | |
345 | if (!delayed_view_updater_.isActive()) | |
346 | delayed_view_updater_.start(); | |
347 | } | |
348 | ||
02c87df7 SA |
349 | void View::on_decoder_reset() |
350 | { | |
351 | // Invalidate the model's data connection immediately - otherwise we | |
352 | // will use a stale pointer in model_->index() when called from the table view | |
353 | model_->set_signal_and_segment(signal_, current_segment_); | |
354 | } | |
355 | ||
24d69d27 SA |
356 | void View::on_decoder_stacked(void* decoder) |
357 | { | |
24d69d27 SA |
358 | Decoder* d = static_cast<Decoder*>(decoder); |
359 | ||
360 | // Find the signal that contains the selected decoder | |
361 | DecodeSignal* signal = nullptr; | |
362 | ||
363 | for (const shared_ptr<DecodeSignal>& ds : decode_signals_) | |
364 | for (const shared_ptr<Decoder>& dec : ds->decoder_stack()) | |
365 | if (d == dec.get()) | |
366 | signal = ds.get(); | |
367 | ||
368 | assert(signal); | |
369 | ||
88a25978 SA |
370 | const shared_ptr<Decoder>& dec = signal->decoder_stack().at(0); |
371 | int index = decoder_selector_->findData(QVariant::fromValue((void*)dec.get())); | |
372 | ||
373 | if (index == -1) { | |
374 | // Add the decoder to the list | |
85125b0f | 375 | decoder_selector_->addItem(signal->name(), QVariant::fromValue((void*)d)); |
88a25978 | 376 | } |
24d69d27 SA |
377 | } |
378 | ||
379 | void View::on_decoder_removed(void* decoder) | |
380 | { | |
381 | Decoder* d = static_cast<Decoder*>(decoder); | |
382 | ||
383 | // Remove the decoder from the list | |
384 | int index = decoder_selector_->findData(QVariant::fromValue((void*)d)); | |
385 | ||
386 | if (index != -1) | |
387 | decoder_selector_->removeItem(index); | |
388 | } | |
389 | ||
390 | void View::on_actionSave_triggered(QAction* action) | |
391 | { | |
392 | (void)action; | |
393 | ||
394 | save_data(); | |
395 | } | |
396 | ||
9a35b05d SA |
397 | void View::on_table_item_clicked(const QModelIndex& index) |
398 | { | |
399 | (void)index; | |
400 | ||
401 | // Force repaint, otherwise the new selection isn't shown for some reason | |
402 | table_view_->viewport()->update(); | |
403 | } | |
404 | ||
405 | void View::on_table_item_double_clicked(const QModelIndex& index) | |
406 | { | |
407 | const Annotation* ann = static_cast<const Annotation*>(index.internalPointer()); | |
408 | ||
409 | shared_ptr<views::ViewBase> main_view = session_.main_view(); | |
410 | ||
411 | main_view->focus_on_range(ann->start_sample(), ann->end_sample()); | |
412 | } | |
413 | ||
414 | void View::on_table_header_requested(const QPoint& pos) | |
415 | { | |
416 | QMenu* menu = new QMenu(this); | |
417 | ||
418 | for (int i = 0; i < table_view_->horizontalHeader()->count(); i++) { | |
419 | int column = table_view_->horizontalHeader()->logicalIndex(i); | |
420 | ||
421 | const QString title = model_->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString(); | |
422 | QAction* action = new QAction(title, this); | |
423 | ||
424 | action->setCheckable(true); | |
425 | action->setChecked(!table_view_->horizontalHeader()->isSectionHidden(column)); | |
426 | action->setData(column); | |
427 | ||
428 | connect(action, SIGNAL(toggled(bool)), this, SLOT(on_table_header_toggled(bool))); | |
429 | ||
430 | menu->addAction(action); | |
431 | } | |
432 | ||
433 | menu->popup(table_view_->horizontalHeader()->viewport()->mapToGlobal(pos)); | |
434 | } | |
435 | ||
436 | void View::on_table_header_toggled(bool checked) | |
437 | { | |
438 | QAction* action = qobject_cast<QAction*>(QObject::sender()); | |
439 | assert(action); | |
440 | ||
441 | const int column = action->data().toInt(); | |
442 | ||
443 | table_view_->horizontalHeader()->setSectionHidden(column, !checked); | |
444 | } | |
445 | ||
24d69d27 SA |
446 | void View::perform_delayed_view_update() |
447 | { | |
448 | update_data(); | |
449 | } | |
450 | ||
451 | ||
452 | } // namespace tabular_decoder | |
453 | } // namespace views | |
454 | } // namespace pv |