]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Session: Fix issue #67 by improving error handling
[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>
403c3e87 33#include <QDebug>
0f8f8c18 34#include <QDockWidget>
f1e2d26b 35#include <QHBoxLayout>
3231fbf9 36#include <QMessageBox>
643f65f9 37#include <QSettings>
45cb64ff 38#include <QShortcut>
aca9aa83 39#include <QWidget>
2953961c 40
2acdb232 41#include "mainwindow.hpp"
107ca6d3 42
0466001b 43#include "application.hpp"
2acdb232 44#include "devicemanager.hpp"
101e7a9b 45#include "devices/hardwaredevice.hpp"
bf9f1268 46#include "dialogs/settings.hpp"
aca9aa83 47#include "globalsettings.hpp"
7c657094 48#include "toolbars/mainbar.hpp"
aca9aa83 49#include "util.hpp"
1573bf16 50#include "views/trace/view.hpp"
e0ba4f6f 51#include "views/trace/standardbar.hpp"
d7bed479 52
1fa702cf
SA
53#ifdef ENABLE_DECODE
54#include "subwindows/decoder_selector/subwindow.hpp"
121307b3 55#include "views/decoder_binary/view.hpp"
24d69d27 56#include "views/tabular_decoder/view.hpp"
1fa702cf
SA
57#endif
58
fe3a1c21 59#include <libsigrokcxx/libsigrokcxx.hpp>
e82fd481 60
101e7a9b 61using std::dynamic_pointer_cast;
25272fee 62using std::make_shared;
f9abf97e 63using std::shared_ptr;
6842b5fc 64using std::string;
e8d00928 65
51e77110
JH
66namespace pv {
67
0f8f8c18 68using toolbars::MainBar;
643f65f9 69
33e1afbe
SA
70const QString MainWindow::WindowTitle = tr("PulseView");
71
3ed18835 72MainWindow::MainWindow(DeviceManager &device_manager, QWidget *parent) :
107ca6d3 73 QMainWindow(parent),
8dbbc7f0 74 device_manager_(device_manager),
3b84fd0b 75 session_selector_(this),
3231fbf9
SA
76 icon_red_(":/icons/status-red.svg"),
77 icon_green_(":/icons/status-green.svg"),
78 icon_grey_(":/icons/status-grey.svg")
d7bed479 79{
7d5425ef 80 setup_ui();
3ed18835 81 restore_ui_settings();
ae8dd875
VPP
82 connect(this, SIGNAL(session_error_raised(const QString, const QString)),
83 this, SLOT(on_session_error_raised(const QString, const QString)));
7d5425ef
JH
84}
85
47e9e7bb
SA
86MainWindow::~MainWindow()
87{
97378470
SA
88 // Make sure we no longer hold any shared pointers to widgets after the
89 // destructor finishes (goes for sessions and sub windows alike)
90
3b84fd0b
SA
91 while (!sessions_.empty())
92 remove_session(sessions_.front());
97378470
SA
93
94 sub_windows_.clear();
47e9e7bb
SA
95}
96
5e685656
SA
97void MainWindow::show_session_error(const QString text, const QString info_text)
98{
fe060a48 99 // TODO Emulate noquote()
1ed73ebd 100 qDebug() << "Notifying user of session error: " << text << "; " << info_text;
5e685656
SA
101
102 QMessageBox msg;
970fca0d 103 msg.setText(text + "\n\n" + info_text);
5e685656
SA
104 msg.setStandardButtons(QMessageBox::Ok);
105 msg.setIcon(QMessageBox::Warning);
106 msg.exec();
107}
108
f4e57597 109shared_ptr<views::ViewBase> MainWindow::get_active_view() const
168bd8ac
SA
110{
111 // If there's only one view, use it...
112 if (view_docks_.size() == 1)
113 return view_docks_.begin()->second;
114
115 // ...otherwise find the dock widget the widget with focus is contained in
116 QObject *w = QApplication::focusWidget();
13e475e4 117 QDockWidget *dock = nullptr;
168bd8ac
SA
118
119 while (w) {
c063290a
UH
120 dock = qobject_cast<QDockWidget*>(w);
121 if (dock)
122 break;
123 w = w->parent();
168bd8ac
SA
124 }
125
126 // Get the view contained in the dock widget
f4ab4b5c 127 for (auto& entry : view_docks_)
cbf7b5db 128 if (entry.first == dock)
168bd8ac
SA
129 return entry.second;
130
82f8a42b 131 return nullptr;
168bd8ac
SA
132}
133
baf867ed
SA
134shared_ptr<views::ViewBase> MainWindow::add_view(views::ViewType type,
135 Session &session)
7cd2b5f3 136{
24c29d4f 137 GlobalSettings settings;
ddaded8b 138 shared_ptr<views::ViewBase> v;
24c29d4f 139
8d466c03 140 QMainWindow *main_window = nullptr;
f4ab4b5c 141 for (auto& entry : session_windows_)
3b84fd0b
SA
142 if (entry.first.get() == &session)
143 main_window = entry.second;
144
145 assert(main_window);
146
143d322d
SA
147 shared_ptr<MainBar> main_bar = session.main_bar();
148
baf867ed
SA
149 // Only use the view type in the name if it's not the main view
150 QString title;
151 if (main_bar)
feda6c6b 152 title = QString("%1 (%2)").arg(session.name(), views::ViewTypeNames[type]);
baf867ed
SA
153 else
154 title = session.name();
155
ddaded8b
SA
156 QDockWidget* dock = new QDockWidget(title, main_window);
157 dock->setObjectName(title);
158 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
0f8f8c18 159
ddaded8b
SA
160 // Insert a QMainWindow into the dock widget to allow for a tool bar
161 QMainWindow *dock_main = new QMainWindow(dock);
162 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
7cd2b5f3 163
ddaded8b 164 if (type == views::ViewTypeTrace)
143d322d 165 // This view will be the main view if there's no main bar yet
baf867ed 166 v = make_shared<views::trace::View>(session, (main_bar ? false : true), dock_main);
8ba6f8b7 167#ifdef ENABLE_DECODE
121307b3
SA
168 if (type == views::ViewTypeDecoderBinary)
169 v = make_shared<views::decoder_binary::View>(session, false, dock_main);
24d69d27
SA
170 if (type == views::ViewTypeTabularDecoder)
171 v = make_shared<views::tabular_decoder::View>(session, false, dock_main);
8ba6f8b7 172#endif
36a8185e 173
ddaded8b
SA
174 if (!v)
175 return nullptr;
36a8185e 176
ddaded8b
SA
177 view_docks_[dock] = v;
178 session.register_view(v);
d6ab7b9a 179
ddaded8b
SA
180 dock_main->setCentralWidget(v.get());
181 dock->setWidget(dock_main);
7cd2b5f3 182
d375140b 183 dock->setContextMenuPolicy(Qt::PreventContextMenu);
ddaded8b
SA
184 dock->setFeatures(QDockWidget::DockWidgetMovable |
185 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
7cd2b5f3 186
ddaded8b 187 QAbstractButton *close_btn =
7a0d99e6
SA
188 dock->findChildren<QAbstractButton*>("qt_dockwidget_closebutton") // clazy:exclude=detaching-temporary
189 .front();
c9da5118 190
ddaded8b
SA
191 connect(close_btn, SIGNAL(clicked(bool)),
192 this, SLOT(on_view_close_clicked()));
dfe1bf82 193
7ea2a4ff 194 connect(&session, SIGNAL(trigger_event(int, util::Timestamp)),
ddaded8b 195 qobject_cast<views::ViewBase*>(v.get()),
7ea2a4ff 196 SLOT(trigger_event(int, util::Timestamp)));
d552c5c7 197
ae8dd875
VPP
198 connect(&session, SIGNAL(session_error_raised(const QString, const QString)),
199 this, SLOT(on_session_error_raised(const QString, const QString)));
200
ddaded8b 201 if (type == views::ViewTypeTrace) {
f23c4692
SA
202 views::trace::View *tv =
203 qobject_cast<views::trace::View*>(v.get());
ddaded8b 204
ddaded8b
SA
205 if (!main_bar) {
206 /* Initial view, create the main bar */
207 main_bar = make_shared<MainBar>(session, this, tv);
208 dock_main->addToolBar(main_bar.get());
209 session.set_main_bar(main_bar);
210
baf867ed
SA
211 connect(main_bar.get(), SIGNAL(new_view(Session*, int)),
212 this, SLOT(on_new_view(Session*, int)));
97378470
SA
213 connect(main_bar.get(), SIGNAL(show_decoder_selector(Session*)),
214 this, SLOT(on_show_decoder_selector(Session*)));
ddaded8b
SA
215
216 main_bar->action_view_show_cursors()->setChecked(tv->cursors_shown());
217
218 /* For the main view we need to prevent the dock widget from
219 * closing itself when its close button is clicked. This is
220 * so we can confirm with the user first. Regular views don't
221 * need this */
222 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
223 } else {
224 /* Additional view, create a standard bar */
225 pv::views::trace::StandardBar *standard_bar =
226 new pv::views::trace::StandardBar(session, this, tv);
227 dock_main->addToolBar(standard_bar);
228
229 standard_bar->action_view_show_cursors()->setChecked(tv->cursors_shown());
a55e7918 230 }
e93f5538
JH
231 }
232
3d6ff1cd
SA
233 v->setFocus();
234
ddaded8b 235 return v;
ed43ef2e
JH
236}
237
f30eb549
SA
238void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
239{
240 for (shared_ptr<Session> session : sessions_) {
241 if (!session->has_view(view))
242 continue;
243
244 // Find the dock the view is contained in and remove it
f4ab4b5c 245 for (auto& entry : view_docks_)
f30eb549
SA
246 if (entry.second == view) {
247 // Remove the view from the session
248 session->deregister_view(view);
249
250 // Remove the view from its parent; otherwise, Qt will
251 // call deleteLater() on it, which causes a double free
252 // since the shared_ptr in view_docks_ doesn't know
253 // that Qt keeps a pointer to the view around
13e475e4 254 view->setParent(nullptr);
f30eb549
SA
255
256 // Delete the view's dock widget and all widgets inside it
257 entry.first->deleteLater();
258
259 // Remove the dock widget from the list and stop iterating
260 view_docks_.erase(entry.first);
261 break;
262 }
263 }
264}
265
97378470
SA
266shared_ptr<subwindows::SubWindowBase> MainWindow::add_subwindow(
267 subwindows::SubWindowType type, Session &session)
268{
269 GlobalSettings settings;
3d6ff1cd 270 shared_ptr<subwindows::SubWindowBase> w;
97378470
SA
271
272 QMainWindow *main_window = nullptr;
1fd847fe 273 for (auto& entry : session_windows_)
97378470
SA
274 if (entry.first.get() == &session)
275 main_window = entry.second;
276
277 assert(main_window);
278
279 QString title = "";
280
281 switch (type) {
1fa702cf 282#ifdef ENABLE_DECODE
97378470
SA
283 case subwindows::SubWindowTypeDecoderSelector:
284 title = tr("Decoder Selector");
1fa702cf
SA
285 break;
286#endif
287 default:
288 break;
97378470
SA
289 }
290
291 QDockWidget* dock = new QDockWidget(title, main_window);
292 dock->setObjectName(title);
293 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
294
295 // Insert a QMainWindow into the dock widget to allow for a tool bar
296 QMainWindow *dock_main = new QMainWindow(dock);
297 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
298
1fa702cf 299#ifdef ENABLE_DECODE
97378470 300 if (type == subwindows::SubWindowTypeDecoderSelector)
3d6ff1cd 301 w = make_shared<subwindows::decoder_selector::SubWindow>(session, dock_main);
1fa702cf 302#endif
97378470 303
3d6ff1cd 304 if (!w)
97378470
SA
305 return nullptr;
306
3d6ff1cd
SA
307 sub_windows_[dock] = w;
308 dock_main->setCentralWidget(w.get());
97378470
SA
309 dock->setWidget(dock_main);
310
311 dock->setContextMenuPolicy(Qt::PreventContextMenu);
312 dock->setFeatures(QDockWidget::DockWidgetMovable |
313 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
314
315 QAbstractButton *close_btn =
1fd847fe 316 dock->findChildren<QAbstractButton*> // clazy:exclude=detaching-temporary
97378470
SA
317 ("qt_dockwidget_closebutton").front();
318
6b8b739d
UH
319 // Allow all subwindows to be closed via ESC.
320 close_btn->setShortcut(QKeySequence(Qt::Key_Escape));
321
97378470
SA
322 connect(close_btn, SIGNAL(clicked(bool)),
323 this, SLOT(on_sub_window_close_clicked()));
324
3d6ff1cd
SA
325 if (w->has_toolbar())
326 dock_main->addToolBar(w->create_toolbar(dock_main));
97378470 327
3d6ff1cd
SA
328 if (w->minimum_width() > 0)
329 dock->setMinimumSize(w->minimum_width(), 0);
c52493c9 330
3d6ff1cd 331 return w;
97378470
SA
332}
333
101e7a9b
SA
334shared_ptr<Session> MainWindow::add_session()
335{
90d77e35 336 static int last_session_id = 1;
7b254679 337 QString name = tr("Session %1").arg(last_session_id++);
101e7a9b
SA
338
339 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
340
2c5b0f46
SA
341 connect(session.get(), SIGNAL(add_view(ViewType, Session*)),
342 this, SLOT(on_add_view(ViewType, Session*)));
33e1afbe
SA
343 connect(session.get(), SIGNAL(name_changed()),
344 this, SLOT(on_session_name_changed()));
31d1aca6
SA
345 connect(session.get(), SIGNAL(device_changed()),
346 this, SLOT(on_session_device_changed()));
3231fbf9 347 connect(session.get(), SIGNAL(capture_state_changed(int)),
f1f3fa71 348 this, SLOT(on_session_capture_state_changed(int)));
3a21afa6 349
101e7a9b
SA
350 sessions_.push_back(session);
351
3b84fd0b
SA
352 QMainWindow *window = new QMainWindow();
353 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
354 session_windows_[session] = window;
085cd160
SA
355
356 int index = session_selector_.addTab(window, name);
357 session_selector_.setCurrentIndex(index);
33bedfc1 358 last_focused_session_ = session;
3b84fd0b 359
68ad932d
SA
360 window->setDockNestingEnabled(true);
361
3d6ff1cd 362 add_view(views::ViewTypeTrace, *session);
101e7a9b
SA
363
364 return session;
365}
366
36a8185e
SA
367void MainWindow::remove_session(shared_ptr<Session> session)
368{
ae1b6126 369 // Determine the height of the button before it collapses
cc964645
SA
370 int h = new_session_button_->height();
371
ae1b6126
SA
372 // Stop capture while the session still exists so that the UI can be
373 // updated in case we're currently running. If so, this will schedule a
374 // call to our on_capture_state_changed() slot for the next run of the
375 // event loop. We need to have this executed immediately or else it will
376 // be dismissed since the session object will be deleted by the time we
377 // leave this method and the event loop gets a chance to run again.
378 session->stop_capture();
379 QApplication::processEvents();
380
f4ab4b5c 381 for (const shared_ptr<views::ViewBase>& view : session->views())
f30eb549 382 remove_view(view);
36a8185e 383
3b84fd0b
SA
384 QMainWindow *window = session_windows_.at(session);
385 session_selector_.removeTab(session_selector_.indexOf(window));
386
387 session_windows_.erase(session);
388
33bedfc1
SA
389 if (last_focused_session_ == session)
390 last_focused_session_.reset();
391
ae1b6126 392 // Remove the session from our list of sessions (which also destroys it)
36a8185e
SA
393 sessions_.remove_if([&](shared_ptr<Session> s) {
394 return s == session; });
33e1afbe 395
cc964645
SA
396 if (sessions_.empty()) {
397 // When there are no more tabs, the height of the QTabWidget
398 // drops to zero. We must prevent this to keep the static
399 // widgets visible
7a0d99e6
SA
400 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>()) // clazy:exclude=range-loop
401 w->setMinimumHeight(h);
cc964645
SA
402
403 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
404 static_tab_widget_->setMinimumHeight(h + 2 * margin);
405 session_selector_.setMinimumHeight(h + 2 * margin);
406
407 // Update the window title if there is no view left to
408 // generate focus change events
2ec81436 409 setWindowTitle(WindowTitle);
cc964645 410 }
36a8185e
SA
411}
412
3ed18835 413void MainWindow::add_session_with_file(string open_file_name,
96dbf014 414 string open_file_format, string open_setup_file_name)
3ed18835
SA
415{
416 shared_ptr<Session> session = add_session();
96dbf014 417 session->load_init_file(open_file_name, open_file_format, open_setup_file_name);
3ed18835
SA
418}
419
420void MainWindow::add_default_session()
421{
422 // Only add the default session if there would be no session otherwise
423 if (sessions_.size() > 0)
424 return;
425
426 shared_ptr<Session> session = add_session();
427
6e2a5b1d
GS
428 // Check the list of available devices. Prefer the one that was
429 // found with user supplied scan specs (if applicable). Then try
430 // one of the auto detected devices that are not the demo device.
431 // Pick demo in the absence of "genuine" hardware devices.
432 shared_ptr<devices::HardwareDevice> user_device, other_device, demo_device;
f4ab4b5c 433 for (const shared_ptr<devices::HardwareDevice>& dev : device_manager_.devices()) {
6e2a5b1d
GS
434 if (dev == device_manager_.user_spec_device()) {
435 user_device = dev;
436 } else if (dev->hardware_device()->driver()->name() == "demo") {
3ed18835
SA
437 demo_device = dev;
438 } else {
439 other_device = dev;
440 }
441 }
6e2a5b1d
GS
442 if (user_device)
443 session->select_device(user_device);
444 else if (other_device)
445 session->select_device(other_device);
446 else
447 session->select_device(demo_device);
3ed18835
SA
448}
449
450void MainWindow::save_sessions()
451{
452 QSettings settings;
453 int id = 0;
454
f4ab4b5c 455 for (shared_ptr<Session>& session : sessions_) {
3ed18835
SA
456 // Ignore sessions using the demo device or no device at all
457 if (session->device()) {
458 shared_ptr<devices::HardwareDevice> device =
459 dynamic_pointer_cast< devices::HardwareDevice >
460 (session->device());
461
462 if (device &&
463 device->hardware_device()->driver()->name() == "demo")
464 continue;
465
466 settings.beginGroup("Session" + QString::number(id++));
467 settings.remove(""); // Remove all keys in this group
468 session->save_settings(settings);
469 settings.endGroup();
470 }
471 }
472
473 settings.setValue("sessions", id);
474}
475
476void MainWindow::restore_sessions()
477{
478 QSettings settings;
479 int i, session_count;
480
481 session_count = settings.value("sessions", 0).toInt();
482
483 for (i = 0; i < session_count; i++) {
484 settings.beginGroup("Session" + QString::number(i));
485 shared_ptr<Session> session = add_session();
486 session->restore_settings(settings);
487 settings.endGroup();
488 }
489}
490
7d5425ef
JH
491void MainWindow::setup_ui()
492{
493 setObjectName(QString::fromUtf8("MainWindow"));
494
3b84fd0b
SA
495 setCentralWidget(&session_selector_);
496
7d5425ef
JH
497 // Set the window icon
498 QIcon icon;
5ea53b3c 499 icon.addFile(QString(":/icons/pulseview.png"));
7d5425ef
JH
500 setWindowIcon(icon);
501
476ca5f4 502 // Set up keyboard shortcuts that affect all views at once
9eae6de4
PET
503 view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut()));
504 view_sticky_scrolling_shortcut_->setAutoRepeat(false);
ab1d13ee 505
051ba3b3
UH
506 view_show_sampling_points_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Period), this, SLOT(on_view_show_sampling_points_shortcut()));
507 view_show_sampling_points_shortcut_->setAutoRepeat(false);
508
8ad61f40
UH
509 view_show_analog_minor_grid_shortcut_ = new QShortcut(QKeySequence(Qt::Key_G), this, SLOT(on_view_show_analog_minor_grid_shortcut()));
510 view_show_analog_minor_grid_shortcut_->setAutoRepeat(false);
511
641574bc
SA
512 view_colored_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_colored_bg_shortcut()));
513 view_colored_bg_shortcut_->setAutoRepeat(false);
0fb9d645 514
f1e2d26b
SA
515 // Set up the tab area
516 new_session_button_ = new QToolButton();
517 new_session_button_->setIcon(QIcon::fromTheme("document-new",
518 QIcon(":/icons/document-new.png")));
5f66b56e 519 new_session_button_->setToolTip(tr("Create New Session"));
f1e2d26b
SA
520 new_session_button_->setAutoRaise(true);
521
3231fbf9
SA
522 run_stop_button_ = new QToolButton();
523 run_stop_button_->setAutoRaise(true);
524 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
5f66b56e 525 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
3231fbf9 526
45cb64ff
PET
527 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
528 run_stop_shortcut_->setAutoRepeat(false);
529
4d1ec09a 530 settings_button_ = new QToolButton();
3432032f
UH
531 settings_button_->setIcon(QIcon::fromTheme("preferences-system",
532 QIcon(":/icons/preferences-system.png")));
5f66b56e 533 settings_button_->setToolTip(tr("Settings"));
4d1ec09a
SA
534 settings_button_->setAutoRaise(true);
535
536 QFrame *separator1 = new QFrame();
537 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
538 QFrame *separator2 = new QFrame();
539 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
baa8560e 540
f1e2d26b
SA
541 QHBoxLayout* layout = new QHBoxLayout();
542 layout->setContentsMargins(2, 2, 2, 2);
543 layout->addWidget(new_session_button_);
4d1ec09a 544 layout->addWidget(separator1);
3231fbf9 545 layout->addWidget(run_stop_button_);
4d1ec09a
SA
546 layout->addWidget(separator2);
547 layout->addWidget(settings_button_);
f1e2d26b 548
cc964645 549 static_tab_widget_ = new QWidget();
f1e2d26b
SA
550 static_tab_widget_->setLayout(layout);
551
552 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
4a4e20f5
SA
553 session_selector_.setTabsClosable(true);
554
1ed73ebd
VPP
555#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
556 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this, SLOT(close()));
557 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), this, SLOT(on_close_current_tab()));
558#else
763945bb 559 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
763945bb 560 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab()));
1ed73ebd
VPP
561#endif
562 close_application_shortcut_->setAutoRepeat(false);
763945bb 563
f1e2d26b
SA
564 connect(new_session_button_, SIGNAL(clicked(bool)),
565 this, SLOT(on_new_session_clicked()));
3231fbf9
SA
566 connect(run_stop_button_, SIGNAL(clicked(bool)),
567 this, SLOT(on_run_stop_clicked()));
bf9f1268
SA
568 connect(settings_button_, SIGNAL(clicked(bool)),
569 this, SLOT(on_settings_clicked()));
f1e2d26b 570
4a4e20f5
SA
571 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
572 this, SLOT(on_tab_close_requested(int)));
e7aff437
SA
573 connect(&session_selector_, SIGNAL(currentChanged(int)),
574 this, SLOT(on_tab_changed(int)));
4a4e20f5 575
f1e2d26b 576
33e1afbe
SA
577 connect(static_cast<QApplication *>(QCoreApplication::instance()),
578 SIGNAL(focusChanged(QWidget*, QWidget*)),
579 this, SLOT(on_focus_changed()));
e3c79b07
JH
580}
581
31d1aca6
SA
582void MainWindow::update_acq_button(Session *session)
583{
09ab7056
SA
584 int state;
585 QString run_caption;
31d1aca6 586
09ab7056
SA
587 if (session) {
588 state = session->get_capture_state();
589 run_caption = session->using_file_device() ? tr("Reload") : tr("Run");
590 } else {
591 state = Session::Stopped;
592 run_caption = tr("Run");
593 }
31d1aca6
SA
594
595 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
596 run_stop_button_->setIcon(*icons[state]);
597 run_stop_button_->setText((state == pv::Session::Stopped) ?
598 run_caption : tr("Stop"));
599}
600
93f683ad
SA
601void MainWindow::save_ui_settings()
602{
39eb0d45 603 QSettings settings;
6842b5fc 604
93f683ad
SA
605 settings.beginGroup("MainWindow");
606 settings.setValue("state", saveState());
607 settings.setValue("geometry", saveGeometry());
608 settings.endGroup();
609}
610
3ed18835 611void MainWindow::restore_ui_settings()
93f683ad 612{
39eb0d45 613 QSettings settings;
93f683ad
SA
614
615 settings.beginGroup("MainWindow");
616
617 if (settings.contains("geometry")) {
618 restoreGeometry(settings.value("geometry").toByteArray());
619 restoreState(settings.value("state").toByteArray());
620 } else
621 resize(1000, 720);
622
623 settings.endGroup();
624}
625
6f925ba9 626shared_ptr<Session> MainWindow::get_tab_session(int index) const
e7aff437
SA
627{
628 // Find the session that belongs to the tab's main window
f4ab4b5c 629 for (auto& entry : session_windows_)
e7aff437
SA
630 if (entry.second == session_selector_.widget(index))
631 return entry.first;
632
633 return nullptr;
634}
635
e3c79b07
JH
636void MainWindow::closeEvent(QCloseEvent *event)
637{
5ccfc97e
SA
638 bool data_saved = true;
639
f4ab4b5c 640 for (auto& entry : session_windows_)
5ccfc97e
SA
641 if (!entry.first->data_saved())
642 data_saved = false;
643
644 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
645 tr("There is unsaved data. Close anyway?"),
646 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
647 event->ignore();
648 } else {
649 save_ui_settings();
3ed18835 650 save_sessions();
5ccfc97e
SA
651 event->accept();
652 }
e3c79b07
JH
653}
654
d290e89f
SA
655QMenu* MainWindow::createPopupMenu()
656{
657 return nullptr;
658}
659
55547a45
SA
660bool MainWindow::restoreState(const QByteArray &state, int version)
661{
662 (void)state;
663 (void)version;
664
665 // Do nothing. We don't want Qt to handle this, or else it
666 // will try to restore all the dock widgets and create havoc.
667
668 return false;
669}
670
ab10caf9
SA
671void MainWindow::on_run_stop_clicked()
672{
5d58e6ce
SA
673 GlobalSettings settings;
674 bool all_sessions = settings.value(GlobalSettings::Key_General_StartAllSessions).toBool();
ab10caf9 675
5d58e6ce
SA
676 if (all_sessions)
677 {
678 vector< shared_ptr<Session> > hw_sessions;
679
680 // Make a list of all sessions where a hardware device is used
8fa8412e 681 for (const shared_ptr<Session>& s : sessions_) {
5d58e6ce
SA
682 shared_ptr<devices::HardwareDevice> hw_device =
683 dynamic_pointer_cast< devices::HardwareDevice >(s->device());
684 if (!hw_device)
685 continue;
686 hw_sessions.push_back(s);
687 }
688
689 // Stop all acquisitions if there are any running ones, start all otherwise
690 bool any_running = any_of(hw_sessions.begin(), hw_sessions.end(),
691 [](const shared_ptr<Session> &s)
692 { return (s->get_capture_state() == Session::AwaitingTrigger) ||
693 (s->get_capture_state() == Session::Running); });
694
695 for (shared_ptr<Session> s : hw_sessions)
696 if (any_running)
697 s->stop_capture();
698 else
ae8dd875 699 s->start_capture([&](QString message) {Q_EMIT session_error_raised("Capture failed", message);});
5d58e6ce
SA
700 } else {
701
702 shared_ptr<Session> session = last_focused_session_;
703
704 if (!session)
705 return;
ab10caf9 706
5d58e6ce
SA
707 switch (session->get_capture_state()) {
708 case Session::Stopped:
ae8dd875 709 session->start_capture([&](QString message) {Q_EMIT session_error_raised("Capture failed", message);});
5d58e6ce
SA
710 break;
711 case Session::AwaitingTrigger:
712 case Session::Running:
713 session->stop_capture();
714 break;
715 }
ab10caf9
SA
716 }
717}
718
baf867ed 719void MainWindow::on_add_view(views::ViewType type, Session *session)
3a21afa6
SA
720{
721 // We get a pointer and need a reference
f4ab4b5c 722 for (shared_ptr<Session>& s : sessions_)
3a21afa6 723 if (s.get() == session)
baf867ed 724 add_view(type, *s);
3a21afa6
SA
725}
726
33e1afbe
SA
727void MainWindow::on_focus_changed()
728{
e7aff437
SA
729 shared_ptr<views::ViewBase> view = get_active_view();
730
731 if (view) {
732 for (shared_ptr<Session> session : sessions_) {
733 if (session->has_view(view)) {
33bedfc1 734 if (session != last_focused_session_) {
e7aff437
SA
735 // Activate correct tab if necessary
736 shared_ptr<Session> tab_session = get_tab_session(
737 session_selector_.currentIndex());
738 if (tab_session != session)
739 session_selector_.setCurrentWidget(
740 session_windows_.at(session));
741
742 on_focused_session_changed(session);
743 }
744
e7aff437
SA
745 break;
746 }
747 }
33e1afbe
SA
748 }
749
e7aff437 750 if (sessions_.empty())
33e1afbe
SA
751 setWindowTitle(WindowTitle);
752}
753
e7aff437
SA
754void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
755{
33bedfc1
SA
756 last_focused_session_ = session;
757
e7aff437 758 setWindowTitle(session->name() + " - " + WindowTitle);
3231fbf9
SA
759
760 // Update the state of the run/stop button, too
f1f3fa71 761 update_acq_button(session.get());
e7aff437
SA
762}
763
f1e2d26b 764void MainWindow::on_new_session_clicked()
c9da5118
SA
765{
766 add_session();
767}
768
bf9f1268
SA
769void MainWindow::on_settings_clicked()
770{
4e4d72b2 771 dialogs::Settings dlg(device_manager_);
bf9f1268
SA
772 dlg.exec();
773}
774
33e1afbe
SA
775void MainWindow::on_session_name_changed()
776{
777 // Update the corresponding dock widget's name(s)
778 Session *session = qobject_cast<Session*>(QObject::sender());
779 assert(session);
780
f4ab4b5c 781 for (const shared_ptr<views::ViewBase>& view : session->views()) {
33e1afbe 782 // Get the dock that contains the view
f4ab4b5c 783 for (auto& entry : view_docks_)
33e1afbe
SA
784 if (entry.second == view) {
785 entry.first->setObjectName(session->name());
786 entry.first->setWindowTitle(session->name());
787 }
788 }
789
3cb15390 790 // Update the tab widget by finding the main window and the tab from that
f4ab4b5c 791 for (auto& entry : session_windows_)
3cb15390
SA
792 if (entry.first.get() == session) {
793 QMainWindow *window = entry.second;
794 const int index = session_selector_.indexOf(window);
795 session_selector_.setTabText(index, session->name());
796 }
797
33e1afbe 798 // Refresh window title if the affected session has focus
33bedfc1 799 if (session == last_focused_session_.get())
e7aff437 800 setWindowTitle(session->name() + " - " + WindowTitle);
33e1afbe
SA
801}
802
31d1aca6
SA
803void MainWindow::on_session_device_changed()
804{
805 Session *session = qobject_cast<Session*>(QObject::sender());
806 assert(session);
807
808 // Ignore if caller is not the currently focused session
809 // unless there is only one session
810 if ((sessions_.size() > 1) && (session != last_focused_session_.get()))
811 return;
812
813 update_acq_button(session);
814}
815
f1f3fa71 816void MainWindow::on_session_capture_state_changed(int state)
3231fbf9 817{
f1f3fa71
SA
818 (void)state;
819
820 Session *session = qobject_cast<Session*>(QObject::sender());
821 assert(session);
3231fbf9
SA
822
823 // Ignore if caller is not the currently focused session
824 // unless there is only one session
f1f3fa71 825 if ((sessions_.size() > 1) && (session != last_focused_session_.get()))
33bedfc1 826 return;
3231fbf9 827
f1f3fa71 828 update_acq_button(session);
3231fbf9
SA
829}
830
baf867ed 831void MainWindow::on_new_view(Session *session, int view_type)
c9da5118
SA
832{
833 // We get a pointer and need a reference
f4ab4b5c 834 for (shared_ptr<Session>& s : sessions_)
c9da5118 835 if (s.get() == session)
baf867ed 836 add_view((views::ViewType)view_type, *s);
c9da5118
SA
837}
838
36a8185e
SA
839void MainWindow::on_view_close_clicked()
840{
841 // Find the dock widget that contains the close button that was clicked
842 QObject *w = QObject::sender();
13e475e4 843 QDockWidget *dock = nullptr;
36a8185e
SA
844
845 while (w) {
846 dock = qobject_cast<QDockWidget*>(w);
847 if (dock)
848 break;
849 w = w->parent();
850 }
851
852 // Get the view contained in the dock widget
f4e57597 853 shared_ptr<views::ViewBase> view;
36a8185e 854
f4ab4b5c 855 for (auto& entry : view_docks_)
cbf7b5db 856 if (entry.first == dock)
36a8185e
SA
857 view = entry.second;
858
859 // Deregister the view
860 for (shared_ptr<Session> session : sessions_) {
861 if (!session->has_view(view))
862 continue;
863
a2b9ac40 864 // Also destroy the entire session if its main view is closing...
36a8185e 865 if (view == session->main_view()) {
a2b9ac40
SA
866 // ...but only if data is saved or the user confirms closing
867 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
868 tr("This session contains unsaved data. Close it anyway?"),
869 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
870 remove_session(session);
36a8185e
SA
871 break;
872 } else
a2b9ac40 873 // All other views can be closed at any time as no data will be lost
f30eb549 874 remove_view(view);
36a8185e
SA
875 }
876}
877
e7aff437
SA
878void MainWindow::on_tab_changed(int index)
879{
880 shared_ptr<Session> session = get_tab_session(index);
881
882 if (session)
883 on_focused_session_changed(session);
884}
885
4a4e20f5
SA
886void MainWindow::on_tab_close_requested(int index)
887{
e7aff437
SA
888 shared_ptr<Session> session = get_tab_session(index);
889
d8d053b6
SA
890 if (!session)
891 return;
5ccfc97e
SA
892
893 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
894 tr("This session contains unsaved data. Close it anyway?"),
895 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
e7aff437 896 remove_session(session);
09ab7056
SA
897
898 if (sessions_.empty())
899 update_acq_button(nullptr);
4a4e20f5
SA
900}
901
97378470
SA
902void MainWindow::on_show_decoder_selector(Session *session)
903{
1fa702cf 904#ifdef ENABLE_DECODE
97378470 905 // Close dock widget if it's already showing and return
1fd847fe 906 for (auto& entry : sub_windows_) {
97378470 907 QDockWidget* dock = entry.first;
ac91f7ad
SA
908 shared_ptr<subwindows::SubWindowBase> decoder_selector =
909 dynamic_pointer_cast<subwindows::decoder_selector::SubWindow>(entry.second);
910
911 if (decoder_selector && (&decoder_selector->session() == session)) {
97378470
SA
912 sub_windows_.erase(dock);
913 dock->close();
914 return;
915 }
916 }
917
918 // We get a pointer and need a reference
1fd847fe 919 for (shared_ptr<Session>& s : sessions_)
97378470
SA
920 if (s.get() == session)
921 add_subwindow(subwindows::SubWindowTypeDecoderSelector, *s);
5f46ec7a
SA
922#else
923 (void)session;
1fa702cf 924#endif
97378470
SA
925}
926
927void MainWindow::on_sub_window_close_clicked()
928{
929 // Find the dock widget that contains the close button that was clicked
930 QObject *w = QObject::sender();
931 QDockWidget *dock = nullptr;
932
933 while (w) {
934 dock = qobject_cast<QDockWidget*>(w);
935 if (dock)
936 break;
937 w = w->parent();
938 }
939
940 sub_windows_.erase(dock);
941 dock->close();
3d6ff1cd
SA
942
943 // Restore focus to the last used main view
944 if (last_focused_session_)
945 last_focused_session_->main_view()->setFocus();
97378470
SA
946}
947
641574bc 948void MainWindow::on_view_colored_bg_shortcut()
0fb9d645 949{
24c29d4f
SA
950 GlobalSettings settings;
951
641574bc
SA
952 bool state = settings.value(GlobalSettings::Key_View_ColoredBG).toBool();
953 settings.setValue(GlobalSettings::Key_View_ColoredBG, !state);
24c29d4f
SA
954}
955
87a97d8a
SA
956void MainWindow::on_view_sticky_scrolling_shortcut()
957{
958 GlobalSettings settings;
959
960 bool state = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool();
961 settings.setValue(GlobalSettings::Key_View_StickyScrolling, !state);
962}
963
051ba3b3
UH
964void MainWindow::on_view_show_sampling_points_shortcut()
965{
966 GlobalSettings settings;
967
968 bool state = settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool();
969 settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, !state);
970}
971
8ad61f40
UH
972void MainWindow::on_view_show_analog_minor_grid_shortcut()
973{
974 GlobalSettings settings;
975
976 bool state = settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool();
977 settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, !state);
978}
979
763945bb
PET
980void MainWindow::on_close_current_tab()
981{
982 int tab = session_selector_.currentIndex();
983
984 on_tab_close_requested(tab);
985}
986
ae8dd875
VPP
987void MainWindow::on_session_error_raised(const QString text, const QString info_text) {
988 MainWindow::show_session_error(text, info_text);
989}
990
51e77110 991} // namespace pv