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