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