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