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 "dialogs/about.hpp"
42 #include "toolbars/mainbar.hpp"
43 #include "view/view.hpp"
47 #include <libsigrokcxx/libsigrokcxx.hpp>
50 using std::make_shared;
52 using std::shared_ptr;
61 using toolbars::MainBar;
63 MainWindow::MainWindow(DeviceManager &device_manager,
64 string open_file_name, string open_file_format,
67 device_manager_(device_manager),
68 session_(device_manager),
69 open_file_name_(open_file_name),
70 open_file_format_(open_file_format),
71 action_view_sticky_scrolling_(new QAction(this)),
72 action_view_coloured_bg_(new QAction(this)),
73 action_about_(new QAction(this))
75 qRegisterMetaType<util::Timestamp>("util::Timestamp");
78 restore_ui_settings();
81 MainWindow::~MainWindow()
83 for (auto entry : view_docks_) {
85 const std::shared_ptr<QDockWidget> dock = entry.first;
87 // Remove view from the dock widget's QMainWindow
88 QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget());
89 dock_main->setCentralWidget(0);
91 // Remove the QMainWindow
94 const std::shared_ptr<pv::view::View> view = entry.second;
95 session_.deregister_view(view);
99 QAction* MainWindow::action_view_sticky_scrolling() const
101 return action_view_sticky_scrolling_;
104 QAction* MainWindow::action_view_coloured_bg() const
106 return action_view_coloured_bg_;
109 QAction* MainWindow::action_about() const
111 return action_about_;
114 shared_ptr<pv::view::View> MainWindow::get_active_view() const
116 // If there's only one view, use it...
117 if (view_docks_.size() == 1)
118 return view_docks_.begin()->second;
120 // ...otherwise find the dock widget the widget with focus is contained in
121 QObject *w = QApplication::focusWidget();
122 QDockWidget *dock = 0;
125 dock = qobject_cast<QDockWidget*>(w);
131 // Get the view contained in the dock widget
132 for (auto entry : view_docks_)
133 if (entry.first.get() == dock)
136 return shared_ptr<pv::view::View>();
139 shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
140 view::ViewType type, Session &session)
142 shared_ptr<pv::view::View> v;
144 if (type == pv::view::TraceView) {
145 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this);
146 dock->setObjectName(title);
147 addDockWidget(Qt::TopDockWidgetArea, dock.get());
149 // Insert a QMainWindow into the dock widget to allow for a tool bar
150 QMainWindow *dock_main = new QMainWindow(dock.get());
151 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
153 v = make_shared<pv::view::View>(session, dock_main);
154 view_docks_[dock] = v;
155 session.register_view(v);
157 dock_main->setCentralWidget(v.get());
158 dock->setWidget(dock_main);
160 dock->setFeatures(QDockWidget::DockWidgetMovable |
161 QDockWidget::DockWidgetFloatable);
163 if (type == view::TraceView) {
164 connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(),
165 SLOT(trigger_event(util::Timestamp)));
166 connect(v.get(), SIGNAL(sticky_scrolling_changed(bool)), this,
167 SLOT(sticky_scrolling_changed(bool)));
168 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), this,
169 SLOT(always_zoom_to_fit_changed(bool)));
171 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
172 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
174 shared_ptr<MainBar> main_bar = session.main_bar();
176 main_bar = make_shared<MainBar>(session_, *this,
177 open_file_name_, open_file_format_);
178 dock_main->addToolBar(main_bar.get());
179 session.set_main_bar(main_bar);
181 open_file_name_.clear();
182 open_file_format_.clear();
184 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
191 void MainWindow::setup_ui()
193 setObjectName(QString::fromUtf8("MainWindow"));
195 // Set the window icon
197 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
200 action_view_sticky_scrolling_->setCheckable(true);
201 action_view_sticky_scrolling_->setChecked(true);
202 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
203 action_view_sticky_scrolling_->setObjectName(
204 QString::fromUtf8("actionViewStickyScrolling"));
205 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
207 action_view_coloured_bg_->setCheckable(true);
208 action_view_coloured_bg_->setChecked(true);
209 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
210 action_view_coloured_bg_->setObjectName(
211 QString::fromUtf8("actionViewColouredBg"));
212 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
214 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
215 action_about_->setText(tr("&About..."));
217 // Set up the initial view
218 shared_ptr<view::View> main_view =
219 add_view(tr("Untitled"), pv::view::TraceView, session_);
222 setWindowTitle(tr("PulseView"));
225 void MainWindow::save_ui_settings()
229 map<string, string> dev_info;
230 list<string> key_list;
232 settings.beginGroup("MainWindow");
233 settings.setValue("state", saveState());
234 settings.setValue("geometry", saveGeometry());
237 if (session_.device()) {
238 settings.beginGroup("Device");
239 key_list.push_back("vendor");
240 key_list.push_back("model");
241 key_list.push_back("version");
242 key_list.push_back("serial_num");
243 key_list.push_back("connection_id");
245 dev_info = device_manager_.get_device_info(
248 for (string key : key_list) {
249 if (dev_info.count(key))
250 settings.setValue(QString::fromUtf8(key.c_str()),
251 QString::fromUtf8(dev_info.at(key).c_str()));
253 settings.remove(QString::fromUtf8(key.c_str()));
260 void MainWindow::restore_ui_settings()
264 settings.beginGroup("MainWindow");
266 if (settings.contains("geometry")) {
267 restoreGeometry(settings.value("geometry").toByteArray());
268 restoreState(settings.value("state").toByteArray());
275 void MainWindow::closeEvent(QCloseEvent *event)
281 QMenu* MainWindow::createPopupMenu()
286 bool MainWindow::restoreState(const QByteArray &state, int version)
291 // Do nothing. We don't want Qt to handle this, or else it
292 // will try to restore all the dock widgets and create havoc.
297 void MainWindow::on_actionViewStickyScrolling_triggered()
299 shared_ptr<pv::view::View> view = get_active_view();
301 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
304 void MainWindow::on_actionViewColouredBg_triggered()
306 shared_ptr<pv::view::View> view = get_active_view();
308 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
311 void MainWindow::on_actionAbout_triggered()
313 dialogs::About dlg(device_manager_.context(), this);