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