]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
MainWindow: Use a QTabWidget to display the QDockWidgets
[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_)
149 if (entry.first.get() == dock)
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) {
3b84fd0b 166 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, main_window);
7cd2b5f3 167 dock->setObjectName(title);
3b84fd0b 168 main_window->addDockWidget(Qt::TopDockWidgetArea, dock.get());
0f8f8c18
SA
169
170 // Insert a QMainWindow into the dock widget to allow for a tool bar
171 QMainWindow *dock_main = new QMainWindow(dock.get());
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
956a945e
SA
310 setDockNestingEnabled(true);
311
33e1afbe
SA
312 connect(static_cast<QApplication *>(QCoreApplication::instance()),
313 SIGNAL(focusChanged(QWidget*, QWidget*)),
314 this, SLOT(on_focus_changed()));
e3c79b07
JH
315}
316
93f683ad
SA
317void MainWindow::save_ui_settings()
318{
39eb0d45 319 QSettings settings;
101e7a9b 320 int id = 0;
6842b5fc 321
93f683ad
SA
322 settings.beginGroup("MainWindow");
323 settings.setValue("state", saveState());
324 settings.setValue("geometry", saveGeometry());
325 settings.endGroup();
6842b5fc 326
101e7a9b 327 for (shared_ptr<Session> session : sessions_) {
3503810c 328 // Ignore sessions using the demo device or no device at all
101e7a9b
SA
329 if (session->device()) {
330 shared_ptr<devices::HardwareDevice> device =
331 dynamic_pointer_cast< devices::HardwareDevice >
332 (session->device());
333
f2040dcb
SA
334 if (device &&
335 device->hardware_device()->driver()->name() == "demo")
101e7a9b 336 continue;
6842b5fc 337
3503810c
SA
338 settings.beginGroup("Session" + QString::number(id++));
339 settings.remove(""); // Remove all keys in this group
340 session->save_settings(settings);
341 settings.endGroup();
342 }
6842b5fc 343 }
101e7a9b
SA
344
345 settings.setValue("sessions", id);
93f683ad
SA
346}
347
348void MainWindow::restore_ui_settings()
349{
39eb0d45 350 QSettings settings;
101e7a9b 351 int i, session_count;
93f683ad
SA
352
353 settings.beginGroup("MainWindow");
354
355 if (settings.contains("geometry")) {
356 restoreGeometry(settings.value("geometry").toByteArray());
357 restoreState(settings.value("state").toByteArray());
358 } else
359 resize(1000, 720);
360
361 settings.endGroup();
101e7a9b
SA
362
363 session_count = settings.value("sessions", 0).toInt();
364
365 for (i = 0; i < session_count; i++) {
366 settings.beginGroup("Session" + QString::number(i));
367 shared_ptr<Session> session = add_session();
368 session->restore_settings(settings);
369 settings.endGroup();
370 }
93f683ad
SA
371}
372
e3c79b07
JH
373void MainWindow::closeEvent(QCloseEvent *event)
374{
375 save_ui_settings();
376 event->accept();
377}
378
d290e89f
SA
379QMenu* MainWindow::createPopupMenu()
380{
381 return nullptr;
382}
383
55547a45
SA
384bool MainWindow::restoreState(const QByteArray &state, int version)
385{
386 (void)state;
387 (void)version;
388
389 // Do nothing. We don't want Qt to handle this, or else it
390 // will try to restore all the dock widgets and create havoc.
391
392 return false;
393}
394
f4e57597 395void MainWindow::on_add_view(const QString &title, views::ViewType type,
3a21afa6
SA
396 Session *session)
397{
398 // We get a pointer and need a reference
399 for (std::shared_ptr<Session> s : sessions_)
400 if (s.get() == session)
401 add_view(title, type, *s);
402}
403
33e1afbe
SA
404void MainWindow::on_focus_changed()
405{
f4e57597 406 shared_ptr<views::ViewBase> view;
33e1afbe
SA
407 bool title_set = false;
408
409 view = get_active_view();
410
411 for (shared_ptr<Session> session : sessions_) {
412 if (!session->has_view(view))
413 continue;
414
415 setWindowTitle(session->name() + " - " + WindowTitle);
416 title_set = true;
417 }
418
419 if (!title_set)
420 setWindowTitle(WindowTitle);
421}
422
c9da5118
SA
423void MainWindow::on_new_session()
424{
425 add_session();
426}
427
33e1afbe
SA
428void MainWindow::on_session_name_changed()
429{
430 // Update the corresponding dock widget's name(s)
431 Session *session = qobject_cast<Session*>(QObject::sender());
432 assert(session);
433
f4e57597 434 for (shared_ptr<views::ViewBase> view : session->views()) {
33e1afbe
SA
435 // Get the dock that contains the view
436 for (auto entry : view_docks_)
437 if (entry.second == view) {
438 entry.first->setObjectName(session->name());
439 entry.first->setWindowTitle(session->name());
440 }
441 }
442
443 // Refresh window title if the affected session has focus
444 on_focus_changed();
445}
446
c9da5118
SA
447void MainWindow::on_new_view(Session *session)
448{
449 // We get a pointer and need a reference
450 for (std::shared_ptr<Session> s : sessions_)
451 if (s.get() == session)
f4e57597 452 add_view(session->name(), views::ViewTypeTrace, *s);
c9da5118
SA
453}
454
36a8185e
SA
455void MainWindow::on_view_close_clicked()
456{
457 // Find the dock widget that contains the close button that was clicked
458 QObject *w = QObject::sender();
459 QDockWidget *dock = 0;
460
461 while (w) {
462 dock = qobject_cast<QDockWidget*>(w);
463 if (dock)
464 break;
465 w = w->parent();
466 }
467
468 // Get the view contained in the dock widget
f4e57597 469 shared_ptr<views::ViewBase> view;
36a8185e
SA
470
471 for (auto entry : view_docks_)
472 if (entry.first.get() == dock)
473 view = entry.second;
474
475 // Deregister the view
476 for (shared_ptr<Session> session : sessions_) {
477 if (!session->has_view(view))
478 continue;
479
480 // Also destroy the entire session if its main view is closing
481 if (view == session->main_view()) {
482 remove_session(session);
483 break;
484 } else
485 session->deregister_view(view);
486 }
487}
488
c7b03d9d
SA
489void MainWindow::on_actionViewStickyScrolling_triggered()
490{
f4e57597
SA
491 shared_ptr<views::ViewBase> viewbase = get_active_view();
492 views::TraceView::View* view =
493 qobject_cast<views::TraceView::View*>(viewbase.get());
168bd8ac
SA
494 if (view)
495 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
c7b03d9d
SA
496}
497
0fb9d645
SA
498void MainWindow::on_actionViewColouredBg_triggered()
499{
f4e57597
SA
500 shared_ptr<views::ViewBase> viewbase = get_active_view();
501 views::TraceView::View* view =
502 qobject_cast<views::TraceView::View*>(viewbase.get());
168bd8ac
SA
503 if (view)
504 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
0fb9d645
SA
505}
506
30236a54
JH
507void MainWindow::on_actionAbout_triggered()
508{
8dbbc7f0 509 dialogs::About dlg(device_manager_.context(), this);
40eb2ff4 510 dlg.exec();
30236a54 511}
274d4f13 512
51e77110 513} // namespace pv