]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Add logging mechanism
[pulseview.git] / pv / mainwindow.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
d7bed479
JH
18 */
19
269528f5 20#ifdef ENABLE_DECODE
aaabd61b 21#include <libsigrokdecode/libsigrokdecode.h>
269528f5 22#endif
30236a54 23
85843b14 24#include <algorithm>
aca9aa83 25#include <cassert>
eb8269e3 26#include <cstdarg>
aca9aa83
UH
27#include <cstdint>
28#include <iterator>
85843b14 29
7d5425ef
JH
30#include <QAction>
31#include <QApplication>
93f683ad 32#include <QCloseEvent>
0f8f8c18 33#include <QDockWidget>
f1e2d26b 34#include <QHBoxLayout>
3231fbf9 35#include <QMessageBox>
643f65f9 36#include <QSettings>
45cb64ff 37#include <QShortcut>
aca9aa83 38#include <QWidget>
2953961c 39
2acdb232 40#include "mainwindow.hpp"
107ca6d3 41
2acdb232 42#include "devicemanager.hpp"
101e7a9b 43#include "devices/hardwaredevice.hpp"
bf9f1268 44#include "dialogs/settings.hpp"
aca9aa83 45#include "globalsettings.hpp"
7c657094 46#include "toolbars/mainbar.hpp"
aca9aa83 47#include "util.hpp"
1573bf16 48#include "views/trace/view.hpp"
e0ba4f6f 49#include "views/trace/standardbar.hpp"
d7bed479 50
fe3a1c21 51#include <libsigrokcxx/libsigrokcxx.hpp>
e82fd481 52
101e7a9b 53using std::dynamic_pointer_cast;
25272fee 54using std::make_shared;
f9abf97e 55using std::shared_ptr;
6842b5fc 56using std::string;
e8d00928 57
51e77110
JH
58namespace pv {
59
b1264f56 60namespace view {
26e3af6b 61class ViewItem;
b1264f56
JH
62}
63
0f8f8c18 64using toolbars::MainBar;
643f65f9 65
33e1afbe
SA
66const QString MainWindow::WindowTitle = tr("PulseView");
67
3ed18835 68MainWindow::MainWindow(DeviceManager &device_manager, QWidget *parent) :
107ca6d3 69 QMainWindow(parent),
8dbbc7f0 70 device_manager_(device_manager),
3b84fd0b 71 session_selector_(this),
3231fbf9 72 session_state_mapper_(this),
3231fbf9
SA
73 icon_red_(":/icons/status-red.svg"),
74 icon_green_(":/icons/status-green.svg"),
75 icon_grey_(":/icons/status-grey.svg")
d7bed479 76{
48257a69 77 qRegisterMetaType<util::Timestamp>("util::Timestamp");
85715407 78 qRegisterMetaType<uint64_t>("uint64_t");
48257a69 79
d0c0573b 80 GlobalSettings::add_change_handler(this);
8ad61f40 81
7d5425ef 82 setup_ui();
3ed18835 83 restore_ui_settings();
7d5425ef
JH
84}
85
47e9e7bb
SA
86MainWindow::~MainWindow()
87{
d0c0573b
SA
88 GlobalSettings::remove_change_handler(this);
89
3b84fd0b
SA
90 while (!sessions_.empty())
91 remove_session(sessions_.front());
47e9e7bb
SA
92}
93
f4e57597 94shared_ptr<views::ViewBase> MainWindow::get_active_view() const
168bd8ac
SA
95{
96 // If there's only one view, use it...
97 if (view_docks_.size() == 1)
98 return view_docks_.begin()->second;
99
100 // ...otherwise find the dock widget the widget with focus is contained in
101 QObject *w = QApplication::focusWidget();
13e475e4 102 QDockWidget *dock = nullptr;
168bd8ac
SA
103
104 while (w) {
c063290a
UH
105 dock = qobject_cast<QDockWidget*>(w);
106 if (dock)
107 break;
108 w = w->parent();
168bd8ac
SA
109 }
110
111 // Get the view contained in the dock widget
112 for (auto entry : view_docks_)
cbf7b5db 113 if (entry.first == dock)
168bd8ac
SA
114 return entry.second;
115
82f8a42b 116 return nullptr;
168bd8ac
SA
117}
118
f4e57597
SA
119shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
120 views::ViewType type, Session &session)
7cd2b5f3 121{
24c29d4f 122 GlobalSettings settings;
ddaded8b 123 shared_ptr<views::ViewBase> v;
24c29d4f 124
8d466c03 125 QMainWindow *main_window = nullptr;
3b84fd0b
SA
126 for (auto entry : session_windows_)
127 if (entry.first.get() == &session)
128 main_window = entry.second;
129
130 assert(main_window);
131
143d322d
SA
132 shared_ptr<MainBar> main_bar = session.main_bar();
133
ddaded8b
SA
134 QDockWidget* dock = new QDockWidget(title, main_window);
135 dock->setObjectName(title);
136 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
0f8f8c18 137
ddaded8b
SA
138 // Insert a QMainWindow into the dock widget to allow for a tool bar
139 QMainWindow *dock_main = new QMainWindow(dock);
140 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
7cd2b5f3 141
ddaded8b 142 if (type == views::ViewTypeTrace)
143d322d 143 // This view will be the main view if there's no main bar yet
f23c4692 144 v = make_shared<views::trace::View>(session,
143d322d 145 (main_bar ? false : true), dock_main);
36a8185e 146
ddaded8b
SA
147 if (!v)
148 return nullptr;
36a8185e 149
ddaded8b
SA
150 view_docks_[dock] = v;
151 session.register_view(v);
d6ab7b9a 152
ddaded8b
SA
153 dock_main->setCentralWidget(v.get());
154 dock->setWidget(dock_main);
7cd2b5f3 155
d375140b 156 dock->setContextMenuPolicy(Qt::PreventContextMenu);
ddaded8b
SA
157 dock->setFeatures(QDockWidget::DockWidgetMovable |
158 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
7cd2b5f3 159
ddaded8b
SA
160 QAbstractButton *close_btn =
161 dock->findChildren<QAbstractButton*>
162 ("qt_dockwidget_closebutton").front();
c9da5118 163
ddaded8b
SA
164 connect(close_btn, SIGNAL(clicked(bool)),
165 this, SLOT(on_view_close_clicked()));
dfe1bf82 166
7ea2a4ff 167 connect(&session, SIGNAL(trigger_event(int, util::Timestamp)),
ddaded8b 168 qobject_cast<views::ViewBase*>(v.get()),
7ea2a4ff 169 SLOT(trigger_event(int, util::Timestamp)));
d552c5c7 170
ddaded8b 171 if (type == views::ViewTypeTrace) {
f23c4692
SA
172 views::trace::View *tv =
173 qobject_cast<views::trace::View*>(v.get());
ddaded8b 174
ddaded8b 175 tv->enable_coloured_bg(settings.value(GlobalSettings::Key_View_ColouredBG).toBool());
051ba3b3 176 tv->enable_show_sampling_points(settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool());
8ad61f40 177 tv->enable_show_analog_minor_grid(settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool());
ddaded8b 178
ddaded8b
SA
179 if (!main_bar) {
180 /* Initial view, create the main bar */
181 main_bar = make_shared<MainBar>(session, this, tv);
182 dock_main->addToolBar(main_bar.get());
183 session.set_main_bar(main_bar);
184
185 connect(main_bar.get(), SIGNAL(new_view(Session*)),
186 this, SLOT(on_new_view(Session*)));
187
188 main_bar->action_view_show_cursors()->setChecked(tv->cursors_shown());
189
190 /* For the main view we need to prevent the dock widget from
191 * closing itself when its close button is clicked. This is
192 * so we can confirm with the user first. Regular views don't
193 * need this */
194 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
195 } else {
196 /* Additional view, create a standard bar */
197 pv::views::trace::StandardBar *standard_bar =
198 new pv::views::trace::StandardBar(session, this, tv);
199 dock_main->addToolBar(standard_bar);
200
201 standard_bar->action_view_show_cursors()->setChecked(tv->cursors_shown());
a55e7918 202 }
e93f5538
JH
203 }
204
ddaded8b 205 return v;
ed43ef2e
JH
206}
207
f30eb549
SA
208void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
209{
210 for (shared_ptr<Session> session : sessions_) {
211 if (!session->has_view(view))
212 continue;
213
214 // Find the dock the view is contained in and remove it
215 for (auto entry : view_docks_)
216 if (entry.second == view) {
217 // Remove the view from the session
218 session->deregister_view(view);
219
220 // Remove the view from its parent; otherwise, Qt will
221 // call deleteLater() on it, which causes a double free
222 // since the shared_ptr in view_docks_ doesn't know
223 // that Qt keeps a pointer to the view around
13e475e4 224 view->setParent(nullptr);
f30eb549
SA
225
226 // Delete the view's dock widget and all widgets inside it
227 entry.first->deleteLater();
228
229 // Remove the dock widget from the list and stop iterating
230 view_docks_.erase(entry.first);
231 break;
232 }
233 }
234}
235
101e7a9b
SA
236shared_ptr<Session> MainWindow::add_session()
237{
90d77e35 238 static int last_session_id = 1;
7b254679 239 QString name = tr("Session %1").arg(last_session_id++);
101e7a9b
SA
240
241 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
242
f4e57597
SA
243 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
244 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
33e1afbe
SA
245 connect(session.get(), SIGNAL(name_changed()),
246 this, SLOT(on_session_name_changed()));
3231fbf9
SA
247 session_state_mapper_.setMapping(session.get(), session.get());
248 connect(session.get(), SIGNAL(capture_state_changed(int)),
249 &session_state_mapper_, SLOT(map()));
3a21afa6 250
101e7a9b
SA
251 sessions_.push_back(session);
252
3b84fd0b
SA
253 QMainWindow *window = new QMainWindow();
254 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
255 session_windows_[session] = window;
085cd160
SA
256
257 int index = session_selector_.addTab(window, name);
258 session_selector_.setCurrentIndex(index);
33bedfc1 259 last_focused_session_ = session;
3b84fd0b 260
68ad932d
SA
261 window->setDockNestingEnabled(true);
262
f4e57597
SA
263 shared_ptr<views::ViewBase> main_view =
264 add_view(name, views::ViewTypeTrace, *session);
101e7a9b
SA
265
266 return session;
267}
268
36a8185e
SA
269void MainWindow::remove_session(shared_ptr<Session> session)
270{
ae1b6126 271 // Determine the height of the button before it collapses
cc964645
SA
272 int h = new_session_button_->height();
273
ae1b6126
SA
274 // Stop capture while the session still exists so that the UI can be
275 // updated in case we're currently running. If so, this will schedule a
276 // call to our on_capture_state_changed() slot for the next run of the
277 // event loop. We need to have this executed immediately or else it will
278 // be dismissed since the session object will be deleted by the time we
279 // leave this method and the event loop gets a chance to run again.
280 session->stop_capture();
281 QApplication::processEvents();
282
f30eb549
SA
283 for (shared_ptr<views::ViewBase> view : session->views())
284 remove_view(view);
36a8185e 285
3b84fd0b
SA
286 QMainWindow *window = session_windows_.at(session);
287 session_selector_.removeTab(session_selector_.indexOf(window));
288
289 session_windows_.erase(session);
290
33bedfc1
SA
291 if (last_focused_session_ == session)
292 last_focused_session_.reset();
293
ae1b6126 294 // Remove the session from our list of sessions (which also destroys it)
36a8185e
SA
295 sessions_.remove_if([&](shared_ptr<Session> s) {
296 return s == session; });
33e1afbe 297
cc964645
SA
298 if (sessions_.empty()) {
299 // When there are no more tabs, the height of the QTabWidget
300 // drops to zero. We must prevent this to keep the static
301 // widgets visible
302 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
303 w->setMinimumHeight(h);
304
305 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
306 static_tab_widget_->setMinimumHeight(h + 2 * margin);
307 session_selector_.setMinimumHeight(h + 2 * margin);
308
309 // Update the window title if there is no view left to
310 // generate focus change events
2ec81436 311 setWindowTitle(WindowTitle);
cc964645 312 }
36a8185e
SA
313}
314
3ed18835
SA
315void MainWindow::add_session_with_file(string open_file_name,
316 string open_file_format)
317{
318 shared_ptr<Session> session = add_session();
319 session->load_init_file(open_file_name, open_file_format);
320}
321
322void MainWindow::add_default_session()
323{
324 // Only add the default session if there would be no session otherwise
325 if (sessions_.size() > 0)
326 return;
327
328 shared_ptr<Session> session = add_session();
329
6e2a5b1d
GS
330 // Check the list of available devices. Prefer the one that was
331 // found with user supplied scan specs (if applicable). Then try
332 // one of the auto detected devices that are not the demo device.
333 // Pick demo in the absence of "genuine" hardware devices.
334 shared_ptr<devices::HardwareDevice> user_device, other_device, demo_device;
3ed18835 335 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
6e2a5b1d
GS
336 if (dev == device_manager_.user_spec_device()) {
337 user_device = dev;
338 } else if (dev->hardware_device()->driver()->name() == "demo") {
3ed18835
SA
339 demo_device = dev;
340 } else {
341 other_device = dev;
342 }
343 }
6e2a5b1d
GS
344 if (user_device)
345 session->select_device(user_device);
346 else if (other_device)
347 session->select_device(other_device);
348 else
349 session->select_device(demo_device);
3ed18835
SA
350}
351
352void MainWindow::save_sessions()
353{
354 QSettings settings;
355 int id = 0;
356
357 for (shared_ptr<Session> session : sessions_) {
358 // Ignore sessions using the demo device or no device at all
359 if (session->device()) {
360 shared_ptr<devices::HardwareDevice> device =
361 dynamic_pointer_cast< devices::HardwareDevice >
362 (session->device());
363
364 if (device &&
365 device->hardware_device()->driver()->name() == "demo")
366 continue;
367
368 settings.beginGroup("Session" + QString::number(id++));
369 settings.remove(""); // Remove all keys in this group
370 session->save_settings(settings);
371 settings.endGroup();
372 }
373 }
374
375 settings.setValue("sessions", id);
376}
377
378void MainWindow::restore_sessions()
379{
380 QSettings settings;
381 int i, session_count;
382
383 session_count = settings.value("sessions", 0).toInt();
384
385 for (i = 0; i < session_count; i++) {
386 settings.beginGroup("Session" + QString::number(i));
387 shared_ptr<Session> session = add_session();
388 session->restore_settings(settings);
389 settings.endGroup();
390 }
391}
392
d0c0573b
SA
393void MainWindow::on_setting_changed(const QString &key, const QVariant &value)
394{
395 if (key == GlobalSettings::Key_View_ColouredBG)
396 on_settingViewColouredBg_changed(value);
397
398 if (key == GlobalSettings::Key_View_ShowSamplingPoints)
399 on_settingViewShowSamplingPoints_changed(value);
400
401 if (key == GlobalSettings::Key_View_ShowAnalogMinorGrid)
402 on_settingViewShowAnalogMinorGrid_changed(value);
403}
404
7d5425ef
JH
405void MainWindow::setup_ui()
406{
407 setObjectName(QString::fromUtf8("MainWindow"));
408
3b84fd0b
SA
409 setCentralWidget(&session_selector_);
410
7d5425ef
JH
411 // Set the window icon
412 QIcon icon;
5ea53b3c 413 icon.addFile(QString(":/icons/pulseview.png"));
7d5425ef
JH
414 setWindowIcon(icon);
415
9eae6de4
PET
416 view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut()));
417 view_sticky_scrolling_shortcut_->setAutoRepeat(false);
ab1d13ee 418
051ba3b3
UH
419 view_show_sampling_points_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Period), this, SLOT(on_view_show_sampling_points_shortcut()));
420 view_show_sampling_points_shortcut_->setAutoRepeat(false);
421
8ad61f40
UH
422 view_show_analog_minor_grid_shortcut_ = new QShortcut(QKeySequence(Qt::Key_G), this, SLOT(on_view_show_analog_minor_grid_shortcut()));
423 view_show_analog_minor_grid_shortcut_->setAutoRepeat(false);
424
9eae6de4
PET
425 view_coloured_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_coloured_bg_shortcut()));
426 view_coloured_bg_shortcut_->setAutoRepeat(false);
0fb9d645 427
f1e2d26b
SA
428 // Set up the tab area
429 new_session_button_ = new QToolButton();
430 new_session_button_->setIcon(QIcon::fromTheme("document-new",
431 QIcon(":/icons/document-new.png")));
5f66b56e 432 new_session_button_->setToolTip(tr("Create New Session"));
f1e2d26b
SA
433 new_session_button_->setAutoRaise(true);
434
3231fbf9
SA
435 run_stop_button_ = new QToolButton();
436 run_stop_button_->setAutoRaise(true);
437 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
5f66b56e 438 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
3231fbf9 439
45cb64ff
PET
440 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
441 run_stop_shortcut_->setAutoRepeat(false);
442
4d1ec09a 443 settings_button_ = new QToolButton();
3432032f
UH
444 settings_button_->setIcon(QIcon::fromTheme("preferences-system",
445 QIcon(":/icons/preferences-system.png")));
5f66b56e 446 settings_button_->setToolTip(tr("Settings"));
4d1ec09a
SA
447 settings_button_->setAutoRaise(true);
448
449 QFrame *separator1 = new QFrame();
450 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
451 QFrame *separator2 = new QFrame();
452 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
baa8560e 453
f1e2d26b
SA
454 QHBoxLayout* layout = new QHBoxLayout();
455 layout->setContentsMargins(2, 2, 2, 2);
456 layout->addWidget(new_session_button_);
4d1ec09a 457 layout->addWidget(separator1);
3231fbf9 458 layout->addWidget(run_stop_button_);
4d1ec09a
SA
459 layout->addWidget(separator2);
460 layout->addWidget(settings_button_);
f1e2d26b 461
cc964645 462 static_tab_widget_ = new QWidget();
f1e2d26b
SA
463 static_tab_widget_->setLayout(layout);
464
465 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
4a4e20f5
SA
466 session_selector_.setTabsClosable(true);
467
763945bb
PET
468 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
469 close_application_shortcut_->setAutoRepeat(false);
470
471 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab()));
472
f1e2d26b
SA
473 connect(new_session_button_, SIGNAL(clicked(bool)),
474 this, SLOT(on_new_session_clicked()));
3231fbf9
SA
475 connect(run_stop_button_, SIGNAL(clicked(bool)),
476 this, SLOT(on_run_stop_clicked()));
477 connect(&session_state_mapper_, SIGNAL(mapped(QObject*)),
478 this, SLOT(on_capture_state_changed(QObject*)));
bf9f1268
SA
479 connect(settings_button_, SIGNAL(clicked(bool)),
480 this, SLOT(on_settings_clicked()));
f1e2d26b 481
4a4e20f5
SA
482 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
483 this, SLOT(on_tab_close_requested(int)));
e7aff437
SA
484 connect(&session_selector_, SIGNAL(currentChanged(int)),
485 this, SLOT(on_tab_changed(int)));
4a4e20f5 486
f1e2d26b 487
33e1afbe
SA
488 connect(static_cast<QApplication *>(QCoreApplication::instance()),
489 SIGNAL(focusChanged(QWidget*, QWidget*)),
490 this, SLOT(on_focus_changed()));
e3c79b07
JH
491}
492
93f683ad
SA
493void MainWindow::save_ui_settings()
494{
39eb0d45 495 QSettings settings;
6842b5fc 496
93f683ad
SA
497 settings.beginGroup("MainWindow");
498 settings.setValue("state", saveState());
499 settings.setValue("geometry", saveGeometry());
500 settings.endGroup();
501}
502
3ed18835 503void MainWindow::restore_ui_settings()
93f683ad 504{
39eb0d45 505 QSettings settings;
93f683ad
SA
506
507 settings.beginGroup("MainWindow");
508
509 if (settings.contains("geometry")) {
510 restoreGeometry(settings.value("geometry").toByteArray());
511 restoreState(settings.value("state").toByteArray());
512 } else
513 resize(1000, 720);
514
515 settings.endGroup();
516}
517
6f925ba9 518shared_ptr<Session> MainWindow::get_tab_session(int index) const
e7aff437
SA
519{
520 // Find the session that belongs to the tab's main window
521 for (auto entry : session_windows_)
522 if (entry.second == session_selector_.widget(index))
523 return entry.first;
524
525 return nullptr;
526}
527
e3c79b07
JH
528void MainWindow::closeEvent(QCloseEvent *event)
529{
5ccfc97e
SA
530 bool data_saved = true;
531
532 for (auto entry : session_windows_)
533 if (!entry.first->data_saved())
534 data_saved = false;
535
536 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
537 tr("There is unsaved data. Close anyway?"),
538 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
539 event->ignore();
540 } else {
541 save_ui_settings();
3ed18835 542 save_sessions();
5ccfc97e
SA
543 event->accept();
544 }
e3c79b07
JH
545}
546
d290e89f
SA
547QMenu* MainWindow::createPopupMenu()
548{
549 return nullptr;
550}
551
55547a45
SA
552bool MainWindow::restoreState(const QByteArray &state, int version)
553{
554 (void)state;
555 (void)version;
556
557 // Do nothing. We don't want Qt to handle this, or else it
558 // will try to restore all the dock widgets and create havoc.
559
560 return false;
561}
562
3231fbf9
SA
563void MainWindow::session_error(const QString text, const QString info_text)
564{
565 QMetaObject::invokeMethod(this, "show_session_error",
566 Qt::QueuedConnection, Q_ARG(QString, text),
567 Q_ARG(QString, info_text));
568}
569
570void MainWindow::show_session_error(const QString text, const QString info_text)
571{
572 QMessageBox msg(this);
573 msg.setText(text);
574 msg.setInformativeText(info_text);
575 msg.setStandardButtons(QMessageBox::Ok);
576 msg.setIcon(QMessageBox::Warning);
577 msg.exec();
578}
579
f4e57597 580void MainWindow::on_add_view(const QString &title, views::ViewType type,
3a21afa6
SA
581 Session *session)
582{
583 // We get a pointer and need a reference
6f925ba9 584 for (shared_ptr<Session> s : sessions_)
3a21afa6
SA
585 if (s.get() == session)
586 add_view(title, type, *s);
587}
588
33e1afbe
SA
589void MainWindow::on_focus_changed()
590{
e7aff437
SA
591 shared_ptr<views::ViewBase> view = get_active_view();
592
593 if (view) {
594 for (shared_ptr<Session> session : sessions_) {
595 if (session->has_view(view)) {
33bedfc1 596 if (session != last_focused_session_) {
e7aff437
SA
597 // Activate correct tab if necessary
598 shared_ptr<Session> tab_session = get_tab_session(
599 session_selector_.currentIndex());
600 if (tab_session != session)
601 session_selector_.setCurrentWidget(
602 session_windows_.at(session));
603
604 on_focused_session_changed(session);
605 }
606
e7aff437
SA
607 break;
608 }
609 }
33e1afbe
SA
610 }
611
e7aff437 612 if (sessions_.empty())
33e1afbe
SA
613 setWindowTitle(WindowTitle);
614}
615
e7aff437
SA
616void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
617{
33bedfc1
SA
618 last_focused_session_ = session;
619
e7aff437 620 setWindowTitle(session->name() + " - " + WindowTitle);
3231fbf9
SA
621
622 // Update the state of the run/stop button, too
623 on_capture_state_changed(session.get());
e7aff437
SA
624}
625
f1e2d26b 626void MainWindow::on_new_session_clicked()
c9da5118
SA
627{
628 add_session();
629}
630
3231fbf9
SA
631void MainWindow::on_run_stop_clicked()
632{
33bedfc1
SA
633 shared_ptr<Session> session = last_focused_session_;
634
635 if (!session)
636 return;
3231fbf9 637
33bedfc1 638 switch (session->get_capture_state()) {
3231fbf9 639 case Session::Stopped:
33bedfc1 640 session->start_capture([&](QString message) {
3231fbf9
SA
641 session_error("Capture failed", message); });
642 break;
643 case Session::AwaitingTrigger:
644 case Session::Running:
33bedfc1 645 session->stop_capture();
3231fbf9
SA
646 break;
647 }
648}
649
bf9f1268
SA
650void MainWindow::on_settings_clicked()
651{
4e4d72b2 652 dialogs::Settings dlg(device_manager_);
bf9f1268
SA
653 dlg.exec();
654}
655
33e1afbe
SA
656void MainWindow::on_session_name_changed()
657{
658 // Update the corresponding dock widget's name(s)
659 Session *session = qobject_cast<Session*>(QObject::sender());
660 assert(session);
661
f4e57597 662 for (shared_ptr<views::ViewBase> view : session->views()) {
33e1afbe
SA
663 // Get the dock that contains the view
664 for (auto entry : view_docks_)
665 if (entry.second == view) {
666 entry.first->setObjectName(session->name());
667 entry.first->setWindowTitle(session->name());
668 }
669 }
670
3cb15390
SA
671 // Update the tab widget by finding the main window and the tab from that
672 for (auto entry : session_windows_)
673 if (entry.first.get() == session) {
674 QMainWindow *window = entry.second;
675 const int index = session_selector_.indexOf(window);
676 session_selector_.setTabText(index, session->name());
677 }
678
33e1afbe 679 // Refresh window title if the affected session has focus
33bedfc1 680 if (session == last_focused_session_.get())
e7aff437 681 setWindowTitle(session->name() + " - " + WindowTitle);
33e1afbe
SA
682}
683
3231fbf9
SA
684void MainWindow::on_capture_state_changed(QObject *obj)
685{
686 Session *caller = qobject_cast<Session*>(obj);
687
688 // Ignore if caller is not the currently focused session
689 // unless there is only one session
33bedfc1
SA
690 if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
691 return;
3231fbf9
SA
692
693 int state = caller->get_capture_state();
694
695 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
696 run_stop_button_->setIcon(*icons[state]);
697 run_stop_button_->setText((state == pv::Session::Stopped) ?
698 tr("Run") : tr("Stop"));
699}
700
c9da5118
SA
701void MainWindow::on_new_view(Session *session)
702{
703 // We get a pointer and need a reference
6f925ba9 704 for (shared_ptr<Session> s : sessions_)
c9da5118 705 if (s.get() == session)
f4e57597 706 add_view(session->name(), views::ViewTypeTrace, *s);
c9da5118
SA
707}
708
36a8185e
SA
709void MainWindow::on_view_close_clicked()
710{
711 // Find the dock widget that contains the close button that was clicked
712 QObject *w = QObject::sender();
13e475e4 713 QDockWidget *dock = nullptr;
36a8185e
SA
714
715 while (w) {
716 dock = qobject_cast<QDockWidget*>(w);
717 if (dock)
718 break;
719 w = w->parent();
720 }
721
722 // Get the view contained in the dock widget
f4e57597 723 shared_ptr<views::ViewBase> view;
36a8185e
SA
724
725 for (auto entry : view_docks_)
cbf7b5db 726 if (entry.first == dock)
36a8185e
SA
727 view = entry.second;
728
729 // Deregister the view
730 for (shared_ptr<Session> session : sessions_) {
731 if (!session->has_view(view))
732 continue;
733
a2b9ac40 734 // Also destroy the entire session if its main view is closing...
36a8185e 735 if (view == session->main_view()) {
a2b9ac40
SA
736 // ...but only if data is saved or the user confirms closing
737 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
738 tr("This session contains unsaved data. Close it anyway?"),
739 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
740 remove_session(session);
36a8185e
SA
741 break;
742 } else
a2b9ac40 743 // All other views can be closed at any time as no data will be lost
f30eb549 744 remove_view(view);
36a8185e
SA
745 }
746}
747
e7aff437
SA
748void MainWindow::on_tab_changed(int index)
749{
750 shared_ptr<Session> session = get_tab_session(index);
751
752 if (session)
753 on_focused_session_changed(session);
754}
755
4a4e20f5
SA
756void MainWindow::on_tab_close_requested(int index)
757{
e7aff437
SA
758 shared_ptr<Session> session = get_tab_session(index);
759
d8d053b6
SA
760 if (!session)
761 return;
5ccfc97e
SA
762
763 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
764 tr("This session contains unsaved data. Close it anyway?"),
765 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
e7aff437 766 remove_session(session);
4a4e20f5
SA
767}
768
9eae6de4 769void MainWindow::on_view_coloured_bg_shortcut()
0fb9d645 770{
24c29d4f
SA
771 GlobalSettings settings;
772
773 bool state = settings.value(GlobalSettings::Key_View_ColouredBG).toBool();
774 settings.setValue(GlobalSettings::Key_View_ColouredBG, !state);
775}
776
87a97d8a
SA
777void MainWindow::on_view_sticky_scrolling_shortcut()
778{
779 GlobalSettings settings;
780
781 bool state = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool();
782 settings.setValue(GlobalSettings::Key_View_StickyScrolling, !state);
783}
784
051ba3b3
UH
785void MainWindow::on_view_show_sampling_points_shortcut()
786{
787 GlobalSettings settings;
788
789 bool state = settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool();
790 settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, !state);
791}
792
8ad61f40
UH
793void MainWindow::on_view_show_analog_minor_grid_shortcut()
794{
795 GlobalSettings settings;
796
797 bool state = settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool();
798 settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, !state);
799}
800
24c29d4f
SA
801void MainWindow::on_settingViewColouredBg_changed(const QVariant new_value)
802{
803 bool state = new_value.toBool();
804
805 for (auto entry : view_docks_) {
806 shared_ptr<views::ViewBase> viewbase = entry.second;
807
808 // Only trace views have this setting
f23c4692
SA
809 views::trace::View* view =
810 qobject_cast<views::trace::View*>(viewbase.get());
24c29d4f
SA
811 if (view)
812 view->enable_coloured_bg(state);
813 }
0fb9d645
SA
814}
815
051ba3b3
UH
816void MainWindow::on_settingViewShowSamplingPoints_changed(const QVariant new_value)
817{
818 bool state = new_value.toBool();
819
820 for (auto entry : view_docks_) {
821 shared_ptr<views::ViewBase> viewbase = entry.second;
822
823 // Only trace views have this setting
f23c4692
SA
824 views::trace::View* view =
825 qobject_cast<views::trace::View*>(viewbase.get());
051ba3b3
UH
826 if (view)
827 view->enable_show_sampling_points(state);
828 }
829}
830
8ad61f40
UH
831void MainWindow::on_settingViewShowAnalogMinorGrid_changed(const QVariant new_value)
832{
833 bool state = new_value.toBool();
834
835 for (auto entry : view_docks_) {
836 shared_ptr<views::ViewBase> viewbase = entry.second;
837
838 // Only trace views have this setting
f23c4692
SA
839 views::trace::View* view =
840 qobject_cast<views::trace::View*>(viewbase.get());
8ad61f40
UH
841 if (view)
842 view->enable_show_analog_minor_grid(state);
843 }
844}
845
763945bb
PET
846void MainWindow::on_close_current_tab()
847{
848 int tab = session_selector_.currentIndex();
849
850 on_tab_close_requested(tab);
851}
852
51e77110 853} // namespace pv