]> sigrok.org Git - pulseview.git/blame - pv/toolbars/mainbar.cpp
Make the first view own the toolbar instead of the main window
[pulseview.git] / pv / toolbars / mainbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7 3 *
079d39ea 4 * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk>
d4984fe7
JH
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
09f5d123 21#include <extdef.h>
dde1a563 22
079d39ea
JH
23#include <algorithm>
24#include <cassert>
215f9499 25
dde1a563 26#include <QAction>
48888313 27#include <QDebug>
0f8f8c18 28#include <QFileDialog>
40065ab6 29#include <QHelpEvent>
87f0df9b 30#include <QMenu>
0f8f8c18
SA
31#include <QMessageBox>
32#include <QSettings>
40065ab6 33#include <QToolTip>
dde1a563 34
7c657094 35#include "mainbar.hpp"
d4984fe7 36
0f8f8c18
SA
37#include <boost/algorithm/string/join.hpp>
38
2acdb232 39#include <pv/devicemanager.hpp>
da30ecb7 40#include <pv/devices/hardwaredevice.hpp>
0f8f8c18
SA
41#include <pv/devices/inputfile.hpp>
42#include <pv/devices/sessionfile.hpp>
43#include <pv/dialogs/connect.hpp>
44#include <pv/dialogs/inputoutputoptions.hpp>
45#include <pv/dialogs/storeprogress.hpp>
4e7f5ba8 46#include <pv/mainwindow.hpp>
2acdb232
JH
47#include <pv/popups/deviceoptions.hpp>
48#include <pv/popups/channels.hpp>
49#include <pv/util.hpp>
0f8f8c18 50#include <pv/view/view.hpp>
998b89fd 51#include <pv/widgets/exportmenu.hpp>
ed43ef2e 52#include <pv/widgets/importmenu.hpp>
0f8f8c18
SA
53#ifdef ENABLE_DECODE
54#include <pv/widgets/decodermenu.hpp>
55#endif
cdb50f67 56
fe3a1c21 57#include <libsigrokcxx/libsigrokcxx.hpp>
e8d00928 58
079d39ea 59using std::back_inserter;
0f8f8c18 60using std::cerr;
079d39ea 61using std::copy;
0f8f8c18 62using std::endl;
079d39ea 63using std::list;
19adbc2c 64using std::map;
b50ef520
JH
65using std::max;
66using std::min;
0f8f8c18 67using std::pair;
f9abf97e 68using std::shared_ptr;
819f4c25 69using std::string;
079d39ea 70using std::vector;
dd63af74 71
e8d00928
ML
72using sigrok::Capability;
73using sigrok::ConfigKey;
e8d00928 74using sigrok::Error;
ed43ef2e 75using sigrok::InputFormat;
0f8f8c18
SA
76using sigrok::OutputFormat;
77
78using boost::algorithm::join;
e8d00928 79
51e77110 80namespace pv {
f4c92e1c 81namespace toolbars {
51e77110 82
7c657094
JH
83const uint64_t MainBar::MinSampleCount = 100ULL;
84const uint64_t MainBar::MaxSampleCount = 1000000000000ULL;
85const uint64_t MainBar::DefaultSampleCount = 1000000;
09f5d123 86
0f8f8c18
SA
87const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
88const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
89
90MainBar::MainBar(Session &session, MainWindow &main_window,
91 string open_file_name, string open_file_format) :
4e7f5ba8 92 QToolBar("Sampling Bar", &main_window),
0f8f8c18
SA
93 action_open_(new QAction(this)),
94 action_save_as_(new QAction(this)),
95 action_save_selection_as_(new QAction(this)),
96 action_connect_(new QAction(this)),
97 action_view_zoom_in_(new QAction(this)),
98 action_view_zoom_out_(new QAction(this)),
99 action_view_zoom_fit_(new QAction(this)),
100 action_view_zoom_one_to_one_(new QAction(this)),
101 action_view_show_cursors_(new QAction(this)),
8dbbc7f0 102 session_(session),
0f8f8c18
SA
103 device_selector_(&main_window, session.device_manager(),
104 action_connect_),
8dbbc7f0 105 configure_button_(this),
4c60462b 106 configure_button_action_(nullptr),
8dbbc7f0 107 channels_button_(this),
9dd88889 108 channels_button_action_(nullptr),
8dbbc7f0
JH
109 sample_count_(" samples", this),
110 sample_rate_("Hz", this),
111 updating_sample_rate_(false),
112 updating_sample_count_(false),
113 sample_count_supported_(false),
114 icon_red_(":/icons/status-red.svg"),
115 icon_green_(":/icons/status-green.svg"),
116 icon_grey_(":/icons/status-grey.svg"),
87f0df9b 117 run_stop_button_(this),
9dd88889 118 run_stop_button_action_(nullptr),
87f0df9b 119 menu_button_(this)
0f8f8c18
SA
120#ifdef ENABLE_DECODE
121 , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
122#endif
6fb67b27 123{
7c657094 124 setObjectName(QString::fromUtf8("MainBar"));
0c7cdea2 125
f2dbf150
JH
126 setMovable(false);
127 setFloatable(false);
758f6023 128 setContextMenuPolicy(Qt::PreventContextMenu);
f2dbf150 129
0f8f8c18
SA
130 // Actions
131 action_open_->setText(tr("&Open..."));
132 action_open_->setIcon(QIcon::fromTheme("document-open",
133 QIcon(":/icons/document-open.png")));
134 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
135 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
136
137 action_save_as_->setText(tr("&Save As..."));
138 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
139 QIcon(":/icons/document-save-as.png")));
140 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
141 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
142
143 action_save_selection_as_->setText(tr("Save Selected &Range As..."));
144 action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as",
145 QIcon(":/icons/document-save-as.png")));
146 action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
147 action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs"));
148
149 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
150 session.device_manager().context());
151 menu_file_export->setTitle(tr("&Export"));
152 connect(menu_file_export,
153 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
154 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
155
156 widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
157 session.device_manager().context());
158 menu_file_import->setTitle(tr("&Import"));
159 connect(menu_file_import,
160 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
161 this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
162
163 action_connect_->setText(tr("&Connect to Device..."));
164 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
165
166 action_view_zoom_in_->setText(tr("Zoom &In"));
167 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
168 QIcon(":/icons/zoom-in.png")));
169 // simply using Qt::Key_Plus shows no + in the menu
170 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
171 action_view_zoom_in_->setObjectName(
172 QString::fromUtf8("actionViewZoomIn"));
173
174 action_view_zoom_out_->setText(tr("Zoom &Out"));
175 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
176 QIcon(":/icons/zoom-out.png")));
177 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
178 action_view_zoom_out_->setObjectName(
179 QString::fromUtf8("actionViewZoomOut"));
180
181 action_view_zoom_fit_->setCheckable(true);
182 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
183 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
184 QIcon(":/icons/zoom-fit.png")));
185 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
186 action_view_zoom_fit_->setObjectName(
187 QString::fromUtf8("actionViewZoomFit"));
188
189 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
190 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
191 QIcon(":/icons/zoom-original.png")));
192 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
193 action_view_zoom_one_to_one_->setObjectName(
194 QString::fromUtf8("actionViewZoomOneToOne"));
195
196 action_view_show_cursors_->setCheckable(true);
197 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
198 QIcon(":/icons/show-cursors.svg")));
199 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
200 action_view_show_cursors_->setObjectName(
201 QString::fromUtf8("actionViewShowCursors"));
202 action_view_show_cursors_->setText(tr("Show &Cursors"));
203
ed43ef2e
JH
204 // Open button
205 QToolButton *const open_button = new QToolButton(this);
206
207 widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
0f8f8c18 208 session.device_manager().context(), action_open_);
ed43ef2e
JH
209 connect(import_menu,
210 SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
0f8f8c18 211 &main_window,
ed43ef2e
JH
212 SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
213
214 open_button->setMenu(import_menu);
0f8f8c18 215 open_button->setDefaultAction(action_open_);
ed43ef2e
JH
216 open_button->setPopupMode(QToolButton::MenuButtonPopup);
217
998b89fd
JH
218 // Save button
219 QToolButton *const save_button = new QToolButton(this);
220
686d9151 221 vector<QAction *> open_actions;
0f8f8c18
SA
222 open_actions.push_back(action_save_as_);
223 open_actions.push_back(action_save_selection_as_);
686d9151 224
998b89fd
JH
225 widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
226 session.device_manager().context(),
686d9151 227 open_actions);
998b89fd
JH
228 connect(export_menu,
229 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
0f8f8c18 230 &main_window,
998b89fd
JH
231 SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
232
233 save_button->setMenu(export_menu);
0f8f8c18 234 save_button->setDefaultAction(action_save_as_);
998b89fd
JH
235 save_button->setPopupMode(QToolButton::MenuButtonPopup);
236
079d39ea
JH
237 // Device selector menu
238 connect(&device_selector_, SIGNAL(device_selected()),
239 this, SLOT(on_device_selected()));
240
168888e2
JH
241 // Setup the decoder button
242#ifdef ENABLE_DECODE
0f8f8c18
SA
243 menu_decoders_add_->setTitle(tr("&Add"));
244 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
245 this, SLOT(add_decoder(srd_decoder*)));
246
168888e2
JH
247 QToolButton *add_decoder_button = new QToolButton(this);
248 add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
249 QIcon(":/icons/add-decoder.svg")));
250 add_decoder_button->setPopupMode(QToolButton::InstantPopup);
0f8f8c18 251 add_decoder_button->setMenu(menu_decoders_add_);
168888e2
JH
252#endif
253
87f0df9b 254 // Setup the toolbar
ed43ef2e 255 addWidget(open_button);
998b89fd 256 addWidget(save_button);
87f0df9b 257 addSeparator();
0f8f8c18
SA
258 addAction(action_view_zoom_in_);
259 addAction(action_view_zoom_out_);
260 addAction(action_view_zoom_fit_);
261 addAction(action_view_zoom_one_to_one_);
87f0df9b 262 addSeparator();
0f8f8c18 263 addAction(action_view_show_cursors_);
bdf57963 264 addSeparator();
87f0df9b 265
8dbbc7f0 266 connect(&run_stop_button_, SIGNAL(clicked()),
9f3d12f3 267 this, SLOT(on_run_stop()));
8dbbc7f0 268 connect(&sample_count_, SIGNAL(value_changed()),
124d97de 269 this, SLOT(on_sample_count_changed()));
8dbbc7f0 270 connect(&sample_rate_, SIGNAL(value_changed()),
1198b887 271 this, SLOT(on_sample_rate_changed()));
dde1a563 272
8dbbc7f0 273 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
215f9499 274
2b81ae46 275 set_capture_state(pv::Session::Stopped);
274d4f13 276
8dbbc7f0 277 configure_button_.setIcon(QIcon::fromTheme("configure",
688ef645 278 QIcon(":/icons/configure.png")));
b7b659aa 279
8dbbc7f0 280 channels_button_.setIcon(QIcon::fromTheme("channels",
6ac6242b 281 QIcon(":/icons/channels.svg")));
cdb50f67 282
8dbbc7f0 283 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
f5798068 284
8dbbc7f0
JH
285 addWidget(&device_selector_);
286 configure_button_action_ = addWidget(&configure_button_);
9dd88889 287 channels_button_action_ = addWidget(&channels_button_);
8dbbc7f0
JH
288 addWidget(&sample_count_);
289 addWidget(&sample_rate_);
9dd88889 290 run_stop_button_action_ = addWidget(&run_stop_button_);
168888e2
JH
291#ifdef ENABLE_DECODE
292 addSeparator();
293 addWidget(add_decoder_button);
294#endif
40065ab6 295
87f0df9b
JH
296 QWidget *const spacer = new QWidget();
297 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
298 addWidget(spacer);
299
300 addWidget(&menu_button_);
301
8dbbc7f0
JH
302 sample_count_.installEventFilter(this);
303 sample_rate_.installEventFilter(this);
0f8f8c18
SA
304
305 QMetaObject::connectSlotsByName(this);
306
307 // Setup session_ events
308 connect(&session_, SIGNAL(capture_state_changed(int)), this,
309 SLOT(capture_state_changed(int)));
310 connect(&session_, SIGNAL(device_selected()), this,
311 SLOT(device_selected()));
312
313 // Figure out which file/device to use
314 if (open_file_name.empty())
315 select_init_device();
316 else
317 load_init_file(open_file_name, open_file_format);
318}
319
320Session &MainBar::session(void) const
321{
322 return session_;
6fb67b27
JH
323}
324
079d39ea 325void MainBar::update_device_list()
18203d86 326{
079d39ea 327 DeviceManager &mgr = session_.device_manager();
da30ecb7
JH
328 shared_ptr<devices::Device> selected_device = session_.device();
329 list< shared_ptr<devices::Device> > devs;
e8d00928 330
079d39ea 331 copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
53a7cce4 332
079d39ea
JH
333 if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
334 devs.push_back(selected_device);
e95e8563 335
079d39ea 336 device_selector_.set_device_list(devs, selected_device);
e95e8563 337 update_device_config_widgets();
18203d86
JH
338}
339
6fb67b27 340
7c657094 341void MainBar::set_capture_state(pv::Session::capture_state state)
6ac96c2e 342{
8dbbc7f0
JH
343 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
344 run_stop_button_.setIcon(*icons[state]);
2b81ae46 345 run_stop_button_.setText((state == pv::Session::Stopped) ?
2b49eeb0 346 tr("Run") : tr("Stop"));
8dbbc7f0 347 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
73e170f9
SA
348
349 bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
350
351 device_selector_.setEnabled(ui_enabled);
352 configure_button_.setEnabled(ui_enabled);
353 channels_button_.setEnabled(ui_enabled);
354 sample_count_.setEnabled(ui_enabled);
355 sample_rate_.setEnabled(ui_enabled);
6ac96c2e
JH
356}
357
7e0c99bf
SA
358void MainBar::reset_device_selector()
359{
360 device_selector_.reset();
361}
362
0f8f8c18
SA
363QAction* MainBar::action_open() const
364{
365 return action_open_;
366}
367
368QAction* MainBar::action_save_as() const
369{
370 return action_save_as_;
371}
372
373QAction* MainBar::action_save_selection_as() const
374{
375 return action_save_selection_as_;
376}
377
378QAction* MainBar::action_connect() const
379{
380 return action_connect_;
381}
382
383QAction* MainBar::action_view_zoom_in() const
384{
385 return action_view_zoom_in_;
386}
387
388QAction* MainBar::action_view_zoom_out() const
389{
390 return action_view_zoom_out_;
391}
392
393QAction* MainBar::action_view_zoom_fit() const
394{
395 return action_view_zoom_fit_;
396}
397
398QAction* MainBar::action_view_zoom_one_to_one() const
399{
400 return action_view_zoom_one_to_one_;
401}
402
403QAction* MainBar::action_view_show_cursors() const
404{
405 return action_view_show_cursors_;
406}
407
408void MainBar::run_stop()
409{
410 switch (session_.get_capture_state()) {
411 case Session::Stopped:
412 session_.start_capture([&](QString message) {
413 session_error("Capture failed", message); });
414 break;
415 case Session::AwaitingTrigger:
416 case Session::Running:
417 session_.stop_capture();
418 break;
419 }
420}
421
422void MainBar::select_device(shared_ptr<devices::Device> device)
423{
424 try {
425 if (device)
426 session_.set_device(device);
427 else
428 session_.set_default_device();
429 } catch (const QString &e) {
430 QMessageBox msg(this);
431 msg.setText(e);
432 msg.setInformativeText(tr("Failed to Select Device"));
433 msg.setStandardButtons(QMessageBox::Ok);
434 msg.setIcon(QMessageBox::Warning);
435 msg.exec();
436 }
437}
438
439void MainBar::select_init_device()
440{
441 QSettings settings;
442 map<string, string> dev_info;
443 list<string> key_list;
444 shared_ptr<devices::HardwareDevice> device;
445
446 DeviceManager& device_manager = session_.device_manager();
447
448 // Re-select last used device if possible but only if it's not demo
449 settings.beginGroup("Device");
450 key_list.push_back("vendor");
451 key_list.push_back("model");
452 key_list.push_back("version");
453 key_list.push_back("serial_num");
454 key_list.push_back("connection_id");
455
456 for (string key : key_list) {
457 const QString k = QString::fromStdString(key);
458 if (!settings.contains(k))
459 continue;
460
461 const string value = settings.value(k).toString().toStdString();
462 if (!value.empty())
463 dev_info.insert(std::make_pair(key, value));
464 }
465
466 if (dev_info.count("model") > 0)
467 if (dev_info.at("model").find("Demo device") == std::string::npos)
468 device = device_manager.find_device_from_info(dev_info);
469
470 // When we can't find a device similar to the one we used last
471 // time and there is at least one device aside from demo, use it
472 if (!device) {
473 for (shared_ptr<devices::HardwareDevice> dev : device_manager.devices()) {
474 dev_info = device_manager.get_device_info(dev);
475
476 if (dev_info.count("model") > 0)
477 if (dev_info.at("model").find("Demo device") == std::string::npos) {
478 device = dev;
479 break;
480 }
481 }
482 }
483
484 select_device(device);
485 update_device_list();
486
487 settings.endGroup();
488}
489
490void MainBar::load_file(QString file_name,
491 std::shared_ptr<sigrok::InputFormat> format,
492 const std::map<std::string, Glib::VariantBase> &options)
493{
494 DeviceManager& device_manager = session_.device_manager();
495
496 const QString errorMessage(
497 QString("Failed to load file %1").arg(file_name));
498
499 try {
500 if (format)
501 session_.set_device(shared_ptr<devices::Device>(
502 new devices::InputFile(
503 device_manager.context(),
504 file_name.toStdString(),
505 format, options)));
506 else
507 session_.set_device(shared_ptr<devices::Device>(
508 new devices::SessionFile(
509 device_manager.context(),
510 file_name.toStdString())));
511 } catch (Error e) {
512 show_session_error(tr("Failed to load ") + file_name, e.what());
513 session_.set_default_device();
514 update_device_list();
515 return;
516 }
517
518 update_device_list();
519
520 session_.start_capture([&, errorMessage](QString infoMessage) {
521 session_error(errorMessage, infoMessage); });
522}
523
524void MainBar::load_init_file(const std::string &file_name,
525 const std::string &format)
526{
527 shared_ptr<InputFormat> input_format;
528
529 DeviceManager& device_manager = session_.device_manager();
530
531 if (!format.empty()) {
532 const map<string, shared_ptr<InputFormat> > formats =
533 device_manager.context()->input_formats();
534 const auto iter = find_if(formats.begin(), formats.end(),
535 [&](const pair<string, shared_ptr<InputFormat> > f) {
536 return f.first == format; });
537 if (iter == formats.end()) {
538 cerr << "Unexpected input format: " << format << endl;
539 return;
540 }
541
542 input_format = (*iter).second;
543 }
544
545 load_file(QString::fromStdString(file_name), input_format);
546}
547
7c657094 548void MainBar::update_sample_rate_selector()
dde1a563 549{
e8d00928
ML
550 Glib::VariantContainerBase gvar_dict;
551 GVariant *gvar_list;
4c60462b 552 const uint64_t *elements = nullptr;
488f5d3f 553 gsize num_elements;
a6ed12bf 554 map< const ConfigKey*, std::set<Capability> > keys;
dde1a563 555
9dd88889
JH
556 if (updating_sample_rate_) {
557 sample_rate_.show_none();
3d4f16af 558 return;
9dd88889 559 }
3d4f16af 560
da30ecb7
JH
561 const shared_ptr<devices::Device> device =
562 device_selector_.selected_device();
e8d00928 563 if (!device)
ef4d0201
JH
564 return;
565
8dbbc7f0
JH
566 assert(!updating_sample_rate_);
567 updating_sample_rate_ = true;
1198b887 568
da30ecb7 569 const shared_ptr<sigrok::Device> sr_dev = device->device();
a6ed12bf 570
7bb0fbf4
ML
571 if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
572 gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
573 } else {
8dbbc7f0
JH
574 sample_rate_.show_none();
575 updating_sample_rate_ = false;
dde1a563 576 return;
1198b887 577 }
dde1a563 578
e8d00928 579 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
2ad82c2e 580 "samplerate-steps", G_VARIANT_TYPE("at")))) {
488f5d3f
BV
581 elements = (const uint64_t *)g_variant_get_fixed_array(
582 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
583
584 const uint64_t min = elements[0];
585 const uint64_t max = elements[1];
586 const uint64_t step = elements[2];
587
488f5d3f 588 g_variant_unref(gvar_list);
aafe53af
JH
589
590 assert(min > 0);
591 assert(max > 0);
592 assert(max > min);
593 assert(step > 0);
594
595 if (step == 1)
8dbbc7f0 596 sample_rate_.show_125_list(min, max);
2ad82c2e 597 else {
aafe53af
JH
598 // When the step is not 1, we cam't make a 1-2-5-10
599 // list of sample rates, because we may not be able to
600 // make round numbers. Therefore in this case, show a
601 // spin box.
8dbbc7f0 602 sample_rate_.show_min_max_step(min, max, step);
aafe53af 603 }
2ad82c2e
UH
604 } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
605 "samplerates", G_VARIANT_TYPE("at")))) {
488f5d3f
BV
606 elements = (const uint64_t *)g_variant_get_fixed_array(
607 gvar_list, &num_elements, sizeof(uint64_t));
8dbbc7f0 608 sample_rate_.show_list(elements, num_elements);
488f5d3f 609 g_variant_unref(gvar_list);
dde1a563 610 }
8dbbc7f0 611 updating_sample_rate_ = false;
48888313
JH
612
613 update_sample_rate_selector_value();
614}
615
7c657094 616void MainBar::update_sample_rate_selector_value()
48888313 617{
8dbbc7f0 618 if (updating_sample_rate_)
3d4f16af
JH
619 return;
620
da30ecb7
JH
621 const shared_ptr<devices::Device> device =
622 device_selector_.selected_device();
e8d00928 623 if (!device)
19adbc2c
JH
624 return;
625
e8d00928 626 try {
da30ecb7 627 auto gvar = device->device()->config_get(ConfigKey::SAMPLERATE);
e8d00928 628 uint64_t samplerate =
15289d5c 629 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
8dbbc7f0
JH
630 assert(!updating_sample_rate_);
631 updating_sample_rate_ = true;
632 sample_rate_.set_value(samplerate);
633 updating_sample_rate_ = false;
e8d00928 634 } catch (Error error) {
8dd44190 635 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
636 return;
637 }
48888313
JH
638}
639
7c657094 640void MainBar::update_sample_count_selector()
85756012 641{
8dbbc7f0 642 if (updating_sample_count_)
3d4f16af
JH
643 return;
644
da30ecb7
JH
645 const shared_ptr<devices::Device> device =
646 device_selector_.selected_device();
e8d00928 647 if (!device)
19adbc2c
JH
648 return;
649
da30ecb7
JH
650 const shared_ptr<sigrok::Device> sr_dev = device->device();
651
8dbbc7f0
JH
652 assert(!updating_sample_count_);
653 updating_sample_count_ = true;
85756012 654
2ad82c2e 655 if (!sample_count_supported_) {
3e7636f9
JH
656 sample_count_.show_none();
657 updating_sample_count_ = false;
658 return;
659 }
b50ef520 660
3e7636f9
JH
661 uint64_t sample_count = sample_count_.value();
662 uint64_t min_sample_count = 0;
663 uint64_t max_sample_count = MaxSampleCount;
3cbffdcf 664
3e7636f9
JH
665 if (sample_count == 0)
666 sample_count = DefaultSampleCount;
b50ef520 667
7bb0fbf4
ML
668 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
669 auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
670 if (gvar.gobj())
671 g_variant_get(gvar.gobj(), "(tt)",
672 &min_sample_count, &max_sample_count);
ed1d9d81 673 }
1d04852f 674
3e7636f9
JH
675 min_sample_count = min(max(min_sample_count, MinSampleCount),
676 max_sample_count);
b50ef520 677
3e7636f9
JH
678 sample_count_.show_125_list(
679 min_sample_count, max_sample_count);
b50ef520 680
7bb0fbf4 681 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
da30ecb7 682 auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
3e7636f9
JH
683 sample_count = g_variant_get_uint64(gvar.gobj());
684 if (sample_count == 0)
685 sample_count = DefaultSampleCount;
686 sample_count = min(max(sample_count, MinSampleCount),
687 max_sample_count);
7bb0fbf4 688 }
3e7636f9
JH
689
690 sample_count_.set_value(sample_count);
df3c1aa3 691
8dbbc7f0 692 updating_sample_count_ = false;
85756012
ML
693}
694
7c657094 695void MainBar::update_device_config_widgets()
e95e8563 696{
e95e8563
JH
697 using namespace pv::popups;
698
da30ecb7
JH
699 const shared_ptr<devices::Device> device =
700 device_selector_.selected_device();
9dd88889
JH
701
702 // Hide the widgets if no device is selected
703 channels_button_action_->setVisible(!!device);
704 run_stop_button_action_->setVisible(!!device);
705 if (!device) {
706 configure_button_action_->setVisible(false);
707 sample_count_.show_none();
708 sample_rate_.show_none();
e95e8563 709 return;
9dd88889 710 }
e95e8563 711
da30ecb7
JH
712 const shared_ptr<sigrok::Device> sr_dev = device->device();
713 if (!sr_dev)
714 return;
715
e95e8563 716 // Update the configure popup
da30ecb7 717 DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
8dbbc7f0 718 configure_button_action_->setVisible(
e95e8563 719 !opts->binding().properties().empty());
8dbbc7f0 720 configure_button_.set_popup(opts);
e95e8563 721
6ac6242b 722 // Update the channels popup
8dbbc7f0
JH
723 Channels *const channels = new Channels(session_, this);
724 channels_button_.set_popup(channels);
e95e8563
JH
725
726 // Update supported options.
8dbbc7f0 727 sample_count_supported_ = false;
e95e8563 728
7bb0fbf4
ML
729 if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
730 sample_count_supported_ = true;
731
732 if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
733 sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
734 Glib::Variant<guint64>::create(1));
735 on_config_changed();
736 }
e95e8563
JH
737
738 // Add notification of reconfigure events
739 disconnect(this, SLOT(on_config_changed()));
e8d00928 740 connect(&opts->binding(), SIGNAL(config_changed()),
e95e8563
JH
741 this, SLOT(on_config_changed()));
742
743 // Update sweep timing widgets.
744 update_sample_count_selector();
745 update_sample_rate_selector();
746}
747
0f8f8c18
SA
748void MainBar::commit_sample_rate()
749{
750 uint64_t sample_rate = 0;
751
752 const shared_ptr<devices::Device> device =
753 device_selector_.selected_device();
754 if (!device)
755 return;
756
757 const shared_ptr<sigrok::Device> sr_dev = device->device();
758
759 sample_rate = sample_rate_.value();
760 if (sample_rate == 0)
761 return;
762
763 try {
764 sr_dev->config_set(ConfigKey::SAMPLERATE,
765 Glib::Variant<guint64>::create(sample_rate));
766 update_sample_rate_selector();
767 } catch (Error error) {
768 qDebug() << "Failed to configure samplerate.";
769 return;
770 }
771
772 // Devices with built-in memory might impose limits on certain
773 // configurations, so let's check what sample count the driver
774 // lets us use now.
775 update_sample_count_selector();
776}
777
7c657094 778void MainBar::commit_sample_count()
124d97de
ML
779{
780 uint64_t sample_count = 0;
781
da30ecb7
JH
782 const shared_ptr<devices::Device> device =
783 device_selector_.selected_device();
e8d00928 784 if (!device)
19adbc2c
JH
785 return;
786
da30ecb7
JH
787 const shared_ptr<sigrok::Device> sr_dev = device->device();
788
8dbbc7f0 789 sample_count = sample_count_.value();
2ad82c2e 790 if (sample_count_supported_) {
e8d00928 791 try {
da30ecb7 792 sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
15289d5c 793 Glib::Variant<guint64>::create(sample_count));
449aec3c 794 update_sample_count_selector();
e8d00928
ML
795 } catch (Error error) {
796 qDebug() << "Failed to configure sample count.";
797 return;
798 }
124d97de 799 }
e13e7879
SA
800
801 // Devices with built-in memory might impose limits on certain
802 // configurations, so let's check what sample rate the driver
803 // lets us use now.
804 update_sample_rate_selector();
124d97de
ML
805}
806
0f8f8c18 807void MainBar::session_error(const QString text, const QString info_text)
48888313 808{
0f8f8c18
SA
809 QMetaObject::invokeMethod(this, "show_session_error",
810 Qt::QueuedConnection, Q_ARG(QString, text),
811 Q_ARG(QString, info_text));
812}
48888313 813
0f8f8c18
SA
814void MainBar::show_session_error(const QString text, const QString info_text)
815{
816 QMessageBox msg(this);
817 msg.setText(text);
818 msg.setInformativeText(info_text);
819 msg.setStandardButtons(QMessageBox::Ok);
820 msg.setIcon(QMessageBox::Warning);
821 msg.exec();
822}
19adbc2c 823
0f8f8c18
SA
824void MainBar::capture_state_changed(int state)
825{
826 set_capture_state((pv::Session::capture_state)state);
827}
da30ecb7 828
0f8f8c18
SA
829void MainBar::add_decoder(srd_decoder *decoder)
830{
831#ifdef ENABLE_DECODE
832 assert(decoder);
833 session_.add_decoder(decoder);
834#else
835 (void)decoder;
836#endif
837}
838
839void MainBar::export_file(shared_ptr<OutputFormat> format,
840 bool selection_only)
841{
842 using pv::dialogs::StoreProgress;
843
844 // Stop any currently running capture session
845 session_.stop_capture();
846
847 QSettings settings;
848 const QString dir = settings.value(SettingSaveDirectory).toString();
849
850 std::pair<uint64_t, uint64_t> sample_range;
851
852 // Selection only? Verify that the cursors are active and fetch their values
853 if (selection_only) {
854 if (!session_.main_view()->cursors()->enabled()) {
855 show_session_error(tr("Missing Cursors"), tr("You need to set the " \
856 "cursors before you can save the data enclosed by them " \
857 "to a session file (e.g. using ALT-V - Show Cursors)."));
858 return;
859 }
860
861 const double samplerate = session_.get_samplerate();
862
863 const pv::util::Timestamp& start_time = session_.main_view()->cursors()->first()->time();
864 const pv::util::Timestamp& end_time = session_.main_view()->cursors()->second()->time();
865
866 const uint64_t start_sample =
867 std::max((double)0, start_time.convert_to<double>() * samplerate);
868 const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
869
870 sample_range = std::make_pair(start_sample, end_sample);
871 } else {
872 sample_range = std::make_pair(0, 0);
873 }
874
875 // Construct the filter
876 const vector<string> exts = format->extensions();
877 QString filter = tr("%1 files ").arg(
878 QString::fromStdString(format->description()));
879
880 if (exts.empty())
881 filter += "(*.*)";
882 else
883 filter += QString("(*.%1);;%2 (*.*)").arg(
884 QString::fromStdString(join(exts, ", *.")),
885 tr("All Files"));
886
887 // Show the file dialog
888 const QString file_name = QFileDialog::getSaveFileName(
889 this, tr("Save File"), dir, filter);
890
891 if (file_name.isEmpty())
f9541bde
JH
892 return;
893
0f8f8c18
SA
894 const QString abs_path = QFileInfo(file_name).absolutePath();
895 settings.setValue(SettingSaveDirectory, abs_path);
896
897 // Show the options dialog
898 map<string, Glib::VariantBase> options;
899 if (!format->options().empty()) {
900 dialogs::InputOutputOptions dlg(
901 tr("Export %1").arg(QString::fromStdString(
902 format->description())),
903 format->options(), this);
904 if (!dlg.exec())
905 return;
906 options = dlg.options();
907 }
908
909 StoreProgress *dlg = new StoreProgress(file_name, format, options,
910 sample_range, session_, this);
911 dlg->run();
912}
913
914void MainBar::import_file(shared_ptr<InputFormat> format)
915{
916 assert(format);
917
918 QSettings settings;
919 const QString dir = settings.value(SettingOpenDirectory).toString();
920
921 // Construct the filter
922 const vector<string> exts = format->extensions();
923 const QString filter = exts.empty() ? "" :
924 tr("%1 files (*.%2)").arg(
925 QString::fromStdString(format->description()),
926 QString::fromStdString(join(exts, ", *.")));
927
928 // Show the file dialog
929 const QString file_name = QFileDialog::getOpenFileName(
930 this, tr("Import File"), dir, tr(
931 "%1 files (*.*);;All Files (*.*)").arg(
932 QString::fromStdString(format->description())));
933
934 if (file_name.isEmpty())
48888313 935 return;
0f8f8c18
SA
936
937 // Show the options dialog
938 map<string, Glib::VariantBase> options;
939 if (!format->options().empty()) {
940 dialogs::InputOutputOptions dlg(
941 tr("Import %1").arg(QString::fromStdString(
942 format->description())),
943 format->options(), this);
944 if (!dlg.exec())
945 return;
946 options = dlg.options();
48888313 947 }
e13e7879 948
0f8f8c18
SA
949 load_file(file_name, format, options);
950
951 const QString abs_path = QFileInfo(file_name).absolutePath();
952 settings.setValue(SettingOpenDirectory, abs_path);
dde1a563
JH
953}
954
7c657094 955void MainBar::on_device_selected()
dde1a563 956{
da30ecb7 957 shared_ptr<devices::Device> device = device_selector_.selected_device();
0f8f8c18
SA
958 if (!device) {
959 reset_device_selector();
19adbc2c 960 return;
0f8f8c18 961 }
19adbc2c 962
0f8f8c18 963 select_device(device);
51d4a9ab 964
e95e8563 965 update_device_config_widgets();
dde1a563 966}
51e77110 967
7c657094 968void MainBar::on_sample_count_changed()
124d97de 969{
449aec3c
SA
970 if (!updating_sample_count_)
971 commit_sample_count();
124d97de
ML
972}
973
7c657094 974void MainBar::on_sample_rate_changed()
48888313 975{
0e0b6b3e
SA
976 if (!updating_sample_rate_)
977 commit_sample_rate();
48888313
JH
978}
979
7c657094 980void MainBar::on_run_stop()
9f3d12f3 981{
b50ef520 982 commit_sample_count();
9f3d12f3 983 commit_sample_rate();
0f8f8c18 984 run_stop();
9f3d12f3
JH
985}
986
7c657094 987void MainBar::on_config_changed()
82afd5e3
JH
988{
989 commit_sample_count();
82afd5e3 990 commit_sample_rate();
82afd5e3
JH
991}
992
0f8f8c18
SA
993void MainBar::on_actionOpen_triggered()
994{
995 QSettings settings;
996 const QString dir = settings.value(SettingOpenDirectory).toString();
997
998 // Show the dialog
999 const QString file_name = QFileDialog::getOpenFileName(
1000 this, tr("Open File"), dir, tr(
1001 "Sigrok Sessions (*.sr);;"
1002 "All Files (*.*)"));
1003
1004 if (!file_name.isEmpty()) {
1005 load_file(file_name);
1006
1007 const QString abs_path = QFileInfo(file_name).absolutePath();
1008 settings.setValue(SettingOpenDirectory, abs_path);
1009 }
1010}
1011
1012void MainBar::on_actionSaveAs_triggered()
1013{
1014 export_file(session_.device_manager().context()->output_formats()["srzip"]);
1015}
1016
1017void MainBar::on_actionSaveSelectionAs_triggered()
1018{
1019 export_file(session_.device_manager().context()->output_formats()["srzip"], true);
1020}
1021
1022void MainBar::on_actionConnect_triggered()
1023{
1024 // Stop any currently running capture session
1025 session_.stop_capture();
1026
1027 dialogs::Connect dlg(this, session_.device_manager());
1028
1029 // If the user selected a device, select it in the device list. Select the
1030 // current device otherwise.
1031 if (dlg.exec())
1032 select_device(dlg.get_selected_device());
1033
1034 update_device_list();
1035}
1036
1037void MainBar::on_actionViewZoomIn_triggered()
1038{
1039 session_.main_view()->zoom(1);
1040}
1041
1042void MainBar::on_actionViewZoomOut_triggered()
1043{
1044 session_.main_view()->zoom(-1);
1045}
1046
1047void MainBar::on_actionViewZoomFit_triggered()
1048{
1049 session_.main_view()->zoom_fit(action_view_zoom_fit_->isChecked());
1050}
1051
1052void MainBar::on_actionViewZoomOneToOne_triggered()
1053{
1054 session_.main_view()->zoom_one_to_one();
1055}
1056
1057void MainBar::on_actionViewShowCursors_triggered()
1058{
1059 const bool show = !session_.main_view()->cursors_shown();
1060 if (show)
1061 session_.main_view()->centre_cursors();
1062
1063 session_.main_view()->show_cursors(show);
1064}
1065
7c657094 1066bool MainBar::eventFilter(QObject *watched, QEvent *event)
40065ab6 1067{
2ad82c2e
UH
1068 if (sample_count_supported_ && (watched == &sample_count_ ||
1069 watched == &sample_rate_) &&
1070 (event->type() == QEvent::ToolTip)) {
60d9b99a 1071 auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
40065ab6 1072 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
62974f45 1073
3ccf0f7f
JS
1074 QString str = tr("Total sampling time: %1").arg(
1075 pv::util::format_time_si(sec, pv::util::SIPrefix::unspecified, 0, "s", false));
40065ab6
JS
1076 QToolTip::showText(help_event->globalPos(), str);
1077
1078 return true;
1079 }
1080
1081 return false;
1082}
1083
f4c92e1c 1084} // namespace toolbars
51e77110 1085} // namespace pv