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