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