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