]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <extdef.h> | |
22 | ||
23 | #include <algorithm> | |
24 | #include <cassert> | |
25 | ||
26 | #include <QAction> | |
27 | #include <QDebug> | |
28 | #include <QHelpEvent> | |
29 | #include <QMenu> | |
30 | #include <QToolTip> | |
31 | ||
32 | #include "mainbar.hpp" | |
33 | ||
34 | #include <pv/devicemanager.hpp> | |
35 | #include <pv/devices/hardwaredevice.hpp> | |
36 | #include <pv/mainwindow.hpp> | |
37 | #include <pv/popups/deviceoptions.hpp> | |
38 | #include <pv/popups/channels.hpp> | |
39 | #include <pv/util.hpp> | |
40 | #include <pv/widgets/exportmenu.hpp> | |
41 | #include <pv/widgets/importmenu.hpp> | |
42 | ||
43 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
44 | ||
45 | using std::back_inserter; | |
46 | using std::copy; | |
47 | using std::list; | |
48 | using std::map; | |
49 | using std::max; | |
50 | using std::min; | |
51 | using std::shared_ptr; | |
52 | using std::string; | |
53 | using std::vector; | |
54 | ||
55 | using sigrok::Capability; | |
56 | using sigrok::ConfigKey; | |
57 | using sigrok::Error; | |
58 | using sigrok::InputFormat; | |
59 | ||
60 | namespace pv { | |
61 | namespace toolbars { | |
62 | ||
63 | const uint64_t MainBar::MinSampleCount = 100ULL; | |
64 | const uint64_t MainBar::MaxSampleCount = 1000000000000ULL; | |
65 | const uint64_t MainBar::DefaultSampleCount = 1000000; | |
66 | ||
67 | MainBar::MainBar(Session &session, MainWindow &main_window) : | |
68 | QToolBar("Sampling Bar", &main_window), | |
69 | session_(session), | |
70 | main_window_(main_window), | |
71 | device_selector_(this, session.device_manager(), | |
72 | main_window.action_connect()), | |
73 | configure_button_(this), | |
74 | configure_button_action_(nullptr), | |
75 | channels_button_(this), | |
76 | sample_count_(" samples", this), | |
77 | sample_rate_("Hz", this), | |
78 | updating_sample_rate_(false), | |
79 | updating_sample_count_(false), | |
80 | sample_count_supported_(false), | |
81 | icon_red_(":/icons/status-red.svg"), | |
82 | icon_green_(":/icons/status-green.svg"), | |
83 | icon_grey_(":/icons/status-grey.svg"), | |
84 | run_stop_button_(this), | |
85 | menu_button_(this) | |
86 | { | |
87 | setObjectName(QString::fromUtf8("MainBar")); | |
88 | ||
89 | setMovable(false); | |
90 | setFloatable(false); | |
91 | setContextMenuPolicy(Qt::PreventContextMenu); | |
92 | ||
93 | // Open button | |
94 | QToolButton *const open_button = new QToolButton(this); | |
95 | ||
96 | widgets::ImportMenu *import_menu = new widgets::ImportMenu(this, | |
97 | session.device_manager().context(), | |
98 | main_window.action_open()); | |
99 | connect(import_menu, | |
100 | SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)), | |
101 | &main_window_, | |
102 | SLOT(import_file(std::shared_ptr<sigrok::InputFormat>))); | |
103 | ||
104 | open_button->setMenu(import_menu); | |
105 | open_button->setDefaultAction(main_window.action_open()); | |
106 | open_button->setPopupMode(QToolButton::MenuButtonPopup); | |
107 | ||
108 | // Save button | |
109 | QToolButton *const save_button = new QToolButton(this); | |
110 | ||
111 | widgets::ExportMenu *export_menu = new widgets::ExportMenu(this, | |
112 | session.device_manager().context(), | |
113 | main_window.action_save_as()); | |
114 | connect(export_menu, | |
115 | SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)), | |
116 | &main_window_, | |
117 | SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>))); | |
118 | ||
119 | save_button->setMenu(export_menu); | |
120 | save_button->setDefaultAction(main_window.action_save_as()); | |
121 | save_button->setPopupMode(QToolButton::MenuButtonPopup); | |
122 | ||
123 | // Device selector menu | |
124 | connect(&device_selector_, SIGNAL(device_selected()), | |
125 | this, SLOT(on_device_selected())); | |
126 | ||
127 | // Setup the decoder button | |
128 | #ifdef ENABLE_DECODE | |
129 | QToolButton *add_decoder_button = new QToolButton(this); | |
130 | add_decoder_button->setIcon(QIcon::fromTheme("add-decoder", | |
131 | QIcon(":/icons/add-decoder.svg"))); | |
132 | add_decoder_button->setPopupMode(QToolButton::InstantPopup); | |
133 | add_decoder_button->setMenu(main_window_.menu_decoder_add()); | |
134 | #endif | |
135 | ||
136 | // Setup the menu | |
137 | QMenu *const menu = new QMenu(this); | |
138 | ||
139 | QMenu *const menu_help = new QMenu; | |
140 | menu_help->setTitle(tr("&Help")); | |
141 | menu_help->addAction(main_window.action_about()); | |
142 | ||
143 | menu->addAction(menu_help->menuAction()); | |
144 | menu->addSeparator(); | |
145 | menu->addAction(main_window.action_quit()); | |
146 | ||
147 | menu_button_.setMenu(menu); | |
148 | menu_button_.setPopupMode(QToolButton::InstantPopup); | |
149 | menu_button_.setIcon(QIcon::fromTheme("menu", | |
150 | QIcon(":/icons/menu.svg"))); | |
151 | ||
152 | // Setup the toolbar | |
153 | addWidget(open_button); | |
154 | addWidget(save_button); | |
155 | addSeparator(); | |
156 | addAction(main_window.action_view_zoom_in()); | |
157 | addAction(main_window.action_view_zoom_out()); | |
158 | addAction(main_window.action_view_zoom_fit()); | |
159 | addAction(main_window.action_view_zoom_one_to_one()); | |
160 | addSeparator(); | |
161 | addAction(main_window.action_view_show_cursors()); | |
162 | addSeparator(); | |
163 | ||
164 | connect(&run_stop_button_, SIGNAL(clicked()), | |
165 | this, SLOT(on_run_stop())); | |
166 | connect(&sample_count_, SIGNAL(value_changed()), | |
167 | this, SLOT(on_sample_count_changed())); | |
168 | connect(&sample_rate_, SIGNAL(value_changed()), | |
169 | this, SLOT(on_sample_rate_changed())); | |
170 | ||
171 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); | |
172 | ||
173 | set_capture_state(pv::Session::Stopped); | |
174 | ||
175 | configure_button_.setIcon(QIcon::fromTheme("configure", | |
176 | QIcon(":/icons/configure.png"))); | |
177 | ||
178 | channels_button_.setIcon(QIcon::fromTheme("channels", | |
179 | QIcon(":/icons/channels.svg"))); | |
180 | ||
181 | run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
182 | ||
183 | addWidget(&device_selector_); | |
184 | configure_button_action_ = addWidget(&configure_button_); | |
185 | addWidget(&channels_button_); | |
186 | addWidget(&sample_count_); | |
187 | addWidget(&sample_rate_); | |
188 | addWidget(&run_stop_button_); | |
189 | #ifdef ENABLE_DECODE | |
190 | addSeparator(); | |
191 | addWidget(add_decoder_button); | |
192 | #endif | |
193 | ||
194 | QWidget *const spacer = new QWidget(); | |
195 | spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | |
196 | addWidget(spacer); | |
197 | ||
198 | addWidget(&menu_button_); | |
199 | ||
200 | sample_count_.installEventFilter(this); | |
201 | sample_rate_.installEventFilter(this); | |
202 | } | |
203 | ||
204 | void MainBar::update_device_list() | |
205 | { | |
206 | DeviceManager &mgr = session_.device_manager(); | |
207 | shared_ptr<devices::Device> selected_device = session_.device(); | |
208 | list< shared_ptr<devices::Device> > devs; | |
209 | ||
210 | copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs)); | |
211 | ||
212 | if (std::find(devs.begin(), devs.end(), selected_device) == devs.end()) | |
213 | devs.push_back(selected_device); | |
214 | assert(selected_device); | |
215 | ||
216 | device_selector_.set_device_list(devs, selected_device); | |
217 | update_device_config_widgets(); | |
218 | } | |
219 | ||
220 | ||
221 | void MainBar::set_capture_state(pv::Session::capture_state state) | |
222 | { | |
223 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; | |
224 | run_stop_button_.setIcon(*icons[state]); | |
225 | run_stop_button_.setText((state == pv::Session::Stopped) ? | |
226 | tr("Run") : tr("Stop")); | |
227 | run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); | |
228 | } | |
229 | ||
230 | void MainBar::update_sample_rate_selector() | |
231 | { | |
232 | Glib::VariantContainerBase gvar_dict; | |
233 | GVariant *gvar_list; | |
234 | const uint64_t *elements = nullptr; | |
235 | gsize num_elements; | |
236 | map< const ConfigKey*, std::set<Capability> > keys; | |
237 | ||
238 | if (updating_sample_rate_) | |
239 | return; | |
240 | ||
241 | const shared_ptr<devices::Device> device = | |
242 | device_selector_.selected_device(); | |
243 | if (!device) | |
244 | return; | |
245 | ||
246 | assert(!updating_sample_rate_); | |
247 | updating_sample_rate_ = true; | |
248 | ||
249 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
250 | ||
251 | try { | |
252 | keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS); | |
253 | } catch (Error) {} | |
254 | ||
255 | const auto iter = keys.find(ConfigKey::SAMPLERATE); | |
256 | if (iter != keys.end() && | |
257 | (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { | |
258 | try { | |
259 | gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE); | |
260 | } catch(const sigrok::Error &e) { | |
261 | // Failed to enunmerate samplerate | |
262 | (void)e; | |
263 | } | |
264 | } | |
265 | ||
266 | if (!gvar_dict.gobj()) { | |
267 | sample_rate_.show_none(); | |
268 | updating_sample_rate_ = false; | |
269 | return; | |
270 | } | |
271 | ||
272 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
273 | "samplerate-steps", G_VARIANT_TYPE("at")))) | |
274 | { | |
275 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
276 | gvar_list, &num_elements, sizeof(uint64_t)); | |
277 | ||
278 | const uint64_t min = elements[0]; | |
279 | const uint64_t max = elements[1]; | |
280 | const uint64_t step = elements[2]; | |
281 | ||
282 | g_variant_unref(gvar_list); | |
283 | ||
284 | assert(min > 0); | |
285 | assert(max > 0); | |
286 | assert(max > min); | |
287 | assert(step > 0); | |
288 | ||
289 | if (step == 1) | |
290 | sample_rate_.show_125_list(min, max); | |
291 | else | |
292 | { | |
293 | // When the step is not 1, we cam't make a 1-2-5-10 | |
294 | // list of sample rates, because we may not be able to | |
295 | // make round numbers. Therefore in this case, show a | |
296 | // spin box. | |
297 | sample_rate_.show_min_max_step(min, max, step); | |
298 | } | |
299 | } | |
300 | else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
301 | "samplerates", G_VARIANT_TYPE("at")))) | |
302 | { | |
303 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
304 | gvar_list, &num_elements, sizeof(uint64_t)); | |
305 | sample_rate_.show_list(elements, num_elements); | |
306 | g_variant_unref(gvar_list); | |
307 | } | |
308 | updating_sample_rate_ = false; | |
309 | ||
310 | update_sample_rate_selector_value(); | |
311 | } | |
312 | ||
313 | void MainBar::update_sample_rate_selector_value() | |
314 | { | |
315 | if (updating_sample_rate_) | |
316 | return; | |
317 | ||
318 | const shared_ptr<devices::Device> device = | |
319 | device_selector_.selected_device(); | |
320 | if (!device) | |
321 | return; | |
322 | ||
323 | try { | |
324 | auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE); | |
325 | uint64_t samplerate = | |
326 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); | |
327 | assert(!updating_sample_rate_); | |
328 | updating_sample_rate_ = true; | |
329 | sample_rate_.set_value(samplerate); | |
330 | updating_sample_rate_ = false; | |
331 | } catch (Error error) { | |
332 | qDebug() << "WARNING: Failed to get value of sample rate"; | |
333 | return; | |
334 | } | |
335 | } | |
336 | ||
337 | void MainBar::update_sample_count_selector() | |
338 | { | |
339 | if (updating_sample_count_) | |
340 | return; | |
341 | ||
342 | const shared_ptr<devices::Device> device = | |
343 | device_selector_.selected_device(); | |
344 | if (!device) | |
345 | return; | |
346 | ||
347 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
348 | ||
349 | assert(!updating_sample_count_); | |
350 | updating_sample_count_ = true; | |
351 | ||
352 | if (!sample_count_supported_) | |
353 | { | |
354 | sample_count_.show_none(); | |
355 | updating_sample_count_ = false; | |
356 | return; | |
357 | } | |
358 | ||
359 | uint64_t sample_count = sample_count_.value(); | |
360 | uint64_t min_sample_count = 0; | |
361 | uint64_t max_sample_count = MaxSampleCount; | |
362 | ||
363 | if (sample_count == 0) | |
364 | sample_count = DefaultSampleCount; | |
365 | ||
366 | const auto keys = sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS); | |
367 | const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES); | |
368 | if (iter != keys.end() && | |
369 | (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { | |
370 | try { | |
371 | auto gvar = | |
372 | sr_dev->config_list(ConfigKey::LIMIT_SAMPLES); | |
373 | if (gvar.gobj()) | |
374 | g_variant_get(gvar.gobj(), "(tt)", | |
375 | &min_sample_count, &max_sample_count); | |
376 | } catch(const sigrok::Error &e) { | |
377 | // Failed to query sample limit | |
378 | (void)e; | |
379 | } | |
380 | } | |
381 | ||
382 | min_sample_count = min(max(min_sample_count, MinSampleCount), | |
383 | max_sample_count); | |
384 | ||
385 | sample_count_.show_125_list( | |
386 | min_sample_count, max_sample_count); | |
387 | ||
388 | try { | |
389 | auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES); | |
390 | sample_count = g_variant_get_uint64(gvar.gobj()); | |
391 | if (sample_count == 0) | |
392 | sample_count = DefaultSampleCount; | |
393 | sample_count = min(max(sample_count, MinSampleCount), | |
394 | max_sample_count); | |
395 | } catch (Error error) {} | |
396 | ||
397 | sample_count_.set_value(sample_count); | |
398 | ||
399 | updating_sample_count_ = false; | |
400 | } | |
401 | ||
402 | void MainBar::update_device_config_widgets() | |
403 | { | |
404 | using namespace pv::popups; | |
405 | ||
406 | const shared_ptr<devices::Device> device = | |
407 | device_selector_.selected_device(); | |
408 | if (!device) | |
409 | return; | |
410 | ||
411 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
412 | if (!sr_dev) | |
413 | return; | |
414 | ||
415 | // Update the configure popup | |
416 | DeviceOptions *const opts = new DeviceOptions(sr_dev, this); | |
417 | configure_button_action_->setVisible( | |
418 | !opts->binding().properties().empty()); | |
419 | configure_button_.set_popup(opts); | |
420 | ||
421 | // Update the channels popup | |
422 | Channels *const channels = new Channels(session_, this); | |
423 | channels_button_.set_popup(channels); | |
424 | ||
425 | // Update supported options. | |
426 | sample_count_supported_ = false; | |
427 | ||
428 | try { | |
429 | for (auto entry : sr_dev->config_keys(ConfigKey::DEVICE_OPTIONS)) | |
430 | { | |
431 | auto key = entry.first; | |
432 | auto capabilities = entry.second; | |
433 | switch (key->id()) { | |
434 | case SR_CONF_LIMIT_SAMPLES: | |
435 | if (capabilities.count(Capability::SET)) | |
436 | sample_count_supported_ = true; | |
437 | break; | |
438 | case SR_CONF_LIMIT_FRAMES: | |
439 | if (capabilities.count(Capability::SET)) | |
440 | { | |
441 | sr_dev->config_set(ConfigKey::LIMIT_FRAMES, | |
442 | Glib::Variant<guint64>::create(1)); | |
443 | on_config_changed(); | |
444 | } | |
445 | break; | |
446 | default: | |
447 | break; | |
448 | } | |
449 | } | |
450 | } catch (Error error) {} | |
451 | ||
452 | // Add notification of reconfigure events | |
453 | disconnect(this, SLOT(on_config_changed())); | |
454 | connect(&opts->binding(), SIGNAL(config_changed()), | |
455 | this, SLOT(on_config_changed())); | |
456 | ||
457 | // Update sweep timing widgets. | |
458 | update_sample_count_selector(); | |
459 | update_sample_rate_selector(); | |
460 | } | |
461 | ||
462 | void MainBar::commit_sample_count() | |
463 | { | |
464 | uint64_t sample_count = 0; | |
465 | ||
466 | if (updating_sample_count_) | |
467 | return; | |
468 | ||
469 | const shared_ptr<devices::Device> device = | |
470 | device_selector_.selected_device(); | |
471 | if (!device) | |
472 | return; | |
473 | ||
474 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
475 | ||
476 | sample_count = sample_count_.value(); | |
477 | ||
478 | // Set the sample count | |
479 | assert(!updating_sample_count_); | |
480 | updating_sample_count_ = true; | |
481 | if (sample_count_supported_) | |
482 | { | |
483 | try { | |
484 | sr_dev->config_set(ConfigKey::LIMIT_SAMPLES, | |
485 | Glib::Variant<guint64>::create(sample_count)); | |
486 | on_config_changed(); | |
487 | } catch (Error error) { | |
488 | qDebug() << "Failed to configure sample count."; | |
489 | return; | |
490 | } | |
491 | } | |
492 | updating_sample_count_ = false; | |
493 | } | |
494 | ||
495 | void MainBar::commit_sample_rate() | |
496 | { | |
497 | uint64_t sample_rate = 0; | |
498 | ||
499 | if (updating_sample_rate_) | |
500 | return; | |
501 | ||
502 | const shared_ptr<devices::Device> device = | |
503 | device_selector_.selected_device(); | |
504 | if (!device) | |
505 | return; | |
506 | ||
507 | const shared_ptr<sigrok::Device> sr_dev = device->device(); | |
508 | ||
509 | sample_rate = sample_rate_.value(); | |
510 | if (sample_rate == 0) | |
511 | return; | |
512 | ||
513 | // Set the samplerate | |
514 | assert(!updating_sample_rate_); | |
515 | updating_sample_rate_ = true; | |
516 | try { | |
517 | sr_dev->config_set(ConfigKey::SAMPLERATE, | |
518 | Glib::Variant<guint64>::create(sample_rate)); | |
519 | on_config_changed(); | |
520 | } catch (Error error) { | |
521 | qDebug() << "Failed to configure samplerate."; | |
522 | return; | |
523 | } | |
524 | updating_sample_rate_ = false; | |
525 | } | |
526 | ||
527 | void MainBar::on_device_selected() | |
528 | { | |
529 | shared_ptr<devices::Device> device = device_selector_.selected_device(); | |
530 | if (!device) | |
531 | return; | |
532 | ||
533 | main_window_.select_device(device); | |
534 | ||
535 | update_device_config_widgets(); | |
536 | } | |
537 | ||
538 | void MainBar::on_sample_count_changed() | |
539 | { | |
540 | commit_sample_count(); | |
541 | } | |
542 | ||
543 | void MainBar::on_sample_rate_changed() | |
544 | { | |
545 | commit_sample_rate(); | |
546 | } | |
547 | ||
548 | void MainBar::on_run_stop() | |
549 | { | |
550 | commit_sample_count(); | |
551 | commit_sample_rate(); | |
552 | main_window_.run_stop(); | |
553 | } | |
554 | ||
555 | void MainBar::on_config_changed() | |
556 | { | |
557 | commit_sample_count(); | |
558 | update_sample_count_selector(); | |
559 | commit_sample_rate(); | |
560 | update_sample_rate_selector(); | |
561 | } | |
562 | ||
563 | bool MainBar::eventFilter(QObject *watched, QEvent *event) | |
564 | { | |
565 | if ((watched == &sample_count_ || watched == &sample_rate_) && | |
566 | (event->type() == QEvent::ToolTip)) { | |
567 | double sec = (double)sample_count_.value() / sample_rate_.value(); | |
568 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); | |
569 | ||
570 | QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec)); | |
571 | QToolTip::showText(help_event->globalPos(), str); | |
572 | ||
573 | return true; | |
574 | } | |
575 | ||
576 | return false; | |
577 | } | |
578 | ||
579 | } // namespace toolbars | |
580 | } // namespace pv |