]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
Bring back sticky scroll and coloured background shortcuts.
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <cassert>
21
22#ifdef ENABLE_DECODE
23#include <libsigrokdecode/libsigrokdecode.h>
24#endif
25
26#include <algorithm>
27#include <iterator>
28
29#include <QAction>
30#include <QApplication>
31#include <QCloseEvent>
32#include <QDockWidget>
33#include <QHBoxLayout>
34#include <QMessageBox>
35#include <QSettings>
36#include <QWidget>
37#include <QShortcut>
38
39#include "mainwindow.hpp"
40
41#include "devicemanager.hpp"
42#include "util.hpp"
43#include "devices/hardwaredevice.hpp"
44#include "dialogs/about.hpp"
45#include "toolbars/mainbar.hpp"
46#include "view/view.hpp"
47#include "views/trace/standardbar.hpp"
48
49#include <stdint.h>
50#include <stdarg.h>
51#include <libsigrokcxx/libsigrokcxx.hpp>
52
53using std::dynamic_pointer_cast;
54using std::list;
55using std::make_shared;
56using std::map;
57using std::shared_ptr;
58using std::string;
59
60namespace pv {
61
62namespace view {
63class ViewItem;
64}
65
66using toolbars::MainBar;
67
68const QString MainWindow::WindowTitle = tr("PulseView");
69
70MainWindow::MainWindow(DeviceManager &device_manager,
71 string open_file_name, string open_file_format,
72 QWidget *parent) :
73 QMainWindow(parent),
74 device_manager_(device_manager),
75 session_selector_(this),
76 session_state_mapper_(this),
77 action_about_(new QAction(this)),
78 icon_red_(":/icons/status-red.svg"),
79 icon_green_(":/icons/status-green.svg"),
80 icon_grey_(":/icons/status-grey.svg")
81{
82 qRegisterMetaType<util::Timestamp>("util::Timestamp");
83 qRegisterMetaType<uint64_t>("uint64_t");
84
85 setup_ui();
86 restore_ui_settings();
87
88 if (!open_file_name.empty()) {
89 shared_ptr<Session> session = add_session();
90 session->load_init_file(open_file_name, open_file_format);
91 }
92
93 // Add empty default session if there aren't any sessions
94 if (sessions_.size() == 0) {
95 shared_ptr<Session> session = add_session();
96
97 map<string, string> dev_info;
98 shared_ptr<devices::HardwareDevice> other_device, demo_device;
99
100 // Use any available device that's not demo
101 for (shared_ptr<devices::HardwareDevice> dev : device_manager_.devices()) {
102 if (dev->hardware_device()->driver()->name() == "demo") {
103 demo_device = dev;
104 } else {
105 other_device = dev;
106 }
107 }
108
109 // ...and if there isn't any, just use demo then
110 session->select_device(other_device ? other_device : demo_device);
111 }
112}
113
114MainWindow::~MainWindow()
115{
116 while (!sessions_.empty())
117 remove_session(sessions_.front());
118}
119
120QAction* MainWindow::action_about() const
121{
122 return action_about_;
123}
124
125shared_ptr<views::ViewBase> MainWindow::get_active_view() const
126{
127 // If there's only one view, use it...
128 if (view_docks_.size() == 1)
129 return view_docks_.begin()->second;
130
131 // ...otherwise find the dock widget the widget with focus is contained in
132 QObject *w = QApplication::focusWidget();
133 QDockWidget *dock = 0;
134
135 while (w) {
136 dock = qobject_cast<QDockWidget*>(w);
137 if (dock)
138 break;
139 w = w->parent();
140 }
141
142 // Get the view contained in the dock widget
143 for (auto entry : view_docks_)
144 if (entry.first == dock)
145 return entry.second;
146
147 return nullptr;
148}
149
150shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
151 views::ViewType type, Session &session)
152{
153 QMainWindow *main_window = nullptr;
154 for (auto entry : session_windows_)
155 if (entry.first.get() == &session)
156 main_window = entry.second;
157
158 assert(main_window);
159
160 if (type == views::ViewTypeTrace) {
161 QDockWidget* dock = new QDockWidget(title, main_window);
162 dock->setObjectName(title);
163 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
164
165 // Insert a QMainWindow into the dock widget to allow for a tool bar
166 QMainWindow *dock_main = new QMainWindow(dock);
167 dock_main->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
168
169 shared_ptr<views::TraceView::View> v =
170 make_shared<views::TraceView::View>(session, dock_main);
171 view_docks_[dock] = v;
172 session.register_view(v);
173
174 dock_main->setCentralWidget(v.get());
175 dock->setWidget(dock_main);
176
177 dock->setFeatures(QDockWidget::DockWidgetMovable |
178 QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
179
180 QAbstractButton *close_btn =
181 dock->findChildren<QAbstractButton*>
182 ("qt_dockwidget_closebutton").front();
183
184 connect(close_btn, SIGNAL(clicked(bool)),
185 this, SLOT(on_view_close_clicked()));
186
187 if (type == views::ViewTypeTrace) {
188 connect(&session, SIGNAL(trigger_event(util::Timestamp)),
189 qobject_cast<views::ViewBase*>(v.get()),
190 SLOT(trigger_event(util::Timestamp)));
191
192 v->enable_sticky_scrolling(true);
193 v->enable_coloured_bg(true);
194
195 shared_ptr<MainBar> main_bar = session.main_bar();
196 if (!main_bar) {
197 /* Initial view, create the main bar */
198 main_bar = make_shared<MainBar>(session, this, v.get());
199 dock_main->addToolBar(main_bar.get());
200 session.set_main_bar(main_bar);
201
202 connect(main_bar.get(), SIGNAL(new_view(Session*)),
203 this, SLOT(on_new_view(Session*)));
204
205 main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
206
207 /* For the main view we need to prevent the dock widget from
208 * closing itself when its close button is clicked. This is
209 * so we can confirm with the user first. Regular views don't
210 * need this */
211 close_btn->disconnect(SIGNAL(clicked()), dock, SLOT(close()));
212 } else {
213 /* Additional view, create a standard bar */
214 pv::views::trace::StandardBar *standard_bar =
215 new pv::views::trace::StandardBar(session, this, v.get());
216 dock_main->addToolBar(standard_bar);
217
218 standard_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
219 }
220 }
221
222 return v;
223 }
224
225 return nullptr;
226}
227
228void MainWindow::remove_view(shared_ptr<views::ViewBase> view)
229{
230 for (shared_ptr<Session> session : sessions_) {
231 if (!session->has_view(view))
232 continue;
233
234 // Find the dock the view is contained in and remove it
235 for (auto entry : view_docks_)
236 if (entry.second == view) {
237 // Remove the view from the session
238 session->deregister_view(view);
239
240 // Remove the view from its parent; otherwise, Qt will
241 // call deleteLater() on it, which causes a double free
242 // since the shared_ptr in view_docks_ doesn't know
243 // that Qt keeps a pointer to the view around
244 view->setParent(0);
245
246 // Delete the view's dock widget and all widgets inside it
247 entry.first->deleteLater();
248
249 // Remove the dock widget from the list and stop iterating
250 view_docks_.erase(entry.first);
251 break;
252 }
253 }
254}
255
256shared_ptr<Session> MainWindow::add_session()
257{
258 static int last_session_id = 1;
259 QString name = tr("Session %1").arg(last_session_id++);
260
261 shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
262
263 connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
264 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
265 connect(session.get(), SIGNAL(name_changed()),
266 this, SLOT(on_session_name_changed()));
267 session_state_mapper_.setMapping(session.get(), session.get());
268 connect(session.get(), SIGNAL(capture_state_changed(int)),
269 &session_state_mapper_, SLOT(map()));
270
271 sessions_.push_back(session);
272
273 QMainWindow *window = new QMainWindow();
274 window->setWindowFlags(Qt::Widget); // Remove Qt::Window flag
275 session_windows_[session] = window;
276
277 int index = session_selector_.addTab(window, name);
278 session_selector_.setCurrentIndex(index);
279 last_focused_session_ = session;
280
281 window->setDockNestingEnabled(true);
282
283 shared_ptr<views::ViewBase> main_view =
284 add_view(name, views::ViewTypeTrace, *session);
285
286 return session;
287}
288
289void MainWindow::remove_session(shared_ptr<Session> session)
290{
291 int h = new_session_button_->height();
292
293 for (shared_ptr<views::ViewBase> view : session->views())
294 remove_view(view);
295
296 QMainWindow *window = session_windows_.at(session);
297 session_selector_.removeTab(session_selector_.indexOf(window));
298
299 session_windows_.erase(session);
300
301 if (last_focused_session_ == session)
302 last_focused_session_.reset();
303
304 sessions_.remove_if([&](shared_ptr<Session> s) {
305 return s == session; });
306
307 if (sessions_.empty()) {
308 // When there are no more tabs, the height of the QTabWidget
309 // drops to zero. We must prevent this to keep the static
310 // widgets visible
311 for (QWidget *w : static_tab_widget_->findChildren<QWidget*>())
312 w->setMinimumHeight(h);
313
314 int margin = static_tab_widget_->layout()->contentsMargins().bottom();
315 static_tab_widget_->setMinimumHeight(h + 2 * margin);
316 session_selector_.setMinimumHeight(h + 2 * margin);
317
318 // Update the window title if there is no view left to
319 // generate focus change events
320 setWindowTitle(WindowTitle);
321 }
322}
323
324void MainWindow::setup_ui()
325{
326 setObjectName(QString::fromUtf8("MainWindow"));
327
328 setCentralWidget(&session_selector_);
329
330 // Set the window icon
331 QIcon icon;
332 icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
333 setWindowIcon(icon);
334
335 view_sticky_scrolling_shortcut_ = new QShortcut(QKeySequence(Qt::Key_S), this, SLOT(on_view_sticky_scrolling_shortcut()));
336 view_sticky_scrolling_shortcut_->setAutoRepeat(false);
337
338 view_coloured_bg_shortcut_ = new QShortcut(QKeySequence(Qt::Key_B), this, SLOT(on_view_coloured_bg_shortcut()));
339 view_coloured_bg_shortcut_->setAutoRepeat(false);
340
341 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
342 action_about_->setToolTip(tr("&About..."));
343
344 // Set up the tab area
345 new_session_button_ = new QToolButton();
346 new_session_button_->setIcon(QIcon::fromTheme("document-new",
347 QIcon(":/icons/document-new.png")));
348 new_session_button_->setToolTip(tr("Create New Session"));
349 new_session_button_->setAutoRaise(true);
350
351 run_stop_button_ = new QToolButton();
352 run_stop_button_->setAutoRaise(true);
353 run_stop_button_->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
354 run_stop_button_->setToolTip(tr("Start/Stop Acquisition"));
355
356 run_stop_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Space), run_stop_button_, SLOT(click()));
357 run_stop_shortcut_->setAutoRepeat(false);
358
359 settings_button_ = new QToolButton();
360 settings_button_->setIcon(QIcon::fromTheme("configure",
361 QIcon(":/icons/configure.png")));
362 settings_button_->setToolTip(tr("Settings"));
363 settings_button_->setAutoRaise(true);
364
365 QFrame *separator1 = new QFrame();
366 separator1->setFrameStyle(QFrame::VLine | QFrame::Raised);
367 QFrame *separator2 = new QFrame();
368 separator2->setFrameStyle(QFrame::VLine | QFrame::Raised);
369
370 QHBoxLayout* layout = new QHBoxLayout();
371 layout->setContentsMargins(2, 2, 2, 2);
372 layout->addWidget(new_session_button_);
373 layout->addWidget(separator1);
374 layout->addWidget(run_stop_button_);
375 layout->addWidget(separator2);
376 layout->addWidget(settings_button_);
377
378 static_tab_widget_ = new QWidget();
379 static_tab_widget_->setLayout(layout);
380
381 session_selector_.setCornerWidget(static_tab_widget_, Qt::TopLeftCorner);
382 session_selector_.setTabsClosable(true);
383
384 close_application_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
385 close_application_shortcut_->setAutoRepeat(false);
386
387 close_current_tab_shortcut_ = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this, SLOT(on_close_current_tab()));
388
389 connect(new_session_button_, SIGNAL(clicked(bool)),
390 this, SLOT(on_new_session_clicked()));
391 connect(run_stop_button_, SIGNAL(clicked(bool)),
392 this, SLOT(on_run_stop_clicked()));
393 connect(&session_state_mapper_, SIGNAL(mapped(QObject*)),
394 this, SLOT(on_capture_state_changed(QObject*)));
395
396 connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
397 this, SLOT(on_tab_close_requested(int)));
398 connect(&session_selector_, SIGNAL(currentChanged(int)),
399 this, SLOT(on_tab_changed(int)));
400
401
402 connect(static_cast<QApplication *>(QCoreApplication::instance()),
403 SIGNAL(focusChanged(QWidget*, QWidget*)),
404 this, SLOT(on_focus_changed()));
405}
406
407void MainWindow::save_ui_settings()
408{
409 QSettings settings;
410 int id = 0;
411
412 settings.beginGroup("MainWindow");
413 settings.setValue("state", saveState());
414 settings.setValue("geometry", saveGeometry());
415 settings.endGroup();
416
417 for (shared_ptr<Session> session : sessions_) {
418 // Ignore sessions using the demo device or no device at all
419 if (session->device()) {
420 shared_ptr<devices::HardwareDevice> device =
421 dynamic_pointer_cast< devices::HardwareDevice >
422 (session->device());
423
424 if (device &&
425 device->hardware_device()->driver()->name() == "demo")
426 continue;
427
428 settings.beginGroup("Session" + QString::number(id++));
429 settings.remove(""); // Remove all keys in this group
430 session->save_settings(settings);
431 settings.endGroup();
432 }
433 }
434
435 settings.setValue("sessions", id);
436}
437
438void MainWindow::restore_ui_settings()
439{
440 QSettings settings;
441 int i, session_count;
442
443 settings.beginGroup("MainWindow");
444
445 if (settings.contains("geometry")) {
446 restoreGeometry(settings.value("geometry").toByteArray());
447 restoreState(settings.value("state").toByteArray());
448 } else
449 resize(1000, 720);
450
451 settings.endGroup();
452
453 session_count = settings.value("sessions", 0).toInt();
454
455 for (i = 0; i < session_count; i++) {
456 settings.beginGroup("Session" + QString::number(i));
457 shared_ptr<Session> session = add_session();
458 session->restore_settings(settings);
459 settings.endGroup();
460 }
461}
462
463std::shared_ptr<Session> MainWindow::get_tab_session(int index) const
464{
465 // Find the session that belongs to the tab's main window
466 for (auto entry : session_windows_)
467 if (entry.second == session_selector_.widget(index))
468 return entry.first;
469
470 return nullptr;
471}
472
473void MainWindow::closeEvent(QCloseEvent *event)
474{
475 bool data_saved = true;
476
477 for (auto entry : session_windows_)
478 if (!entry.first->data_saved())
479 data_saved = false;
480
481 if (!data_saved && (QMessageBox::question(this, tr("Confirmation"),
482 tr("There is unsaved data. Close anyway?"),
483 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)) {
484 event->ignore();
485 } else {
486 save_ui_settings();
487 event->accept();
488 }
489}
490
491QMenu* MainWindow::createPopupMenu()
492{
493 return nullptr;
494}
495
496bool MainWindow::restoreState(const QByteArray &state, int version)
497{
498 (void)state;
499 (void)version;
500
501 // Do nothing. We don't want Qt to handle this, or else it
502 // will try to restore all the dock widgets and create havoc.
503
504 return false;
505}
506
507void MainWindow::session_error(const QString text, const QString info_text)
508{
509 QMetaObject::invokeMethod(this, "show_session_error",
510 Qt::QueuedConnection, Q_ARG(QString, text),
511 Q_ARG(QString, info_text));
512}
513
514void MainWindow::show_session_error(const QString text, const QString info_text)
515{
516 QMessageBox msg(this);
517 msg.setText(text);
518 msg.setInformativeText(info_text);
519 msg.setStandardButtons(QMessageBox::Ok);
520 msg.setIcon(QMessageBox::Warning);
521 msg.exec();
522}
523
524void MainWindow::on_add_view(const QString &title, views::ViewType type,
525 Session *session)
526{
527 // We get a pointer and need a reference
528 for (std::shared_ptr<Session> s : sessions_)
529 if (s.get() == session)
530 add_view(title, type, *s);
531}
532
533void MainWindow::on_focus_changed()
534{
535 shared_ptr<views::ViewBase> view = get_active_view();
536
537 if (view) {
538 for (shared_ptr<Session> session : sessions_) {
539 if (session->has_view(view)) {
540 if (session != last_focused_session_) {
541 // Activate correct tab if necessary
542 shared_ptr<Session> tab_session = get_tab_session(
543 session_selector_.currentIndex());
544 if (tab_session != session)
545 session_selector_.setCurrentWidget(
546 session_windows_.at(session));
547
548 on_focused_session_changed(session);
549 }
550
551 break;
552 }
553 }
554 }
555
556 if (sessions_.empty())
557 setWindowTitle(WindowTitle);
558}
559
560void MainWindow::on_focused_session_changed(shared_ptr<Session> session)
561{
562 last_focused_session_ = session;
563
564 setWindowTitle(session->name() + " - " + WindowTitle);
565
566 // Update the state of the run/stop button, too
567 on_capture_state_changed(session.get());
568}
569
570void MainWindow::on_new_session_clicked()
571{
572 add_session();
573}
574
575void MainWindow::on_run_stop_clicked()
576{
577 shared_ptr<Session> session = last_focused_session_;
578
579 if (!session)
580 return;
581
582 switch (session->get_capture_state()) {
583 case Session::Stopped:
584 session->start_capture([&](QString message) {
585 session_error("Capture failed", message); });
586 break;
587 case Session::AwaitingTrigger:
588 case Session::Running:
589 session->stop_capture();
590 break;
591 }
592}
593
594void MainWindow::on_session_name_changed()
595{
596 // Update the corresponding dock widget's name(s)
597 Session *session = qobject_cast<Session*>(QObject::sender());
598 assert(session);
599
600 for (shared_ptr<views::ViewBase> view : session->views()) {
601 // Get the dock that contains the view
602 for (auto entry : view_docks_)
603 if (entry.second == view) {
604 entry.first->setObjectName(session->name());
605 entry.first->setWindowTitle(session->name());
606 }
607 }
608
609 // Update the tab widget by finding the main window and the tab from that
610 for (auto entry : session_windows_)
611 if (entry.first.get() == session) {
612 QMainWindow *window = entry.second;
613 const int index = session_selector_.indexOf(window);
614 session_selector_.setTabText(index, session->name());
615 }
616
617 // Refresh window title if the affected session has focus
618 if (session == last_focused_session_.get())
619 setWindowTitle(session->name() + " - " + WindowTitle);
620}
621
622void MainWindow::on_capture_state_changed(QObject *obj)
623{
624 Session *caller = qobject_cast<Session*>(obj);
625
626 // Ignore if caller is not the currently focused session
627 // unless there is only one session
628 if ((sessions_.size() > 1) && (caller != last_focused_session_.get()))
629 return;
630
631 int state = caller->get_capture_state();
632
633 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
634 run_stop_button_->setIcon(*icons[state]);
635 run_stop_button_->setText((state == pv::Session::Stopped) ?
636 tr("Run") : tr("Stop"));
637}
638
639void MainWindow::on_new_view(Session *session)
640{
641 // We get a pointer and need a reference
642 for (std::shared_ptr<Session> s : sessions_)
643 if (s.get() == session)
644 add_view(session->name(), views::ViewTypeTrace, *s);
645}
646
647void MainWindow::on_view_close_clicked()
648{
649 // Find the dock widget that contains the close button that was clicked
650 QObject *w = QObject::sender();
651 QDockWidget *dock = 0;
652
653 while (w) {
654 dock = qobject_cast<QDockWidget*>(w);
655 if (dock)
656 break;
657 w = w->parent();
658 }
659
660 // Get the view contained in the dock widget
661 shared_ptr<views::ViewBase> view;
662
663 for (auto entry : view_docks_)
664 if (entry.first == dock)
665 view = entry.second;
666
667 // Deregister the view
668 for (shared_ptr<Session> session : sessions_) {
669 if (!session->has_view(view))
670 continue;
671
672 // Also destroy the entire session if its main view is closing...
673 if (view == session->main_view()) {
674 // ...but only if data is saved or the user confirms closing
675 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
676 tr("This session contains unsaved data. Close it anyway?"),
677 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
678 remove_session(session);
679 break;
680 } else
681 // All other views can be closed at any time as no data will be lost
682 remove_view(view);
683 }
684}
685
686void MainWindow::on_tab_changed(int index)
687{
688 shared_ptr<Session> session = get_tab_session(index);
689
690 if (session)
691 on_focused_session_changed(session);
692}
693
694void MainWindow::on_tab_close_requested(int index)
695{
696 shared_ptr<Session> session = get_tab_session(index);
697
698 assert(session);
699
700 if (session->data_saved() || (QMessageBox::question(this, tr("Confirmation"),
701 tr("This session contains unsaved data. Close it anyway?"),
702 QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes))
703 remove_session(session);
704}
705
706void MainWindow::on_view_sticky_scrolling_shortcut()
707{
708 shared_ptr<views::ViewBase> viewbase = get_active_view();
709 views::TraceView::View* view =
710 qobject_cast<views::TraceView::View*>(viewbase.get());
711 if (view)
712 view->toggle_sticky_scrolling();
713}
714
715void MainWindow::on_view_coloured_bg_shortcut()
716{
717 shared_ptr<views::ViewBase> viewbase = get_active_view();
718 views::TraceView::View* view =
719 qobject_cast<views::TraceView::View*>(viewbase.get());
720 if (view)
721 view->toggle_coloured_bg();
722}
723
724void MainWindow::on_actionAbout_triggered()
725{
726 dialogs::About dlg(device_manager_.context(), this);
727 dlg.exec();
728}
729
730void MainWindow::on_close_current_tab()
731{
732 int tab = session_selector_.currentIndex();
733
734 on_tab_close_requested(tab);
735}
736
737} // namespace pv