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>
37 #include "mainwindow.hpp"
39 #include "devicemanager.hpp"
41 #include "devices/hardwaredevice.hpp"
42 #include "dialogs/about.hpp"
43 #include "toolbars/mainbar.hpp"
44 #include "view/view.hpp"
48 #include <libsigrokcxx/libsigrokcxx.hpp>
50 using std::dynamic_pointer_cast;
52 using std::make_shared;
54 using std::shared_ptr;
63 using toolbars::MainBar;
65 const QString MainWindow::WindowTitle = tr("PulseView");
67 MainWindow::MainWindow(DeviceManager &device_manager,
68 string open_file_name, string open_file_format,
71 device_manager_(device_manager),
72 session_selector_(this),
73 action_view_sticky_scrolling_(new QAction(this)),
74 action_view_coloured_bg_(new QAction(this)),
75 action_about_(new QAction(this))
77 qRegisterMetaType<util::Timestamp>("util::Timestamp");
80 restore_ui_settings();
82 if (!open_file_name.empty()) {
83 shared_ptr<Session> session = add_session();
84 session->main_bar()->load_init_file(open_file_name, open_file_format);
87 // Add empty default session if there aren't any sessions
88 if (sessions_.size() == 0) {
89 shared_ptr<Session> session = add_session();
91 map<string, string> dev_info;
92 shared_ptr<devices::HardwareDevice> other_device, demo_device;
94 // Use any available device that's not demo
95 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
96 if (dev->hardware_device()->driver()->name() == "demo") {
103 // ...and if there isn't any, just use demo then
104 session->main_bar()->select_device(other_device ?
105 other_device : demo_device);
109 MainWindow::~MainWindow()
111 while (!sessions_.empty())
112 remove_session(sessions_.front());
115 QAction* MainWindow::action_view_sticky_scrolling() const
117 return action_view_sticky_scrolling_;
120 QAction* MainWindow::action_view_coloured_bg() const
122 return action_view_coloured_bg_;
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.get() == dock)
155 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
156 views::ViewType type, Session &session)
158 QMainWindow *main_window;
159 for (auto entry : session_windows_)
160 if (entry.first.get() == &session)
161 main_window = entry.second;
165 if (type == views::ViewTypeTrace) {
166 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, main_window);
167 dock->setObjectName(title);
168 main_window->addDockWidget(Qt::TopDockWidgetArea, dock.get());
170 // Insert a QMainWindow into the dock widget to allow for a tool bar
171 QMainWindow *dock_main = new QMainWindow(dock.get());
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(action_view_sticky_scrolling_->isChecked());
198 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
200 shared_ptr<MainBar> main_bar = session.main_bar();
202 main_bar = make_shared<MainBar>(session, *this);
203 dock_main->addToolBar(main_bar.get());
204 session.set_main_bar(main_bar);
206 connect(main_bar.get(), SIGNAL(new_session()),
207 this, SLOT(on_new_session()));
208 connect(main_bar.get(), SIGNAL(new_view(Session*)),
209 this, SLOT(on_new_view(Session*)));
211 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
213 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
214 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
223 shared_ptr<Session> MainWindow::add_session()
225 static int last_session_id = 1;
226 QString name = tr("Untitled-%1").arg(last_session_id++);
228 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
230 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
231 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
232 connect(session.get(), SIGNAL(name_changed()),
233 this, SLOT(on_session_name_changed()));
235 sessions_.push_back(session);
237 QMainWindow *window = new QMainWindow();
238 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
239 session_windows_[session] = window;
240 session_selector_.addTab(window, name);
242 shared_ptr<views::ViewBase> main_view =
243 add_view(name, views::ViewTypeTrace, *session);
248 void MainWindow::remove_session(shared_ptr<Session> session)
250 for (shared_ptr<views::ViewBase> view : session->views()) {
251 // Find the dock the view is contained in and remove it
252 for (auto entry : view_docks_)
253 if (entry.second == view) {
254 // Remove the view from the session
255 session->deregister_view(view);
257 // Remove the view from its parent; otherwise, Qt will
258 // call deleteLater() on it, which causes a double free
259 // since the shared_ptr in view_docks_ doesn't know
260 // that Qt keeps a pointer to the view around
261 entry.second->setParent(0);
263 // Remove this entry from the container
264 view_docks_.erase(entry.first);
268 QMainWindow *window = session_windows_.at(session);
269 session_selector_.removeTab(session_selector_.indexOf(window));
271 session_windows_.erase(session);
273 sessions_.remove_if([&](shared_ptr<Session> s) {
274 return s == session; });
276 // Update the window title if there is no view left to
277 // generate focus change events
278 if (sessions_.empty())
279 setWindowTitle(WindowTitle);
282 void MainWindow::setup_ui()
284 setObjectName(QString::fromUtf8("MainWindow"));
286 setCentralWidget(&session_selector_);
288 // Set the window icon
290 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
293 action_view_sticky_scrolling_->setCheckable(true);
294 action_view_sticky_scrolling_->setChecked(true);
295 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
296 action_view_sticky_scrolling_->setObjectName(
297 QString::fromUtf8("actionViewStickyScrolling"));
298 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
300 action_view_coloured_bg_->setCheckable(true);
301 action_view_coloured_bg_->setChecked(true);
302 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
303 action_view_coloured_bg_->setObjectName(
304 QString::fromUtf8("actionViewColouredBg"));
305 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
307 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
308 action_about_->setText(tr("&About..."));
310 setDockNestingEnabled(true);
312 connect(static_cast<QApplication *>(QCoreApplication::instance()),
313 SIGNAL(focusChanged(QWidget*, QWidget*)),
314 this, SLOT(on_focus_changed()));
317 void MainWindow::save_ui_settings()
322 settings.beginGroup("MainWindow");
323 settings.setValue("state", saveState());
324 settings.setValue("geometry", saveGeometry());
327 for (shared_ptr<Session> session : sessions_) {
328 // Ignore sessions using the demo device or no device at all
329 if (session->device()) {
330 shared_ptr<devices::HardwareDevice> device =
331 dynamic_pointer_cast< devices::HardwareDevice >
335 device->hardware_device()->driver()->name() == "demo")
338 settings.beginGroup("Session" + QString::number(id++));
339 settings.remove(""); // Remove all keys in this group
340 session->save_settings(settings);
345 settings.setValue("sessions", id);
348 void MainWindow::restore_ui_settings()
351 int i, session_count;
353 settings.beginGroup("MainWindow");
355 if (settings.contains("geometry")) {
356 restoreGeometry(settings.value("geometry").toByteArray());
357 restoreState(settings.value("state").toByteArray());
363 session_count = settings.value("sessions", 0).toInt();
365 for (i = 0; i < session_count; i++) {
366 settings.beginGroup("Session" + QString::number(i));
367 shared_ptr<Session> session = add_session();
368 session->restore_settings(settings);
373 void MainWindow::closeEvent(QCloseEvent *event)
379 QMenu* MainWindow::createPopupMenu()
384 bool MainWindow::restoreState(const QByteArray &state, int version)
389 // Do nothing. We don't want Qt to handle this, or else it
390 // will try to restore all the dock widgets and create havoc.
395 void MainWindow::on_add_view(const QString &title, views::ViewType type,
398 // We get a pointer and need a reference
399 for (std::shared_ptr<Session> s : sessions_)
400 if (s.get() == session)
401 add_view(title, type, *s);
404 void MainWindow::on_focus_changed()
406 shared_ptr<views::ViewBase> view;
407 bool title_set = false;
409 view = get_active_view();
411 for (shared_ptr<Session> session : sessions_) {
412 if (!session->has_view(view))
415 setWindowTitle(session->name() + " - " + WindowTitle);
420 setWindowTitle(WindowTitle);
423 void MainWindow::on_new_session()
428 void MainWindow::on_session_name_changed()
430 // Update the corresponding dock widget's name(s)
431 Session *session = qobject_cast<Session*>(QObject::sender());
434 for (shared_ptr<views::ViewBase> view : session->views()) {
435 // Get the dock that contains the view
436 for (auto entry : view_docks_)
437 if (entry.second == view) {
438 entry.first->setObjectName(session->name());
439 entry.first->setWindowTitle(session->name());
443 // Refresh window title if the affected session has focus
447 void MainWindow::on_new_view(Session *session)
449 // We get a pointer and need a reference
450 for (std::shared_ptr<Session> s : sessions_)
451 if (s.get() == session)
452 add_view(session->name(), views::ViewTypeTrace, *s);
455 void MainWindow::on_view_close_clicked()
457 // Find the dock widget that contains the close button that was clicked
458 QObject *w = QObject::sender();
459 QDockWidget *dock = 0;
462 dock = qobject_cast<QDockWidget*>(w);
468 // Get the view contained in the dock widget
469 shared_ptr<views::ViewBase> view;
471 for (auto entry : view_docks_)
472 if (entry.first.get() == dock)
475 // Deregister the view
476 for (shared_ptr<Session> session : sessions_) {
477 if (!session->has_view(view))
480 // Also destroy the entire session if its main view is closing
481 if (view == session->main_view()) {
482 remove_session(session);
485 session->deregister_view(view);
489 void MainWindow::on_actionViewStickyScrolling_triggered()
491 shared_ptr<views::ViewBase> viewbase = get_active_view();
492 views::TraceView::View* view =
493 qobject_cast<views::TraceView::View*>(viewbase.get());
495 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
498 void MainWindow::on_actionViewColouredBg_triggered()
500 shared_ptr<views::ViewBase> viewbase = get_active_view();
501 views::TraceView::View* view =
502 qobject_cast<views::TraceView::View*>(viewbase.get());
504 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
507 void MainWindow::on_actionAbout_triggered()
509 dialogs::About dlg(device_manager_.context(), this);