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 action_view_sticky_scrolling_(new QAction(this)),
73 action_view_coloured_bg_(new QAction(this)),
74 action_about_(new QAction(this))
76 qRegisterMetaType<util::Timestamp>("util::Timestamp");
79 restore_ui_settings();
81 if (!open_file_name.empty()) {
82 shared_ptr<Session> session = add_session();
83 session->main_bar()->load_init_file(open_file_name, open_file_format);
86 // Add empty default session if there aren't any sessions
87 if (sessions_.size() == 0) {
88 shared_ptr<Session> session = add_session();
90 map<string, string> dev_info;
91 shared_ptr<devices::HardwareDevice> other_device, demo_device;
93 // Use any available device that's not demo
94 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
95 if (dev->hardware_device()->driver()->name() == "demo") {
102 // ...and if there isn't any, just use demo then
103 session->main_bar()->select_device(other_device ?
104 other_device : demo_device);
108 MainWindow::~MainWindow()
110 for (auto entry : view_docks_) {
112 const std::shared_ptr<QDockWidget> dock = entry.first;
114 // Remove view from the dock widget's QMainWindow
115 QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget());
116 dock_main->setCentralWidget(0);
118 // Remove the QMainWindow
121 const std::shared_ptr<views::ViewBase> view = entry.second;
123 for (shared_ptr<Session> session : sessions_)
124 if (session->has_view(view))
125 session->deregister_view(view);
129 QAction* MainWindow::action_view_sticky_scrolling() const
131 return action_view_sticky_scrolling_;
134 QAction* MainWindow::action_view_coloured_bg() const
136 return action_view_coloured_bg_;
139 QAction* MainWindow::action_about() const
141 return action_about_;
144 shared_ptr<views::ViewBase> MainWindow::get_active_view() const
146 // If there's only one view, use it...
147 if (view_docks_.size() == 1)
148 return view_docks_.begin()->second;
150 // ...otherwise find the dock widget the widget with focus is contained in
151 QObject *w = QApplication::focusWidget();
152 QDockWidget *dock = 0;
155 dock = qobject_cast<QDockWidget*>(w);
161 // Get the view contained in the dock widget
162 for (auto entry : view_docks_)
163 if (entry.first.get() == dock)
169 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
170 views::ViewType type, Session &session)
172 if (type == views::ViewTypeTrace) {
173 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this);
174 dock->setObjectName(title);
175 addDockWidget(Qt::TopDockWidgetArea, dock.get());
177 // Insert a QMainWindow into the dock widget to allow for a tool bar
178 QMainWindow *dock_main = new QMainWindow(dock.get());
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 main_bar = make_shared<MainBar>(session, *this);
210 dock_main->addToolBar(main_bar.get());
211 session.set_main_bar(main_bar);
213 connect(main_bar.get(), SIGNAL(new_session()),
214 this, SLOT(on_new_session()));
215 connect(main_bar.get(), SIGNAL(new_view(Session*)),
216 this, SLOT(on_new_view(Session*)));
218 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
220 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
221 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
230 shared_ptr<Session> MainWindow::add_session()
232 static int last_session_id = 1;
233 QString name = tr("Untitled-%1").arg(last_session_id++);
235 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
237 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
238 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
239 connect(session.get(), SIGNAL(name_changed()),
240 this, SLOT(on_session_name_changed()));
242 sessions_.push_back(session);
244 shared_ptr<views::ViewBase> main_view =
245 add_view(name, views::ViewTypeTrace, *session);
250 void MainWindow::remove_session(shared_ptr<Session> session)
252 for (shared_ptr<views::ViewBase> view : session->views()) {
253 // Find the dock the view is contained in and close it
254 for (auto entry : view_docks_)
255 if (entry.second == view)
256 entry.first->close();
259 sessions_.remove_if([&](shared_ptr<Session> s) {
260 return s == session; });
262 // Update the window title if there is no view left to
263 // generate focus change events
264 if (sessions_.empty())
265 setWindowTitle(WindowTitle);
268 void MainWindow::setup_ui()
270 setObjectName(QString::fromUtf8("MainWindow"));
272 // Set the window icon
274 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
277 action_view_sticky_scrolling_->setCheckable(true);
278 action_view_sticky_scrolling_->setChecked(true);
279 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
280 action_view_sticky_scrolling_->setObjectName(
281 QString::fromUtf8("actionViewStickyScrolling"));
282 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
284 action_view_coloured_bg_->setCheckable(true);
285 action_view_coloured_bg_->setChecked(true);
286 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
287 action_view_coloured_bg_->setObjectName(
288 QString::fromUtf8("actionViewColouredBg"));
289 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
291 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
292 action_about_->setText(tr("&About..."));
294 setDockNestingEnabled(true);
296 connect(static_cast<QApplication *>(QCoreApplication::instance()),
297 SIGNAL(focusChanged(QWidget*, QWidget*)),
298 this, SLOT(on_focus_changed()));
301 void MainWindow::save_ui_settings()
306 settings.beginGroup("MainWindow");
307 settings.setValue("state", saveState());
308 settings.setValue("geometry", saveGeometry());
311 for (shared_ptr<Session> session : sessions_) {
312 // Ignore sessions using the demo device or no device at all
313 if (session->device()) {
314 shared_ptr<devices::HardwareDevice> device =
315 dynamic_pointer_cast< devices::HardwareDevice >
319 device->hardware_device()->driver()->name() == "demo")
322 settings.beginGroup("Session" + QString::number(id++));
323 settings.remove(""); // Remove all keys in this group
324 session->save_settings(settings);
329 settings.setValue("sessions", id);
332 void MainWindow::restore_ui_settings()
335 int i, session_count;
337 settings.beginGroup("MainWindow");
339 if (settings.contains("geometry")) {
340 restoreGeometry(settings.value("geometry").toByteArray());
341 restoreState(settings.value("state").toByteArray());
347 session_count = settings.value("sessions", 0).toInt();
349 for (i = 0; i < session_count; i++) {
350 settings.beginGroup("Session" + QString::number(i));
351 shared_ptr<Session> session = add_session();
352 session->restore_settings(settings);
357 void MainWindow::closeEvent(QCloseEvent *event)
363 QMenu* MainWindow::createPopupMenu()
368 bool MainWindow::restoreState(const QByteArray &state, int version)
373 // Do nothing. We don't want Qt to handle this, or else it
374 // will try to restore all the dock widgets and create havoc.
379 void MainWindow::on_add_view(const QString &title, views::ViewType type,
382 // We get a pointer and need a reference
383 for (std::shared_ptr<Session> s : sessions_)
384 if (s.get() == session)
385 add_view(title, type, *s);
388 void MainWindow::on_focus_changed()
390 shared_ptr<views::ViewBase> view;
391 bool title_set = false;
393 view = get_active_view();
395 for (shared_ptr<Session> session : sessions_) {
396 if (!session->has_view(view))
399 setWindowTitle(session->name() + " - " + WindowTitle);
404 setWindowTitle(WindowTitle);
407 void MainWindow::on_new_session()
412 void MainWindow::on_session_name_changed()
414 // Update the corresponding dock widget's name(s)
415 Session *session = qobject_cast<Session*>(QObject::sender());
418 for (shared_ptr<views::ViewBase> view : session->views()) {
419 // Get the dock that contains the view
420 for (auto entry : view_docks_)
421 if (entry.second == view) {
422 entry.first->setObjectName(session->name());
423 entry.first->setWindowTitle(session->name());
427 // Refresh window title if the affected session has focus
431 void MainWindow::on_new_view(Session *session)
433 // We get a pointer and need a reference
434 for (std::shared_ptr<Session> s : sessions_)
435 if (s.get() == session)
436 add_view(session->name(), views::ViewTypeTrace, *s);
439 void MainWindow::on_view_close_clicked()
441 // Find the dock widget that contains the close button that was clicked
442 QObject *w = QObject::sender();
443 QDockWidget *dock = 0;
446 dock = qobject_cast<QDockWidget*>(w);
452 // Get the view contained in the dock widget
453 shared_ptr<views::ViewBase> view;
455 for (auto entry : view_docks_)
456 if (entry.first.get() == dock)
459 // Deregister the view
460 for (shared_ptr<Session> session : sessions_) {
461 if (!session->has_view(view))
464 // Also destroy the entire session if its main view is closing
465 if (view == session->main_view()) {
466 remove_session(session);
469 session->deregister_view(view);
473 void MainWindow::on_actionViewStickyScrolling_triggered()
475 shared_ptr<views::ViewBase> viewbase = get_active_view();
476 views::TraceView::View* view =
477 qobject_cast<views::TraceView::View*>(viewbase.get());
479 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
482 void MainWindow::on_actionViewColouredBg_triggered()
484 shared_ptr<views::ViewBase> viewbase = get_active_view();
485 views::TraceView::View* view =
486 qobject_cast<views::TraceView::View*>(viewbase.get());
488 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
491 void MainWindow::on_actionAbout_triggered()
493 dialogs::About dlg(device_manager_.context(), this);