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