]>
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 | ||
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) | |
149 | title = QString("%1 (%2)").arg(session.name()).arg(views::ViewTypeNames[type]); | |
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 | ||
309 | connect(close_btn, SIGNAL(clicked(bool)), | |
310 | this, SLOT(on_sub_window_close_clicked())); | |
311 | ||
312 | if (v->has_toolbar()) | |
313 | dock_main->addToolBar(v->create_toolbar(dock_main)); | |
314 | ||
c52493c9 SA |
315 | if (v->minimum_width() > 0) |
316 | dock->setMinimumSize(v->minimum_width(), 0); | |
317 | ||
97378470 SA |
318 | return v; |
319 | } | |
320 | ||
101e7a9b SA |
321 | shared_ptr<Session> MainWindow::add_session() |
322 | { | |
90d77e35 | 323 | static int last_session_id = 1; |
7b254679 | 324 | QString name = tr("Session %1").arg(last_session_id++); |
101e7a9b SA |
325 | |
326 | shared_ptr<Session> session = make_shared<Session>(device_manager_, name); | |
327 | ||
baf867ed SA |
328 | connect(session.get(), SIGNAL(add_view(views::ViewType, Session*)), |
329 | this, SLOT(on_add_view(views::ViewType, Session*))); | |
33e1afbe SA |
330 | connect(session.get(), SIGNAL(name_changed()), |
331 | this, SLOT(on_session_name_changed())); | |
3231fbf9 SA |
332 | session_state_mapper_.setMapping(session.get(), session.get()); |
333 | connect(session.get(), SIGNAL(capture_state_changed(int)), | |
334 | &session_state_mapper_, SLOT(map())); | |
3a21afa6 | 335 | |
101e7a9b SA |
336 | sessions_.push_back(session); |
337 | ||
3b84fd0b SA |
338 | QMainWindow *window = new QMainWindow(); |
339 | window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag | |
340 | session_windows_[session] = window; | |
085cd160 SA |
341 | |
342 | int index = session_selector_.addTab(window, name); | |
343 | session_selector_.setCurrentIndex(index); | |
33bedfc1 | 344 | last_focused_session_ = session; |
3b84fd0b | 345 | |
68ad932d SA |
346 | window->setDockNestingEnabled(true); |
347 | ||
baf867ed | 348 | shared_ptr<views::ViewBase> main_view = add_view(views::ViewTypeTrace, *session); |
101e7a9b SA |
349 | |
350 | return session; | |
351 | } | |
352 | ||
36a8185e SA |
353 | void MainWindow::remove_session(shared_ptr<Session> session) |
354 | { | |
ae1b6126 | 355 | // Determine the height of the button before it collapses |
cc964645 SA |
356 | int h = new_session_button_->height(); |
357 | ||
ae1b6126 SA |
358 | // Stop capture while the session still exists so that the UI can be |
359 | // updated in case we're currently running. If so, this will schedule a | |
360 | // call to our on_capture_state_changed() slot for the next run of the | |
361 | // event loop. We need to have this executed immediately or else it will | |
362 | // be dismissed since the session object will be deleted by the time we | |
363 | // leave this method and the event loop gets a chance to run again. | |
364 | session->stop_capture(); | |
365 | QApplication::processEvents(); | |
366 | ||
f4ab4b5c | 367 | for (const shared_ptr<views::ViewBase>& view : session->views()) |
f30eb549 | 368 | remove_view(view); |
36a8185e | 369 | |
3b84fd0b SA |
370 | QMainWindow *window = session_windows_.at(session); |
371 | session_selector_.removeTab(session_selector_.indexOf(window)); | |
372 | ||
373 | session_windows_.erase(session); | |
374 | ||
33bedfc1 SA |
375 | if (last_focused_session_ == session) |
376 | last_focused_session_.reset(); | |
377 | ||
ae1b6126 | 378 | // Remove the session from our list of sessions (which also destroys it) |
36a8185e SA |
379 | sessions_.remove_if([&](shared_ptr<Session> s) { |
380 | return s == session; }); | |
33e1afbe | 381 | |
cc964645 SA |
382 | if (sessions_.empty()) { |
383 | // When there are no more tabs, the height of the QTabWidget | |
384 | // drops to zero. We must prevent this to keep the static | |
385 | // widgets visible | |
7a0d99e6 SA |
386 | for (QWidget *w : static_tab_widget_->findChildren<QWidget*>()) // clazy:exclude=range-loop |
387 | w->setMinimumHeight(h); | |
cc964645 SA |
388 | |
389 | int margin = static_tab_widget_->layout()->contentsMargins().bottom(); | |
390 | static_tab_widget_->setMinimumHeight(h + 2 * margin); | |
391 | session_selector_.setMinimumHeight(h + 2 * margin); | |
392 | ||
393 | // Update the window title if there is no view left to | |
394 | // generate focus change events | |
2ec81436 | 395 | setWindowTitle(WindowTitle); |
cc964645 | 396 | } |
36a8185e SA |
397 | } |
398 | ||
3ed18835 | 399 | void MainWindow::add_session_with_file(string open_file_name, |
96dbf014 | 400 | string open_file_format, string open_setup_file_name) |
3ed18835 SA |
401 | { |
402 | shared_ptr<Session> session = add_session(); | |
96dbf014 | 403 | session->load_init_file(open_file_name, open_file_format, open_setup_file_name); |
3ed18835 SA |
404 | } |
405 | ||
406 | void MainWindow::add_default_session() | |
407 | { | |
408 | // Only add the default session if there would be no session otherwise | |
409 | if (sessions_.size() > 0) | |
410 | return; | |
411 | ||
412 | shared_ptr<Session> session = add_session(); | |
413 | ||
6e2a5b1d GS |
414 | // Check the list of available devices. Prefer the one that was |
415 | // found with user supplied scan specs (if applicable). Then try | |
416 | // one of the auto detected devices that are not the demo device. | |
417 | // Pick demo in the absence of "genuine" hardware devices. | |
418 | shared_ptr<devices::HardwareDevice> user_device, other_device, demo_device; | |
f4ab4b5c | 419 | for (const shared_ptr<devices::HardwareDevice>& dev : device_manager_.devices()) { |
6e2a5b1d GS |
420 | if (dev == device_manager_.user_spec_device()) { |
421 | user_device = dev; | |
422 | } else if (dev->hardware_device()->driver()->name() == "demo") { | |
3ed18835 SA |
423 | demo_device = dev; |
424 | } else { | |
425 | other_device = dev; | |
426 | } | |
427 | } | |
6e2a5b1d GS |
428 | if (user_device) |
429 | session->select_device(user_device); | |
430 | else if (other_device) | |
431 | session->select_device(other_device); | |
432 | else | |
433 | session->select_device(demo_device); | |
3ed18835 SA |
434 | } |
435 | ||
436 | void MainWindow::save_sessions() | |
437 | { | |
438 | QSettings settings; | |
439 | int id = 0; | |
440 | ||
f4ab4b5c | 441 | for (shared_ptr<Session>& session : sessions_) { |
3ed18835 SA |
442 | // Ignore sessions using the demo device or no device at all |
443 | if (session->device()) { | |
444 | shared_ptr<devices::HardwareDevice> device = | |
445 | dynamic_pointer_cast< devices::HardwareDevice > | |
446 | (session->device()); | |
447 | ||
448 | if (device && | |
449 | device->hardware_device()->driver()->name() == "demo") | |
450 | continue; | |
451 | ||
452 | settings.beginGroup("Session" + QString::number(id++)); | |
453 | settings.remove(""); // Remove all keys in this group | |
454 | session->save_settings(settings); | |
455 | settings.endGroup(); | |
456 | } | |
457 | } | |
458 | ||
459 | settings.setValue("sessions", id); | |
460 | } | |
461 | ||
462 | void MainWindow::restore_sessions() | |
463 | { | |
464 | QSettings settings; | |
465 | int i, session_count; | |
466 | ||
467 | session_count = settings.value("sessions", 0).toInt(); | |
468 | ||
469 | for (i = 0; i < session_count; i++) { | |
470 | settings.beginGroup("Session" + QString::number(i)); | |
471 | shared_ptr<Session> session = add_session(); | |
472 | session->restore_settings(settings); | |
473 | settings.endGroup(); | |
474 | } | |
475 | } | |
476 | ||
7d5425ef JH |
477 | void MainWindow::setup_ui() |
478 | { | |
479 | setObjectName(QString::fromUtf8("MainWindow")); | |
480 | ||
3b84fd0b SA |
481 | setCentralWidget(&session_selector_); |
482 | ||
7d5425ef JH |
483 | // Set the window icon |
484 | QIcon icon; | |
5ea53b3c | 485 | icon.addFile(QString(":/icons/pulseview.png")); |
7d5425ef JH |
486 | setWindowIcon(icon); |
487 | ||
476ca5f4 | 488 | // Set up keyboard shortcuts that affect all views at once |
9eae6de4 PET |
489 | view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut())); |
490 | view_sticky_scrolling_shortcut_->setAutoRepeat(false); | |
ab1d13ee | 491 | |
051ba3b3 UH |
492 | view_show_sampling_points_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Period), this, SLOT(on_view_show_sampling_points_shortcut())); |
493 | view_show_sampling_points_shortcut_->setAutoRepeat(false); | |
494 | ||
8ad61f40 UH |
495 | view_show_analog_minor_grid_shortcut_ = new QShortcut(QKeySequence(Qt::Key_G), this, SLOT(on_view_show_analog_minor_grid_shortcut())); |
496 | view_show_analog_minor_grid_shortcut_->setAutoRepeat(false); | |
497 | ||
641574bc SA |
498 | view_colored_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_colored_bg_shortcut())); |
499 | view_colored_bg_shortcut_->setAutoRepeat(false); | |
0fb9d645 | 500 | |
f1e2d26b SA |
501 | // Set up the tab area |
502 | new_session_button_ = new QToolButton(); | |
503 | new_session_button_->setIcon(QIcon::fromTheme("document-new", | |
504 | QIcon(":/icons/document-new.png"))); | |
5f66b56e | 505 | new_session_button_->setToolTip(tr("Create New Session")); |
f1e2d26b SA |
506 | new_session_button_->setAutoRaise(true); |
507 | ||
3231fbf9 SA |
508 | run_stop_button_ = new QToolButton(); |
509 | run_stop_button_->setAutoRaise(true); | |
510 | run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
5f66b56e | 511 | run_stop_button_->setToolTip(tr("Start/Stop Acquisition")); |
3231fbf9 | 512 | |
45cb64ff PET |
513 | run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click())); |
514 | run_stop_shortcut_->setAutoRepeat(false); | |
515 | ||
4d1ec09a | 516 | settings_button_ = new QToolButton(); |
3432032f UH |
517 | settings_button_->setIcon(QIcon::fromTheme("preferences-system", |
518 | QIcon(":/icons/preferences-system.png"))); | |
5f66b56e | 519 | settings_button_->setToolTip(tr("Settings")); |
4d1ec09a SA |
520 | settings_button_->setAutoRaise(true); |
521 | ||
522 | QFrame *separator1 = new QFrame(); | |
523 | separator1->setFrameStyle(QFrame::VLine | QFrame::Raised); | |
524 | QFrame *separator2 = new QFrame(); | |
525 | separator2->setFrameStyle(QFrame::VLine | QFrame::Raised); | |
baa8560e | 526 | |
f1e2d26b SA |
527 | QHBoxLayout* layout = new QHBoxLayout(); |
528 | layout->setContentsMargins(2, 2, 2, 2); | |
529 | layout->addWidget(new_session_button_); | |
4d1ec09a | 530 | layout->addWidget(separator1); |
3231fbf9 | 531 | layout->addWidget(run_stop_button_); |
4d1ec09a SA |
532 | layout->addWidget(separator2); |
533 | layout->addWidget(settings_button_); | |
f1e2d26b | 534 | |
cc964645 | 535 | static_tab_widget_ = new QWidget(); |
f1e2d26b SA |
536 | static_tab_widget_->setLayout(layout); |
537 | ||
538 | session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner); | |
4a4e20f5 SA |
539 | session_selector_.setTabsClosable(true); |
540 | ||
763945bb PET |
541 | close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close())); |
542 | close_application_shortcut_->setAutoRepeat(false); | |
543 | ||
544 | close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab())); | |
545 | ||
f1e2d26b SA |
546 | connect(new_session_button_, SIGNAL(clicked(bool)), |
547 | this, SLOT(on_new_session_clicked())); | |
3231fbf9 SA |
548 | connect(run_stop_button_, SIGNAL(clicked(bool)), |
549 | this, SLOT(on_run_stop_clicked())); | |
550 | connect(&session_state_mapper_, SIGNAL(mapped(QObject*)), | |
551 | this, SLOT(on_capture_state_changed(QObject*))); | |
bf9f1268 SA |
552 | connect(settings_button_, SIGNAL(clicked(bool)), |
553 | this, SLOT(on_settings_clicked())); | |
f1e2d26b | 554 | |
4a4e20f5 SA |
555 | connect(&session_selector_, SIGNAL(tabCloseRequested(int)), |
556 | this, SLOT(on_tab_close_requested(int))); | |
e7aff437 SA |
557 | connect(&session_selector_, SIGNAL(currentChanged(int)), |
558 | this, SLOT(on_tab_changed(int))); | |
4a4e20f5 | 559 | |
f1e2d26b | 560 | |
33e1afbe SA |
561 | connect(static_cast<QApplication *>(QCoreApplication::instance()), |
562 | SIGNAL(focusChanged(QWidget*, QWidget*)), | |
563 | this, SLOT(on_focus_changed())); | |
e3c79b07 JH |
564 | } |
565 | ||
93f683ad SA |
566 | void MainWindow::save_ui_settings() |
567 | { | |
39eb0d45 | 568 | QSettings settings; |
6842b5fc | 569 | |
93f683ad SA |
570 | settings.beginGroup("MainWindow"); |
571 | settings.setValue("state", saveState()); | |
572 | settings.setValue("geometry", saveGeometry()); | |
573 | settings.endGroup(); | |
574 | } | |
575 | ||
3ed18835 | 576 | void MainWindow::restore_ui_settings() |
93f683ad | 577 | { |
39eb0d45 | 578 | QSettings settings; |
93f683ad SA |
579 | |
580 | settings.beginGroup("MainWindow"); | |
581 | ||
582 | if (settings.contains("geometry")) { | |
583 | restoreGeometry(settings.value("geometry").toByteArray()); | |
584 | restoreState(settings.value("state").toByteArray()); | |
585 | } else | |
586 | resize(1000, 720); | |
587 | ||
588 | settings.endGroup(); | |
589 | } | |
590 | ||
6f925ba9 | 591 | shared_ptr<Session> MainWindow::get_tab_session(int index) const |
e7aff437 SA |
592 | { |
593 | // Find the session that belongs to the tab's main window | |
f4ab4b5c | 594 | for (auto& entry : session_windows_) |
e7aff437 SA |
595 | if (entry.second == session_selector_.widget(index)) |
596 | return entry.first; | |
597 | ||
598 | return nullptr; | |
599 | } | |
600 | ||
e3c79b07 JH |
601 | void MainWindow::closeEvent(QCloseEvent *event) |
602 | { | |
5ccfc97e SA |
603 | bool data_saved = true; |
604 | ||
f4ab4b5c | 605 | for (auto& entry : session_windows_) |
5ccfc97e SA |
606 | if (!entry.first->data_saved()) |
607 | data_saved = false; | |
608 | ||
609 | if (!data_saved && (QMessageBox::question(this, tr("Confirmation"), | |
610 | tr("There is unsaved data. Close anyway?"), | |
611 | QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) { | |
612 | event->ignore(); | |
613 | } else { | |
614 | save_ui_settings(); | |
3ed18835 | 615 | save_sessions(); |
5ccfc97e SA |
616 | event->accept(); |
617 | } | |
e3c79b07 JH |
618 | } |
619 | ||
d290e89f SA |
620 | QMenu* MainWindow::createPopupMenu() |
621 | { | |
622 | return nullptr; | |
623 | } | |
624 | ||
55547a45 SA |
625 | bool MainWindow::restoreState(const QByteArray &state, int version) |
626 | { | |
627 | (void)state; | |
628 | (void)version; | |
629 | ||
630 | // Do nothing. We don't want Qt to handle this, or else it | |
631 | // will try to restore all the dock widgets and create havoc. | |
632 | ||
633 | return false; | |
634 | } | |
635 | ||
baf867ed | 636 | void MainWindow::on_add_view(views::ViewType type, Session *session) |
3a21afa6 SA |
637 | { |
638 | // We get a pointer and need a reference | |
f4ab4b5c | 639 | for (shared_ptr<Session>& s : sessions_) |
3a21afa6 | 640 | if (s.get() == session) |
baf867ed | 641 | add_view(type, *s); |
3a21afa6 SA |
642 | } |
643 | ||
33e1afbe SA |
644 | void MainWindow::on_focus_changed() |
645 | { | |
e7aff437 SA |
646 | shared_ptr<views::ViewBase> view = get_active_view(); |
647 | ||
648 | if (view) { | |
649 | for (shared_ptr<Session> session : sessions_) { | |
650 | if (session->has_view(view)) { | |
33bedfc1 | 651 | if (session != last_focused_session_) { |
e7aff437 SA |
652 | // Activate correct tab if necessary |
653 | shared_ptr<Session> tab_session = get_tab_session( | |
654 | session_selector_.currentIndex()); | |
655 | if (tab_session != session) | |
656 | session_selector_.setCurrentWidget( | |
657 | session_windows_.at(session)); | |
658 | ||
659 | on_focused_session_changed(session); | |
660 | } | |
661 | ||
e7aff437 SA |
662 | break; |
663 | } | |
664 | } | |
33e1afbe SA |
665 | } |
666 | ||
e7aff437 | 667 | if (sessions_.empty()) |
33e1afbe SA |
668 | setWindowTitle(WindowTitle); |
669 | } | |
670 | ||
e7aff437 SA |
671 | void MainWindow::on_focused_session_changed(shared_ptr<Session> session) |
672 | { | |
33bedfc1 SA |
673 | last_focused_session_ = session; |
674 | ||
e7aff437 | 675 | setWindowTitle(session->name() + " - " + WindowTitle); |
3231fbf9 SA |
676 | |
677 | // Update the state of the run/stop button, too | |
678 | on_capture_state_changed(session.get()); | |
e7aff437 SA |
679 | } |
680 | ||
f1e2d26b | 681 | void MainWindow::on_new_session_clicked() |
c9da5118 SA |
682 | { |
683 | add_session(); | |
684 | } | |
685 | ||
3231fbf9 SA |
686 | void MainWindow::on_run_stop_clicked() |
687 | { | |
33bedfc1 SA |
688 | shared_ptr<Session> session = last_focused_session_; |
689 | ||
690 | if (!session) | |
691 | return; | |
3231fbf9 | 692 | |
33bedfc1 | 693 | switch (session->get_capture_state()) { |
3231fbf9 | 694 | case Session::Stopped: |
33bedfc1 | 695 | session->start_capture([&](QString message) { |
5e685656 | 696 | show_session_error("Capture failed", message); }); |
3231fbf9 SA |
697 | break; |
698 | case Session::AwaitingTrigger: | |
699 | case Session::Running: | |
33bedfc1 | 700 | session->stop_capture(); |
3231fbf9 SA |
701 | break; |
702 | } | |
703 | } | |
704 | ||
bf9f1268 SA |
705 | void MainWindow::on_settings_clicked() |
706 | { | |
4e4d72b2 | 707 | dialogs::Settings dlg(device_manager_); |
bf9f1268 SA |
708 | dlg.exec(); |
709 | } | |
710 | ||
33e1afbe SA |
711 | void MainWindow::on_session_name_changed() |
712 | { | |
713 | // Update the corresponding dock widget's name(s) | |
714 | Session *session = qobject_cast<Session*>(QObject::sender()); | |
715 | assert(session); | |
716 | ||
f4ab4b5c | 717 | for (const shared_ptr<views::ViewBase>& view : session->views()) { |
33e1afbe | 718 | // Get the dock that contains the view |
f4ab4b5c | 719 | for (auto& entry : view_docks_) |
33e1afbe SA |
720 | if (entry.second == view) { |
721 | entry.first->setObjectName(session->name()); | |
722 | entry.first->setWindowTitle(session->name()); | |
723 | } | |
724 | } | |
725 | ||
3cb15390 | 726 | // Update the tab widget by finding the main window and the tab from that |
f4ab4b5c | 727 | for (auto& entry : session_windows_) |
3cb15390 SA |
728 | if (entry.first.get() == session) { |
729 | QMainWindow *window = entry.second; | |
730 | const int index = session_selector_.indexOf(window); | |
731 | session_selector_.setTabText(index, session->name()); | |
732 | } | |
733 | ||
33e1afbe | 734 | // Refresh window title if the affected session has focus |
33bedfc1 | 735 | if (session == last_focused_session_.get()) |
e7aff437 | 736 | setWindowTitle(session->name() + " - " + WindowTitle); |
33e1afbe SA |
737 | } |
738 | ||
3231fbf9 SA |
739 | void MainWindow::on_capture_state_changed(QObject *obj) |
740 | { | |
741 | Session *caller = qobject_cast<Session*>(obj); | |
742 | ||
743 | // Ignore if caller is not the currently focused session | |
744 | // unless there is only one session | |
33bedfc1 SA |
745 | if ((sessions_.size() > 1) && (caller != last_focused_session_.get())) |
746 | return; | |
3231fbf9 SA |
747 | |
748 | int state = caller->get_capture_state(); | |
749 | ||
750 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; | |
751 | run_stop_button_->setIcon(*icons[state]); | |
752 | run_stop_button_->setText((state == pv::Session::Stopped) ? | |
753 | tr("Run") : tr("Stop")); | |
754 | } | |
755 | ||
baf867ed | 756 | void MainWindow::on_new_view(Session *session, int view_type) |
c9da5118 SA |
757 | { |
758 | // We get a pointer and need a reference | |
f4ab4b5c | 759 | for (shared_ptr<Session>& s : sessions_) |
c9da5118 | 760 | if (s.get() == session) |
baf867ed | 761 | add_view((views::ViewType)view_type, *s); |
c9da5118 SA |
762 | } |
763 | ||
36a8185e SA |
764 | void MainWindow::on_view_close_clicked() |
765 | { | |
766 | // Find the dock widget that contains the close button that was clicked | |
767 | QObject *w = QObject::sender(); | |
13e475e4 | 768 | QDockWidget *dock = nullptr; |
36a8185e SA |
769 | |
770 | while (w) { | |
771 | dock = qobject_cast<QDockWidget*>(w); | |
772 | if (dock) | |
773 | break; | |
774 | w = w->parent(); | |
775 | } | |
776 | ||
777 | // Get the view contained in the dock widget | |
f4e57597 | 778 | shared_ptr<views::ViewBase> view; |
36a8185e | 779 | |
f4ab4b5c | 780 | for (auto& entry : view_docks_) |
cbf7b5db | 781 | if (entry.first == dock) |
36a8185e SA |
782 | view = entry.second; |
783 | ||
784 | // Deregister the view | |
785 | for (shared_ptr<Session> session : sessions_) { | |
786 | if (!session->has_view(view)) | |
787 | continue; | |
788 | ||
a2b9ac40 | 789 | // Also destroy the entire session if its main view is closing... |
36a8185e | 790 | if (view == session->main_view()) { |
a2b9ac40 SA |
791 | // ...but only if data is saved or the user confirms closing |
792 | if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"), | |
793 | tr("This session contains unsaved data. Close it anyway?"), | |
794 | QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)) | |
795 | remove_session(session); | |
36a8185e SA |
796 | break; |
797 | } else | |
a2b9ac40 | 798 | // All other views can be closed at any time as no data will be lost |
f30eb549 | 799 | remove_view(view); |
36a8185e SA |
800 | } |
801 | } | |
802 | ||
e7aff437 SA |
803 | void MainWindow::on_tab_changed(int index) |
804 | { | |
805 | shared_ptr<Session> session = get_tab_session(index); | |
806 | ||
807 | if (session) | |
808 | on_focused_session_changed(session); | |
809 | } | |
810 | ||
4a4e20f5 SA |
811 | void MainWindow::on_tab_close_requested(int index) |
812 | { | |
e7aff437 SA |
813 | shared_ptr<Session> session = get_tab_session(index); |
814 | ||
d8d053b6 SA |
815 | if (!session) |
816 | return; | |
5ccfc97e SA |
817 | |
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)) | |
e7aff437 | 821 | remove_session(session); |
4a4e20f5 SA |
822 | } |
823 | ||
97378470 SA |
824 | void MainWindow::on_show_decoder_selector(Session *session) |
825 | { | |
1fa702cf | 826 | #ifdef ENABLE_DECODE |
97378470 | 827 | // Close dock widget if it's already showing and return |
1fd847fe | 828 | for (auto& entry : sub_windows_) { |
97378470 | 829 | QDockWidget* dock = entry.first; |
ac91f7ad SA |
830 | shared_ptr<subwindows::SubWindowBase> decoder_selector = |
831 | dynamic_pointer_cast<subwindows::decoder_selector::SubWindow>(entry.second); | |
832 | ||
833 | if (decoder_selector && (&decoder_selector->session() == session)) { | |
97378470 SA |
834 | sub_windows_.erase(dock); |
835 | dock->close(); | |
836 | return; | |
837 | } | |
838 | } | |
839 | ||
840 | // We get a pointer and need a reference | |
1fd847fe | 841 | for (shared_ptr<Session>& s : sessions_) |
97378470 SA |
842 | if (s.get() == session) |
843 | add_subwindow(subwindows::SubWindowTypeDecoderSelector, *s); | |
1fa702cf | 844 | #endif |
97378470 SA |
845 | } |
846 | ||
847 | void MainWindow::on_sub_window_close_clicked() | |
848 | { | |
849 | // Find the dock widget that contains the close button that was clicked | |
850 | QObject *w = QObject::sender(); | |
851 | QDockWidget *dock = nullptr; | |
852 | ||
853 | while (w) { | |
854 | dock = qobject_cast<QDockWidget*>(w); | |
855 | if (dock) | |
856 | break; | |
857 | w = w->parent(); | |
858 | } | |
859 | ||
860 | sub_windows_.erase(dock); | |
861 | dock->close(); | |
862 | } | |
863 | ||
641574bc | 864 | void MainWindow::on_view_colored_bg_shortcut() |
0fb9d645 | 865 | { |
24c29d4f SA |
866 | GlobalSettings settings; |
867 | ||
641574bc SA |
868 | bool state = settings.value(GlobalSettings::Key_View_ColoredBG).toBool(); |
869 | settings.setValue(GlobalSettings::Key_View_ColoredBG, !state); | |
24c29d4f SA |
870 | } |
871 | ||
87a97d8a SA |
872 | void MainWindow::on_view_sticky_scrolling_shortcut() |
873 | { | |
874 | GlobalSettings settings; | |
875 | ||
876 | bool state = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool(); | |
877 | settings.setValue(GlobalSettings::Key_View_StickyScrolling, !state); | |
878 | } | |
879 | ||
051ba3b3 UH |
880 | void MainWindow::on_view_show_sampling_points_shortcut() |
881 | { | |
882 | GlobalSettings settings; | |
883 | ||
884 | bool state = settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool(); | |
885 | settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, !state); | |
886 | } | |
887 | ||
8ad61f40 UH |
888 | void MainWindow::on_view_show_analog_minor_grid_shortcut() |
889 | { | |
890 | GlobalSettings settings; | |
891 | ||
892 | bool state = settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool(); | |
893 | settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, !state); | |
894 | } | |
895 | ||
763945bb PET |
896 | void MainWindow::on_close_current_tab() |
897 | { | |
898 | int tab = session_selector_.currentIndex(); | |
899 | ||
900 | on_tab_close_requested(tab); | |
901 | } | |
902 | ||
51e77110 | 903 | } // namespace pv |