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