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<pv::view::View> 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<pv::view::View> 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<pv::view::View> MainWindow::add_view(const QString &title,
170 view::ViewType type, Session &session)
172 shared_ptr<pv::view::View> v;
174 if (type == pv::view::TraceView) {
175 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this);
176 dock->setObjectName(title);
177 addDockWidget(Qt::TopDockWidgetArea, dock.get());
179 // Insert a QMainWindow into the dock widget to allow for a tool bar
180 QMainWindow *dock_main = new QMainWindow(dock.get());
181 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
183 v = make_shared<pv::view::View>(session, dock_main);
184 view_docks_[dock] = v;
185 session.register_view(v);
187 dock_main->setCentralWidget(v.get());
188 dock->setWidget(dock_main);
190 dock->setFeatures(QDockWidget::DockWidgetMovable |
191 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
193 QAbstractButton *close_btn =
194 dock->findChildren<QAbstractButton*>
195 ("qt_dockwidget_closebutton").front();
197 connect(close_btn, SIGNAL(clicked(bool)),
198 this, SLOT(on_view_close_clicked()));
200 if (type == view::TraceView) {
201 connect(&session, SIGNAL(trigger_event(util::Timestamp)), 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)));
228 shared_ptr<Session> MainWindow::add_session()
230 int id = sessions_.size();
231 QString name = tr("Untitled-%1").arg(id + 1);
233 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
235 connect(session.get(), SIGNAL(add_view(const QString&, view::ViewType, Session*)),
236 this, SLOT(on_add_view(const QString&, view::ViewType, Session*)));
237 connect(session.get(), SIGNAL(name_changed()),
238 this, SLOT(on_session_name_changed()));
240 sessions_.push_back(session);
242 shared_ptr<view::View> main_view =
243 add_view(name, pv::view::TraceView, *session);
248 void MainWindow::remove_session(shared_ptr<Session> session)
250 for (shared_ptr<view::View> view : session->views()) {
251 // Find the dock the view is contained in and close it
252 for (auto entry : view_docks_)
253 if (entry.second == view)
254 entry.first->close();
257 sessions_.remove_if([&](shared_ptr<Session> s) {
258 return s == session; });
260 // Update the window title if there is no view left to
261 // generate focus change events
262 if (sessions_.empty())
263 setWindowTitle(WindowTitle);
266 void MainWindow::setup_ui()
268 setObjectName(QString::fromUtf8("MainWindow"));
270 // Set the window icon
272 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
275 action_view_sticky_scrolling_->setCheckable(true);
276 action_view_sticky_scrolling_->setChecked(true);
277 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
278 action_view_sticky_scrolling_->setObjectName(
279 QString::fromUtf8("actionViewStickyScrolling"));
280 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
282 action_view_coloured_bg_->setCheckable(true);
283 action_view_coloured_bg_->setChecked(true);
284 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
285 action_view_coloured_bg_->setObjectName(
286 QString::fromUtf8("actionViewColouredBg"));
287 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
289 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
290 action_about_->setText(tr("&About..."));
292 setDockNestingEnabled(true);
294 connect(static_cast<QApplication *>(QCoreApplication::instance()),
295 SIGNAL(focusChanged(QWidget*, QWidget*)),
296 this, SLOT(on_focus_changed()));
299 void MainWindow::save_ui_settings()
304 settings.beginGroup("MainWindow");
305 settings.setValue("state", saveState());
306 settings.setValue("geometry", saveGeometry());
309 for (shared_ptr<Session> session : sessions_) {
310 // Ignore sessions using the demo device or no device at all
311 if (session->device()) {
312 shared_ptr<devices::HardwareDevice> device =
313 dynamic_pointer_cast< devices::HardwareDevice >
317 device->hardware_device()->driver()->name() == "demo")
320 settings.beginGroup("Session" + QString::number(id++));
321 settings.remove(""); // Remove all keys in this group
322 session->save_settings(settings);
327 settings.setValue("sessions", id);
330 void MainWindow::restore_ui_settings()
333 int i, session_count;
335 settings.beginGroup("MainWindow");
337 if (settings.contains("geometry")) {
338 restoreGeometry(settings.value("geometry").toByteArray());
339 restoreState(settings.value("state").toByteArray());
345 session_count = settings.value("sessions", 0).toInt();
347 for (i = 0; i < session_count; i++) {
348 settings.beginGroup("Session" + QString::number(i));
349 shared_ptr<Session> session = add_session();
350 session->restore_settings(settings);
355 void MainWindow::closeEvent(QCloseEvent *event)
361 QMenu* MainWindow::createPopupMenu()
366 bool MainWindow::restoreState(const QByteArray &state, int version)
371 // Do nothing. We don't want Qt to handle this, or else it
372 // will try to restore all the dock widgets and create havoc.
377 void MainWindow::on_add_view(const QString &title, view::ViewType type,
380 // We get a pointer and need a reference
381 for (std::shared_ptr<Session> s : sessions_)
382 if (s.get() == session)
383 add_view(title, type, *s);
386 void MainWindow::on_focus_changed()
388 shared_ptr<view::View> view;
389 bool title_set = false;
391 view = get_active_view();
393 for (shared_ptr<Session> session : sessions_) {
394 if (!session->has_view(view))
397 setWindowTitle(session->name() + " - " + WindowTitle);
402 setWindowTitle(WindowTitle);
405 void MainWindow::on_new_session()
410 void MainWindow::on_session_name_changed()
412 // Update the corresponding dock widget's name(s)
413 Session *session = qobject_cast<Session*>(QObject::sender());
416 for (shared_ptr<view::View> view : session->views()) {
417 // Get the dock that contains the view
418 for (auto entry : view_docks_)
419 if (entry.second == view) {
420 entry.first->setObjectName(session->name());
421 entry.first->setWindowTitle(session->name());
425 // Refresh window title if the affected session has focus
429 void MainWindow::on_new_view(Session *session)
431 // We get a pointer and need a reference
432 for (std::shared_ptr<Session> s : sessions_)
433 if (s.get() == session)
434 add_view(session->name(), pv::view::TraceView, *s);
437 void MainWindow::on_view_close_clicked()
439 // Find the dock widget that contains the close button that was clicked
440 QObject *w = QObject::sender();
441 QDockWidget *dock = 0;
444 dock = qobject_cast<QDockWidget*>(w);
450 // Get the view contained in the dock widget
451 shared_ptr<view::View> view;
453 for (auto entry : view_docks_)
454 if (entry.first.get() == dock)
457 // Deregister the view
458 for (shared_ptr<Session> session : sessions_) {
459 if (!session->has_view(view))
462 // Also destroy the entire session if its main view is closing
463 if (view == session->main_view()) {
464 remove_session(session);
467 session->deregister_view(view);
471 void MainWindow::on_actionViewStickyScrolling_triggered()
473 shared_ptr<pv::view::View> view = get_active_view();
475 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
478 void MainWindow::on_actionViewColouredBg_triggered()
480 shared_ptr<pv::view::View> view = get_active_view();
482 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
485 void MainWindow::on_actionAbout_triggered()
487 dialogs::About dlg(device_manager_.context(), this);