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