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