]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Change namespace for the trace view and implement ViewBase
[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
33e1afbe
SA
65const QString MainWindow::WindowTitle = tr("PulseView");
66
107ca6d3 67MainWindow::MainWindow(DeviceManager &device_manager,
e3c79b07 68 string open_file_name, string open_file_format,
1d478458 69 QWidget *parent) :
107ca6d3 70 QMainWindow(parent),
8dbbc7f0 71 device_manager_(device_manager),
c7b03d9d 72 action_view_sticky_scrolling_(new QAction(this)),
0fb9d645 73 action_view_coloured_bg_(new QAction(this)),
e79171dc 74 action_about_(new QAction(this))
d7bed479 75{
48257a69
SA
76 qRegisterMetaType<util::Timestamp>("util::Timestamp");
77
7d5425ef 78 setup_ui();
93f683ad 79 restore_ui_settings();
101e7a9b
SA
80
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);
84 }
85
86 // Add empty default session if there aren't any sessions
87 if (sessions_.size() == 0) {
88 shared_ptr<Session> session = add_session();
89
90 map<string, string> dev_info;
91 shared_ptr<devices::HardwareDevice> other_device, demo_device;
92
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") {
96 demo_device = dev;
97 } else {
98 other_device = dev;
99 }
100 }
101
102 // ...and if there isn't any, just use demo then
103 session->main_bar()->select_device(other_device ?
104 other_device : demo_device);
105 }
7d5425ef
JH
106}
107
47e9e7bb
SA
108MainWindow::~MainWindow()
109{
110 for (auto entry : view_docks_) {
0f8f8c18 111
47e9e7bb 112 const std::shared_ptr<QDockWidget> dock = entry.first;
0f8f8c18
SA
113
114 // Remove view from the dock widget's QMainWindow
115 QMainWindow *dock_main = dynamic_cast<QMainWindow*>(dock->widget());
116 dock_main->setCentralWidget(0);
117
118 // Remove the QMainWindow
47e9e7bb 119 dock->setWidget(0);
0f8f8c18 120
f4e57597 121 const std::shared_ptr<views::ViewBase> view = entry.second;
101e7a9b
SA
122
123 for (shared_ptr<Session> session : sessions_)
124 if (session->has_view(view))
125 session->deregister_view(view);
47e9e7bb
SA
126 }
127}
128
c7b03d9d
SA
129QAction* MainWindow::action_view_sticky_scrolling() const
130{
131 return action_view_sticky_scrolling_;
132}
133
bea236d6
JH
134QAction* MainWindow::action_view_coloured_bg() const
135{
136 return action_view_coloured_bg_;
137}
138
69654681
JH
139QAction* MainWindow::action_about() const
140{
141 return action_about_;
142}
143
f4e57597 144shared_ptr<views::ViewBase> MainWindow::get_active_view() const
168bd8ac
SA
145{
146 // If there's only one view, use it...
147 if (view_docks_.size() == 1)
148 return view_docks_.begin()->second;
149
150 // ...otherwise find the dock widget the widget with focus is contained in
151 QObject *w = QApplication::focusWidget();
152 QDockWidget *dock = 0;
153
154 while (w) {
155 dock = qobject_cast<QDockWidget*>(w);
156 if (dock)
157 break;
158 w = w->parent();
159 }
160
161 // Get the view contained in the dock widget
162 for (auto entry : view_docks_)
163 if (entry.first.get() == dock)
164 return entry.second;
165
82f8a42b 166 return nullptr;
168bd8ac
SA
167}
168
f4e57597
SA
169shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
170 views::ViewType type, Session &session)
7cd2b5f3 171{
f4e57597 172 if (type == views::ViewTypeTrace) {
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
f4e57597
SA
181 shared_ptr<views::TraceView::View> v =
182 make_shared<views::TraceView::View>(session, dock_main);
7cd2b5f3 183 view_docks_[dock] = v;
0f8f8c18
SA
184 session.register_view(v);
185
186 dock_main->setCentralWidget(v.get());
187 dock->setWidget(dock_main);
7cd2b5f3 188
d6ab7b9a 189 dock->setFeatures(QDockWidget::DockWidgetMovable |
36a8185e
SA
190 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
191
192 QAbstractButton *close_btn =
193 dock->findChildren<QAbstractButton*>
194 ("qt_dockwidget_closebutton").front();
195
196 connect(close_btn, SIGNAL(clicked(bool)),
197 this, SLOT(on_view_close_clicked()));
d6ab7b9a 198
f4e57597
SA
199 if (type == views::ViewTypeTrace) {
200 connect(&session, SIGNAL(trigger_event(util::Timestamp)),
201 qobject_cast<views::ViewBase*>(v.get()),
7cd2b5f3 202 SLOT(trigger_event(util::Timestamp)));
7cd2b5f3
SA
203
204 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
205 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
7cd2b5f3 206
0f8f8c18
SA
207 shared_ptr<MainBar> main_bar = session.main_bar();
208 if (!main_bar) {
101e7a9b 209 main_bar = make_shared<MainBar>(session, *this);
0f8f8c18
SA
210 dock_main->addToolBar(main_bar.get());
211 session.set_main_bar(main_bar);
c9da5118
SA
212
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*)));
0f8f8c18
SA
217 }
218 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
d552c5c7
SA
219
220 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
221 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
a55e7918 222 }
f4e57597
SA
223
224 return v;
e93f5538
JH
225 }
226
f4e57597 227 return nullptr;
ed43ef2e
JH
228}
229
101e7a9b
SA
230shared_ptr<Session> MainWindow::add_session()
231{
90d77e35
SA
232 static int last_session_id = 1;
233 QString name = tr("Untitled-%1").arg(last_session_id++);
101e7a9b
SA
234
235 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
236
f4e57597
SA
237 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
238 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
33e1afbe
SA
239 connect(session.get(), SIGNAL(name_changed()),
240 this, SLOT(on_session_name_changed()));
3a21afa6 241
101e7a9b
SA
242 sessions_.push_back(session);
243
f4e57597
SA
244 shared_ptr<views::ViewBase> main_view =
245 add_view(name, views::ViewTypeTrace, *session);
101e7a9b
SA
246
247 return session;
248}
249
36a8185e
SA
250void MainWindow::remove_session(shared_ptr<Session> session)
251{
f4e57597 252 for (shared_ptr<views::ViewBase> view : session->views()) {
36a8185e
SA
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();
257 }
258
259 sessions_.remove_if([&](shared_ptr<Session> s) {
260 return s == session; });
33e1afbe
SA
261
262 // Update the window title if there is no view left to
263 // generate focus change events
264 if (sessions_.empty())
2ec81436 265 setWindowTitle(WindowTitle);
36a8185e
SA
266}
267
7d5425ef
JH
268void MainWindow::setup_ui()
269{
270 setObjectName(QString::fromUtf8("MainWindow"));
271
7d5425ef
JH
272 // Set the window icon
273 QIcon icon;
14f9d4a1 274 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
7d5425ef
JH
275 setWindowIcon(icon);
276
c7b03d9d
SA
277 action_view_sticky_scrolling_->setCheckable(true);
278 action_view_sticky_scrolling_->setChecked(true);
69282fae 279 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
c7b03d9d
SA
280 action_view_sticky_scrolling_->setObjectName(
281 QString::fromUtf8("actionViewStickyScrolling"));
69282fae 282 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
ab1d13ee 283
0fb9d645
SA
284 action_view_coloured_bg_->setCheckable(true);
285 action_view_coloured_bg_->setChecked(true);
574c568d 286 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
0fb9d645
SA
287 action_view_coloured_bg_->setObjectName(
288 QString::fromUtf8("actionViewColouredBg"));
289 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
0fb9d645 290
69654681
JH
291 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
292 action_about_->setText(tr("&About..."));
7d5425ef 293
956a945e
SA
294 setDockNestingEnabled(true);
295
33e1afbe
SA
296 connect(static_cast<QApplication *>(QCoreApplication::instance()),
297 SIGNAL(focusChanged(QWidget*, QWidget*)),
298 this, SLOT(on_focus_changed()));
e3c79b07
JH
299}
300
93f683ad
SA
301void MainWindow::save_ui_settings()
302{
39eb0d45 303 QSettings settings;
101e7a9b 304 int id = 0;
6842b5fc 305
93f683ad
SA
306 settings.beginGroup("MainWindow");
307 settings.setValue("state", saveState());
308 settings.setValue("geometry", saveGeometry());
309 settings.endGroup();
6842b5fc 310
101e7a9b 311 for (shared_ptr<Session> session : sessions_) {
3503810c 312 // Ignore sessions using the demo device or no device at all
101e7a9b
SA
313 if (session->device()) {
314 shared_ptr<devices::HardwareDevice> device =
315 dynamic_pointer_cast< devices::HardwareDevice >
316 (session->device());
317
f2040dcb
SA
318 if (device &&
319 device->hardware_device()->driver()->name() == "demo")
101e7a9b 320 continue;
6842b5fc 321
3503810c
SA
322 settings.beginGroup("Session" + QString::number(id++));
323 settings.remove(""); // Remove all keys in this group
324 session->save_settings(settings);
325 settings.endGroup();
326 }
6842b5fc 327 }
101e7a9b
SA
328
329 settings.setValue("sessions", id);
93f683ad
SA
330}
331
332void MainWindow::restore_ui_settings()
333{
39eb0d45 334 QSettings settings;
101e7a9b 335 int i, session_count;
93f683ad
SA
336
337 settings.beginGroup("MainWindow");
338
339 if (settings.contains("geometry")) {
340 restoreGeometry(settings.value("geometry").toByteArray());
341 restoreState(settings.value("state").toByteArray());
342 } else
343 resize(1000, 720);
344
345 settings.endGroup();
101e7a9b
SA
346
347 session_count = settings.value("sessions", 0).toInt();
348
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);
353 settings.endGroup();
354 }
93f683ad
SA
355}
356
e3c79b07
JH
357void MainWindow::closeEvent(QCloseEvent *event)
358{
359 save_ui_settings();
360 event->accept();
361}
362
d290e89f
SA
363QMenu* MainWindow::createPopupMenu()
364{
365 return nullptr;
366}
367
55547a45
SA
368bool MainWindow::restoreState(const QByteArray &state, int version)
369{
370 (void)state;
371 (void)version;
372
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.
375
376 return false;
377}
378
f4e57597 379void MainWindow::on_add_view(const QString &title, views::ViewType type,
3a21afa6
SA
380 Session *session)
381{
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);
386}
387
33e1afbe
SA
388void MainWindow::on_focus_changed()
389{
f4e57597 390 shared_ptr<views::ViewBase> view;
33e1afbe
SA
391 bool title_set = false;
392
393 view = get_active_view();
394
395 for (shared_ptr<Session> session : sessions_) {
396 if (!session->has_view(view))
397 continue;
398
399 setWindowTitle(session->name() + " - " + WindowTitle);
400 title_set = true;
401 }
402
403 if (!title_set)
404 setWindowTitle(WindowTitle);
405}
406
c9da5118
SA
407void MainWindow::on_new_session()
408{
409 add_session();
410}
411
33e1afbe
SA
412void MainWindow::on_session_name_changed()
413{
414 // Update the corresponding dock widget's name(s)
415 Session *session = qobject_cast<Session*>(QObject::sender());
416 assert(session);
417
f4e57597 418 for (shared_ptr<views::ViewBase> view : session->views()) {
33e1afbe
SA
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());
424 }
425 }
426
427 // Refresh window title if the affected session has focus
428 on_focus_changed();
429}
430
c9da5118
SA
431void MainWindow::on_new_view(Session *session)
432{
433 // We get a pointer and need a reference
434 for (std::shared_ptr<Session> s : sessions_)
435 if (s.get() == session)
f4e57597 436 add_view(session->name(), views::ViewTypeTrace, *s);
c9da5118
SA
437}
438
36a8185e
SA
439void MainWindow::on_view_close_clicked()
440{
441 // Find the dock widget that contains the close button that was clicked
442 QObject *w = QObject::sender();
443 QDockWidget *dock = 0;
444
445 while (w) {
446 dock = qobject_cast<QDockWidget*>(w);
447 if (dock)
448 break;
449 w = w->parent();
450 }
451
452 // Get the view contained in the dock widget
f4e57597 453 shared_ptr<views::ViewBase> view;
36a8185e
SA
454
455 for (auto entry : view_docks_)
456 if (entry.first.get() == dock)
457 view = entry.second;
458
459 // Deregister the view
460 for (shared_ptr<Session> session : sessions_) {
461 if (!session->has_view(view))
462 continue;
463
464 // Also destroy the entire session if its main view is closing
465 if (view == session->main_view()) {
466 remove_session(session);
467 break;
468 } else
469 session->deregister_view(view);
470 }
471}
472
c7b03d9d
SA
473void MainWindow::on_actionViewStickyScrolling_triggered()
474{
f4e57597
SA
475 shared_ptr<views::ViewBase> viewbase = get_active_view();
476 views::TraceView::View* view =
477 qobject_cast<views::TraceView::View*>(viewbase.get());
168bd8ac
SA
478 if (view)
479 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
c7b03d9d
SA
480}
481
0fb9d645
SA
482void MainWindow::on_actionViewColouredBg_triggered()
483{
f4e57597
SA
484 shared_ptr<views::ViewBase> viewbase = get_active_view();
485 views::TraceView::View* view =
486 qobject_cast<views::TraceView::View*>(viewbase.get());
168bd8ac
SA
487 if (view)
488 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
0fb9d645
SA
489}
490
30236a54
JH
491void MainWindow::on_actionAbout_triggered()
492{
8dbbc7f0 493 dialogs::About dlg(device_manager_.context(), this);
40eb2ff4 494 dlg.exec();
30236a54 495}
274d4f13 496
51e77110 497} // namespace pv