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