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