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