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"
43 #include "devices/hardwaredevice.hpp"
44 #include "dialogs/about.hpp"
45 #include "toolbars/mainbar.hpp"
46 #include "view/view.hpp"
47 #include "views/trace/standardbar.hpp"
51 #include <libsigrokcxx/libsigrokcxx.hpp>
53 using std::dynamic_pointer_cast;
55 using std::make_shared;
57 using std::shared_ptr;
66 using toolbars::MainBar;
68 const QString MainWindow::WindowTitle = tr("PulseView");
70 MainWindow::MainWindow(DeviceManager &device_manager,
71 string open_file_name, string open_file_format,
74 device_manager_(device_manager),
75 session_selector_(this),
76 session_state_mapper_(this),
77 action_about_(new QAction(this)),
78 icon_red_(":/icons/status-red.svg"),
79 icon_green_(":/icons/status-green.svg"),
80 icon_grey_(":/icons/status-grey.svg")
82 qRegisterMetaType<util::Timestamp>("util::Timestamp");
83 qRegisterMetaType<uint64_t>("uint64_t");
86 restore_ui_settings();
88 if (!open_file_name.empty()) {
89 shared_ptr<Session> session = add_session();
90 session->load_init_file(open_file_name, open_file_format);
93 // Add empty default session if there aren't any sessions
94 if (sessions_.size() == 0) {
95 shared_ptr<Session> session = add_session();
97 map<string, string> dev_info;
98 shared_ptr<devices::HardwareDevice> other_device, demo_device;
100 // Use any available device that's not demo
101 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
102 if (dev->hardware_device()->driver()->name() == "demo") {
109 // ...and if there isn't any, just use demo then
110 session->select_device(other_device ? other_device : demo_device);
114 MainWindow::~MainWindow()
116 while (!sessions_.empty())
117 remove_session(sessions_.front());
120 QAction* MainWindow::action_about() const
122 return action_about_;
125 shared_ptr<views::ViewBase> MainWindow::get_active_view() const
127 // If there's only one view, use it...
128 if (view_docks_.size() == 1)
129 return view_docks_.begin()->second;
131 // ...otherwise find the dock widget the widget with focus is contained in
132 QObject *w = QApplication::focusWidget();
133 QDockWidget *dock = 0;
136 dock = qobject_cast<QDockWidget*>(w);
142 // Get the view contained in the dock widget
143 for (auto entry : view_docks_)
144 if (entry.first == dock)
150 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
151 views::ViewType type, Session &session)
153 QMainWindow *main_window = nullptr;
154 for (auto entry : session_windows_)
155 if (entry.first.get() == &session)
156 main_window = entry.second;
160 if (type == views::ViewTypeTrace) {
161 QDockWidget* dock = new QDockWidget(title, main_window);
162 dock->setObjectName(title);
163 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
165 // Insert a QMainWindow into the dock widget to allow for a tool bar
166 QMainWindow *dock_main = new QMainWindow(dock);
167 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
169 shared_ptr<views::TraceView::View> v =
170 make_shared<views::TraceView::View>(session, dock_main);
171 view_docks_[dock] = v;
172 session.register_view(v);
174 dock_main->setCentralWidget(v.get());
175 dock->setWidget(dock_main);
177 dock->setFeatures(QDockWidget::DockWidgetMovable |
178 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
180 QAbstractButton *close_btn =
181 dock->findChildren<QAbstractButton*>
182 ("qt_dockwidget_closebutton").front();
184 connect(close_btn, SIGNAL(clicked(bool)),
185 this, SLOT(on_view_close_clicked()));
187 if (type == views::ViewTypeTrace) {
188 connect(&session, SIGNAL(trigger_event(util::Timestamp)),
189 qobject_cast<views::ViewBase*>(v.get()),
190 SLOT(trigger_event(util::Timestamp)));
192 v->enable_sticky_scrolling(true);
193 v->enable_coloured_bg(true);
195 shared_ptr<MainBar> main_bar = session.main_bar();
197 /* Initial view, create the main bar */
198 main_bar = make_shared<MainBar>(session, this, v.get());
199 dock_main->addToolBar(main_bar.get());
200 session.set_main_bar(main_bar);
202 connect(main_bar.get(), SIGNAL(new_view(Session*)),
203 this, SLOT(on_new_view(Session*)));
205 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
207 /* For the main view we need to prevent the dock widget from
208 * closing itself when its close button is clicked. This is
209 * so we can confirm with the user first. Regular views don't
211 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
213 /* Additional view, create a standard bar */
214 pv::views::trace::StandardBar *standard_bar =
215 new pv::views::trace::StandardBar(session, this, v.get());
216 dock_main->addToolBar(standard_bar);
218 standard_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
228 void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
230 for (shared_ptr<Session> session : sessions_) {
231 if (!session->has_view(view))
234 // Find the dock the view is contained in and remove it
235 for (auto entry : view_docks_)
236 if (entry.second == view) {
237 // Remove the view from the session
238 session->deregister_view(view);
240 // Remove the view from its parent; otherwise, Qt will
241 // call deleteLater() on it, which causes a double free
242 // since the shared_ptr in view_docks_ doesn't know
243 // that Qt keeps a pointer to the view around
246 // Delete the view's dock widget and all widgets inside it
247 entry.first->deleteLater();
249 // Remove the dock widget from the list and stop iterating
250 view_docks_.erase(entry.first);
256 shared_ptr<Session> MainWindow::add_session()
258 static int last_session_id = 1;
259 QString name = tr("Session %1").arg(last_session_id++);
261 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
263 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
264 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
265 connect(session.get(), SIGNAL(name_changed()),
266 this, SLOT(on_session_name_changed()));
267 session_state_mapper_.setMapping(session.get(), session.get());
268 connect(session.get(), SIGNAL(capture_state_changed(int)),
269 &session_state_mapper_, SLOT(map()));
271 sessions_.push_back(session);
273 QMainWindow *window = new QMainWindow();
274 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
275 session_windows_[session] = window;
277 int index = session_selector_.addTab(window, name);
278 session_selector_.setCurrentIndex(index);
279 last_focused_session_ = session;
281 window->setDockNestingEnabled(true);
283 shared_ptr<views::ViewBase> main_view =
284 add_view(name, views::ViewTypeTrace, *session);
289 void MainWindow::remove_session(shared_ptr<Session> session)
291 int h = new_session_button_->height();
293 for (shared_ptr<views::ViewBase> view : session->views())
296 QMainWindow *window = session_windows_.at(session);
297 session_selector_.removeTab(session_selector_.indexOf(window));
299 session_windows_.erase(session);
301 if (last_focused_session_ == session)
302 last_focused_session_.reset();
304 sessions_.remove_if([&](shared_ptr<Session> s) {
305 return s == session; });
307 if (sessions_.empty()) {
308 // When there are no more tabs, the height of the QTabWidget
309 // drops to zero. We must prevent this to keep the static
311 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
312 w->setMinimumHeight(h);
314 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
315 static_tab_widget_->setMinimumHeight(h + 2 * margin);
316 session_selector_.setMinimumHeight(h + 2 * margin);
318 // Update the window title if there is no view left to
319 // generate focus change events
320 setWindowTitle(WindowTitle);
324 void MainWindow::setup_ui()
326 setObjectName(QString::fromUtf8("MainWindow"));
328 setCentralWidget(&session_selector_);
330 // Set the window icon
332 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
335 view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut()));
336 view_sticky_scrolling_shortcut_->setAutoRepeat(false);
338 view_coloured_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_coloured_bg_shortcut()));
339 view_coloured_bg_shortcut_->setAutoRepeat(false);
341 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
342 action_about_->setToolTip(tr("&About..."));
344 // Set up the tab area
345 new_session_button_ = new QToolButton();
346 new_session_button_->setIcon(QIcon::fromTheme("document-new",
347 QIcon(":/icons/document-new.png")));
348 new_session_button_->setToolTip(tr("Create New Session"));
349 new_session_button_->setAutoRaise(true);
351 run_stop_button_ = new QToolButton();
352 run_stop_button_->setAutoRaise(true);
353 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
354 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
356 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
357 run_stop_shortcut_->setAutoRepeat(false);
359 settings_button_ = new QToolButton();
360 settings_button_->setIcon(QIcon::fromTheme("configure",
361 QIcon(":/icons/configure.png")));
362 settings_button_->setToolTip(tr("Settings"));
363 settings_button_->setAutoRaise(true);
365 QFrame *separator1 = new QFrame();
366 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
367 QFrame *separator2 = new QFrame();
368 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
370 QHBoxLayout* layout = new QHBoxLayout();
371 layout->setContentsMargins(2, 2, 2, 2);
372 layout->addWidget(new_session_button_);
373 layout->addWidget(separator1);
374 layout->addWidget(run_stop_button_);
375 layout->addWidget(separator2);
376 layout->addWidget(settings_button_);
378 static_tab_widget_ = new QWidget();
379 static_tab_widget_->setLayout(layout);
381 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
382 session_selector_.setTabsClosable(true);
384 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
385 close_application_shortcut_->setAutoRepeat(false);
387 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab()));
389 connect(new_session_button_, SIGNAL(clicked(bool)),
390 this, SLOT(on_new_session_clicked()));
391 connect(run_stop_button_, SIGNAL(clicked(bool)),
392 this, SLOT(on_run_stop_clicked()));
393 connect(&session_state_mapper_, SIGNAL(mapped(QObject*)),
394 this, SLOT(on_capture_state_changed(QObject*)));
396 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
397 this, SLOT(on_tab_close_requested(int)));
398 connect(&session_selector_, SIGNAL(currentChanged(int)),
399 this, SLOT(on_tab_changed(int)));
402 connect(static_cast<QApplication *>(QCoreApplication::instance()),
403 SIGNAL(focusChanged(QWidget*, QWidget*)),
404 this, SLOT(on_focus_changed()));
407 void MainWindow::save_ui_settings()
412 settings.beginGroup("MainWindow");
413 settings.setValue("state", saveState());
414 settings.setValue("geometry", saveGeometry());
417 for (shared_ptr<Session> session : sessions_) {
418 // Ignore sessions using the demo device or no device at all
419 if (session->device()) {
420 shared_ptr<devices::HardwareDevice> device =
421 dynamic_pointer_cast< devices::HardwareDevice >
425 device->hardware_device()->driver()->name() == "demo")
428 settings.beginGroup("Session" + QString::number(id++));
429 settings.remove(""); // Remove all keys in this group
430 session->save_settings(settings);
435 settings.setValue("sessions", id);
438 void MainWindow::restore_ui_settings()
441 int i, session_count;
443 settings.beginGroup("MainWindow");
445 if (settings.contains("geometry")) {
446 restoreGeometry(settings.value("geometry").toByteArray());
447 restoreState(settings.value("state").toByteArray());
453 session_count = settings.value("sessions", 0).toInt();
455 for (i = 0; i < session_count; i++) {
456 settings.beginGroup("Session" + QString::number(i));
457 shared_ptr<Session> session = add_session();
458 session->restore_settings(settings);
463 std::shared_ptr<Session> MainWindow::get_tab_session(int index) const
465 // Find the session that belongs to the tab's main window
466 for (auto entry : session_windows_)
467 if (entry.second == session_selector_.widget(index))
473 void MainWindow::closeEvent(QCloseEvent *event)
475 bool data_saved = true;
477 for (auto entry : session_windows_)
478 if (!entry.first->data_saved())
481 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
482 tr("There is unsaved data. Close anyway?"),
483 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
491 QMenu* MainWindow::createPopupMenu()
496 bool MainWindow::restoreState(const QByteArray &state, int version)
501 // Do nothing. We don't want Qt to handle this, or else it
502 // will try to restore all the dock widgets and create havoc.
507 void MainWindow::session_error(const QString text, const QString info_text)
509 QMetaObject::invokeMethod(this, "show_session_error",
510 Qt::QueuedConnection, Q_ARG(QString, text),
511 Q_ARG(QString, info_text));
514 void MainWindow::show_session_error(const QString text, const QString info_text)
516 QMessageBox msg(this);
518 msg.setInformativeText(info_text);
519 msg.setStandardButtons(QMessageBox::Ok);
520 msg.setIcon(QMessageBox::Warning);
524 void MainWindow::on_add_view(const QString &title, views::ViewType type,
527 // We get a pointer and need a reference
528 for (std::shared_ptr<Session> s : sessions_)
529 if (s.get() == session)
530 add_view(title, type, *s);
533 void MainWindow::on_focus_changed()
535 shared_ptr<views::ViewBase> view = get_active_view();
538 for (shared_ptr<Session> session : sessions_) {
539 if (session->has_view(view)) {
540 if (session != last_focused_session_) {
541 // Activate correct tab if necessary
542 shared_ptr<Session> tab_session = get_tab_session(
543 session_selector_.currentIndex());
544 if (tab_session != session)
545 session_selector_.setCurrentWidget(
546 session_windows_.at(session));
548 on_focused_session_changed(session);
556 if (sessions_.empty())
557 setWindowTitle(WindowTitle);
560 void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
562 last_focused_session_ = session;
564 setWindowTitle(session->name() + " - " + WindowTitle);
566 // Update the state of the run/stop button, too
567 on_capture_state_changed(session.get());
570 void MainWindow::on_new_session_clicked()
575 void MainWindow::on_run_stop_clicked()
577 shared_ptr<Session> session = last_focused_session_;
582 switch (session->get_capture_state()) {
583 case Session::Stopped:
584 session->start_capture([&](QString message) {
585 session_error("Capture failed", message); });
587 case Session::AwaitingTrigger:
588 case Session::Running:
589 session->stop_capture();
594 void MainWindow::on_session_name_changed()
596 // Update the corresponding dock widget's name(s)
597 Session *session = qobject_cast<Session*>(QObject::sender());
600 for (shared_ptr<views::ViewBase> view : session->views()) {
601 // Get the dock that contains the view
602 for (auto entry : view_docks_)
603 if (entry.second == view) {
604 entry.first->setObjectName(session->name());
605 entry.first->setWindowTitle(session->name());
609 // Update the tab widget by finding the main window and the tab from that
610 for (auto entry : session_windows_)
611 if (entry.first.get() == session) {
612 QMainWindow *window = entry.second;
613 const int index = session_selector_.indexOf(window);
614 session_selector_.setTabText(index, session->name());
617 // Refresh window title if the affected session has focus
618 if (session == last_focused_session_.get())
619 setWindowTitle(session->name() + " - " + WindowTitle);
622 void MainWindow::on_capture_state_changed(QObject *obj)
624 Session *caller = qobject_cast<Session*>(obj);
626 // Ignore if caller is not the currently focused session
627 // unless there is only one session
628 if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
631 int state = caller->get_capture_state();
633 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
634 run_stop_button_->setIcon(*icons[state]);
635 run_stop_button_->setText((state == pv::Session::Stopped) ?
636 tr("Run") : tr("Stop"));
639 void MainWindow::on_new_view(Session *session)
641 // We get a pointer and need a reference
642 for (std::shared_ptr<Session> s : sessions_)
643 if (s.get() == session)
644 add_view(session->name(), views::ViewTypeTrace, *s);
647 void MainWindow::on_view_close_clicked()
649 // Find the dock widget that contains the close button that was clicked
650 QObject *w = QObject::sender();
651 QDockWidget *dock = 0;
654 dock = qobject_cast<QDockWidget*>(w);
660 // Get the view contained in the dock widget
661 shared_ptr<views::ViewBase> view;
663 for (auto entry : view_docks_)
664 if (entry.first == dock)
667 // Deregister the view
668 for (shared_ptr<Session> session : sessions_) {
669 if (!session->has_view(view))
672 // Also destroy the entire session if its main view is closing...
673 if (view == session->main_view()) {
674 // ...but only if data is saved or the user confirms closing
675 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
676 tr("This session contains unsaved data. Close it anyway?"),
677 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
678 remove_session(session);
681 // All other views can be closed at any time as no data will be lost
686 void MainWindow::on_tab_changed(int index)
688 shared_ptr<Session> session = get_tab_session(index);
691 on_focused_session_changed(session);
694 void MainWindow::on_tab_close_requested(int index)
696 shared_ptr<Session> session = get_tab_session(index);
700 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
701 tr("This session contains unsaved data. Close it anyway?"),
702 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
703 remove_session(session);
706 void MainWindow::on_view_sticky_scrolling_shortcut()
708 shared_ptr<views::ViewBase> viewbase = get_active_view();
709 views::TraceView::View* view =
710 qobject_cast<views::TraceView::View*>(viewbase.get());
712 view->toggle_sticky_scrolling();
715 void MainWindow::on_view_coloured_bg_shortcut()
717 shared_ptr<views::ViewBase> viewbase = get_active_view();
718 views::TraceView::View* view =
719 qobject_cast<views::TraceView::View*>(viewbase.get());
721 view->toggle_coloured_bg();
724 void MainWindow::on_actionAbout_triggered()
726 dialogs::About dlg(device_manager_.context(), this);
730 void MainWindow::on_close_current_tab()
732 int tab = session_selector_.currentIndex();
734 on_tab_close_requested(tab);