]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
f64345cc532f9d33af4684ad1d7da43046aee7ce
[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<views::ViewBase> 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<views::ViewBase> 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<views::ViewBase> MainWindow::add_view(const QString &title,
170         views::ViewType type, Session &session)
171 {
172         if (type == views::ViewTypeTrace) {
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                 shared_ptr<views::TraceView::View> v =
182                         make_shared<views::TraceView::View>(session, dock_main);
183                 view_docks_[dock] = v;
184                 session.register_view(v);
185
186                 dock_main->setCentralWidget(v.get());
187                 dock->setWidget(dock_main);
188
189                 dock->setFeatures(QDockWidget::DockWidgetMovable |
190                         QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
191
192                 QAbstractButton *close_btn =
193                         dock->findChildren<QAbstractButton*>
194                                 ("qt_dockwidget_closebutton").front();
195
196                 connect(close_btn, SIGNAL(clicked(bool)),
197                         this, SLOT(on_view_close_clicked()));
198
199                 if (type == views::ViewTypeTrace) {
200                         connect(&session, SIGNAL(trigger_event(util::Timestamp)),
201                                 qobject_cast<views::ViewBase*>(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                 return v;
225         }
226
227         return nullptr;
228 }
229
230 shared_ptr<Session> MainWindow::add_session()
231 {
232         static int last_session_id = 1;
233         QString name = tr("Untitled-%1").arg(last_session_id++);
234
235         shared_ptr<Session> session = make_shared<Session>(device_manager_, name);
236
237         connect(session.get(), SIGNAL(add_view(const QString&, views::ViewType, Session*)),
238                 this, SLOT(on_add_view(const QString&, views::ViewType, Session*)));
239         connect(session.get(), SIGNAL(name_changed()),
240                 this, SLOT(on_session_name_changed()));
241
242         sessions_.push_back(session);
243
244         shared_ptr<views::ViewBase> main_view =
245                 add_view(name, views::ViewTypeTrace, *session);
246
247         return session;
248 }
249
250 void MainWindow::remove_session(shared_ptr<Session> session)
251 {
252         for (shared_ptr<views::ViewBase> view : session->views()) {
253                 // Find the dock the view is contained in and close it
254                 for (auto entry : view_docks_)
255                         if (entry.second == view)
256                                 entry.first->close();
257         }
258
259         sessions_.remove_if([&](shared_ptr<Session> s) {
260                 return s == session; });
261
262         // Update the window title if there is no view left to
263         // generate focus change events
264         if (sessions_.empty())
265                 setWindowTitle(WindowTitle);
266 }
267
268 void MainWindow::setup_ui()
269 {
270         setObjectName(QString::fromUtf8("MainWindow"));
271
272         // Set the window icon
273         QIcon icon;
274         icon.addFile(QString(":/icons/sigrok-logo-notext.png"));
275         setWindowIcon(icon);
276
277         action_view_sticky_scrolling_->setCheckable(true);
278         action_view_sticky_scrolling_->setChecked(true);
279         action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S));
280         action_view_sticky_scrolling_->setObjectName(
281                 QString::fromUtf8("actionViewStickyScrolling"));
282         action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling"));
283
284         action_view_coloured_bg_->setCheckable(true);
285         action_view_coloured_bg_->setChecked(true);
286         action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B));
287         action_view_coloured_bg_->setObjectName(
288                 QString::fromUtf8("actionViewColouredBg"));
289         action_view_coloured_bg_->setText(tr("Use &coloured backgrounds"));
290
291         action_about_->setObjectName(QString::fromUtf8("actionAbout"));
292         action_about_->setText(tr("&About..."));
293
294         setDockNestingEnabled(true);
295
296         connect(static_cast<QApplication *>(QCoreApplication::instance()),
297                 SIGNAL(focusChanged(QWidget*, QWidget*)),
298                 this, SLOT(on_focus_changed()));
299 }
300
301 void MainWindow::save_ui_settings()
302 {
303         QSettings settings;
304         int id = 0;
305
306         settings.beginGroup("MainWindow");
307         settings.setValue("state", saveState());
308         settings.setValue("geometry", saveGeometry());
309         settings.endGroup();
310
311         for (shared_ptr<Session> session : sessions_) {
312                 // Ignore sessions using the demo device or no device at all
313                 if (session->device()) {
314                         shared_ptr<devices::HardwareDevice> device =
315                                 dynamic_pointer_cast< devices::HardwareDevice >
316                                 (session->device());
317
318                         if (device &&
319                                 device->hardware_device()->driver()->name() == "demo")
320                                 continue;
321
322                         settings.beginGroup("Session" + QString::number(id++));
323                         settings.remove("");  // Remove all keys in this group
324                         session->save_settings(settings);
325                         settings.endGroup();
326                 }
327         }
328
329         settings.setValue("sessions", id);
330 }
331
332 void MainWindow::restore_ui_settings()
333 {
334         QSettings settings;
335         int i, session_count;
336
337         settings.beginGroup("MainWindow");
338
339         if (settings.contains("geometry")) {
340                 restoreGeometry(settings.value("geometry").toByteArray());
341                 restoreState(settings.value("state").toByteArray());
342         } else
343                 resize(1000, 720);
344
345         settings.endGroup();
346
347         session_count = settings.value("sessions", 0).toInt();
348
349         for (i = 0; i < session_count; i++) {
350                 settings.beginGroup("Session" + QString::number(i));
351                 shared_ptr<Session> session = add_session();
352                 session->restore_settings(settings);
353                 settings.endGroup();
354         }
355 }
356
357 void MainWindow::closeEvent(QCloseEvent *event)
358 {
359         save_ui_settings();
360         event->accept();
361 }
362
363 QMenu* MainWindow::createPopupMenu()
364 {
365         return nullptr;
366 }
367
368 bool MainWindow::restoreState(const QByteArray &state, int version)
369 {
370         (void)state;
371         (void)version;
372
373         // Do nothing. We don't want Qt to handle this, or else it
374         // will try to restore all the dock widgets and create havoc.
375
376         return false;
377 }
378
379 void MainWindow::on_add_view(const QString &title, views::ViewType type,
380         Session *session)
381 {
382         // We get a pointer and need a reference
383         for (std::shared_ptr<Session> s : sessions_)
384                 if (s.get() == session)
385                         add_view(title, type, *s);
386 }
387
388 void MainWindow::on_focus_changed()
389 {
390         shared_ptr<views::ViewBase> view;
391         bool title_set = false;
392
393         view = get_active_view();
394
395         for (shared_ptr<Session> session : sessions_) {
396                 if (!session->has_view(view))
397                         continue;
398
399                 setWindowTitle(session->name() + " - " + WindowTitle);
400                 title_set = true;
401         }
402
403         if (!title_set)
404                 setWindowTitle(WindowTitle);
405 }
406
407 void MainWindow::on_new_session()
408 {
409         add_session();
410 }
411
412 void MainWindow::on_session_name_changed()
413 {
414         // Update the corresponding dock widget's name(s)
415         Session *session = qobject_cast<Session*>(QObject::sender());
416         assert(session);
417
418         for (shared_ptr<views::ViewBase> view : session->views()) {
419                 // Get the dock that contains the view
420                 for (auto entry : view_docks_)
421                         if (entry.second == view) {
422                                 entry.first->setObjectName(session->name());
423                                 entry.first->setWindowTitle(session->name());
424                         }
425         }
426
427         // Refresh window title if the affected session has focus
428         on_focus_changed();
429 }
430
431 void MainWindow::on_new_view(Session *session)
432 {
433         // We get a pointer and need a reference
434         for (std::shared_ptr<Session> s : sessions_)
435                 if (s.get() == session)
436                         add_view(session->name(), views::ViewTypeTrace, *s);
437 }
438
439 void MainWindow::on_view_close_clicked()
440 {
441         // Find the dock widget that contains the close button that was clicked
442         QObject *w = QObject::sender();
443         QDockWidget *dock = 0;
444
445         while (w) {
446             dock = qobject_cast<QDockWidget*>(w);
447             if (dock)
448                 break;
449             w = w->parent();
450         }
451
452         // Get the view contained in the dock widget
453         shared_ptr<views::ViewBase> view;
454
455         for (auto entry : view_docks_)
456                 if (entry.first.get() == dock)
457                         view = entry.second;
458
459         // Deregister the view
460         for (shared_ptr<Session> session : sessions_) {
461                 if (!session->has_view(view))
462                         continue;
463
464                 // Also destroy the entire session if its main view is closing
465                 if (view == session->main_view()) {
466                         remove_session(session);
467                         break;
468                 } else
469                         session->deregister_view(view);
470         }
471 }
472
473 void MainWindow::on_actionViewStickyScrolling_triggered()
474 {
475         shared_ptr<views::ViewBase> viewbase = get_active_view();
476         views::TraceView::View* view =
477                 qobject_cast<views::TraceView::View*>(viewbase.get());
478         if (view)
479                 view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked());
480 }
481
482 void MainWindow::on_actionViewColouredBg_triggered()
483 {
484         shared_ptr<views::ViewBase> viewbase = get_active_view();
485         views::TraceView::View* view =
486                         qobject_cast<views::TraceView::View*>(viewbase.get());
487         if (view)
488                 view->enable_coloured_bg(action_view_coloured_bg_->isChecked());
489 }
490
491 void MainWindow::on_actionAbout_triggered()
492 {
493         dialogs::About dlg(device_manager_.context(), this);
494         dlg.exec();
495 }
496
497 } // namespace pv