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