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_view_sticky_scrolling_(new QAction(this)),
78 action_view_coloured_bg_(new QAction(this)),
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")
84 qRegisterMetaType<util::Timestamp>("util::Timestamp");
85 qRegisterMetaType<uint64_t>("uint64_t");
88 restore_ui_settings();
90 if (!open_file_name.empty()) {
91 shared_ptr<Session> session = add_session();
92 session->load_init_file(open_file_name, open_file_format);
95 // Add empty default session if there aren't any sessions
96 if (sessions_.size() == 0) {
97 shared_ptr<Session> session = add_session();
99 map<string, string> dev_info;
100 shared_ptr<devices::HardwareDevice> other_device, demo_device;
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") {
111 // ...and if there isn't any, just use demo then
112 session->select_device(other_device ? other_device : demo_device);
116 MainWindow::~MainWindow()
118 while (!sessions_.empty())
119 remove_session(sessions_.front());
122 QAction* MainWindow::action_view_sticky_scrolling() const
124 return action_view_sticky_scrolling_;
127 QAction* MainWindow::action_view_coloured_bg() const
129 return action_view_coloured_bg_;
132 QAction* MainWindow::action_about() const
134 return action_about_;
137 shared_ptr<views::ViewBase> MainWindow::get_active_view() const
139 // If there's only one view, use it...
140 if (view_docks_.size() == 1)
141 return view_docks_.begin()->second;
143 // ...otherwise find the dock widget the widget with focus is contained in
144 QObject *w = QApplication::focusWidget();
145 QDockWidget *dock = 0;
148 dock = qobject_cast<QDockWidget*>(w);
154 // Get the view contained in the dock widget
155 for (auto entry : view_docks_)
156 if (entry.first == dock)
162 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
163 views::ViewType type, Session &session)
165 QMainWindow *main_window = nullptr;
166 for (auto entry : session_windows_)
167 if (entry.first.get() == &session)
168 main_window = entry.second;
172 if (type == views::ViewTypeTrace) {
173 QDockWidget* dock = new QDockWidget(title, main_window);
174 dock->setObjectName(title);
175 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
177 // Insert a QMainWindow into the dock widget to allow for a tool bar
178 QMainWindow *dock_main = new QMainWindow(dock);
179 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
181 shared_ptr<views::TraceView::View> v =
182 make_shared<views::TraceView::View>(session, dock_main);
183 view_docks_[dock] = v;
184 session.register_view(v);
186 dock_main->setCentralWidget(v.get());
187 dock->setWidget(dock_main);
189 dock->setFeatures(QDockWidget::DockWidgetMovable |
190 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
192 QAbstractButton *close_btn =
193 dock->findChildren<QAbstractButton*>
194 ("qt_dockwidget_closebutton").front();
196 connect(close_btn, SIGNAL(clicked(bool)),
197 this, SLOT(on_view_close_clicked()));
199 if (type == views::ViewTypeTrace) {
200 connect(&session, SIGNAL(trigger_event(util::Timestamp)),
201 qobject_cast<views::ViewBase*>(v.get()),
202 SLOT(trigger_event(util::Timestamp)));
204 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
205 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
207 shared_ptr<MainBar> main_bar = session.main_bar();
209 /* Initial view, create the main bar */
210 main_bar = make_shared<MainBar>(session, this, v.get());
211 dock_main->addToolBar(main_bar.get());
212 session.set_main_bar(main_bar);
214 connect(main_bar.get(), SIGNAL(new_view(Session*)),
215 this, SLOT(on_new_view(Session*)));
217 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
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
223 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
225 /* Additional view, create a standard bar */
226 pv::views::trace::StandardBar *standard_bar =
227 new pv::views::trace::StandardBar(session, this, v.get());
228 dock_main->addToolBar(standard_bar);
230 standard_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
240 void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
242 for (shared_ptr<Session> session : sessions_) {
243 if (!session->has_view(view))
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);
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
258 // Delete the view's dock widget and all widgets inside it
259 entry.first->deleteLater();
261 // Remove the dock widget from the list and stop iterating
262 view_docks_.erase(entry.first);
268 shared_ptr<Session> MainWindow::add_session()
270 static int last_session_id = 1;
271 QString name = tr("Untitled-%1").arg(last_session_id++);
273 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
275 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
276 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
277 connect(session.get(), SIGNAL(name_changed()),
278 this, SLOT(on_session_name_changed()));
279 session_state_mapper_.setMapping(session.get(), session.get());
280 connect(session.get(), SIGNAL(capture_state_changed(int)),
281 &session_state_mapper_, SLOT(map()));
283 sessions_.push_back(session);
285 QMainWindow *window = new QMainWindow();
286 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
287 session_windows_[session] = window;
289 int index = session_selector_.addTab(window, name);
290 session_selector_.setCurrentIndex(index);
291 last_focused_session_ = session;
293 window->setDockNestingEnabled(true);
295 shared_ptr<views::ViewBase> main_view =
296 add_view(name, views::ViewTypeTrace, *session);
301 void MainWindow::remove_session(shared_ptr<Session> session)
303 int h = new_session_button_->height();
305 for (shared_ptr<views::ViewBase> view : session->views())
308 QMainWindow *window = session_windows_.at(session);
309 session_selector_.removeTab(session_selector_.indexOf(window));
311 session_windows_.erase(session);
313 if (last_focused_session_ == session)
314 last_focused_session_.reset();
316 sessions_.remove_if([&](shared_ptr<Session> s) {
317 return s == session; });
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
323 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
324 w->setMinimumHeight(h);
326 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
327 static_tab_widget_->setMinimumHeight(h + 2 * margin);
328 session_selector_.setMinimumHeight(h + 2 * margin);
330 // Update the window title if there is no view left to
331 // generate focus change events
332 setWindowTitle(WindowTitle);
336 void MainWindow::setup_ui()
338 setObjectName(QString::fromUtf8("MainWindow"));
340 setCentralWidget(&session_selector_);
342 // Set the window icon
344 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
347 action_view_sticky_scrolling_->setCheckable(true);
348 action_view_sticky_scrolling_->setChecked(true);
349 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
350 action_view_sticky_scrolling_->setObjectName(
351 QString::fromUtf8("actionViewStickyScrolling"));
352 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
354 action_view_coloured_bg_->setCheckable(true);
355 action_view_coloured_bg_->setChecked(true);
356 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
357 action_view_coloured_bg_->setObjectName(
358 QString::fromUtf8("actionViewColouredBg"));
359 action_view_coloured_bg_->setText(tr("Use &Coloured Backgrounds"));
361 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
362 action_about_->setToolTip(tr("&About..."));
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")));
368 new_session_button_->setToolTip(tr("Create New Session"));
369 new_session_button_->setAutoRaise(true);
371 run_stop_button_ = new QToolButton();
372 run_stop_button_->setAutoRaise(true);
373 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
374 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
376 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
377 run_stop_shortcut_->setAutoRepeat(false);
379 settings_button_ = new QToolButton();
380 settings_button_->setIcon(QIcon::fromTheme("configure",
381 QIcon(":/icons/configure.png")));
382 settings_button_->setToolTip(tr("Settings"));
383 settings_button_->setAutoRaise(true);
385 QFrame *separator1 = new QFrame();
386 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
387 QFrame *separator2 = new QFrame();
388 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
390 QHBoxLayout* layout = new QHBoxLayout();
391 layout->setContentsMargins(2, 2, 2, 2);
392 layout->addWidget(new_session_button_);
393 layout->addWidget(separator1);
394 layout->addWidget(run_stop_button_);
395 layout->addWidget(separator2);
396 layout->addWidget(settings_button_);
398 static_tab_widget_ = new QWidget();
399 static_tab_widget_->setLayout(layout);
401 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
402 session_selector_.setTabsClosable(true);
404 connect(new_session_button_, SIGNAL(clicked(bool)),
405 this, SLOT(on_new_session_clicked()));
406 connect(run_stop_button_, SIGNAL(clicked(bool)),
407 this, SLOT(on_run_stop_clicked()));
408 connect(&session_state_mapper_, SIGNAL(mapped(QObject*)),
409 this, SLOT(on_capture_state_changed(QObject*)));
411 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
412 this, SLOT(on_tab_close_requested(int)));
413 connect(&session_selector_, SIGNAL(currentChanged(int)),
414 this, SLOT(on_tab_changed(int)));
417 connect(static_cast<QApplication *>(QCoreApplication::instance()),
418 SIGNAL(focusChanged(QWidget*, QWidget*)),
419 this, SLOT(on_focus_changed()));
422 void MainWindow::save_ui_settings()
427 settings.beginGroup("MainWindow");
428 settings.setValue("state", saveState());
429 settings.setValue("geometry", saveGeometry());
432 for (shared_ptr<Session> session : sessions_) {
433 // Ignore sessions using the demo device or no device at all
434 if (session->device()) {
435 shared_ptr<devices::HardwareDevice> device =
436 dynamic_pointer_cast< devices::HardwareDevice >
440 device->hardware_device()->driver()->name() == "demo")
443 settings.beginGroup("Session" + QString::number(id++));
444 settings.remove(""); // Remove all keys in this group
445 session->save_settings(settings);
450 settings.setValue("sessions", id);
453 void MainWindow::restore_ui_settings()
456 int i, session_count;
458 settings.beginGroup("MainWindow");
460 if (settings.contains("geometry")) {
461 restoreGeometry(settings.value("geometry").toByteArray());
462 restoreState(settings.value("state").toByteArray());
468 session_count = settings.value("sessions", 0).toInt();
470 for (i = 0; i < session_count; i++) {
471 settings.beginGroup("Session" + QString::number(i));
472 shared_ptr<Session> session = add_session();
473 session->restore_settings(settings);
478 std::shared_ptr<Session> MainWindow::get_tab_session(int index) const
480 // Find the session that belongs to the tab's main window
481 for (auto entry : session_windows_)
482 if (entry.second == session_selector_.widget(index))
488 void MainWindow::closeEvent(QCloseEvent *event)
490 bool data_saved = true;
492 for (auto entry : session_windows_)
493 if (!entry.first->data_saved())
496 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
497 tr("There is unsaved data. Close anyway?"),
498 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
506 QMenu* MainWindow::createPopupMenu()
511 bool MainWindow::restoreState(const QByteArray &state, int version)
516 // Do nothing. We don't want Qt to handle this, or else it
517 // will try to restore all the dock widgets and create havoc.
522 void MainWindow::session_error(const QString text, const QString info_text)
524 QMetaObject::invokeMethod(this, "show_session_error",
525 Qt::QueuedConnection, Q_ARG(QString, text),
526 Q_ARG(QString, info_text));
529 void MainWindow::show_session_error(const QString text, const QString info_text)
531 QMessageBox msg(this);
533 msg.setInformativeText(info_text);
534 msg.setStandardButtons(QMessageBox::Ok);
535 msg.setIcon(QMessageBox::Warning);
539 void MainWindow::on_add_view(const QString &title, views::ViewType type,
542 // We get a pointer and need a reference
543 for (std::shared_ptr<Session> s : sessions_)
544 if (s.get() == session)
545 add_view(title, type, *s);
548 void MainWindow::on_focus_changed()
550 shared_ptr<views::ViewBase> view = get_active_view();
553 for (shared_ptr<Session> session : sessions_) {
554 if (session->has_view(view)) {
555 if (session != last_focused_session_) {
556 // Activate correct tab if necessary
557 shared_ptr<Session> tab_session = get_tab_session(
558 session_selector_.currentIndex());
559 if (tab_session != session)
560 session_selector_.setCurrentWidget(
561 session_windows_.at(session));
563 on_focused_session_changed(session);
571 if (sessions_.empty())
572 setWindowTitle(WindowTitle);
575 void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
577 last_focused_session_ = session;
579 setWindowTitle(session->name() + " - " + WindowTitle);
581 // Update the state of the run/stop button, too
582 on_capture_state_changed(session.get());
585 void MainWindow::on_new_session_clicked()
590 void MainWindow::on_run_stop_clicked()
592 shared_ptr<Session> session = last_focused_session_;
597 switch (session->get_capture_state()) {
598 case Session::Stopped:
599 session->start_capture([&](QString message) {
600 session_error("Capture failed", message); });
602 case Session::AwaitingTrigger:
603 case Session::Running:
604 session->stop_capture();
609 void MainWindow::on_session_name_changed()
611 // Update the corresponding dock widget's name(s)
612 Session *session = qobject_cast<Session*>(QObject::sender());
615 for (shared_ptr<views::ViewBase> view : session->views()) {
616 // Get the dock that contains the view
617 for (auto entry : view_docks_)
618 if (entry.second == view) {
619 entry.first->setObjectName(session->name());
620 entry.first->setWindowTitle(session->name());
624 // Update the tab widget by finding the main window and the tab from that
625 for (auto entry : session_windows_)
626 if (entry.first.get() == session) {
627 QMainWindow *window = entry.second;
628 const int index = session_selector_.indexOf(window);
629 session_selector_.setTabText(index, session->name());
632 // Refresh window title if the affected session has focus
633 if (session == last_focused_session_.get())
634 setWindowTitle(session->name() + " - " + WindowTitle);
637 void MainWindow::on_capture_state_changed(QObject *obj)
639 Session *caller = qobject_cast<Session*>(obj);
641 // Ignore if caller is not the currently focused session
642 // unless there is only one session
643 if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
646 int state = caller->get_capture_state();
648 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
649 run_stop_button_->setIcon(*icons[state]);
650 run_stop_button_->setText((state == pv::Session::Stopped) ?
651 tr("Run") : tr("Stop"));
654 void MainWindow::on_new_view(Session *session)
656 // We get a pointer and need a reference
657 for (std::shared_ptr<Session> s : sessions_)
658 if (s.get() == session)
659 add_view(session->name(), views::ViewTypeTrace, *s);
662 void MainWindow::on_view_close_clicked()
664 // Find the dock widget that contains the close button that was clicked
665 QObject *w = QObject::sender();
666 QDockWidget *dock = 0;
669 dock = qobject_cast<QDockWidget*>(w);
675 // Get the view contained in the dock widget
676 shared_ptr<views::ViewBase> view;
678 for (auto entry : view_docks_)
679 if (entry.first == dock)
682 // Deregister the view
683 for (shared_ptr<Session> session : sessions_) {
684 if (!session->has_view(view))
687 // Also destroy the entire session if its main view is closing...
688 if (view == session->main_view()) {
689 // ...but only if data is saved or the user confirms closing
690 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
691 tr("This session contains unsaved data. Close it anyway?"),
692 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
693 remove_session(session);
696 // All other views can be closed at any time as no data will be lost
701 void MainWindow::on_tab_changed(int index)
703 shared_ptr<Session> session = get_tab_session(index);
706 on_focused_session_changed(session);
709 void MainWindow::on_tab_close_requested(int index)
711 shared_ptr<Session> session = get_tab_session(index);
715 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
716 tr("This session contains unsaved data. Close it anyway?"),
717 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
718 remove_session(session);
721 void MainWindow::on_actionViewStickyScrolling_triggered()
723 shared_ptr<views::ViewBase> viewbase = get_active_view();
724 views::TraceView::View* view =
725 qobject_cast<views::TraceView::View*>(viewbase.get());
727 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
730 void MainWindow::on_actionViewColouredBg_triggered()
732 shared_ptr<views::ViewBase> viewbase = get_active_view();
733 views::TraceView::View* view =
734 qobject_cast<views::TraceView::View*>(viewbase.get());
736 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
739 void MainWindow::on_actionAbout_triggered()
741 dialogs::About dlg(device_manager_.context(), this);