2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include <libsigrokdecode/libsigrokdecode.h>
30 #include <QApplication>
31 #include <QCloseEvent>
32 #include <QDockWidget>
33 #include <QHBoxLayout>
34 #include <QMessageBox>
39 #include "mainwindow.hpp"
41 #include "devicemanager.hpp"
42 #include "globalsettings.hpp"
44 #include "devices/hardwaredevice.hpp"
45 #include "dialogs/about.hpp"
46 #include "dialogs/settings.hpp"
47 #include "toolbars/mainbar.hpp"
48 #include "view/view.hpp"
49 #include "views/trace/standardbar.hpp"
53 #include <libsigrokcxx/libsigrokcxx.hpp>
55 using std::dynamic_pointer_cast;
57 using std::make_shared;
59 using std::shared_ptr;
68 using toolbars::MainBar;
71 using std::placeholders::_1;
73 const QString MainWindow::WindowTitle = tr("PulseView");
75 MainWindow::MainWindow(DeviceManager &device_manager,
76 string open_file_name, string open_file_format,
79 device_manager_(device_manager),
80 session_selector_(this),
81 session_state_mapper_(this),
82 action_about_(new QAction(this)),
83 icon_red_(":/icons/status-red.svg"),
84 icon_green_(":/icons/status-green.svg"),
85 icon_grey_(":/icons/status-grey.svg")
87 qRegisterMetaType<util::Timestamp>("util::Timestamp");
88 qRegisterMetaType<uint64_t>("uint64_t");
91 restore_ui_settings();
93 if (!open_file_name.empty()) {
94 shared_ptr<Session> session = add_session();
95 session->load_init_file(open_file_name, open_file_format);
98 // Add empty default session if there aren't any sessions
99 if (sessions_.size() == 0) {
100 shared_ptr<Session> session = add_session();
102 map<string, string> dev_info;
103 shared_ptr<devices::HardwareDevice> other_device, demo_device;
105 // Use any available device that's not demo
106 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
107 if (dev->hardware_device()->driver()->name() == "demo") {
114 // ...and if there isn't any, just use demo then
115 session->select_device(other_device ? other_device : demo_device);
119 MainWindow::~MainWindow()
121 while (!sessions_.empty())
122 remove_session(sessions_.front());
125 QAction* MainWindow::action_about() const
127 return action_about_;
130 shared_ptr<views::ViewBase> MainWindow::get_active_view() const
132 // If there's only one view, use it...
133 if (view_docks_.size() == 1)
134 return view_docks_.begin()->second;
136 // ...otherwise find the dock widget the widget with focus is contained in
137 QObject *w = QApplication::focusWidget();
138 QDockWidget *dock = 0;
141 dock = qobject_cast<QDockWidget*>(w);
147 // Get the view contained in the dock widget
148 for (auto entry : view_docks_)
149 if (entry.first == dock)
155 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
156 views::ViewType type, Session &session)
158 QMainWindow *main_window = nullptr;
159 for (auto entry : session_windows_)
160 if (entry.first.get() == &session)
161 main_window = entry.second;
165 if (type == views::ViewTypeTrace) {
166 QDockWidget* dock = new QDockWidget(title, main_window);
167 dock->setObjectName(title);
168 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
170 // Insert a QMainWindow into the dock widget to allow for a tool bar
171 QMainWindow *dock_main = new QMainWindow(dock);
172 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
174 shared_ptr<views::TraceView::View> v =
175 make_shared<views::TraceView::View>(session, dock_main);
176 view_docks_[dock] = v;
177 session.register_view(v);
179 dock_main->setCentralWidget(v.get());
180 dock->setWidget(dock_main);
182 dock->setFeatures(QDockWidget::DockWidgetMovable |
183 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
185 QAbstractButton *close_btn =
186 dock->findChildren<QAbstractButton*>
187 ("qt_dockwidget_closebutton").front();
189 connect(close_btn, SIGNAL(clicked(bool)),
190 this, SLOT(on_view_close_clicked()));
192 if (type == views::ViewTypeTrace) {
193 connect(&session, SIGNAL(trigger_event(util::Timestamp)),
194 qobject_cast<views::ViewBase*>(v.get()),
195 SLOT(trigger_event(util::Timestamp)));
197 v->enable_sticky_scrolling(true);
198 v->enable_coloured_bg(true);
200 shared_ptr<MainBar> main_bar = session.main_bar();
202 /* Initial view, create the main bar */
203 main_bar = make_shared<MainBar>(session, this, v.get());
204 dock_main->addToolBar(main_bar.get());
205 session.set_main_bar(main_bar);
207 connect(main_bar.get(), SIGNAL(new_view(Session*)),
208 this, SLOT(on_new_view(Session*)));
210 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
212 /* For the main view we need to prevent the dock widget from
213 * closing itself when its close button is clicked. This is
214 * so we can confirm with the user first. Regular views don't
216 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
218 /* Additional view, create a standard bar */
219 pv::views::trace::StandardBar *standard_bar =
220 new pv::views::trace::StandardBar(session, this, v.get());
221 dock_main->addToolBar(standard_bar);
223 standard_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
233 void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
235 for (shared_ptr<Session> session : sessions_) {
236 if (!session->has_view(view))
239 // Find the dock the view is contained in and remove it
240 for (auto entry : view_docks_)
241 if (entry.second == view) {
242 // Remove the view from the session
243 session->deregister_view(view);
245 // Remove the view from its parent; otherwise, Qt will
246 // call deleteLater() on it, which causes a double free
247 // since the shared_ptr in view_docks_ doesn't know
248 // that Qt keeps a pointer to the view around
251 // Delete the view's dock widget and all widgets inside it
252 entry.first->deleteLater();
254 // Remove the dock widget from the list and stop iterating
255 view_docks_.erase(entry.first);
261 shared_ptr<Session> MainWindow::add_session()
263 static int last_session_id = 1;
264 QString name = tr("Session %1").arg(last_session_id++);
266 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
268 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
269 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
270 connect(session.get(), SIGNAL(name_changed()),
271 this, SLOT(on_session_name_changed()));
272 session_state_mapper_.setMapping(session.get(), session.get());
273 connect(session.get(), SIGNAL(capture_state_changed(int)),
274 &session_state_mapper_, SLOT(map()));
276 sessions_.push_back(session);
278 QMainWindow *window = new QMainWindow();
279 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
280 session_windows_[session] = window;
282 int index = session_selector_.addTab(window, name);
283 session_selector_.setCurrentIndex(index);
284 last_focused_session_ = session;
286 window->setDockNestingEnabled(true);
288 shared_ptr<views::ViewBase> main_view =
289 add_view(name, views::ViewTypeTrace, *session);
294 void MainWindow::remove_session(shared_ptr<Session> session)
296 int h = new_session_button_->height();
298 for (shared_ptr<views::ViewBase> view : session->views())
301 QMainWindow *window = session_windows_.at(session);
302 session_selector_.removeTab(session_selector_.indexOf(window));
304 session_windows_.erase(session);
306 if (last_focused_session_ == session)
307 last_focused_session_.reset();
309 sessions_.remove_if([&](shared_ptr<Session> s) {
310 return s == session; });
312 if (sessions_.empty()) {
313 // When there are no more tabs, the height of the QTabWidget
314 // drops to zero. We must prevent this to keep the static
316 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
317 w->setMinimumHeight(h);
319 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
320 static_tab_widget_->setMinimumHeight(h + 2 * margin);
321 session_selector_.setMinimumHeight(h + 2 * margin);
323 // Update the window title if there is no view left to
324 // generate focus change events
325 setWindowTitle(WindowTitle);
329 void MainWindow::setup_ui()
331 setObjectName(QString::fromUtf8("MainWindow"));
333 setCentralWidget(&session_selector_);
335 // Set the window icon
337 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
340 view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut()));
341 view_sticky_scrolling_shortcut_->setAutoRepeat(false);
343 view_coloured_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_coloured_bg_shortcut()));
344 view_coloured_bg_shortcut_->setAutoRepeat(false);
346 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
347 action_about_->setToolTip(tr("&About..."));
349 // Set up the tab area
350 new_session_button_ = new QToolButton();
351 new_session_button_->setIcon(QIcon::fromTheme("document-new",
352 QIcon(":/icons/document-new.png")));
353 new_session_button_->setToolTip(tr("Create New Session"));
354 new_session_button_->setAutoRaise(true);
356 run_stop_button_ = new QToolButton();
357 run_stop_button_->setAutoRaise(true);
358 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
359 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
361 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
362 run_stop_shortcut_->setAutoRepeat(false);
364 settings_button_ = new QToolButton();
365 settings_button_->setIcon(QIcon::fromTheme("configure",
366 QIcon(":/icons/configure.png")));
367 settings_button_->setToolTip(tr("Settings"));
368 settings_button_->setAutoRaise(true);
370 QFrame *separator1 = new QFrame();
371 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
372 QFrame *separator2 = new QFrame();
373 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
375 QHBoxLayout* layout = new QHBoxLayout();
376 layout->setContentsMargins(2, 2, 2, 2);
377 layout->addWidget(new_session_button_);
378 layout->addWidget(separator1);
379 layout->addWidget(run_stop_button_);
380 layout->addWidget(separator2);
381 layout->addWidget(settings_button_);
383 static_tab_widget_ = new QWidget();
384 static_tab_widget_->setLayout(layout);
386 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
387 session_selector_.setTabsClosable(true);
389 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
390 close_application_shortcut_->setAutoRepeat(false);
392 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab()));
394 connect(new_session_button_, SIGNAL(clicked(bool)),
395 this, SLOT(on_new_session_clicked()));
396 connect(run_stop_button_, SIGNAL(clicked(bool)),
397 this, SLOT(on_run_stop_clicked()));
398 connect(&session_state_mapper_, SIGNAL(mapped(QObject*)),
399 this, SLOT(on_capture_state_changed(QObject*)));
400 connect(settings_button_, SIGNAL(clicked(bool)),
401 this, SLOT(on_settings_clicked()));
403 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
404 this, SLOT(on_tab_close_requested(int)));
405 connect(&session_selector_, SIGNAL(currentChanged(int)),
406 this, SLOT(on_tab_changed(int)));
409 connect(static_cast<QApplication *>(QCoreApplication::instance()),
410 SIGNAL(focusChanged(QWidget*, QWidget*)),
411 this, SLOT(on_focus_changed()));
414 void MainWindow::save_ui_settings()
419 settings.beginGroup("MainWindow");
420 settings.setValue("state", saveState());
421 settings.setValue("geometry", saveGeometry());
424 for (shared_ptr<Session> session : sessions_) {
425 // Ignore sessions using the demo device or no device at all
426 if (session->device()) {
427 shared_ptr<devices::HardwareDevice> device =
428 dynamic_pointer_cast< devices::HardwareDevice >
432 device->hardware_device()->driver()->name() == "demo")
435 settings.beginGroup("Session" + QString::number(id++));
436 settings.remove(""); // Remove all keys in this group
437 session->save_settings(settings);
442 settings.setValue("sessions", id);
445 void MainWindow::restore_ui_settings()
448 int i, session_count;
450 settings.beginGroup("MainWindow");
452 if (settings.contains("geometry")) {
453 restoreGeometry(settings.value("geometry").toByteArray());
454 restoreState(settings.value("state").toByteArray());
460 session_count = settings.value("sessions", 0).toInt();
462 for (i = 0; i < session_count; i++) {
463 settings.beginGroup("Session" + QString::number(i));
464 shared_ptr<Session> session = add_session();
465 session->restore_settings(settings);
470 std::shared_ptr<Session> MainWindow::get_tab_session(int index) const
472 // Find the session that belongs to the tab's main window
473 for (auto entry : session_windows_)
474 if (entry.second == session_selector_.widget(index))
480 void MainWindow::closeEvent(QCloseEvent *event)
482 bool data_saved = true;
484 for (auto entry : session_windows_)
485 if (!entry.first->data_saved())
488 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
489 tr("There is unsaved data. Close anyway?"),
490 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
498 QMenu* MainWindow::createPopupMenu()
503 bool MainWindow::restoreState(const QByteArray &state, int version)
508 // Do nothing. We don't want Qt to handle this, or else it
509 // will try to restore all the dock widgets and create havoc.
514 void MainWindow::session_error(const QString text, const QString info_text)
516 QMetaObject::invokeMethod(this, "show_session_error",
517 Qt::QueuedConnection, Q_ARG(QString, text),
518 Q_ARG(QString, info_text));
521 void MainWindow::show_session_error(const QString text, const QString info_text)
523 QMessageBox msg(this);
525 msg.setInformativeText(info_text);
526 msg.setStandardButtons(QMessageBox::Ok);
527 msg.setIcon(QMessageBox::Warning);
531 void MainWindow::on_add_view(const QString &title, views::ViewType type,
534 // We get a pointer and need a reference
535 for (std::shared_ptr<Session> s : sessions_)
536 if (s.get() == session)
537 add_view(title, type, *s);
540 void MainWindow::on_focus_changed()
542 shared_ptr<views::ViewBase> view = get_active_view();
545 for (shared_ptr<Session> session : sessions_) {
546 if (session->has_view(view)) {
547 if (session != last_focused_session_) {
548 // Activate correct tab if necessary
549 shared_ptr<Session> tab_session = get_tab_session(
550 session_selector_.currentIndex());
551 if (tab_session != session)
552 session_selector_.setCurrentWidget(
553 session_windows_.at(session));
555 on_focused_session_changed(session);
563 if (sessions_.empty())
564 setWindowTitle(WindowTitle);
567 void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
569 last_focused_session_ = session;
571 setWindowTitle(session->name() + " - " + WindowTitle);
573 // Update the state of the run/stop button, too
574 on_capture_state_changed(session.get());
577 void MainWindow::on_new_session_clicked()
582 void MainWindow::on_run_stop_clicked()
584 shared_ptr<Session> session = last_focused_session_;
589 switch (session->get_capture_state()) {
590 case Session::Stopped:
591 session->start_capture([&](QString message) {
592 session_error("Capture failed", message); });
594 case Session::AwaitingTrigger:
595 case Session::Running:
596 session->stop_capture();
601 void MainWindow::on_settings_clicked()
603 dialogs::Settings dlg;
607 void MainWindow::on_session_name_changed()
609 // Update the corresponding dock widget's name(s)
610 Session *session = qobject_cast<Session*>(QObject::sender());
613 for (shared_ptr<views::ViewBase> view : session->views()) {
614 // Get the dock that contains the view
615 for (auto entry : view_docks_)
616 if (entry.second == view) {
617 entry.first->setObjectName(session->name());
618 entry.first->setWindowTitle(session->name());
622 // Update the tab widget by finding the main window and the tab from that
623 for (auto entry : session_windows_)
624 if (entry.first.get() == session) {
625 QMainWindow *window = entry.second;
626 const int index = session_selector_.indexOf(window);
627 session_selector_.setTabText(index, session->name());
630 // Refresh window title if the affected session has focus
631 if (session == last_focused_session_.get())
632 setWindowTitle(session->name() + " - " + WindowTitle);
635 void MainWindow::on_capture_state_changed(QObject *obj)
637 Session *caller = qobject_cast<Session*>(obj);
639 // Ignore if caller is not the currently focused session
640 // unless there is only one session
641 if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
644 int state = caller->get_capture_state();
646 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
647 run_stop_button_->setIcon(*icons[state]);
648 run_stop_button_->setText((state == pv::Session::Stopped) ?
649 tr("Run") : tr("Stop"));
652 void MainWindow::on_new_view(Session *session)
654 // We get a pointer and need a reference
655 for (std::shared_ptr<Session> s : sessions_)
656 if (s.get() == session)
657 add_view(session->name(), views::ViewTypeTrace, *s);
660 void MainWindow::on_view_close_clicked()
662 // Find the dock widget that contains the close button that was clicked
663 QObject *w = QObject::sender();
664 QDockWidget *dock = 0;
667 dock = qobject_cast<QDockWidget*>(w);
673 // Get the view contained in the dock widget
674 shared_ptr<views::ViewBase> view;
676 for (auto entry : view_docks_)
677 if (entry.first == dock)
680 // Deregister the view
681 for (shared_ptr<Session> session : sessions_) {
682 if (!session->has_view(view))
685 // Also destroy the entire session if its main view is closing...
686 if (view == session->main_view()) {
687 // ...but only if data is saved or the user confirms closing
688 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
689 tr("This session contains unsaved data. Close it anyway?"),
690 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
691 remove_session(session);
694 // All other views can be closed at any time as no data will be lost
699 void MainWindow::on_tab_changed(int index)
701 shared_ptr<Session> session = get_tab_session(index);
704 on_focused_session_changed(session);
707 void MainWindow::on_tab_close_requested(int index)
709 shared_ptr<Session> session = get_tab_session(index);
713 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
714 tr("This session contains unsaved data. Close it anyway?"),
715 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
716 remove_session(session);
719 void MainWindow::on_view_sticky_scrolling_shortcut()
721 shared_ptr<views::ViewBase> viewbase = get_active_view();
722 views::TraceView::View* view =
723 qobject_cast<views::TraceView::View*>(viewbase.get());
725 view->toggle_sticky_scrolling();
728 void MainWindow::on_view_coloured_bg_shortcut()
730 shared_ptr<views::ViewBase> viewbase = get_active_view();
731 views::TraceView::View* view =
732 qobject_cast<views::TraceView::View*>(viewbase.get());
734 view->toggle_coloured_bg();
737 void MainWindow::on_actionAbout_triggered()
739 dialogs::About dlg(device_manager_.context(), this);
743 void MainWindow::on_close_current_tab()
745 int tab = session_selector_.currentIndex();
747 on_tab_close_requested(tab);