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