]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
MainWindow: Use a QTabWidget to display the QDockWidgets
[pulseview.git] / pv / mainwindow.cpp
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <cassert>
22
23 #ifdef ENABLE_DECODE
24 #include <libsigrokdecode/libsigrokdecode.h>
25 #endif
26
27 #include <algorithm>
28 #include <iterator>
29
30 #include <QAction>
31 #include <QApplication>
32 #include <QCloseEvent>
33 #include <QDockWidget>
34 #include <QSettings>
35 #include <QWidget>
36
37 #include "mainwindow.hpp"
38
39 #include "devicemanager.hpp"
40 #include "util.hpp"
41 #include "devices/hardwaredevice.hpp"
42 #include "dialogs/about.hpp"
43 #include "toolbars/mainbar.hpp"
44 #include "view/view.hpp"
45
46 #include <stdint.h>
47 #include <stdarg.h>
48 #include <libsigrokcxx/libsigrokcxx.hpp>
49
50 using std::dynamic_pointer_cast;
51 using std::list;
52 using std::make_shared;
53 using std::map;
54 using std::shared_ptr;
55 using std::string;
56
57 namespace pv {
58
59 namespace view {
60 class ViewItem;
61 }
62
63 using toolbars::MainBar;
64
65 const QString MainWindow::WindowTitle = tr("PulseView");
66
67 MainWindow::MainWindow(DeviceManager &device_manager,
68         string open_file_name, string open_file_format,
69         QWidget *parent) :
70         QMainWindow(parent),
71         device_manager_(device_manager),
72         session_selector_(this),
73         action_view_sticky_scrolling_(new QAction(this)),
74         action_view_coloured_bg_(new QAction(this)),
75         action_about_(new QAction(this))
76 {
77         qRegisterMetaType<util::Timestamp>("util::Timestamp");
78
79         setup_ui();
80         restore_ui_settings();
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         }
107 }
108
109 MainWindow::~MainWindow()
110 {
111         while (!sessions_.empty())
112                 remove_session(sessions_.front());
113 }
114
115 QAction* MainWindow::action_view_sticky_scrolling() const
116 {
117         return action_view_sticky_scrolling_;
118 }
119
120 QAction* MainWindow::action_view_coloured_bg() const
121 {
122         return action_view_coloured_bg_;
123 }
124
125 QAction* MainWindow::action_about() const
126 {
127         return action_about_;
128 }
129
130 shared_ptr<views::ViewBase> MainWindow::get_active_view() const
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
152         return nullptr;
153 }
154
155 shared_ptr<views::ViewBase> MainWindow::add_view(const QString &title,
156         views::ViewType type, Session &session)
157 {
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
165         if (type == views::ViewTypeTrace) {
166                 shared_ptr<QDockWidget> dock = make_shared<QDockWidget>(title, main_window);
167                 dock->setObjectName(title);
168                 main_window->addDockWidget(Qt::TopDockWidgetArea, dock.get());
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
174                 shared_ptr<views::TraceView::View> v =
175                         make_shared<views::TraceView::View>(session, dock_main);
176                 view_docks_[dock] = v;
177                 session.register_view(v);
178
179                 dock_main->setCentralWidget(v.get());
180                 dock->setWidget(dock_main);
181
182                 dock->setFeatures(QDockWidget::DockWidgetMovable |
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()));
191
192                 if (type == views::ViewTypeTrace) {
193                         connect(&session, SIGNAL(trigger_event(util::Timestamp)),
194                                 qobject_cast<views::ViewBase*>(v.get()),
195                                 SLOT(trigger_event(util::Timestamp)));
196
197                         v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
198                         v->enable_coloured_bg(action_view_coloured_bg_->isChecked());
199
200                         shared_ptr<MainBar> main_bar = session.main_bar();
201                         if (!main_bar) {
202                                 main_bar = make_shared<MainBar>(session, *this);
203                                 dock_main->addToolBar(main_bar.get());
204                                 session.set_main_bar(main_bar);
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*)));
210                         }
211                         main_bar->action_view_show_cursors()->setChecked(v->cursors_shown());
212
213                         connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)),
214                                 main_bar.get(), SLOT(on_always_zoom_to_fit_changed(bool)));
215                 }
216
217                 return v;
218         }
219
220         return nullptr;
221 }
222
223 shared_ptr<Session> MainWindow::add_session()
224 {
225         static int last_session_id = 1;
226         QString name = tr("Untitled-%1").arg(last_session_id++);
227
228         shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
229
230         connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
231                 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
232         connect(session.get(), SIGNAL(name_changed()),
233                 this, SLOT(on_session_name_changed()));
234
235         sessions_.push_back(session);
236
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
242         shared_ptr<views::ViewBase> main_view =
243                 add_view(name, views::ViewTypeTrace, *session);
244
245         return session;
246 }
247
248 void MainWindow::remove_session(shared_ptr<Session> session)
249 {
250         for (shared_ptr<views::ViewBase> view : session->views()) {
251                 // Find the dock the view is contained in and remove it
252                 for (auto entry : view_docks_)
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                         }
266         }
267
268         QMainWindow *window = session_windows_.at(session);
269         session_selector_.removeTab(session_selector_.indexOf(window));
270
271         session_windows_.erase(session);
272
273         sessions_.remove_if([&](shared_ptr<Session> s) {
274                 return s == session; });
275
276         // Update the window title if there is no view left to
277         // generate focus change events
278         if (sessions_.empty())
279                 setWindowTitle(WindowTitle);
280 }
281
282 void MainWindow::setup_ui()
283 {
284         setObjectName(QString::fromUtf8("MainWindow"));
285
286         setCentralWidget(&session_selector_);
287
288         // Set the window icon
289         QIcon icon;
290         icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
291         setWindowIcon(icon);
292
293         action_view_sticky_scrolling_->setCheckable(true);
294         action_view_sticky_scrolling_->setChecked(true);
295         action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
296         action_view_sticky_scrolling_->setObjectName(
297                 QString::fromUtf8("actionViewStickyScrolling"));
298         action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
299
300         action_view_coloured_bg_->setCheckable(true);
301         action_view_coloured_bg_->setChecked(true);
302         action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
303         action_view_coloured_bg_->setObjectName(
304                 QString::fromUtf8("actionViewColouredBg"));
305         action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
306
307         action_about_->setObjectName(QString::fromUtf8("actionAbout"));
308         action_about_->setText(tr("&About..."));
309
310         setDockNestingEnabled(true);
311
312         connect(static_cast<QApplication *>(QCoreApplication::instance()),
313                 SIGNAL(focusChanged(QWidget*, QWidget*)),
314                 this, SLOT(on_focus_changed()));
315 }
316
317 void MainWindow::save_ui_settings()
318 {
319         QSettings settings;
320         int id = 0;
321
322         settings.beginGroup("MainWindow");
323         settings.setValue("state", saveState());
324         settings.setValue("geometry", saveGeometry());
325         settings.endGroup();
326
327         for (shared_ptr<Session> session : sessions_) {
328                 // Ignore sessions using the demo device or no device at all
329                 if (session->device()) {
330                         shared_ptr<devices::HardwareDevice> device =
331                                 dynamic_pointer_cast< devices::HardwareDevice >
332                                 (session->device());
333
334                         if (device &&
335                                 device->hardware_device()->driver()->name() == "demo")
336                                 continue;
337
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                 }
343         }
344
345         settings.setValue("sessions", id);
346 }
347
348 void MainWindow::restore_ui_settings()
349 {
350         QSettings settings;
351         int i, session_count;
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();
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         }
371 }
372
373 void MainWindow::closeEvent(QCloseEvent *event)
374 {
375         save_ui_settings();
376         event->accept();
377 }
378
379 QMenu* MainWindow::createPopupMenu()
380 {
381         return nullptr;
382 }
383
384 bool 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
395 void MainWindow::on_add_view(const QString &title, views::ViewType type,
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
404 void MainWindow::on_focus_changed()
405 {
406         shared_ptr<views::ViewBase> view;
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
423 void MainWindow::on_new_session()
424 {
425         add_session();
426 }
427
428 void 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
434         for (shared_ptr<views::ViewBase> view : session->views()) {
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
447 void 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)
452                         add_view(session->name(), views::ViewTypeTrace, *s);
453 }
454
455 void 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
469         shared_ptr<views::ViewBase> view;
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
489 void MainWindow::on_actionViewStickyScrolling_triggered()
490 {
491         shared_ptr<views::ViewBase> viewbase = get_active_view();
492         views::TraceView::View* view =
493                 qobject_cast<views::TraceView::View*>(viewbase.get());
494         if (view)
495                 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
496 }
497
498 void MainWindow::on_actionViewColouredBg_triggered()
499 {
500         shared_ptr<views::ViewBase> viewbase = get_active_view();
501         views::TraceView::View* view =
502                         qobject_cast<views::TraceView::View*>(viewbase.get());
503         if (view)
504                 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
505 }
506
507 void MainWindow::on_actionAbout_triggered()
508 {
509         dialogs::About dlg(device_manager_.context(), this);
510         dlg.exec();
511 }
512
513 } // namespace pv