]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
MainWindow: Select new session after creation
[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>
f1e2d26b 34#include <QHBoxLayout>
643f65f9 35#include <QSettings>
7d5425ef 36#include <QWidget>
2953961c 37
2acdb232 38#include "mainwindow.hpp"
107ca6d3 39
2acdb232 40#include "devicemanager.hpp"
d2fc6be9 41#include "util.hpp"
101e7a9b 42#include "devices/hardwaredevice.hpp"
2acdb232 43#include "dialogs/about.hpp"
7c657094 44#include "toolbars/mainbar.hpp"
2acdb232 45#include "view/view.hpp"
d7bed479 46
30236a54
JH
47#include <stdint.h>
48#include <stdarg.h>
fe3a1c21 49#include <libsigrokcxx/libsigrokcxx.hpp>
e82fd481 50
101e7a9b 51using std::dynamic_pointer_cast;
819f4c25 52using std::list;
25272fee 53using std::make_shared;
6842b5fc 54using std::map;
f9abf97e 55using std::shared_ptr;
6842b5fc 56using std::string;
e8d00928 57
51e77110
JH
58namespace pv {
59
b1264f56 60namespace view {
26e3af6b 61class ViewItem;
b1264f56
JH
62}
63
0f8f8c18 64using toolbars::MainBar;
643f65f9 65
33e1afbe
SA
66const QString MainWindow::WindowTitle = tr("PulseView");
67
107ca6d3 68MainWindow::MainWindow(DeviceManager &device_manager,
e3c79b07 69 string open_file_name, string open_file_format,
1d478458 70 QWidget *parent) :
107ca6d3 71 QMainWindow(parent),
8dbbc7f0 72 device_manager_(device_manager),
3b84fd0b 73 session_selector_(this),
c7b03d9d 74 action_view_sticky_scrolling_(new QAction(this)),
0fb9d645 75 action_view_coloured_bg_(new QAction(this)),
e79171dc 76 action_about_(new QAction(this))
d7bed479 77{
48257a69
SA
78 qRegisterMetaType<util::Timestamp>("util::Timestamp");
79
7d5425ef 80 setup_ui();
93f683ad 81 restore_ui_settings();
101e7a9b
SA
82
83 if (!open_file_name.empty()) {
84 shared_ptr<Session> session = add_session();
85 session->main_bar()->load_init_file(open_file_name, open_file_format);
86 }
87
88 // Add empty default session if there aren't any sessions
89 if (sessions_.size() == 0) {
90 shared_ptr<Session> session = add_session();
91
92 map<string, string> dev_info;
93 shared_ptr<devices::HardwareDevice> other_device, demo_device;
94
95 // Use any available device that's not demo
96 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
97 if (dev->hardware_device()->driver()->name() == "demo") {
98 demo_device = dev;
99 } else {
100 other_device = dev;
101 }
102 }
103
104 // ...and if there isn't any, just use demo then
105 session->main_bar()->select_device(other_device ?
106 other_device : demo_device);
107 }
7d5425ef
JH
108}
109
47e9e7bb
SA
110MainWindow::~MainWindow()
111{
3b84fd0b
SA
112 while (!sessions_.empty())
113 remove_session(sessions_.front());
47e9e7bb
SA
114}
115
c7b03d9d
SA
116QAction* MainWindow::action_view_sticky_scrolling() const
117{
118 return action_view_sticky_scrolling_;
119}
120
bea236d6
JH
121QAction* MainWindow::action_view_coloured_bg() const
122{
123 return action_view_coloured_bg_;
124}
125
69654681
JH
126QAction* MainWindow::action_about() const
127{
128 return action_about_;
129}
130
f4e57597 131shared_ptr<views::ViewBase> MainWindow::get_active_view() const
168bd8ac
SA
132{
133 // If there's only one view, use it...
134 if (view_docks_.size() == 1)
135 return view_docks_.begin()->second;
136
137 // ...otherwise find the dock widget the widget with focus is contained in
138 QObject *w = QApplication::focusWidget();
139 QDockWidget *dock = 0;
140
141 while (w) {
142 dock = qobject_cast<QDockWidget*>(w);
143 if (dock)
144 break;
145 w = w->parent();
146 }
147
148 // Get the view contained in the dock widget
149 for (auto entry : view_docks_)
cbf7b5db 150 if (entry.first == dock)
168bd8ac
SA
151 return entry.second;
152
82f8a42b 153 return nullptr;
168bd8ac
SA
154}
155
f4e57597
SA
156shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
157 views::ViewType type, Session &session)
7cd2b5f3 158{
3b84fd0b
SA
159 QMainWindow *main_window;
160 for (auto entry : session_windows_)
161 if (entry.first.get() == &session)
162 main_window = entry.second;
163
164 assert(main_window);
165
f4e57597 166 if (type == views::ViewTypeTrace) {
cbf7b5db 167 QDockWidget* dock = new QDockWidget(title, main_window);
7cd2b5f3 168 dock->setObjectName(title);
cbf7b5db 169 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
0f8f8c18
SA
170
171 // Insert a QMainWindow into the dock widget to allow for a tool bar
cbf7b5db 172 QMainWindow *dock_main = new QMainWindow(dock);
0f8f8c18
SA
173 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
174
f4e57597
SA
175 shared_ptr<views::TraceView::View> v =
176 make_shared<views::TraceView::View>(session, dock_main);
7cd2b5f3 177 view_docks_[dock] = v;
0f8f8c18
SA
178 session.register_view(v);
179
180 dock_main->setCentralWidget(v.get());
181 dock->setWidget(dock_main);
7cd2b5f3 182
d6ab7b9a 183 dock->setFeatures(QDockWidget::DockWidgetMovable |
36a8185e
SA
184 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
185
186 QAbstractButton *close_btn =
187 dock->findChildren<QAbstractButton*>
188 ("qt_dockwidget_closebutton").front();
189
190 connect(close_btn, SIGNAL(clicked(bool)),
191 this, SLOT(on_view_close_clicked()));
d6ab7b9a 192
f4e57597
SA
193 if (type == views::ViewTypeTrace) {
194 connect(&session, SIGNAL(trigger_event(util::Timestamp)),
195 qobject_cast<views::ViewBase*>(v.get()),
7cd2b5f3 196 SLOT(trigger_event(util::Timestamp)));
7cd2b5f3
SA
197
198 v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
199 v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
7cd2b5f3 200
0f8f8c18
SA
201 shared_ptr<MainBar> main_bar = session.main_bar();
202 if (!main_bar) {
101e7a9b 203 main_bar = make_shared<MainBar>(session, *this);
0f8f8c18
SA
204 dock_main->addToolBar(main_bar.get());
205 session.set_main_bar(main_bar);
c9da5118 206
c9da5118
SA
207 connect(main_bar.get(), SIGNAL(new_view(Session*)),
208 this, SLOT(on_new_view(Session*)));
0f8f8c18
SA
209 }
210 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
d552c5c7
SA
211
212 connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
213 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
a55e7918 214 }
f4e57597
SA
215
216 return v;
e93f5538
JH
217 }
218
f4e57597 219 return nullptr;
ed43ef2e
JH
220}
221
101e7a9b
SA
222shared_ptr<Session> MainWindow::add_session()
223{
90d77e35
SA
224 static int last_session_id = 1;
225 QString name = tr("Untitled-%1").arg(last_session_id++);
101e7a9b
SA
226
227 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
228
f4e57597
SA
229 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
230 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
33e1afbe
SA
231 connect(session.get(), SIGNAL(name_changed()),
232 this, SLOT(on_session_name_changed()));
3a21afa6 233
101e7a9b
SA
234 sessions_.push_back(session);
235
3b84fd0b
SA
236 QMainWindow *window = new QMainWindow();
237 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
238 session_windows_[session] = window;
085cd160
SA
239
240 int index = session_selector_.addTab(window, name);
241 session_selector_.setCurrentIndex(index);
3b84fd0b 242
68ad932d
SA
243 window->setDockNestingEnabled(true);
244
f4e57597
SA
245 shared_ptr<views::ViewBase> main_view =
246 add_view(name, views::ViewTypeTrace, *session);
101e7a9b
SA
247
248 return session;
249}
250
36a8185e
SA
251void MainWindow::remove_session(shared_ptr<Session> session)
252{
cc964645
SA
253 int h = new_session_button_->height();
254
f4e57597 255 for (shared_ptr<views::ViewBase> view : session->views()) {
3b84fd0b 256 // Find the dock the view is contained in and remove it
36a8185e 257 for (auto entry : view_docks_)
3b84fd0b
SA
258 if (entry.second == view) {
259 // Remove the view from the session
260 session->deregister_view(view);
261
262 // Remove the view from its parent; otherwise, Qt will
263 // call deleteLater() on it, which causes a double free
264 // since the shared_ptr in view_docks_ doesn't know
265 // that Qt keeps a pointer to the view around
266 entry.second->setParent(0);
267
268 // Remove this entry from the container
269 view_docks_.erase(entry.first);
270 }
36a8185e
SA
271 }
272
3b84fd0b
SA
273 QMainWindow *window = session_windows_.at(session);
274 session_selector_.removeTab(session_selector_.indexOf(window));
275
276 session_windows_.erase(session);
277
36a8185e
SA
278 sessions_.remove_if([&](shared_ptr<Session> s) {
279 return s == session; });
33e1afbe 280
cc964645
SA
281 if (sessions_.empty()) {
282 // When there are no more tabs, the height of the QTabWidget
283 // drops to zero. We must prevent this to keep the static
284 // widgets visible
285 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
286 w->setMinimumHeight(h);
287
288 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
289 static_tab_widget_->setMinimumHeight(h + 2 * margin);
290 session_selector_.setMinimumHeight(h + 2 * margin);
291
292 // Update the window title if there is no view left to
293 // generate focus change events
2ec81436 294 setWindowTitle(WindowTitle);
cc964645 295 }
36a8185e
SA
296}
297
7d5425ef
JH
298void MainWindow::setup_ui()
299{
300 setObjectName(QString::fromUtf8("MainWindow"));
301
3b84fd0b
SA
302 setCentralWidget(&session_selector_);
303
7d5425ef
JH
304 // Set the window icon
305 QIcon icon;
14f9d4a1 306 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
7d5425ef
JH
307 setWindowIcon(icon);
308
c7b03d9d
SA
309 action_view_sticky_scrolling_->setCheckable(true);
310 action_view_sticky_scrolling_->setChecked(true);
69282fae 311 action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
c7b03d9d
SA
312 action_view_sticky_scrolling_->setObjectName(
313 QString::fromUtf8("actionViewStickyScrolling"));
69282fae 314 action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
ab1d13ee 315
0fb9d645
SA
316 action_view_coloured_bg_->setCheckable(true);
317 action_view_coloured_bg_->setChecked(true);
574c568d 318 action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
0fb9d645
SA
319 action_view_coloured_bg_->setObjectName(
320 QString::fromUtf8("actionViewColouredBg"));
321 action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
0fb9d645 322
69654681
JH
323 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
324 action_about_->setText(tr("&About..."));
7d5425ef 325
f1e2d26b
SA
326 // Set up the tab area
327 new_session_button_ = new QToolButton();
328 new_session_button_->setIcon(QIcon::fromTheme("document-new",
329 QIcon(":/icons/document-new.png")));
330 new_session_button_->setAutoRaise(true);
331
332 QHBoxLayout* layout = new QHBoxLayout();
333 layout->setContentsMargins(2, 2, 2, 2);
334 layout->addWidget(new_session_button_);
335
cc964645 336 static_tab_widget_ = new QWidget();
f1e2d26b
SA
337 static_tab_widget_->setLayout(layout);
338
339 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
4a4e20f5
SA
340 session_selector_.setTabsClosable(true);
341
f1e2d26b
SA
342 connect(new_session_button_, SIGNAL(clicked(bool)),
343 this, SLOT(on_new_session_clicked()));
344
4a4e20f5
SA
345 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
346 this, SLOT(on_tab_close_requested(int)));
e7aff437
SA
347 connect(&session_selector_, SIGNAL(currentChanged(int)),
348 this, SLOT(on_tab_changed(int)));
4a4e20f5 349
f1e2d26b 350
33e1afbe
SA
351 connect(static_cast<QApplication *>(QCoreApplication::instance()),
352 SIGNAL(focusChanged(QWidget*, QWidget*)),
353 this, SLOT(on_focus_changed()));
e3c79b07
JH
354}
355
93f683ad
SA
356void MainWindow::save_ui_settings()
357{
39eb0d45 358 QSettings settings;
101e7a9b 359 int id = 0;
6842b5fc 360
93f683ad
SA
361 settings.beginGroup("MainWindow");
362 settings.setValue("state", saveState());
363 settings.setValue("geometry", saveGeometry());
364 settings.endGroup();
6842b5fc 365
101e7a9b 366 for (shared_ptr<Session> session : sessions_) {
3503810c 367 // Ignore sessions using the demo device or no device at all
101e7a9b
SA
368 if (session->device()) {
369 shared_ptr<devices::HardwareDevice> device =
370 dynamic_pointer_cast< devices::HardwareDevice >
371 (session->device());
372
f2040dcb
SA
373 if (device &&
374 device->hardware_device()->driver()->name() == "demo")
101e7a9b 375 continue;
6842b5fc 376
3503810c
SA
377 settings.beginGroup("Session" + QString::number(id++));
378 settings.remove(""); // Remove all keys in this group
379 session->save_settings(settings);
380 settings.endGroup();
381 }
6842b5fc 382 }
101e7a9b
SA
383
384 settings.setValue("sessions", id);
93f683ad
SA
385}
386
387void MainWindow::restore_ui_settings()
388{
39eb0d45 389 QSettings settings;
101e7a9b 390 int i, session_count;
93f683ad
SA
391
392 settings.beginGroup("MainWindow");
393
394 if (settings.contains("geometry")) {
395 restoreGeometry(settings.value("geometry").toByteArray());
396 restoreState(settings.value("state").toByteArray());
397 } else
398 resize(1000, 720);
399
400 settings.endGroup();
101e7a9b
SA
401
402 session_count = settings.value("sessions", 0).toInt();
403
404 for (i = 0; i < session_count; i++) {
405 settings.beginGroup("Session" + QString::number(i));
406 shared_ptr<Session> session = add_session();
407 session->restore_settings(settings);
408 settings.endGroup();
409 }
93f683ad
SA
410}
411
e7aff437
SA
412std::shared_ptr<Session> MainWindow::get_tab_session(int index) const
413{
414 // Find the session that belongs to the tab's main window
415 for (auto entry : session_windows_)
416 if (entry.second == session_selector_.widget(index))
417 return entry.first;
418
419 return nullptr;
420}
421
e3c79b07
JH
422void MainWindow::closeEvent(QCloseEvent *event)
423{
424 save_ui_settings();
425 event->accept();
426}
427
d290e89f
SA
428QMenu* MainWindow::createPopupMenu()
429{
430 return nullptr;
431}
432
55547a45
SA
433bool MainWindow::restoreState(const QByteArray &state, int version)
434{
435 (void)state;
436 (void)version;
437
438 // Do nothing. We don't want Qt to handle this, or else it
439 // will try to restore all the dock widgets and create havoc.
440
441 return false;
442}
443
f4e57597 444void MainWindow::on_add_view(const QString &title, views::ViewType type,
3a21afa6
SA
445 Session *session)
446{
447 // We get a pointer and need a reference
448 for (std::shared_ptr<Session> s : sessions_)
449 if (s.get() == session)
450 add_view(title, type, *s);
451}
452
33e1afbe
SA
453void MainWindow::on_focus_changed()
454{
e7aff437
SA
455 static shared_ptr<Session> prev_session;
456
457 shared_ptr<views::ViewBase> view = get_active_view();
458
459 if (view) {
460 for (shared_ptr<Session> session : sessions_) {
461 if (session->has_view(view)) {
462 if (session != prev_session) {
463 // Activate correct tab if necessary
464 shared_ptr<Session> tab_session = get_tab_session(
465 session_selector_.currentIndex());
466 if (tab_session != session)
467 session_selector_.setCurrentWidget(
468 session_windows_.at(session));
469
470 on_focused_session_changed(session);
471 }
472
473 prev_session = session;
474 break;
475 }
476 }
33e1afbe
SA
477 }
478
e7aff437 479 if (sessions_.empty())
33e1afbe
SA
480 setWindowTitle(WindowTitle);
481}
482
e7aff437
SA
483void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
484{
485 setWindowTitle(session->name() + " - " + WindowTitle);
486}
487
f1e2d26b 488void MainWindow::on_new_session_clicked()
c9da5118
SA
489{
490 add_session();
491}
492
33e1afbe
SA
493void MainWindow::on_session_name_changed()
494{
495 // Update the corresponding dock widget's name(s)
496 Session *session = qobject_cast<Session*>(QObject::sender());
497 assert(session);
498
f4e57597 499 for (shared_ptr<views::ViewBase> view : session->views()) {
33e1afbe
SA
500 // Get the dock that contains the view
501 for (auto entry : view_docks_)
502 if (entry.second == view) {
503 entry.first->setObjectName(session->name());
504 entry.first->setWindowTitle(session->name());
505 }
506 }
507
508 // Refresh window title if the affected session has focus
e7aff437
SA
509 shared_ptr<views::ViewBase> view = get_active_view();
510
511 if (view && session->has_view(view))
512 setWindowTitle(session->name() + " - " + WindowTitle);
33e1afbe
SA
513}
514
c9da5118
SA
515void MainWindow::on_new_view(Session *session)
516{
517 // We get a pointer and need a reference
518 for (std::shared_ptr<Session> s : sessions_)
519 if (s.get() == session)
f4e57597 520 add_view(session->name(), views::ViewTypeTrace, *s);
c9da5118
SA
521}
522
36a8185e
SA
523void MainWindow::on_view_close_clicked()
524{
525 // Find the dock widget that contains the close button that was clicked
526 QObject *w = QObject::sender();
527 QDockWidget *dock = 0;
528
529 while (w) {
530 dock = qobject_cast<QDockWidget*>(w);
531 if (dock)
532 break;
533 w = w->parent();
534 }
535
536 // Get the view contained in the dock widget
f4e57597 537 shared_ptr<views::ViewBase> view;
36a8185e
SA
538
539 for (auto entry : view_docks_)
cbf7b5db 540 if (entry.first == dock)
36a8185e
SA
541 view = entry.second;
542
543 // Deregister the view
544 for (shared_ptr<Session> session : sessions_) {
545 if (!session->has_view(view))
546 continue;
547
548 // Also destroy the entire session if its main view is closing
549 if (view == session->main_view()) {
550 remove_session(session);
551 break;
552 } else
553 session->deregister_view(view);
554 }
555}
556
e7aff437
SA
557void MainWindow::on_tab_changed(int index)
558{
559 shared_ptr<Session> session = get_tab_session(index);
560
561 if (session)
562 on_focused_session_changed(session);
563}
564
4a4e20f5
SA
565void MainWindow::on_tab_close_requested(int index)
566{
567 // TODO Ask user if this is intended in case data is unsaved
568
e7aff437
SA
569 shared_ptr<Session> session = get_tab_session(index);
570
571 if (session)
572 remove_session(session);
4a4e20f5
SA
573}
574
c7b03d9d
SA
575void MainWindow::on_actionViewStickyScrolling_triggered()
576{
f4e57597
SA
577 shared_ptr<views::ViewBase> viewbase = get_active_view();
578 views::TraceView::View* view =
579 qobject_cast<views::TraceView::View*>(viewbase.get());
168bd8ac
SA
580 if (view)
581 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
c7b03d9d
SA
582}
583
0fb9d645
SA
584void MainWindow::on_actionViewColouredBg_triggered()
585{
f4e57597
SA
586 shared_ptr<views::ViewBase> viewbase = get_active_view();
587 views::TraceView::View* view =
588 qobject_cast<views::TraceView::View*>(viewbase.get());
168bd8ac
SA
589 if (view)
590 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
0fb9d645
SA
591}
592
30236a54
JH
593void MainWindow::on_actionAbout_triggered()
594{
8dbbc7f0 595 dialogs::About dlg(device_manager_.context(), this);
40eb2ff4 596 dlg.exec();
30236a54 597}
274d4f13 598
51e77110 599} // namespace pv