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