]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Add "new session" and "new view" toolbar buttons
[pulseview.git] / pv / mainwindow.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
943edd76
MC
21#include <cassert>
22
269528f5 23#ifdef ENABLE_DECODE
aaabd61b 24#include <libsigrokdecode/libsigrokdecode.h>
269528f5 25#endif
30236a54 26
85843b14
JH
27#include <algorithm>
28#include <iterator>
29
7d5425ef
JH
30#include <QAction>
31#include <QApplication>
93f683ad 32#include <QCloseEvent>
0f8f8c18 33#include <QDockWidget>
643f65f9 34#include <QSettings>
7d5425ef 35#include <QWidget>
2953961c 36
2acdb232 37#include "mainwindow.hpp"
107ca6d3 38
2acdb232 39#include "devicemanager.hpp"
d2fc6be9 40#include "util.hpp"
101e7a9b 41#include "devices/hardwaredevice.hpp"
2acdb232 42#include "dialogs/about.hpp"
7c657094 43#include "toolbars/mainbar.hpp"
2acdb232 44#include "view/view.hpp"
d7bed479 45
30236a54
JH
46#include <stdint.h>
47#include <stdarg.h>
fe3a1c21 48#include <libsigrokcxx/libsigrokcxx.hpp>
e82fd481 49
101e7a9b 50using std::dynamic_pointer_cast;
819f4c25 51using std::list;
25272fee 52using std::make_shared;
6842b5fc 53using std::map;
f9abf97e 54using std::shared_ptr;
6842b5fc 55using std::string;
e8d00928 56
51e77110
JH
57namespace pv {
58
b1264f56 59namespace view {
26e3af6b 60class ViewItem;
b1264f56
JH
61}
62
0f8f8c18 63using toolbars::MainBar;
643f65f9 64
107ca6d3 65MainWindow::MainWindow(DeviceManager &device_manager,
e3c79b07 66 string open_file_name, string open_file_format,
1d478458 67 QWidget *parent) :
107ca6d3 68 QMainWindow(parent),
8dbbc7f0 69 device_manager_(device_manager),
c7b03d9d 70 action_view_sticky_scrolling_(new QAction(this)),
0fb9d645 71 action_view_coloured_bg_(new QAction(this)),
e79171dc 72 action_about_(new QAction(this))
d7bed479 73{
48257a69
SA
74 qRegisterMetaType<util::Timestamp>("util::Timestamp");
75
7d5425ef 76 setup_ui();
93f683ad 77 restore_ui_settings();
101e7a9b
SA
78
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);
82 }
83
84 // Add empty default session if there aren't any sessions
85 if (sessions_.size() == 0) {
86 shared_ptr<Session> session = add_session();
87
88 map<string, string> dev_info;
89 shared_ptr<devices::HardwareDevice> other_device, demo_device;
90
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") {
94 demo_device = dev;
95 } else {
96 other_device = dev;
97 }
98 }
99
100 // ...and if there isn't any, just use demo then
101 session->main_bar()->select_device(other_device ?
102 other_device : demo_device);
103 }
7d5425ef
JH
104}
105
47e9e7bb
SA
106MainWindow::~MainWindow()
107{
108 for (auto entry : view_docks_) {
0f8f8c18 109
47e9e7bb 110 const std::shared_ptr<QDockWidget> dock = entry.first;
0f8f8c18
SA
111
112 // Remove view from the dock widget's QMainWindow
113 QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget());
114 dock_main->setCentralWidget(0);
115
116 // Remove the QMainWindow
47e9e7bb 117 dock->setWidget(0);
0f8f8c18 118
47e9e7bb 119 const std::shared_ptr<pv::view::View> view = entry.second;
101e7a9b
SA
120
121 for (shared_ptr<Session> session : sessions_)
122 if (session->has_view(view))
123 session->deregister_view(view);
47e9e7bb
SA
124 }
125}
126
c7b03d9d
SA
127QAction* MainWindow::action_view_sticky_scrolling() const
128{
129 return action_view_sticky_scrolling_;
130}
131
bea236d6
JH
132QAction* MainWindow::action_view_coloured_bg() const
133{
134 return action_view_coloured_bg_;
135}
136
69654681
JH
137QAction* MainWindow::action_about() const
138{
139 return action_about_;
140}
141
168bd8ac
SA
142shared_ptr<pv::view::View> MainWindow::get_active_view() const
143{
144 // If there's only one view, use it...
145 if (view_docks_.size() == 1)
146 return view_docks_.begin()->second;
147
148 // ...otherwise find the dock widget the widget with focus is contained in
149 QObject *w = QApplication::focusWidget();
150 QDockWidget *dock = 0;
151
152 while (w) {
153 dock = qobject_cast<QDockWidget*>(w);
154 if (dock)
155 break;
156 w = w->parent();
157 }
158
159 // Get the view contained in the dock widget
160 for (auto entry : view_docks_)
161 if (entry.first.get() == dock)
162 return entry.second;
163
164 return shared_ptr<pv::view::View>();
165}
166
7cd2b5f3
SA
167shared_ptr<pv::view::View> MainWindow::add_view(const QString &title,
168 view::ViewType type, Session &session)
169{
170 shared_ptr<pv::view::View> v;
171
0f8f8c18 172 if (type == pv::view::TraceView) {
7cd2b5f3 173 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, this);
7cd2b5f3
SA
174 dock->setObjectName(title);
175 addDockWidget(Qt::TopDockWidgetArea, dock.get());
0f8f8c18
SA
176
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
180
181 v = make_shared<pv::view::View>(session, dock_main);
7cd2b5f3 182 view_docks_[dock] = v;
0f8f8c18
SA
183 session.register_view(v);
184
185 dock_main->setCentralWidget(v.get());
186 dock->setWidget(dock_main);
7cd2b5f3 187
d6ab7b9a
SA
188 dock->setFeatures(QDockWidget::DockWidgetMovable |
189 QDockWidget::DockWidgetFloatable);
190
7cd2b5f3
SA
191 if (type == view::TraceView) {
192 connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(),
193 SLOT(trigger_event(util::Timestamp)));
7cd2b5f3
SA
194
195 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
196 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
7cd2b5f3 197
0f8f8c18
SA
198 shared_ptr<MainBar> main_bar = session.main_bar();
199 if (!main_bar) {
101e7a9b 200 main_bar = make_shared<MainBar>(session, *this);
0f8f8c18
SA
201 dock_main->addToolBar(main_bar.get());
202 session.set_main_bar(main_bar);
c9da5118
SA
203
204 connect(main_bar.get(), SIGNAL(new_session()),
205 this, SLOT(on_new_session()));
206 connect(main_bar.get(), SIGNAL(new_view(Session*)),
207 this, SLOT(on_new_view(Session*)));
0f8f8c18
SA
208 }
209 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
d552c5c7
SA
210
211 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
212 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
a55e7918 213 }
e93f5538
JH
214 }
215
0f8f8c18 216 return v;
ed43ef2e
JH
217}
218
101e7a9b
SA
219shared_ptr<Session> MainWindow::add_session()
220{
221 int id = sessions_.size();
222 QString name = tr("Untitled-%1").arg(id + 1);
223
224 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
225
3a21afa6
SA
226 connect(session.get(), SIGNAL(add_view(const QString&, view::ViewType, Session*)),
227 this, SLOT(on_add_view(const QString&, view::ViewType, Session*)));
228
101e7a9b
SA
229 sessions_.push_back(session);
230
231 shared_ptr<view::View> main_view =
232 add_view(name, pv::view::TraceView, *session);
233
234 return session;
235}
236
7d5425ef
JH
237void MainWindow::setup_ui()
238{
239 setObjectName(QString::fromUtf8("MainWindow"));
240
7d5425ef
JH
241 // Set the window icon
242 QIcon icon;
14f9d4a1 243 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
7d5425ef
JH
244 setWindowIcon(icon);
245
c7b03d9d
SA
246 action_view_sticky_scrolling_->setCheckable(true);
247 action_view_sticky_scrolling_->setChecked(true);
69282fae 248 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
c7b03d9d
SA
249 action_view_sticky_scrolling_->setObjectName(
250 QString::fromUtf8("actionViewStickyScrolling"));
69282fae 251 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
ab1d13ee 252
0fb9d645
SA
253 action_view_coloured_bg_->setCheckable(true);
254 action_view_coloured_bg_->setChecked(true);
574c568d 255 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
0fb9d645
SA
256 action_view_coloured_bg_->setObjectName(
257 QString::fromUtf8("actionViewColouredBg"));
258 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
0fb9d645 259
69654681
JH
260 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
261 action_about_->setText(tr("&About..."));
7d5425ef 262
956a945e
SA
263 setDockNestingEnabled(true);
264
5ac961e3 265 // Set the title
0a9fdca5 266 setWindowTitle(tr("PulseView"));
e3c79b07
JH
267}
268
93f683ad
SA
269void MainWindow::save_ui_settings()
270{
39eb0d45 271 QSettings settings;
101e7a9b 272 int id = 0;
6842b5fc 273
93f683ad
SA
274 settings.beginGroup("MainWindow");
275 settings.setValue("state", saveState());
276 settings.setValue("geometry", saveGeometry());
277 settings.endGroup();
6842b5fc 278
101e7a9b
SA
279 for (shared_ptr<Session> session : sessions_) {
280 // Ignore sessions using the demo device
281 if (session->device()) {
282 shared_ptr<devices::HardwareDevice> device =
283 dynamic_pointer_cast< devices::HardwareDevice >
284 (session->device());
285
f2040dcb
SA
286 if (device &&
287 device->hardware_device()->driver()->name() == "demo")
101e7a9b 288 continue;
6842b5fc
SA
289 }
290
101e7a9b 291 settings.beginGroup("Session" + QString::number(id++));
aecae05c 292 settings.remove(""); // Remove all keys in this group
101e7a9b 293 session->save_settings(settings);
6842b5fc
SA
294 settings.endGroup();
295 }
101e7a9b
SA
296
297 settings.setValue("sessions", id);
93f683ad
SA
298}
299
300void MainWindow::restore_ui_settings()
301{
39eb0d45 302 QSettings settings;
101e7a9b 303 int i, session_count;
93f683ad
SA
304
305 settings.beginGroup("MainWindow");
306
307 if (settings.contains("geometry")) {
308 restoreGeometry(settings.value("geometry").toByteArray());
309 restoreState(settings.value("state").toByteArray());
310 } else
311 resize(1000, 720);
312
313 settings.endGroup();
101e7a9b
SA
314
315 session_count = settings.value("sessions", 0).toInt();
316
317 for (i = 0; i < session_count; i++) {
318 settings.beginGroup("Session" + QString::number(i));
319 shared_ptr<Session> session = add_session();
320 session->restore_settings(settings);
321 settings.endGroup();
322 }
93f683ad
SA
323}
324
e3c79b07
JH
325void MainWindow::closeEvent(QCloseEvent *event)
326{
327 save_ui_settings();
328 event->accept();
329}
330
d290e89f
SA
331QMenu* MainWindow::createPopupMenu()
332{
333 return nullptr;
334}
335
55547a45
SA
336bool MainWindow::restoreState(const QByteArray &state, int version)
337{
338 (void)state;
339 (void)version;
340
341 // Do nothing. We don't want Qt to handle this, or else it
342 // will try to restore all the dock widgets and create havoc.
343
344 return false;
345}
346
3a21afa6
SA
347void MainWindow::on_add_view(const QString &title, view::ViewType type,
348 Session *session)
349{
350 // We get a pointer and need a reference
351 for (std::shared_ptr<Session> s : sessions_)
352 if (s.get() == session)
353 add_view(title, type, *s);
354}
355
c9da5118
SA
356void MainWindow::on_new_session()
357{
358 add_session();
359}
360
361void MainWindow::on_new_view(Session *session)
362{
363 // We get a pointer and need a reference
364 for (std::shared_ptr<Session> s : sessions_)
365 if (s.get() == session)
366 add_view(session->name(), pv::view::TraceView, *s);
367}
368
c7b03d9d
SA
369void MainWindow::on_actionViewStickyScrolling_triggered()
370{
168bd8ac
SA
371 shared_ptr<pv::view::View> view = get_active_view();
372 if (view)
373 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
c7b03d9d
SA
374}
375
0fb9d645
SA
376void MainWindow::on_actionViewColouredBg_triggered()
377{
168bd8ac
SA
378 shared_ptr<pv::view::View> view = get_active_view();
379 if (view)
380 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
0fb9d645
SA
381}
382
30236a54
JH
383void MainWindow::on_actionAbout_triggered()
384{
8dbbc7f0 385 dialogs::About dlg(device_manager_.context(), this);
40eb2ff4 386 dlg.exec();
30236a54 387}
274d4f13 388
51e77110 389} // namespace pv