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