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