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