]>
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 | { |
d0c0573b | 78 | GlobalSettings::add_change_handler(this); |
8ad61f40 | 79 | |
7d5425ef | 80 | setup_ui(); |
3ed18835 | 81 | restore_ui_settings(); |
7d5425ef JH |
82 | } |
83 | ||
47e9e7bb SA |
84 | MainWindow::~MainWindow() |
85 | { | |
d0c0573b SA |
86 | GlobalSettings::remove_change_handler(this); |
87 | ||
3b84fd0b SA |
88 | while (!sessions_.empty()) |
89 | remove_session(sessions_.front()); | |
47e9e7bb SA |
90 | } |
91 | ||
5e685656 SA |
92 | void MainWindow::show_session_error(const QString text, const QString info_text) |
93 | { | |
fe060a48 SA |
94 | // TODO Emulate noquote() |
95 | qDebug() << "Notifying user of session error:" << info_text; | |
5e685656 SA |
96 | |
97 | QMessageBox msg; | |
970fca0d | 98 | msg.setText(text + "\n\n" + info_text); |
5e685656 SA |
99 | msg.setStandardButtons(QMessageBox::Ok); |
100 | msg.setIcon(QMessageBox::Warning); | |
101 | msg.exec(); | |
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 | |
f4ab4b5c | 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; |
f4ab4b5c | 136 | for (auto& entry : session_windows_) |
3b84fd0b SA |
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 | 170 | QAbstractButton *close_btn = |
7a0d99e6 SA |
171 | dock->findChildren<QAbstractButton*>("qt_dockwidget_closebutton") // clazy:exclude=detaching-temporary |
172 | .front(); | |
c9da5118 | 173 | |
ddaded8b SA |
174 | connect(close_btn, SIGNAL(clicked(bool)), |
175 | this, SLOT(on_view_close_clicked())); | |
dfe1bf82 | 176 | |
7ea2a4ff | 177 | connect(&session, SIGNAL(trigger_event(int, util::Timestamp)), |
ddaded8b | 178 | qobject_cast<views::ViewBase*>(v.get()), |
7ea2a4ff | 179 | SLOT(trigger_event(int, 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 | |
641574bc | 185 | tv->enable_colored_bg(settings.value(GlobalSettings::Key_View_ColoredBG).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 | |
f4ab4b5c | 225 | for (auto& entry : view_docks_) |
f30eb549 SA |
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 | ||
f4ab4b5c | 293 | for (const shared_ptr<views::ViewBase>& view : session->views()) |
f30eb549 | 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 | |
7a0d99e6 SA |
312 | for (QWidget *w : static_tab_widget_->findChildren<QWidget*>()) // clazy:exclude=range-loop |
313 | w->setMinimumHeight(h); | |
cc964645 SA |
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; | |
f4ab4b5c | 345 | for (const 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 | ||
f4ab4b5c | 367 | for (shared_ptr<Session>& session : sessions_) { |
3ed18835 SA |
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 | ||
d0c0573b SA |
403 | void MainWindow::on_setting_changed(const QString &key, const QVariant &value) |
404 | { | |
641574bc SA |
405 | if (key == GlobalSettings::Key_View_ColoredBG) |
406 | on_settingViewColoredBg_changed(value); | |
d0c0573b SA |
407 | |
408 | if (key == GlobalSettings::Key_View_ShowSamplingPoints) | |
409 | on_settingViewShowSamplingPoints_changed(value); | |
410 | ||
411 | if (key == GlobalSettings::Key_View_ShowAnalogMinorGrid) | |
412 | on_settingViewShowAnalogMinorGrid_changed(value); | |
413 | } | |
414 | ||
7d5425ef JH |
415 | void MainWindow::setup_ui() |
416 | { | |
417 | setObjectName(QString::fromUtf8("MainWindow")); | |
418 | ||
3b84fd0b SA |
419 | setCentralWidget(&session_selector_); |
420 | ||
7d5425ef JH |
421 | // Set the window icon |
422 | QIcon icon; | |
5ea53b3c | 423 | icon.addFile(QString(":/icons/pulseview.png")); |
7d5425ef JH |
424 | setWindowIcon(icon); |
425 | ||
9eae6de4 PET |
426 | view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut())); |
427 | view_sticky_scrolling_shortcut_->setAutoRepeat(false); | |
ab1d13ee | 428 | |
051ba3b3 UH |
429 | view_show_sampling_points_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Period), this, SLOT(on_view_show_sampling_points_shortcut())); |
430 | view_show_sampling_points_shortcut_->setAutoRepeat(false); | |
431 | ||
8ad61f40 UH |
432 | view_show_analog_minor_grid_shortcut_ = new QShortcut(QKeySequence(Qt::Key_G), this, SLOT(on_view_show_analog_minor_grid_shortcut())); |
433 | view_show_analog_minor_grid_shortcut_->setAutoRepeat(false); | |
434 | ||
641574bc SA |
435 | view_colored_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_colored_bg_shortcut())); |
436 | view_colored_bg_shortcut_->setAutoRepeat(false); | |
0fb9d645 | 437 | |
f1e2d26b SA |
438 | // Set up the tab area |
439 | new_session_button_ = new QToolButton(); | |
440 | new_session_button_->setIcon(QIcon::fromTheme("document-new", | |
441 | QIcon(":/icons/document-new.png"))); | |
5f66b56e | 442 | new_session_button_->setToolTip(tr("Create New Session")); |
f1e2d26b SA |
443 | new_session_button_->setAutoRaise(true); |
444 | ||
3231fbf9 SA |
445 | run_stop_button_ = new QToolButton(); |
446 | run_stop_button_->setAutoRaise(true); | |
447 | run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
5f66b56e | 448 | run_stop_button_->setToolTip(tr("Start/Stop Acquisition")); |
3231fbf9 | 449 | |
45cb64ff PET |
450 | run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click())); |
451 | run_stop_shortcut_->setAutoRepeat(false); | |
452 | ||
4d1ec09a | 453 | settings_button_ = new QToolButton(); |
3432032f UH |
454 | settings_button_->setIcon(QIcon::fromTheme("preferences-system", |
455 | QIcon(":/icons/preferences-system.png"))); | |
5f66b56e | 456 | settings_button_->setToolTip(tr("Settings")); |
4d1ec09a SA |
457 | settings_button_->setAutoRaise(true); |
458 | ||
459 | QFrame *separator1 = new QFrame(); | |
460 | separator1->setFrameStyle(QFrame::VLine | QFrame::Raised); | |
461 | QFrame *separator2 = new QFrame(); | |
462 | separator2->setFrameStyle(QFrame::VLine | QFrame::Raised); | |
baa8560e | 463 | |
f1e2d26b SA |
464 | QHBoxLayout* layout = new QHBoxLayout(); |
465 | layout->setContentsMargins(2, 2, 2, 2); | |
466 | layout->addWidget(new_session_button_); | |
4d1ec09a | 467 | layout->addWidget(separator1); |
3231fbf9 | 468 | layout->addWidget(run_stop_button_); |
4d1ec09a SA |
469 | layout->addWidget(separator2); |
470 | layout->addWidget(settings_button_); | |
f1e2d26b | 471 | |
cc964645 | 472 | static_tab_widget_ = new QWidget(); |
f1e2d26b SA |
473 | static_tab_widget_->setLayout(layout); |
474 | ||
475 | session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner); | |
4a4e20f5 SA |
476 | session_selector_.setTabsClosable(true); |
477 | ||
763945bb PET |
478 | close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close())); |
479 | close_application_shortcut_->setAutoRepeat(false); | |
480 | ||
481 | close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab())); | |
482 | ||
f1e2d26b SA |
483 | connect(new_session_button_, SIGNAL(clicked(bool)), |
484 | this, SLOT(on_new_session_clicked())); | |
3231fbf9 SA |
485 | connect(run_stop_button_, SIGNAL(clicked(bool)), |
486 | this, SLOT(on_run_stop_clicked())); | |
487 | connect(&session_state_mapper_, SIGNAL(mapped(QObject*)), | |
488 | this, SLOT(on_capture_state_changed(QObject*))); | |
bf9f1268 SA |
489 | connect(settings_button_, SIGNAL(clicked(bool)), |
490 | this, SLOT(on_settings_clicked())); | |
f1e2d26b | 491 | |
4a4e20f5 SA |
492 | connect(&session_selector_, SIGNAL(tabCloseRequested(int)), |
493 | this, SLOT(on_tab_close_requested(int))); | |
e7aff437 SA |
494 | connect(&session_selector_, SIGNAL(currentChanged(int)), |
495 | this, SLOT(on_tab_changed(int))); | |
4a4e20f5 | 496 | |
f1e2d26b | 497 | |
33e1afbe SA |
498 | connect(static_cast<QApplication *>(QCoreApplication::instance()), |
499 | SIGNAL(focusChanged(QWidget*, QWidget*)), | |
500 | this, SLOT(on_focus_changed())); | |
e3c79b07 JH |
501 | } |
502 | ||
93f683ad SA |
503 | void MainWindow::save_ui_settings() |
504 | { | |
39eb0d45 | 505 | QSettings settings; |
6842b5fc | 506 | |
93f683ad SA |
507 | settings.beginGroup("MainWindow"); |
508 | settings.setValue("state", saveState()); | |
509 | settings.setValue("geometry", saveGeometry()); | |
510 | settings.endGroup(); | |
511 | } | |
512 | ||
3ed18835 | 513 | void MainWindow::restore_ui_settings() |
93f683ad | 514 | { |
39eb0d45 | 515 | QSettings settings; |
93f683ad SA |
516 | |
517 | settings.beginGroup("MainWindow"); | |
518 | ||
519 | if (settings.contains("geometry")) { | |
520 | restoreGeometry(settings.value("geometry").toByteArray()); | |
521 | restoreState(settings.value("state").toByteArray()); | |
522 | } else | |
523 | resize(1000, 720); | |
524 | ||
525 | settings.endGroup(); | |
526 | } | |
527 | ||
6f925ba9 | 528 | shared_ptr<Session> MainWindow::get_tab_session(int index) const |
e7aff437 SA |
529 | { |
530 | // Find the session that belongs to the tab's main window | |
f4ab4b5c | 531 | for (auto& entry : session_windows_) |
e7aff437 SA |
532 | if (entry.second == session_selector_.widget(index)) |
533 | return entry.first; | |
534 | ||
535 | return nullptr; | |
536 | } | |
537 | ||
e3c79b07 JH |
538 | void MainWindow::closeEvent(QCloseEvent *event) |
539 | { | |
5ccfc97e SA |
540 | bool data_saved = true; |
541 | ||
f4ab4b5c | 542 | for (auto& entry : session_windows_) |
5ccfc97e SA |
543 | if (!entry.first->data_saved()) |
544 | data_saved = false; | |
545 | ||
546 | if (!data_saved && (QMessageBox::question(this, tr("Confirmation"), | |
547 | tr("There is unsaved data. Close anyway?"), | |
548 | QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) { | |
549 | event->ignore(); | |
550 | } else { | |
551 | save_ui_settings(); | |
3ed18835 | 552 | save_sessions(); |
5ccfc97e SA |
553 | event->accept(); |
554 | } | |
e3c79b07 JH |
555 | } |
556 | ||
d290e89f SA |
557 | QMenu* MainWindow::createPopupMenu() |
558 | { | |
559 | return nullptr; | |
560 | } | |
561 | ||
55547a45 SA |
562 | bool MainWindow::restoreState(const QByteArray &state, int version) |
563 | { | |
564 | (void)state; | |
565 | (void)version; | |
566 | ||
567 | // Do nothing. We don't want Qt to handle this, or else it | |
568 | // will try to restore all the dock widgets and create havoc. | |
569 | ||
570 | return false; | |
571 | } | |
572 | ||
f4e57597 | 573 | void MainWindow::on_add_view(const QString &title, views::ViewType type, |
3a21afa6 SA |
574 | Session *session) |
575 | { | |
576 | // We get a pointer and need a reference | |
f4ab4b5c | 577 | for (shared_ptr<Session>& s : sessions_) |
3a21afa6 SA |
578 | if (s.get() == session) |
579 | add_view(title, type, *s); | |
580 | } | |
581 | ||
33e1afbe SA |
582 | void MainWindow::on_focus_changed() |
583 | { | |
e7aff437 SA |
584 | shared_ptr<views::ViewBase> view = get_active_view(); |
585 | ||
586 | if (view) { | |
587 | for (shared_ptr<Session> session : sessions_) { | |
588 | if (session->has_view(view)) { | |
33bedfc1 | 589 | if (session != last_focused_session_) { |
e7aff437 SA |
590 | // Activate correct tab if necessary |
591 | shared_ptr<Session> tab_session = get_tab_session( | |
592 | session_selector_.currentIndex()); | |
593 | if (tab_session != session) | |
594 | session_selector_.setCurrentWidget( | |
595 | session_windows_.at(session)); | |
596 | ||
597 | on_focused_session_changed(session); | |
598 | } | |
599 | ||
e7aff437 SA |
600 | break; |
601 | } | |
602 | } | |
33e1afbe SA |
603 | } |
604 | ||
e7aff437 | 605 | if (sessions_.empty()) |
33e1afbe SA |
606 | setWindowTitle(WindowTitle); |
607 | } | |
608 | ||
e7aff437 SA |
609 | void MainWindow::on_focused_session_changed(shared_ptr<Session> session) |
610 | { | |
33bedfc1 SA |
611 | last_focused_session_ = session; |
612 | ||
e7aff437 | 613 | setWindowTitle(session->name() + " - " + WindowTitle); |
3231fbf9 SA |
614 | |
615 | // Update the state of the run/stop button, too | |
616 | on_capture_state_changed(session.get()); | |
e7aff437 SA |
617 | } |
618 | ||
f1e2d26b | 619 | void MainWindow::on_new_session_clicked() |
c9da5118 SA |
620 | { |
621 | add_session(); | |
622 | } | |
623 | ||
3231fbf9 SA |
624 | void MainWindow::on_run_stop_clicked() |
625 | { | |
33bedfc1 SA |
626 | shared_ptr<Session> session = last_focused_session_; |
627 | ||
628 | if (!session) | |
629 | return; | |
3231fbf9 | 630 | |
33bedfc1 | 631 | switch (session->get_capture_state()) { |
3231fbf9 | 632 | case Session::Stopped: |
33bedfc1 | 633 | session->start_capture([&](QString message) { |
5e685656 | 634 | show_session_error("Capture failed", message); }); |
3231fbf9 SA |
635 | break; |
636 | case Session::AwaitingTrigger: | |
637 | case Session::Running: | |
33bedfc1 | 638 | session->stop_capture(); |
3231fbf9 SA |
639 | break; |
640 | } | |
641 | } | |
642 | ||
bf9f1268 SA |
643 | void MainWindow::on_settings_clicked() |
644 | { | |
4e4d72b2 | 645 | dialogs::Settings dlg(device_manager_); |
bf9f1268 SA |
646 | dlg.exec(); |
647 | } | |
648 | ||
33e1afbe SA |
649 | void MainWindow::on_session_name_changed() |
650 | { | |
651 | // Update the corresponding dock widget's name(s) | |
652 | Session *session = qobject_cast<Session*>(QObject::sender()); | |
653 | assert(session); | |
654 | ||
f4ab4b5c | 655 | for (const shared_ptr<views::ViewBase>& view : session->views()) { |
33e1afbe | 656 | // Get the dock that contains the view |
f4ab4b5c | 657 | for (auto& entry : view_docks_) |
33e1afbe SA |
658 | if (entry.second == view) { |
659 | entry.first->setObjectName(session->name()); | |
660 | entry.first->setWindowTitle(session->name()); | |
661 | } | |
662 | } | |
663 | ||
3cb15390 | 664 | // Update the tab widget by finding the main window and the tab from that |
f4ab4b5c | 665 | for (auto& entry : session_windows_) |
3cb15390 SA |
666 | if (entry.first.get() == session) { |
667 | QMainWindow *window = entry.second; | |
668 | const int index = session_selector_.indexOf(window); | |
669 | session_selector_.setTabText(index, session->name()); | |
670 | } | |
671 | ||
33e1afbe | 672 | // Refresh window title if the affected session has focus |
33bedfc1 | 673 | if (session == last_focused_session_.get()) |
e7aff437 | 674 | setWindowTitle(session->name() + " - " + WindowTitle); |
33e1afbe SA |
675 | } |
676 | ||
3231fbf9 SA |
677 | void MainWindow::on_capture_state_changed(QObject *obj) |
678 | { | |
679 | Session *caller = qobject_cast<Session*>(obj); | |
680 | ||
681 | // Ignore if caller is not the currently focused session | |
682 | // unless there is only one session | |
33bedfc1 SA |
683 | if ((sessions_.size() > 1) && (caller != last_focused_session_.get())) |
684 | return; | |
3231fbf9 SA |
685 | |
686 | int state = caller->get_capture_state(); | |
687 | ||
688 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; | |
689 | run_stop_button_->setIcon(*icons[state]); | |
690 | run_stop_button_->setText((state == pv::Session::Stopped) ? | |
691 | tr("Run") : tr("Stop")); | |
692 | } | |
693 | ||
c9da5118 SA |
694 | void MainWindow::on_new_view(Session *session) |
695 | { | |
696 | // We get a pointer and need a reference | |
f4ab4b5c | 697 | for (shared_ptr<Session>& s : sessions_) |
c9da5118 | 698 | if (s.get() == session) |
f4e57597 | 699 | add_view(session->name(), views::ViewTypeTrace, *s); |
c9da5118 SA |
700 | } |
701 | ||
36a8185e SA |
702 | void MainWindow::on_view_close_clicked() |
703 | { | |
704 | // Find the dock widget that contains the close button that was clicked | |
705 | QObject *w = QObject::sender(); | |
13e475e4 | 706 | QDockWidget *dock = nullptr; |
36a8185e SA |
707 | |
708 | while (w) { | |
709 | dock = qobject_cast<QDockWidget*>(w); | |
710 | if (dock) | |
711 | break; | |
712 | w = w->parent(); | |
713 | } | |
714 | ||
715 | // Get the view contained in the dock widget | |
f4e57597 | 716 | shared_ptr<views::ViewBase> view; |
36a8185e | 717 | |
f4ab4b5c | 718 | for (auto& entry : view_docks_) |
cbf7b5db | 719 | if (entry.first == dock) |
36a8185e SA |
720 | view = entry.second; |
721 | ||
722 | // Deregister the view | |
723 | for (shared_ptr<Session> session : sessions_) { | |
724 | if (!session->has_view(view)) | |
725 | continue; | |
726 | ||
a2b9ac40 | 727 | // Also destroy the entire session if its main view is closing... |
36a8185e | 728 | if (view == session->main_view()) { |
a2b9ac40 SA |
729 | // ...but only if data is saved or the user confirms closing |
730 | if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"), | |
731 | tr("This session contains unsaved data. Close it anyway?"), | |
732 | QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)) | |
733 | remove_session(session); | |
36a8185e SA |
734 | break; |
735 | } else | |
a2b9ac40 | 736 | // All other views can be closed at any time as no data will be lost |
f30eb549 | 737 | remove_view(view); |
36a8185e SA |
738 | } |
739 | } | |
740 | ||
e7aff437 SA |
741 | void MainWindow::on_tab_changed(int index) |
742 | { | |
743 | shared_ptr<Session> session = get_tab_session(index); | |
744 | ||
745 | if (session) | |
746 | on_focused_session_changed(session); | |
747 | } | |
748 | ||
4a4e20f5 SA |
749 | void MainWindow::on_tab_close_requested(int index) |
750 | { | |
e7aff437 SA |
751 | shared_ptr<Session> session = get_tab_session(index); |
752 | ||
d8d053b6 SA |
753 | if (!session) |
754 | return; | |
5ccfc97e SA |
755 | |
756 | if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"), | |
757 | tr("This session contains unsaved data. Close it anyway?"), | |
758 | QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)) | |
e7aff437 | 759 | remove_session(session); |
4a4e20f5 SA |
760 | } |
761 | ||
641574bc | 762 | void MainWindow::on_view_colored_bg_shortcut() |
0fb9d645 | 763 | { |
24c29d4f SA |
764 | GlobalSettings settings; |
765 | ||
641574bc SA |
766 | bool state = settings.value(GlobalSettings::Key_View_ColoredBG).toBool(); |
767 | settings.setValue(GlobalSettings::Key_View_ColoredBG, !state); | |
24c29d4f SA |
768 | } |
769 | ||
87a97d8a SA |
770 | void MainWindow::on_view_sticky_scrolling_shortcut() |
771 | { | |
772 | GlobalSettings settings; | |
773 | ||
774 | bool state = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool(); | |
775 | settings.setValue(GlobalSettings::Key_View_StickyScrolling, !state); | |
776 | } | |
777 | ||
051ba3b3 UH |
778 | void MainWindow::on_view_show_sampling_points_shortcut() |
779 | { | |
780 | GlobalSettings settings; | |
781 | ||
782 | bool state = settings.value(GlobalSettings::Key_View_ShowSamplingPoints).toBool(); | |
783 | settings.setValue(GlobalSettings::Key_View_ShowSamplingPoints, !state); | |
784 | } | |
785 | ||
8ad61f40 UH |
786 | void MainWindow::on_view_show_analog_minor_grid_shortcut() |
787 | { | |
788 | GlobalSettings settings; | |
789 | ||
790 | bool state = settings.value(GlobalSettings::Key_View_ShowAnalogMinorGrid).toBool(); | |
791 | settings.setValue(GlobalSettings::Key_View_ShowAnalogMinorGrid, !state); | |
792 | } | |
793 | ||
641574bc | 794 | void MainWindow::on_settingViewColoredBg_changed(const QVariant new_value) |
24c29d4f SA |
795 | { |
796 | bool state = new_value.toBool(); | |
797 | ||
f4ab4b5c | 798 | for (auto& entry : view_docks_) { |
24c29d4f SA |
799 | shared_ptr<views::ViewBase> viewbase = entry.second; |
800 | ||
801 | // Only trace views have this setting | |
f23c4692 SA |
802 | views::trace::View* view = |
803 | qobject_cast<views::trace::View*>(viewbase.get()); | |
24c29d4f | 804 | if (view) |
641574bc | 805 | view->enable_colored_bg(state); |
24c29d4f | 806 | } |
0fb9d645 SA |
807 | } |
808 | ||
051ba3b3 UH |
809 | void MainWindow::on_settingViewShowSamplingPoints_changed(const QVariant new_value) |
810 | { | |
811 | bool state = new_value.toBool(); | |
812 | ||
f4ab4b5c | 813 | for (auto& entry : view_docks_) { |
051ba3b3 UH |
814 | shared_ptr<views::ViewBase> viewbase = entry.second; |
815 | ||
816 | // Only trace views have this setting | |
f23c4692 SA |
817 | views::trace::View* view = |
818 | qobject_cast<views::trace::View*>(viewbase.get()); | |
051ba3b3 UH |
819 | if (view) |
820 | view->enable_show_sampling_points(state); | |
821 | } | |
822 | } | |
823 | ||
8ad61f40 UH |
824 | void MainWindow::on_settingViewShowAnalogMinorGrid_changed(const QVariant new_value) |
825 | { | |
826 | bool state = new_value.toBool(); | |
827 | ||
f4ab4b5c | 828 | for (auto& entry : view_docks_) { |
8ad61f40 UH |
829 | shared_ptr<views::ViewBase> viewbase = entry.second; |
830 | ||
831 | // Only trace views have this setting | |
f23c4692 SA |
832 | views::trace::View* view = |
833 | qobject_cast<views::trace::View*>(viewbase.get()); | |
8ad61f40 UH |
834 | if (view) |
835 | view->enable_show_analog_minor_grid(state); | |
836 | } | |
837 | } | |
838 | ||
763945bb PET |
839 | void MainWindow::on_close_current_tab() |
840 | { | |
841 | int tab = session_selector_.currentIndex(); | |
842 | ||
843 | on_tab_close_requested(tab); | |
844 | } | |
845 | ||
51e77110 | 846 | } // namespace pv |