MainWindow: Allow tabs to be closed
[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 == 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                 QDockWidget* dock = new QDockWidget(title, main_window);
167                 dock->setObjectName(title);
168                 main_window->addDockWidget(Qt::TopDockWidgetArea, dock);
169
170                 // Insert a QMainWindow into the dock widget to allow for a tool bar
171                 QMainWindow *dock_main = new QMainWindow(dock);
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         session_selector_.setTabsClosable(true);
311
312         connect(&session_selector_, SIGNAL(tabCloseRequested(int)),
313                 this, SLOT(on_tab_close_requested(int)));
314
315         setDockNestingEnabled(true);
316
317         connect(static_cast<QApplication *>(QCoreApplication::instance()),
318                 SIGNAL(focusChanged(QWidget*, QWidget*)),
319                 this, SLOT(on_focus_changed()));
320 }
321
322 void MainWindow::save_ui_settings()
323 {
324         QSettings settings;
325         int id = 0;
326
327         settings.beginGroup("MainWindow");
328         settings.setValue("state", saveState());
329         settings.setValue("geometry", saveGeometry());
330         settings.endGroup();
331
332         for (shared_ptr<Session> session : sessions_) {
333                 // Ignore sessions using the demo device or no device at all
334                 if (session->device()) {
335                         shared_ptr<devices::HardwareDevice> device =
336                                 dynamic_pointer_cast< devices::HardwareDevice >
337                                 (session->device());
338
339                         if (device &&
340                                 device->hardware_device()->driver()->name() == "demo")
341                                 continue;
342
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                 }
348         }
349
350         settings.setValue("sessions", id);
351 }
352
353 void MainWindow::restore_ui_settings()
354 {
355         QSettings settings;
356         int i, session_count;
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();
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         }
376 }
377
378 void MainWindow::closeEvent(QCloseEvent *event)
379 {
380         save_ui_settings();
381         event->accept();
382 }
383
384 QMenu* MainWindow::createPopupMenu()
385 {
386         return nullptr;
387 }
388
389 bool 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
400 void MainWindow::on_add_view(const QString &title, views::ViewType type,
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
409 void MainWindow::on_focus_changed()
410 {
411         shared_ptr<views::ViewBase> view;
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
428 void MainWindow::on_new_session()
429 {
430         add_session();
431 }
432
433 void 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
439         for (shared_ptr<views::ViewBase> view : session->views()) {
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
452 void 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)
457                         add_view(session->name(), views::ViewTypeTrace, *s);
458 }
459
460 void 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
474         shared_ptr<views::ViewBase> view;
475
476         for (auto entry : view_docks_)
477                 if (entry.first == dock)
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
494 void 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
506 void MainWindow::on_actionViewStickyScrolling_triggered()
507 {
508         shared_ptr<views::ViewBase> viewbase = get_active_view();
509         views::TraceView::View* view =
510                 qobject_cast<views::TraceView::View*>(viewbase.get());
511         if (view)
512                 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
513 }
514
515 void MainWindow::on_actionViewColouredBg_triggered()
516 {
517         shared_ptr<views::ViewBase> viewbase = get_active_view();
518         views::TraceView::View* view =
519                         qobject_cast<views::TraceView::View*>(viewbase.get());
520         if (view)
521                 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
522 }
523
524 void MainWindow::on_actionAbout_triggered()
525 {
526         dialogs::About dlg(device_manager_.context(), this);
527         dlg.exec();
528 }
529
530 } // namespace pv