]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
943edd76 MC |
21 | #include <cassert> |
22 | ||
269528f5 | 23 | #ifdef ENABLE_DECODE |
aaabd61b | 24 | #include <libsigrokdecode/libsigrokdecode.h> |
269528f5 | 25 | #endif |
30236a54 | 26 | |
85843b14 JH |
27 | #include <algorithm> |
28 | #include <iterator> | |
29 | ||
7d5425ef JH |
30 | #include <QAction> |
31 | #include <QApplication> | |
93f683ad | 32 | #include <QCloseEvent> |
0f8f8c18 | 33 | #include <QDockWidget> |
643f65f9 | 34 | #include <QSettings> |
7d5425ef | 35 | #include <QWidget> |
2953961c | 36 | |
2acdb232 | 37 | #include "mainwindow.hpp" |
107ca6d3 | 38 | |
2acdb232 | 39 | #include "devicemanager.hpp" |
d2fc6be9 | 40 | #include "util.hpp" |
101e7a9b | 41 | #include "devices/hardwaredevice.hpp" |
2acdb232 | 42 | #include "dialogs/about.hpp" |
7c657094 | 43 | #include "toolbars/mainbar.hpp" |
2acdb232 | 44 | #include "view/view.hpp" |
d7bed479 | 45 | |
30236a54 JH |
46 | #include <stdint.h> |
47 | #include <stdarg.h> | |
fe3a1c21 | 48 | #include <libsigrokcxx/libsigrokcxx.hpp> |
e82fd481 | 49 | |
101e7a9b | 50 | using std::dynamic_pointer_cast; |
819f4c25 | 51 | using std::list; |
25272fee | 52 | using std::make_shared; |
6842b5fc | 53 | using std::map; |
f9abf97e | 54 | using std::shared_ptr; |
6842b5fc | 55 | using std::string; |
e8d00928 | 56 | |
51e77110 JH |
57 | namespace pv { |
58 | ||
b1264f56 | 59 | namespace view { |
26e3af6b | 60 | class ViewItem; |
b1264f56 JH |
61 | } |
62 | ||
0f8f8c18 | 63 | using toolbars::MainBar; |
643f65f9 | 64 | |
33e1afbe SA |
65 | const QString MainWindow::WindowTitle = tr("PulseView"); |
66 | ||
107ca6d3 | 67 | MainWindow::MainWindow(DeviceManager &device_manager, |
e3c79b07 | 68 | string open_file_name, string open_file_format, |
1d478458 | 69 | QWidget *parent) : |
107ca6d3 | 70 | QMainWindow(parent), |
8dbbc7f0 | 71 | device_manager_(device_manager), |
c7b03d9d | 72 | action_view_sticky_scrolling_(new QAction(this)), |
0fb9d645 | 73 | action_view_coloured_bg_(new QAction(this)), |
e79171dc | 74 | action_about_(new QAction(this)) |
d7bed479 | 75 | { |
48257a69 SA |
76 | qRegisterMetaType<util::Timestamp>("util::Timestamp"); |
77 | ||
7d5425ef | 78 | setup_ui(); |
93f683ad | 79 | restore_ui_settings(); |
101e7a9b SA |
80 | |
81 | if (!open_file_name.empty()) { | |
82 | shared_ptr<Session> session = add_session(); | |
83 | session->main_bar()->load_init_file(open_file_name, open_file_format); | |
84 | } | |
85 | ||
86 | // Add empty default session if there aren't any sessions | |
87 | if (sessions_.size() == 0) { | |
88 | shared_ptr<Session> session = add_session(); | |
89 | ||
90 | map<string, string> dev_info; | |
91 | shared_ptr<devices::HardwareDevice> other_device, demo_device; | |
92 | ||
93 | // Use any available device that's not demo | |
94 | for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) { | |
95 | if (dev->hardware_device()->driver()->name() == "demo") { | |
96 | demo_device = dev; | |
97 | } else { | |
98 | other_device = dev; | |
99 | } | |
100 | } | |
101 | ||
102 | // ...and if there isn't any, just use demo then | |
103 | session->main_bar()->select_device(other_device ? | |
104 | other_device : demo_device); | |
105 | } | |
7d5425ef JH |
106 | } |
107 | ||
47e9e7bb SA |
108 | MainWindow::~MainWindow() |
109 | { | |
110 | for (auto entry : view_docks_) { | |
0f8f8c18 | 111 | |
47e9e7bb | 112 | const std::shared_ptr<QDockWidget> dock = entry.first; |
0f8f8c18 SA |
113 | |
114 | // Remove view from the dock widget's QMainWindow | |
115 | QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget()); | |
116 | dock_main->setCentralWidget(0); | |
117 | ||
118 | // Remove the QMainWindow | |
47e9e7bb | 119 | dock->setWidget(0); |
0f8f8c18 | 120 | |
47e9e7bb | 121 | const std::shared_ptr<pv::view::View> view = entry.second; |
101e7a9b SA |
122 | |
123 | for (shared_ptr<Session> session : sessions_) | |
124 | if (session->has_view(view)) | |
125 | session->deregister_view(view); | |
47e9e7bb SA |
126 | } |
127 | } | |
128 | ||
c7b03d9d SA |
129 | QAction* MainWindow::action_view_sticky_scrolling() const |
130 | { | |
131 | return action_view_sticky_scrolling_; | |
132 | } | |
133 | ||
bea236d6 JH |
134 | QAction* MainWindow::action_view_coloured_bg() const |
135 | { | |
136 | return action_view_coloured_bg_; | |
137 | } | |
138 | ||
69654681 JH |
139 | QAction* MainWindow::action_about() const |
140 | { | |
141 | return action_about_; | |
142 | } | |
143 | ||
168bd8ac SA |
144 | shared_ptr<pv::view::View> MainWindow::get_active_view() const |
145 | { | |
146 | // If there's only one view, use it... | |
147 | if (view_docks_.size() == 1) | |
148 | return view_docks_.begin()->second; | |
149 | ||
150 | // ...otherwise find the dock widget the widget with focus is contained in | |
151 | QObject *w = QApplication::focusWidget(); | |
152 | QDockWidget *dock = 0; | |
153 | ||
154 | while (w) { | |
155 | dock = qobject_cast<QDockWidget*>(w); | |
156 | if (dock) | |
157 | break; | |
158 | w = w->parent(); | |
159 | } | |
160 | ||
161 | // Get the view contained in the dock widget | |
162 | for (auto entry : view_docks_) | |
163 | if (entry.first.get() == dock) | |
164 | return entry.second; | |
165 | ||
82f8a42b | 166 | return nullptr; |
168bd8ac SA |
167 | } |
168 | ||
7cd2b5f3 SA |
169 | shared_ptr<pv::view::View> MainWindow::add_view(const QString &title, |
170 | view::ViewType type, Session &session) | |
171 | { | |
172 | shared_ptr<pv::view::View> v; | |
173 | ||
0f8f8c18 | 174 | if (type == pv::view::TraceView) { |
7cd2b5f3 | 175 | shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this); |
7cd2b5f3 SA |
176 | dock->setObjectName(title); |
177 | addDockWidget(Qt::TopDockWidgetArea, dock.get()); | |
0f8f8c18 SA |
178 | |
179 | // Insert a QMainWindow into the dock widget to allow for a tool bar | |
180 | QMainWindow *dock_main = new QMainWindow(dock.get()); | |
181 | dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag | |
182 | ||
183 | v = make_shared<pv::view::View>(session, dock_main); | |
7cd2b5f3 | 184 | view_docks_[dock] = v; |
0f8f8c18 SA |
185 | session.register_view(v); |
186 | ||
187 | dock_main->setCentralWidget(v.get()); | |
188 | dock->setWidget(dock_main); | |
7cd2b5f3 | 189 | |
d6ab7b9a | 190 | dock->setFeatures(QDockWidget::DockWidgetMovable | |
36a8185e SA |
191 | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable); |
192 | ||
193 | QAbstractButton *close_btn = | |
194 | dock->findChildren<QAbstractButton*> | |
195 | ("qt_dockwidget_closebutton").front(); | |
196 | ||
197 | connect(close_btn, SIGNAL(clicked(bool)), | |
198 | this, SLOT(on_view_close_clicked())); | |
d6ab7b9a | 199 | |
7cd2b5f3 SA |
200 | if (type == view::TraceView) { |
201 | connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(), | |
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) { | |
101e7a9b | 209 | main_bar = make_shared<MainBar>(session, *this); |
0f8f8c18 SA |
210 | dock_main->addToolBar(main_bar.get()); |
211 | session.set_main_bar(main_bar); | |
c9da5118 SA |
212 | |
213 | connect(main_bar.get(), SIGNAL(new_session()), | |
214 | this, SLOT(on_new_session())); | |
215 | connect(main_bar.get(), SIGNAL(new_view(Session*)), | |
216 | this, SLOT(on_new_view(Session*))); | |
0f8f8c18 SA |
217 | } |
218 | main_bar->action_view_show_cursors()->setChecked(v->cursors_shown()); | |
d552c5c7 SA |
219 | |
220 | connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), | |
221 | main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool))); | |
a55e7918 | 222 | } |
e93f5538 JH |
223 | } |
224 | ||
0f8f8c18 | 225 | return v; |
ed43ef2e JH |
226 | } |
227 | ||
101e7a9b SA |
228 | shared_ptr<Session> MainWindow::add_session() |
229 | { | |
230 | int id = sessions_.size(); | |
231 | QString name = tr("Untitled-%1").arg(id + 1); | |
232 | ||
233 | shared_ptr<Session> session = make_shared<Session>(device_manager_, name); | |
234 | ||
3a21afa6 SA |
235 | connect(session.get(), SIGNAL(add_view(const QString&, view::ViewType, Session*)), |
236 | this, SLOT(on_add_view(const QString&, view::ViewType, Session*))); | |
33e1afbe SA |
237 | connect(session.get(), SIGNAL(name_changed()), |
238 | this, SLOT(on_session_name_changed())); | |
3a21afa6 | 239 | |
101e7a9b SA |
240 | sessions_.push_back(session); |
241 | ||
242 | shared_ptr<view::View> main_view = | |
243 | add_view(name, pv::view::TraceView, *session); | |
244 | ||
245 | return session; | |
246 | } | |
247 | ||
36a8185e SA |
248 | void MainWindow::remove_session(shared_ptr<Session> session) |
249 | { | |
250 | for (shared_ptr<view::View> view : session->views()) { | |
251 | // Find the dock the view is contained in and close it | |
252 | for (auto entry : view_docks_) | |
253 | if (entry.second == view) | |
254 | entry.first->close(); | |
255 | } | |
256 | ||
257 | sessions_.remove_if([&](shared_ptr<Session> s) { | |
258 | return s == session; }); | |
33e1afbe SA |
259 | |
260 | // Update the window title if there is no view left to | |
261 | // generate focus change events | |
262 | if (sessions_.empty()) | |
263 | on_session_name_changed(); | |
36a8185e SA |
264 | } |
265 | ||
7d5425ef JH |
266 | void MainWindow::setup_ui() |
267 | { | |
268 | setObjectName(QString::fromUtf8("MainWindow")); | |
269 | ||
7d5425ef JH |
270 | // Set the window icon |
271 | QIcon icon; | |
14f9d4a1 | 272 | icon.addFile(QString(":/icons/sigrok-logo-notext.png")); |
7d5425ef JH |
273 | setWindowIcon(icon); |
274 | ||
c7b03d9d SA |
275 | action_view_sticky_scrolling_->setCheckable(true); |
276 | action_view_sticky_scrolling_->setChecked(true); | |
69282fae | 277 | action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S)); |
c7b03d9d SA |
278 | action_view_sticky_scrolling_->setObjectName( |
279 | QString::fromUtf8("actionViewStickyScrolling")); | |
69282fae | 280 | action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling")); |
ab1d13ee | 281 | |
0fb9d645 SA |
282 | action_view_coloured_bg_->setCheckable(true); |
283 | action_view_coloured_bg_->setChecked(true); | |
574c568d | 284 | action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B)); |
0fb9d645 SA |
285 | action_view_coloured_bg_->setObjectName( |
286 | QString::fromUtf8("actionViewColouredBg")); | |
287 | action_view_coloured_bg_->setText(tr("Use &coloured backgrounds")); | |
0fb9d645 | 288 | |
69654681 JH |
289 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); |
290 | action_about_->setText(tr("&About...")); | |
7d5425ef | 291 | |
956a945e SA |
292 | setDockNestingEnabled(true); |
293 | ||
33e1afbe SA |
294 | connect(static_cast<QApplication *>(QCoreApplication::instance()), |
295 | SIGNAL(focusChanged(QWidget*, QWidget*)), | |
296 | this, SLOT(on_focus_changed())); | |
e3c79b07 JH |
297 | } |
298 | ||
93f683ad SA |
299 | void MainWindow::save_ui_settings() |
300 | { | |
39eb0d45 | 301 | QSettings settings; |
101e7a9b | 302 | int id = 0; |
6842b5fc | 303 | |
93f683ad SA |
304 | settings.beginGroup("MainWindow"); |
305 | settings.setValue("state", saveState()); | |
306 | settings.setValue("geometry", saveGeometry()); | |
307 | settings.endGroup(); | |
6842b5fc | 308 | |
101e7a9b | 309 | for (shared_ptr<Session> session : sessions_) { |
3503810c | 310 | // Ignore sessions using the demo device or no device at all |
101e7a9b SA |
311 | if (session->device()) { |
312 | shared_ptr<devices::HardwareDevice> device = | |
313 | dynamic_pointer_cast< devices::HardwareDevice > | |
314 | (session->device()); | |
315 | ||
f2040dcb SA |
316 | if (device && |
317 | device->hardware_device()->driver()->name() == "demo") | |
101e7a9b | 318 | continue; |
6842b5fc | 319 | |
3503810c SA |
320 | settings.beginGroup("Session" + QString::number(id++)); |
321 | settings.remove(""); // Remove all keys in this group | |
322 | session->save_settings(settings); | |
323 | settings.endGroup(); | |
324 | } | |
6842b5fc | 325 | } |
101e7a9b SA |
326 | |
327 | settings.setValue("sessions", id); | |
93f683ad SA |
328 | } |
329 | ||
330 | void MainWindow::restore_ui_settings() | |
331 | { | |
39eb0d45 | 332 | QSettings settings; |
101e7a9b | 333 | int i, session_count; |
93f683ad SA |
334 | |
335 | settings.beginGroup("MainWindow"); | |
336 | ||
337 | if (settings.contains("geometry")) { | |
338 | restoreGeometry(settings.value("geometry").toByteArray()); | |
339 | restoreState(settings.value("state").toByteArray()); | |
340 | } else | |
341 | resize(1000, 720); | |
342 | ||
343 | settings.endGroup(); | |
101e7a9b SA |
344 | |
345 | session_count = settings.value("sessions", 0).toInt(); | |
346 | ||
347 | for (i = 0; i < session_count; i++) { | |
348 | settings.beginGroup("Session" + QString::number(i)); | |
349 | shared_ptr<Session> session = add_session(); | |
350 | session->restore_settings(settings); | |
351 | settings.endGroup(); | |
352 | } | |
93f683ad SA |
353 | } |
354 | ||
e3c79b07 JH |
355 | void MainWindow::closeEvent(QCloseEvent *event) |
356 | { | |
357 | save_ui_settings(); | |
358 | event->accept(); | |
359 | } | |
360 | ||
d290e89f SA |
361 | QMenu* MainWindow::createPopupMenu() |
362 | { | |
363 | return nullptr; | |
364 | } | |
365 | ||
55547a45 SA |
366 | bool MainWindow::restoreState(const QByteArray &state, int version) |
367 | { | |
368 | (void)state; | |
369 | (void)version; | |
370 | ||
371 | // Do nothing. We don't want Qt to handle this, or else it | |
372 | // will try to restore all the dock widgets and create havoc. | |
373 | ||
374 | return false; | |
375 | } | |
376 | ||
3a21afa6 SA |
377 | void MainWindow::on_add_view(const QString &title, view::ViewType type, |
378 | Session *session) | |
379 | { | |
380 | // We get a pointer and need a reference | |
381 | for (std::shared_ptr<Session> s : sessions_) | |
382 | if (s.get() == session) | |
383 | add_view(title, type, *s); | |
384 | } | |
385 | ||
33e1afbe SA |
386 | void MainWindow::on_focus_changed() |
387 | { | |
388 | shared_ptr<view::View> view; | |
389 | bool title_set = false; | |
390 | ||
391 | view = get_active_view(); | |
392 | ||
393 | for (shared_ptr<Session> session : sessions_) { | |
394 | if (!session->has_view(view)) | |
395 | continue; | |
396 | ||
397 | setWindowTitle(session->name() + " - " + WindowTitle); | |
398 | title_set = true; | |
399 | } | |
400 | ||
401 | if (!title_set) | |
402 | setWindowTitle(WindowTitle); | |
403 | } | |
404 | ||
c9da5118 SA |
405 | void MainWindow::on_new_session() |
406 | { | |
407 | add_session(); | |
408 | } | |
409 | ||
33e1afbe SA |
410 | void MainWindow::on_session_name_changed() |
411 | { | |
412 | // Update the corresponding dock widget's name(s) | |
413 | Session *session = qobject_cast<Session*>(QObject::sender()); | |
414 | assert(session); | |
415 | ||
416 | for (shared_ptr<view::View> view : session->views()) { | |
417 | // Get the dock that contains the view | |
418 | for (auto entry : view_docks_) | |
419 | if (entry.second == view) { | |
420 | entry.first->setObjectName(session->name()); | |
421 | entry.first->setWindowTitle(session->name()); | |
422 | } | |
423 | } | |
424 | ||
425 | // Refresh window title if the affected session has focus | |
426 | on_focus_changed(); | |
427 | } | |
428 | ||
c9da5118 SA |
429 | void MainWindow::on_new_view(Session *session) |
430 | { | |
431 | // We get a pointer and need a reference | |
432 | for (std::shared_ptr<Session> s : sessions_) | |
433 | if (s.get() == session) | |
434 | add_view(session->name(), pv::view::TraceView, *s); | |
435 | } | |
436 | ||
36a8185e SA |
437 | void MainWindow::on_view_close_clicked() |
438 | { | |
439 | // Find the dock widget that contains the close button that was clicked | |
440 | QObject *w = QObject::sender(); | |
441 | QDockWidget *dock = 0; | |
442 | ||
443 | while (w) { | |
444 | dock = qobject_cast<QDockWidget*>(w); | |
445 | if (dock) | |
446 | break; | |
447 | w = w->parent(); | |
448 | } | |
449 | ||
450 | // Get the view contained in the dock widget | |
451 | shared_ptr<view::View> view; | |
452 | ||
453 | for (auto entry : view_docks_) | |
454 | if (entry.first.get() == dock) | |
455 | view = entry.second; | |
456 | ||
457 | // Deregister the view | |
458 | for (shared_ptr<Session> session : sessions_) { | |
459 | if (!session->has_view(view)) | |
460 | continue; | |
461 | ||
462 | // Also destroy the entire session if its main view is closing | |
463 | if (view == session->main_view()) { | |
464 | remove_session(session); | |
465 | break; | |
466 | } else | |
467 | session->deregister_view(view); | |
468 | } | |
469 | } | |
470 | ||
c7b03d9d SA |
471 | void MainWindow::on_actionViewStickyScrolling_triggered() |
472 | { | |
168bd8ac SA |
473 | shared_ptr<pv::view::View> view = get_active_view(); |
474 | if (view) | |
475 | view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); | |
c7b03d9d SA |
476 | } |
477 | ||
0fb9d645 SA |
478 | void MainWindow::on_actionViewColouredBg_triggered() |
479 | { | |
168bd8ac SA |
480 | shared_ptr<pv::view::View> view = get_active_view(); |
481 | if (view) | |
482 | view->enable_coloured_bg(action_view_coloured_bg_->isChecked()); | |
0fb9d645 SA |
483 | } |
484 | ||
30236a54 JH |
485 | void MainWindow::on_actionAbout_triggered() |
486 | { | |
8dbbc7f0 | 487 | dialogs::About dlg(device_manager_.context(), this); |
40eb2ff4 | 488 | dlg.exec(); |
30236a54 | 489 | } |
274d4f13 | 490 | |
51e77110 | 491 | } // namespace pv |