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