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