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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <libsigrokdecode/libsigrokdecode.h>
31 #include <QApplication>
32 #include <QCloseEvent>
33 #include <QDockWidget>
34 #include <QHBoxLayout>
38 #include "mainwindow.hpp"
40 #include "devicemanager.hpp"
42 #include "devices/hardwaredevice.hpp"
43 #include "dialogs/about.hpp"
44 #include "toolbars/mainbar.hpp"
45 #include "view/view.hpp"
49 #include <libsigrokcxx/libsigrokcxx.hpp>
51 using std::dynamic_pointer_cast;
53 using std::make_shared;
55 using std::shared_ptr;
64 using toolbars::MainBar;
66 const QString MainWindow::WindowTitle = tr("PulseView");
68 MainWindow::MainWindow(DeviceManager &device_manager,
69 string open_file_name, string open_file_format,
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))
78 qRegisterMetaType<util::Timestamp>("util::Timestamp");
81 restore_ui_settings();
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);
88 // Add empty default session if there aren't any sessions
89 if (sessions_.size() == 0) {
90 shared_ptr<Session> session = add_session();
92 map<string, string> dev_info;
93 shared_ptr<devices::HardwareDevice> other_device, demo_device;
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") {
104 // ...and if there isn't any, just use demo then
105 session->main_bar()->select_device(other_device ?
106 other_device : demo_device);
110 MainWindow::~MainWindow()
112 while (!sessions_.empty())
113 remove_session(sessions_.front());
116 QAction* MainWindow::action_view_sticky_scrolling() const
118 return action_view_sticky_scrolling_;
121 QAction* MainWindow::action_view_coloured_bg() const
123 return action_view_coloured_bg_;
126 QAction* MainWindow::action_about() const
128 return action_about_;
131 shared_ptr<views::ViewBase> MainWindow::get_active_view() const
133 // If there's only one view, use it...
134 if (view_docks_.size() == 1)
135 return view_docks_.begin()->second;
137 // ...otherwise find the dock widget the widget with focus is contained in
138 QObject *w = QApplication::focusWidget();
139 QDockWidget *dock = 0;
142 dock = qobject_cast<QDockWidget*>(w);
148 // Get the view contained in the dock widget
149 for (auto entry : view_docks_)
150 if (entry.first == dock)
156 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
157 views::ViewType type, Session &session)
159 QMainWindow *main_window;
160 for (auto entry : session_windows_)
161 if (entry.first.get() == &session)
162 main_window = entry.second;
166 if (type == views::ViewTypeTrace) {
167 QDockWidget* dock = new QDockWidget(title, main_window);
168 dock->setObjectName(title);
169 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
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
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);
180 dock_main->setCentralWidget(v.get());
181 dock->setWidget(dock_main);
183 dock->setFeatures(QDockWidget::DockWidgetMovable |
184 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
186 QAbstractButton *close_btn =
187 dock->findChildren<QAbstractButton*>
188 ("qt_dockwidget_closebutton").front();
190 connect(close_btn, SIGNAL(clicked(bool)),
191 this, SLOT(on_view_close_clicked()));
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)));
198 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
199 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
201 shared_ptr<MainBar> main_bar = session.main_bar();
203 main_bar = make_shared<MainBar>(session, *this);
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 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
213 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
222 shared_ptr<Session> MainWindow::add_session()
224 static int last_session_id = 1;
225 QString name = tr("Untitled-%1").arg(last_session_id++);
227 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
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()));
234 sessions_.push_back(session);
236 QMainWindow *window = new QMainWindow();
237 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
238 session_windows_[session] = window;
240 int index = session_selector_.addTab(window, name);
241 session_selector_.setCurrentIndex(index);
243 window->setDockNestingEnabled(true);
245 shared_ptr<views::ViewBase> main_view =
246 add_view(name, views::ViewTypeTrace, *session);
251 void MainWindow::remove_session(shared_ptr<Session> session)
253 int h = new_session_button_->height();
255 for (shared_ptr<views::ViewBase> view : session->views()) {
256 // Find the dock the view is contained in and remove it
257 for (auto entry : view_docks_)
258 if (entry.second == view) {
259 // Remove the view from the session
260 session->deregister_view(view);
262 // Remove the view from its parent; otherwise, Qt will
263 // call deleteLater() on it, which causes a double free
264 // since the shared_ptr in view_docks_ doesn't know
265 // that Qt keeps a pointer to the view around
266 entry.second->setParent(0);
268 // Remove this entry from the container
269 view_docks_.erase(entry.first);
273 QMainWindow *window = session_windows_.at(session);
274 session_selector_.removeTab(session_selector_.indexOf(window));
276 session_windows_.erase(session);
278 sessions_.remove_if([&](shared_ptr<Session> s) {
279 return s == session; });
281 if (sessions_.empty()) {
282 // When there are no more tabs, the height of the QTabWidget
283 // drops to zero. We must prevent this to keep the static
285 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
286 w->setMinimumHeight(h);
288 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
289 static_tab_widget_->setMinimumHeight(h + 2 * margin);
290 session_selector_.setMinimumHeight(h + 2 * margin);
292 // Update the window title if there is no view left to
293 // generate focus change events
294 setWindowTitle(WindowTitle);
298 void MainWindow::setup_ui()
300 setObjectName(QString::fromUtf8("MainWindow"));
302 setCentralWidget(&session_selector_);
304 // Set the window icon
306 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
309 action_view_sticky_scrolling_->setCheckable(true);
310 action_view_sticky_scrolling_->setChecked(true);
311 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
312 action_view_sticky_scrolling_->setObjectName(
313 QString::fromUtf8("actionViewStickyScrolling"));
314 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
316 action_view_coloured_bg_->setCheckable(true);
317 action_view_coloured_bg_->setChecked(true);
318 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
319 action_view_coloured_bg_->setObjectName(
320 QString::fromUtf8("actionViewColouredBg"));
321 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
323 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
324 action_about_->setText(tr("&About..."));
326 // Set up the tab area
327 new_session_button_ = new QToolButton();
328 new_session_button_->setIcon(QIcon::fromTheme("document-new",
329 QIcon(":/icons/document-new.png")));
330 new_session_button_->setAutoRaise(true);
332 QHBoxLayout* layout = new QHBoxLayout();
333 layout->setContentsMargins(2, 2, 2, 2);
334 layout->addWidget(new_session_button_);
336 static_tab_widget_ = new QWidget();
337 static_tab_widget_->setLayout(layout);
339 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
340 session_selector_.setTabsClosable(true);
342 connect(new_session_button_, SIGNAL(clicked(bool)),
343 this, SLOT(on_new_session_clicked()));
345 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
346 this, SLOT(on_tab_close_requested(int)));
347 connect(&session_selector_, SIGNAL(currentChanged(int)),
348 this, SLOT(on_tab_changed(int)));
351 connect(static_cast<QApplication *>(QCoreApplication::instance()),
352 SIGNAL(focusChanged(QWidget*, QWidget*)),
353 this, SLOT(on_focus_changed()));
356 void MainWindow::save_ui_settings()
361 settings.beginGroup("MainWindow");
362 settings.setValue("state", saveState());
363 settings.setValue("geometry", saveGeometry());
366 for (shared_ptr<Session> session : sessions_) {
367 // Ignore sessions using the demo device or no device at all
368 if (session->device()) {
369 shared_ptr<devices::HardwareDevice> device =
370 dynamic_pointer_cast< devices::HardwareDevice >
374 device->hardware_device()->driver()->name() == "demo")
377 settings.beginGroup("Session" + QString::number(id++));
378 settings.remove(""); // Remove all keys in this group
379 session->save_settings(settings);
384 settings.setValue("sessions", id);
387 void MainWindow::restore_ui_settings()
390 int i, session_count;
392 settings.beginGroup("MainWindow");
394 if (settings.contains("geometry")) {
395 restoreGeometry(settings.value("geometry").toByteArray());
396 restoreState(settings.value("state").toByteArray());
402 session_count = settings.value("sessions", 0).toInt();
404 for (i = 0; i < session_count; i++) {
405 settings.beginGroup("Session" + QString::number(i));
406 shared_ptr<Session> session = add_session();
407 session->restore_settings(settings);
412 std::shared_ptr<Session> MainWindow::get_tab_session(int index) const
414 // Find the session that belongs to the tab's main window
415 for (auto entry : session_windows_)
416 if (entry.second == session_selector_.widget(index))
422 void MainWindow::closeEvent(QCloseEvent *event)
428 QMenu* MainWindow::createPopupMenu()
433 bool MainWindow::restoreState(const QByteArray &state, int version)
438 // Do nothing. We don't want Qt to handle this, or else it
439 // will try to restore all the dock widgets and create havoc.
444 void MainWindow::on_add_view(const QString &title, views::ViewType type,
447 // We get a pointer and need a reference
448 for (std::shared_ptr<Session> s : sessions_)
449 if (s.get() == session)
450 add_view(title, type, *s);
453 void MainWindow::on_focus_changed()
455 static shared_ptr<Session> prev_session;
457 shared_ptr<views::ViewBase> view = get_active_view();
460 for (shared_ptr<Session> session : sessions_) {
461 if (session->has_view(view)) {
462 if (session != prev_session) {
463 // Activate correct tab if necessary
464 shared_ptr<Session> tab_session = get_tab_session(
465 session_selector_.currentIndex());
466 if (tab_session != session)
467 session_selector_.setCurrentWidget(
468 session_windows_.at(session));
470 on_focused_session_changed(session);
473 prev_session = session;
479 if (sessions_.empty())
480 setWindowTitle(WindowTitle);
483 void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
485 setWindowTitle(session->name() + " - " + WindowTitle);
488 void MainWindow::on_new_session_clicked()
493 void MainWindow::on_session_name_changed()
495 // Update the corresponding dock widget's name(s)
496 Session *session = qobject_cast<Session*>(QObject::sender());
499 for (shared_ptr<views::ViewBase> view : session->views()) {
500 // Get the dock that contains the view
501 for (auto entry : view_docks_)
502 if (entry.second == view) {
503 entry.first->setObjectName(session->name());
504 entry.first->setWindowTitle(session->name());
508 // Refresh window title if the affected session has focus
509 shared_ptr<views::ViewBase> view = get_active_view();
511 if (view && session->has_view(view))
512 setWindowTitle(session->name() + " - " + WindowTitle);
515 void MainWindow::on_new_view(Session *session)
517 // We get a pointer and need a reference
518 for (std::shared_ptr<Session> s : sessions_)
519 if (s.get() == session)
520 add_view(session->name(), views::ViewTypeTrace, *s);
523 void MainWindow::on_view_close_clicked()
525 // Find the dock widget that contains the close button that was clicked
526 QObject *w = QObject::sender();
527 QDockWidget *dock = 0;
530 dock = qobject_cast<QDockWidget*>(w);
536 // Get the view contained in the dock widget
537 shared_ptr<views::ViewBase> view;
539 for (auto entry : view_docks_)
540 if (entry.first == dock)
543 // Deregister the view
544 for (shared_ptr<Session> session : sessions_) {
545 if (!session->has_view(view))
548 // Also destroy the entire session if its main view is closing
549 if (view == session->main_view()) {
550 remove_session(session);
553 session->deregister_view(view);
557 void MainWindow::on_tab_changed(int index)
559 shared_ptr<Session> session = get_tab_session(index);
562 on_focused_session_changed(session);
565 void MainWindow::on_tab_close_requested(int index)
567 // TODO Ask user if this is intended in case data is unsaved
569 shared_ptr<Session> session = get_tab_session(index);
572 remove_session(session);
575 void MainWindow::on_actionViewStickyScrolling_triggered()
577 shared_ptr<views::ViewBase> viewbase = get_active_view();
578 views::TraceView::View* view =
579 qobject_cast<views::TraceView::View*>(viewbase.get());
581 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
584 void MainWindow::on_actionViewColouredBg_triggered()
586 shared_ptr<views::ViewBase> viewbase = get_active_view();
587 views::TraceView::View* view =
588 qobject_cast<views::TraceView::View*>(viewbase.get());
590 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
593 void MainWindow::on_actionAbout_triggered()
595 dialogs::About dlg(device_manager_.context(), this);