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