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 MainWindow::MainWindow(DeviceManager &device_manager,
66 string open_file_name, string open_file_format,
69 device_manager_(device_manager),
70 action_view_sticky_scrolling_(new QAction(this)),
71 action_view_coloured_bg_(new QAction(this)),
72 action_about_(new QAction(this))
74 qRegisterMetaType<util::Timestamp>("util::Timestamp");
77 restore_ui_settings();
79 if (!open_file_name.empty()) {
80 shared_ptr<Session> session = add_session();
81 session->main_bar()->load_init_file(open_file_name, open_file_format);
84 // Add empty default session if there aren't any sessions
85 if (sessions_.size() == 0) {
86 shared_ptr<Session> session = add_session();
88 map<string, string> dev_info;
89 shared_ptr<devices::HardwareDevice> other_device, demo_device;
91 // Use any available device that's not demo
92 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
93 if (dev->hardware_device()->driver()->name() == "demo") {
100 // ...and if there isn't any, just use demo then
101 session->main_bar()->select_device(other_device ?
102 other_device : demo_device);
106 MainWindow::~MainWindow()
108 for (auto entry : view_docks_) {
110 const std::shared_ptr<QDockWidget> dock = entry.first;
112 // Remove view from the dock widget's QMainWindow
113 QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget());
114 dock_main->setCentralWidget(0);
116 // Remove the QMainWindow
119 const std::shared_ptr<pv::view::View> view = entry.second;
121 for (shared_ptr<Session> session : sessions_)
122 if (session->has_view(view))
123 session->deregister_view(view);
127 QAction* MainWindow::action_view_sticky_scrolling() const
129 return action_view_sticky_scrolling_;
132 QAction* MainWindow::action_view_coloured_bg() const
134 return action_view_coloured_bg_;
137 QAction* MainWindow::action_about() const
139 return action_about_;
142 shared_ptr<pv::view::View> MainWindow::get_active_view() const
144 // If there's only one view, use it...
145 if (view_docks_.size() == 1)
146 return view_docks_.begin()->second;
148 // ...otherwise find the dock widget the widget with focus is contained in
149 QObject *w = QApplication::focusWidget();
150 QDockWidget *dock = 0;
153 dock = qobject_cast<QDockWidget*>(w);
159 // Get the view contained in the dock widget
160 for (auto entry : view_docks_)
161 if (entry.first.get() == dock)
164 return shared_ptr<pv::view::View>();
167 shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
168 view::ViewType type, Session &session)
170 shared_ptr<pv::view::View> v;
172 if (type == pv::view::TraceView) {
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 v = make_shared<pv::view::View>(session, dock_main);
182 view_docks_[dock] = v;
183 session.register_view(v);
185 dock_main->setCentralWidget(v.get());
186 dock->setWidget(dock_main);
188 dock->setFeatures(QDockWidget::DockWidgetMovable |
189 QDockWidget::DockWidgetFloatable);
191 if (type == view::TraceView) {
192 connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(),
193 SLOT(trigger_event(util::Timestamp)));
195 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
196 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
198 shared_ptr<MainBar> main_bar = session.main_bar();
200 main_bar = make_shared<MainBar>(session, *this);
201 dock_main->addToolBar(main_bar.get());
202 session.set_main_bar(main_bar);
204 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
206 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
207 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
214 shared_ptr<Session> MainWindow::add_session()
216 int id = sessions_.size();
217 QString name = tr("Untitled-%1").arg(id + 1);
219 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
221 connect(session.get(), SIGNAL(add_view(const QString&, view::ViewType, Session*)),
222 this, SLOT(on_add_view(const QString&, view::ViewType, Session*)));
224 sessions_.push_back(session);
226 shared_ptr<view::View> main_view =
227 add_view(name, pv::view::TraceView, *session);
232 void MainWindow::setup_ui()
234 setObjectName(QString::fromUtf8("MainWindow"));
236 // Set the window icon
238 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
241 action_view_sticky_scrolling_->setCheckable(true);
242 action_view_sticky_scrolling_->setChecked(true);
243 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
244 action_view_sticky_scrolling_->setObjectName(
245 QString::fromUtf8("actionViewStickyScrolling"));
246 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
248 action_view_coloured_bg_->setCheckable(true);
249 action_view_coloured_bg_->setChecked(true);
250 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
251 action_view_coloured_bg_->setObjectName(
252 QString::fromUtf8("actionViewColouredBg"));
253 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
255 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
256 action_about_->setText(tr("&About..."));
259 setWindowTitle(tr("PulseView"));
262 void MainWindow::save_ui_settings()
267 settings.beginGroup("MainWindow");
268 settings.setValue("state", saveState());
269 settings.setValue("geometry", saveGeometry());
272 for (shared_ptr<Session> session : sessions_) {
273 // Ignore sessions using the demo device
274 if (session->device()) {
275 shared_ptr<devices::HardwareDevice> device =
276 dynamic_pointer_cast< devices::HardwareDevice >
280 device->hardware_device()->driver()->name() == "demo")
284 settings.beginGroup("Session" + QString::number(id++));
285 settings.remove(""); // Remove all keys in this group
286 session->save_settings(settings);
290 settings.setValue("sessions", id);
293 void MainWindow::restore_ui_settings()
296 int i, session_count;
298 settings.beginGroup("MainWindow");
300 if (settings.contains("geometry")) {
301 restoreGeometry(settings.value("geometry").toByteArray());
302 restoreState(settings.value("state").toByteArray());
308 session_count = settings.value("sessions", 0).toInt();
310 for (i = 0; i < session_count; i++) {
311 settings.beginGroup("Session" + QString::number(i));
312 shared_ptr<Session> session = add_session();
313 session->restore_settings(settings);
318 void MainWindow::closeEvent(QCloseEvent *event)
324 QMenu* MainWindow::createPopupMenu()
329 bool MainWindow::restoreState(const QByteArray &state, int version)
334 // Do nothing. We don't want Qt to handle this, or else it
335 // will try to restore all the dock widgets and create havoc.
340 void MainWindow::on_add_view(const QString &title, view::ViewType type,
343 // We get a pointer and need a reference
344 for (std::shared_ptr<Session> s : sessions_)
345 if (s.get() == session)
346 add_view(title, type, *s);
349 void MainWindow::on_actionViewStickyScrolling_triggered()
351 shared_ptr<pv::view::View> view = get_active_view();
353 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
356 void MainWindow::on_actionViewColouredBg_triggered()
358 shared_ptr<pv::view::View> view = get_active_view();
360 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
363 void MainWindow::on_actionAbout_triggered()
365 dialogs::About dlg(device_manager_.context(), this);