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