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