]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the PulseView project. | |
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 | ||
21 | #include <cassert> | |
22 | ||
23 | #ifdef ENABLE_DECODE | |
24 | #include <libsigrokdecode/libsigrokdecode.h> | |
25 | #endif | |
26 | ||
27 | #include <algorithm> | |
28 | #include <iterator> | |
29 | ||
30 | #include <QAction> | |
31 | #include <QApplication> | |
32 | #include <QCloseEvent> | |
33 | #include <QDockWidget> | |
34 | #include <QHBoxLayout> | |
35 | #include <QSettings> | |
36 | #include <QWidget> | |
37 | ||
38 | #include "mainwindow.hpp" | |
39 | ||
40 | #include "devicemanager.hpp" | |
41 | #include "util.hpp" | |
42 | #include "devices/hardwaredevice.hpp" | |
43 | #include "dialogs/about.hpp" | |
44 | #include "toolbars/mainbar.hpp" | |
45 | #include "view/view.hpp" | |
46 | ||
47 | #include <stdint.h> | |
48 | #include <stdarg.h> | |
49 | #include <libsigrokcxx/libsigrokcxx.hpp> | |
50 | ||
51 | using std::dynamic_pointer_cast; | |
52 | using std::list; | |
53 | using std::make_shared; | |
54 | using std::map; | |
55 | using std::shared_ptr; | |
56 | using std::string; | |
57 | ||
58 | namespace pv { | |
59 | ||
60 | namespace view { | |
61 | class ViewItem; | |
62 | } | |
63 | ||
64 | using toolbars::MainBar; | |
65 | ||
66 | const QString MainWindow::WindowTitle = tr("PulseView"); | |
67 | ||
68 | MainWindow::MainWindow(DeviceManager &device_manager, | |
69 | string open_file_name, string open_file_format, | |
70 | QWidget *parent) : | |
71 | QMainWindow(parent), | |
72 | device_manager_(device_manager), | |
73 | session_selector_(this), | |
74 | action_view_sticky_scrolling_(new QAction(this)), | |
75 | action_view_coloured_bg_(new QAction(this)), | |
76 | action_about_(new QAction(this)) | |
77 | { | |
78 | qRegisterMetaType<util::Timestamp>("util::Timestamp"); | |
79 | ||
80 | setup_ui(); | |
81 | restore_ui_settings(); | |
82 | ||
83 | if (!open_file_name.empty()) { | |
84 | shared_ptr<Session> session = add_session(); | |
85 | session->main_bar()->load_init_file(open_file_name, open_file_format); | |
86 | } | |
87 | ||
88 | // Add empty default session if there aren't any sessions | |
89 | if (sessions_.size() == 0) { | |
90 | shared_ptr<Session> session = add_session(); | |
91 | ||
92 | map<string, string> dev_info; | |
93 | shared_ptr<devices::HardwareDevice> other_device, demo_device; | |
94 | ||
95 | // Use any available device that's not demo | |
96 | for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) { | |
97 | if (dev->hardware_device()->driver()->name() == "demo") { | |
98 | demo_device = dev; | |
99 | } else { | |
100 | other_device = dev; | |
101 | } | |
102 | } | |
103 | ||
104 | // ...and if there isn't any, just use demo then | |
105 | session->main_bar()->select_device(other_device ? | |
106 | other_device : demo_device); | |
107 | } | |
108 | } | |
109 | ||
110 | MainWindow::~MainWindow() | |
111 | { | |
112 | while (!sessions_.empty()) | |
113 | remove_session(sessions_.front()); | |
114 | } | |
115 | ||
116 | QAction* MainWindow::action_view_sticky_scrolling() const | |
117 | { | |
118 | return action_view_sticky_scrolling_; | |
119 | } | |
120 | ||
121 | QAction* MainWindow::action_view_coloured_bg() const | |
122 | { | |
123 | return action_view_coloured_bg_; | |
124 | } | |
125 | ||
126 | QAction* MainWindow::action_about() const | |
127 | { | |
128 | return action_about_; | |
129 | } | |
130 | ||
131 | shared_ptr<views::ViewBase> MainWindow::get_active_view() const | |
132 | { | |
133 | // If there's only one view, use it... | |
134 | if (view_docks_.size() == 1) | |
135 | return view_docks_.begin()->second; | |
136 | ||
137 | // ...otherwise find the dock widget the widget with focus is contained in | |
138 | QObject *w = QApplication::focusWidget(); | |
139 | QDockWidget *dock = 0; | |
140 | ||
141 | while (w) { | |
142 | dock = qobject_cast<QDockWidget*>(w); | |
143 | if (dock) | |
144 | break; | |
145 | w = w->parent(); | |
146 | } | |
147 | ||
148 | // Get the view contained in the dock widget | |
149 | for (auto entry : view_docks_) | |
150 | if (entry.first == dock) | |
151 | return entry.second; | |
152 | ||
153 | return nullptr; | |
154 | } | |
155 | ||
156 | shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title, | |
157 | views::ViewType type, Session &session) | |
158 | { | |
159 | QMainWindow *main_window; | |
160 | for (auto entry : session_windows_) | |
161 | if (entry.first.get() == &session) | |
162 | main_window = entry.second; | |
163 | ||
164 | assert(main_window); | |
165 | ||
166 | if (type == views::ViewTypeTrace) { | |
167 | QDockWidget* dock = new QDockWidget(title, main_window); | |
168 | dock->setObjectName(title); | |
169 | main_window->addDockWidget(Qt::TopDockWidgetArea, dock); | |
170 | ||
171 | // Insert a QMainWindow into the dock widget to allow for a tool bar | |
172 | QMainWindow *dock_main = new QMainWindow(dock); | |
173 | dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag | |
174 | ||
175 | shared_ptr<views::TraceView::View> v = | |
176 | make_shared<views::TraceView::View>(session, dock_main); | |
177 | view_docks_[dock] = v; | |
178 | session.register_view(v); | |
179 | ||
180 | dock_main->setCentralWidget(v.get()); | |
181 | dock->setWidget(dock_main); | |
182 | ||
183 | dock->setFeatures(QDockWidget::DockWidgetMovable | | |
184 | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable); | |
185 | ||
186 | QAbstractButton *close_btn = | |
187 | dock->findChildren<QAbstractButton*> | |
188 | ("qt_dockwidget_closebutton").front(); | |
189 | ||
190 | connect(close_btn, SIGNAL(clicked(bool)), | |
191 | this, SLOT(on_view_close_clicked())); | |
192 | ||
193 | if (type == views::ViewTypeTrace) { | |
194 | connect(&session, SIGNAL(trigger_event(util::Timestamp)), | |
195 | qobject_cast<views::ViewBase*>(v.get()), | |
196 | SLOT(trigger_event(util::Timestamp))); | |
197 | ||
198 | v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); | |
199 | v->enable_coloured_bg(action_view_coloured_bg_->isChecked()); | |
200 | ||
201 | shared_ptr<MainBar> main_bar = session.main_bar(); | |
202 | if (!main_bar) { | |
203 | main_bar = make_shared<MainBar>(session, *this); | |
204 | dock_main->addToolBar(main_bar.get()); | |
205 | session.set_main_bar(main_bar); | |
206 | ||
207 | connect(main_bar.get(), SIGNAL(new_view(Session*)), | |
208 | this, SLOT(on_new_view(Session*))); | |
209 | } | |
210 | main_bar->action_view_show_cursors()->setChecked(v->cursors_shown()); | |
211 | ||
212 | connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), | |
213 | main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool))); | |
214 | } | |
215 | ||
216 | return v; | |
217 | } | |
218 | ||
219 | return nullptr; | |
220 | } | |
221 | ||
222 | shared_ptr<Session> MainWindow::add_session() | |
223 | { | |
224 | static int last_session_id = 1; | |
225 | QString name = tr("Untitled-%1").arg(last_session_id++); | |
226 | ||
227 | shared_ptr<Session> session = make_shared<Session>(device_manager_, name); | |
228 | ||
229 | connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)), | |
230 | this, SLOT(on_add_view(const QString&, views::ViewType, Session*))); | |
231 | connect(session.get(), SIGNAL(name_changed()), | |
232 | this, SLOT(on_session_name_changed())); | |
233 | ||
234 | sessions_.push_back(session); | |
235 | ||
236 | QMainWindow *window = new QMainWindow(); | |
237 | window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag | |
238 | session_windows_[session] = window; | |
239 | session_selector_.addTab(window, name); | |
240 | ||
241 | window->setDockNestingEnabled(true); | |
242 | ||
243 | shared_ptr<views::ViewBase> main_view = | |
244 | add_view(name, views::ViewTypeTrace, *session); | |
245 | ||
246 | return session; | |
247 | } | |
248 | ||
249 | void MainWindow::remove_session(shared_ptr<Session> session) | |
250 | { | |
251 | int h = new_session_button_->height(); | |
252 | ||
253 | for (shared_ptr<views::ViewBase> view : session->views()) { | |
254 | // Find the dock the view is contained in and remove it | |
255 | for (auto entry : view_docks_) | |
256 | if (entry.second == view) { | |
257 | // Remove the view from the session | |
258 | session->deregister_view(view); | |
259 | ||
260 | // Remove the view from its parent; otherwise, Qt will | |
261 | // call deleteLater() on it, which causes a double free | |
262 | // since the shared_ptr in view_docks_ doesn't know | |
263 | // that Qt keeps a pointer to the view around | |
264 | entry.second->setParent(0); | |
265 | ||
266 | // Remove this entry from the container | |
267 | view_docks_.erase(entry.first); | |
268 | } | |
269 | } | |
270 | ||
271 | QMainWindow *window = session_windows_.at(session); | |
272 | session_selector_.removeTab(session_selector_.indexOf(window)); | |
273 | ||
274 | session_windows_.erase(session); | |
275 | ||
276 | sessions_.remove_if([&](shared_ptr<Session> s) { | |
277 | return s == session; }); | |
278 | ||
279 | if (sessions_.empty()) { | |
280 | // When there are no more tabs, the height of the QTabWidget | |
281 | // drops to zero. We must prevent this to keep the static | |
282 | // widgets visible | |
283 | for (QWidget *w : static_tab_widget_->findChildren<QWidget*>()) | |
284 | w->setMinimumHeight(h); | |
285 | ||
286 | int margin = static_tab_widget_->layout()->contentsMargins().bottom(); | |
287 | static_tab_widget_->setMinimumHeight(h + 2 * margin); | |
288 | session_selector_.setMinimumHeight(h + 2 * margin); | |
289 | ||
290 | // Update the window title if there is no view left to | |
291 | // generate focus change events | |
292 | setWindowTitle(WindowTitle); | |
293 | } | |
294 | } | |
295 | ||
296 | void MainWindow::setup_ui() | |
297 | { | |
298 | setObjectName(QString::fromUtf8("MainWindow")); | |
299 | ||
300 | setCentralWidget(&session_selector_); | |
301 | ||
302 | // Set the window icon | |
303 | QIcon icon; | |
304 | icon.addFile(QString(":/icons/sigrok-logo-notext.png")); | |
305 | setWindowIcon(icon); | |
306 | ||
307 | action_view_sticky_scrolling_->setCheckable(true); | |
308 | action_view_sticky_scrolling_->setChecked(true); | |
309 | action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S)); | |
310 | action_view_sticky_scrolling_->setObjectName( | |
311 | QString::fromUtf8("actionViewStickyScrolling")); | |
312 | action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling")); | |
313 | ||
314 | action_view_coloured_bg_->setCheckable(true); | |
315 | action_view_coloured_bg_->setChecked(true); | |
316 | action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B)); | |
317 | action_view_coloured_bg_->setObjectName( | |
318 | QString::fromUtf8("actionViewColouredBg")); | |
319 | action_view_coloured_bg_->setText(tr("Use &coloured backgrounds")); | |
320 | ||
321 | action_about_->setObjectName(QString::fromUtf8("actionAbout")); | |
322 | action_about_->setText(tr("&About...")); | |
323 | ||
324 | // Set up the tab area | |
325 | new_session_button_ = new QToolButton(); | |
326 | new_session_button_->setIcon(QIcon::fromTheme("document-new", | |
327 | QIcon(":/icons/document-new.png"))); | |
328 | new_session_button_->setAutoRaise(true); | |
329 | ||
330 | QHBoxLayout* layout = new QHBoxLayout(); | |
331 | layout->setContentsMargins(2, 2, 2, 2); | |
332 | layout->addWidget(new_session_button_); | |
333 | ||
334 | static_tab_widget_ = new QWidget(); | |
335 | static_tab_widget_->setLayout(layout); | |
336 | ||
337 | session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner); | |
338 | session_selector_.setTabsClosable(true); | |
339 | ||
340 | connect(new_session_button_, SIGNAL(clicked(bool)), | |
341 | this, SLOT(on_new_session_clicked())); | |
342 | ||
343 | connect(&session_selector_, SIGNAL(tabCloseRequested(int)), | |
344 | this, SLOT(on_tab_close_requested(int))); | |
345 | connect(&session_selector_, SIGNAL(currentChanged(int)), | |
346 | this, SLOT(on_tab_changed(int))); | |
347 | ||
348 | ||
349 | connect(static_cast<QApplication *>(QCoreApplication::instance()), | |
350 | SIGNAL(focusChanged(QWidget*, QWidget*)), | |
351 | this, SLOT(on_focus_changed())); | |
352 | } | |
353 | ||
354 | void MainWindow::save_ui_settings() | |
355 | { | |
356 | QSettings settings; | |
357 | int id = 0; | |
358 | ||
359 | settings.beginGroup("MainWindow"); | |
360 | settings.setValue("state", saveState()); | |
361 | settings.setValue("geometry", saveGeometry()); | |
362 | settings.endGroup(); | |
363 | ||
364 | for (shared_ptr<Session> session : sessions_) { | |
365 | // Ignore sessions using the demo device or no device at all | |
366 | if (session->device()) { | |
367 | shared_ptr<devices::HardwareDevice> device = | |
368 | dynamic_pointer_cast< devices::HardwareDevice > | |
369 | (session->device()); | |
370 | ||
371 | if (device && | |
372 | device->hardware_device()->driver()->name() == "demo") | |
373 | continue; | |
374 | ||
375 | settings.beginGroup("Session" + QString::number(id++)); | |
376 | settings.remove(""); // Remove all keys in this group | |
377 | session->save_settings(settings); | |
378 | settings.endGroup(); | |
379 | } | |
380 | } | |
381 | ||
382 | settings.setValue("sessions", id); | |
383 | } | |
384 | ||
385 | void MainWindow::restore_ui_settings() | |
386 | { | |
387 | QSettings settings; | |
388 | int i, session_count; | |
389 | ||
390 | settings.beginGroup("MainWindow"); | |
391 | ||
392 | if (settings.contains("geometry")) { | |
393 | restoreGeometry(settings.value("geometry").toByteArray()); | |
394 | restoreState(settings.value("state").toByteArray()); | |
395 | } else | |
396 | resize(1000, 720); | |
397 | ||
398 | settings.endGroup(); | |
399 | ||
400 | session_count = settings.value("sessions", 0).toInt(); | |
401 | ||
402 | for (i = 0; i < session_count; i++) { | |
403 | settings.beginGroup("Session" + QString::number(i)); | |
404 | shared_ptr<Session> session = add_session(); | |
405 | session->restore_settings(settings); | |
406 | settings.endGroup(); | |
407 | } | |
408 | } | |
409 | ||
410 | std::shared_ptr<Session> MainWindow::get_tab_session(int index) const | |
411 | { | |
412 | // Find the session that belongs to the tab's main window | |
413 | for (auto entry : session_windows_) | |
414 | if (entry.second == session_selector_.widget(index)) | |
415 | return entry.first; | |
416 | ||
417 | return nullptr; | |
418 | } | |
419 | ||
420 | void MainWindow::closeEvent(QCloseEvent *event) | |
421 | { | |
422 | save_ui_settings(); | |
423 | event->accept(); | |
424 | } | |
425 | ||
426 | QMenu* MainWindow::createPopupMenu() | |
427 | { | |
428 | return nullptr; | |
429 | } | |
430 | ||
431 | bool MainWindow::restoreState(const QByteArray &state, int version) | |
432 | { | |
433 | (void)state; | |
434 | (void)version; | |
435 | ||
436 | // Do nothing. We don't want Qt to handle this, or else it | |
437 | // will try to restore all the dock widgets and create havoc. | |
438 | ||
439 | return false; | |
440 | } | |
441 | ||
442 | void MainWindow::on_add_view(const QString &title, views::ViewType type, | |
443 | Session *session) | |
444 | { | |
445 | // We get a pointer and need a reference | |
446 | for (std::shared_ptr<Session> s : sessions_) | |
447 | if (s.get() == session) | |
448 | add_view(title, type, *s); | |
449 | } | |
450 | ||
451 | void MainWindow::on_focus_changed() | |
452 | { | |
453 | static shared_ptr<Session> prev_session; | |
454 | ||
455 | shared_ptr<views::ViewBase> view = get_active_view(); | |
456 | ||
457 | if (view) { | |
458 | for (shared_ptr<Session> session : sessions_) { | |
459 | if (session->has_view(view)) { | |
460 | if (session != prev_session) { | |
461 | // Activate correct tab if necessary | |
462 | shared_ptr<Session> tab_session = get_tab_session( | |
463 | session_selector_.currentIndex()); | |
464 | if (tab_session != session) | |
465 | session_selector_.setCurrentWidget( | |
466 | session_windows_.at(session)); | |
467 | ||
468 | on_focused_session_changed(session); | |
469 | } | |
470 | ||
471 | prev_session = session; | |
472 | break; | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | if (sessions_.empty()) | |
478 | setWindowTitle(WindowTitle); | |
479 | } | |
480 | ||
481 | void MainWindow::on_focused_session_changed(shared_ptr<Session> session) | |
482 | { | |
483 | setWindowTitle(session->name() + " - " + WindowTitle); | |
484 | } | |
485 | ||
486 | void MainWindow::on_new_session_clicked() | |
487 | { | |
488 | add_session(); | |
489 | } | |
490 | ||
491 | void MainWindow::on_session_name_changed() | |
492 | { | |
493 | // Update the corresponding dock widget's name(s) | |
494 | Session *session = qobject_cast<Session*>(QObject::sender()); | |
495 | assert(session); | |
496 | ||
497 | for (shared_ptr<views::ViewBase> view : session->views()) { | |
498 | // Get the dock that contains the view | |
499 | for (auto entry : view_docks_) | |
500 | if (entry.second == view) { | |
501 | entry.first->setObjectName(session->name()); | |
502 | entry.first->setWindowTitle(session->name()); | |
503 | } | |
504 | } | |
505 | ||
506 | // Refresh window title if the affected session has focus | |
507 | shared_ptr<views::ViewBase> view = get_active_view(); | |
508 | ||
509 | if (view && session->has_view(view)) | |
510 | setWindowTitle(session->name() + " - " + WindowTitle); | |
511 | } | |
512 | ||
513 | void MainWindow::on_new_view(Session *session) | |
514 | { | |
515 | // We get a pointer and need a reference | |
516 | for (std::shared_ptr<Session> s : sessions_) | |
517 | if (s.get() == session) | |
518 | add_view(session->name(), views::ViewTypeTrace, *s); | |
519 | } | |
520 | ||
521 | void MainWindow::on_view_close_clicked() | |
522 | { | |
523 | // Find the dock widget that contains the close button that was clicked | |
524 | QObject *w = QObject::sender(); | |
525 | QDockWidget *dock = 0; | |
526 | ||
527 | while (w) { | |
528 | dock = qobject_cast<QDockWidget*>(w); | |
529 | if (dock) | |
530 | break; | |
531 | w = w->parent(); | |
532 | } | |
533 | ||
534 | // Get the view contained in the dock widget | |
535 | shared_ptr<views::ViewBase> view; | |
536 | ||
537 | for (auto entry : view_docks_) | |
538 | if (entry.first == dock) | |
539 | view = entry.second; | |
540 | ||
541 | // Deregister the view | |
542 | for (shared_ptr<Session> session : sessions_) { | |
543 | if (!session->has_view(view)) | |
544 | continue; | |
545 | ||
546 | // Also destroy the entire session if its main view is closing | |
547 | if (view == session->main_view()) { | |
548 | remove_session(session); | |
549 | break; | |
550 | } else | |
551 | session->deregister_view(view); | |
552 | } | |
553 | } | |
554 | ||
555 | void MainWindow::on_tab_changed(int index) | |
556 | { | |
557 | shared_ptr<Session> session = get_tab_session(index); | |
558 | ||
559 | if (session) | |
560 | on_focused_session_changed(session); | |
561 | } | |
562 | ||
563 | void MainWindow::on_tab_close_requested(int index) | |
564 | { | |
565 | // TODO Ask user if this is intended in case data is unsaved | |
566 | ||
567 | shared_ptr<Session> session = get_tab_session(index); | |
568 | ||
569 | if (session) | |
570 | remove_session(session); | |
571 | } | |
572 | ||
573 | void MainWindow::on_actionViewStickyScrolling_triggered() | |
574 | { | |
575 | shared_ptr<views::ViewBase> viewbase = get_active_view(); | |
576 | views::TraceView::View* view = | |
577 | qobject_cast<views::TraceView::View*>(viewbase.get()); | |
578 | if (view) | |
579 | view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); | |
580 | } | |
581 | ||
582 | void MainWindow::on_actionViewColouredBg_triggered() | |
583 | { | |
584 | shared_ptr<views::ViewBase> viewbase = get_active_view(); | |
585 | views::TraceView::View* view = | |
586 | qobject_cast<views::TraceView::View*>(viewbase.get()); | |
587 | if (view) | |
588 | view->enable_coloured_bg(action_view_coloured_bg_->isChecked()); | |
589 | } | |
590 | ||
591 | void MainWindow::on_actionAbout_triggered() | |
592 | { | |
593 | dialogs::About dlg(device_manager_.context(), this); | |
594 | dlg.exec(); | |
595 | } | |
596 | ||
597 | } // namespace pv |