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