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