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