]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
719d608989a82b92a8d42f1be66b04ef9264a1ce
[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 <QButtonGroup>
33 #include <QCloseEvent>
34 #include <QFileDialog>
35 #include <QMessageBox>
36 #include <QMenu>
37 #include <QMenuBar>
38 #include <QSettings>
39 #include <QStatusBar>
40 #include <QVBoxLayout>
41 #include <QWidget>
42
43 #include "mainwindow.h"
44
45 #include "devicemanager.h"
46 #include "device/device.h"
47 #include "dialogs/about.h"
48 #include "dialogs/connect.h"
49 #include "dialogs/storeprogress.h"
50 #include "toolbars/samplingbar.h"
51 #include "view/logicsignal.h"
52 #include "view/view.h"
53 #ifdef ENABLE_DECODE
54 #include "widgets/decodermenu.h"
55 #endif
56
57 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
58 #define __STDC_FORMAT_MACROS
59 #include <inttypes.h>
60 #include <stdint.h>
61 #include <stdarg.h>
62 #include <glib.h>
63 #include <libsigrok/libsigrok.h>
64
65 using std::list;
66 using std::shared_ptr;
67
68 namespace pv {
69
70 namespace view {
71 class SelectableItem;
72 }
73
74 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
75 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
76
77 MainWindow::MainWindow(DeviceManager &device_manager,
78         const char *open_file_name,
79         QWidget *parent) :
80         QMainWindow(parent),
81         _device_manager(device_manager),
82         _session(device_manager)
83 {
84         setup_ui();
85         restore_ui_settings();
86         if (open_file_name) {
87                 const QString s(QString::fromUtf8(open_file_name));
88                 QMetaObject::invokeMethod(this, "load_file",
89                         Qt::QueuedConnection,
90                         Q_ARG(QString, s));
91         }
92 }
93
94 void MainWindow::setup_ui()
95 {
96         setObjectName(QString::fromUtf8("MainWindow"));
97
98         // Set the window icon
99         QIcon icon;
100         icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
101                 QSize(), QIcon::Normal, QIcon::Off);
102         setWindowIcon(icon);
103
104         // Setup the central widget
105         _central_widget = new QWidget(this);
106         _vertical_layout = new QVBoxLayout(_central_widget);
107         _vertical_layout->setSpacing(6);
108         _vertical_layout->setContentsMargins(0, 0, 0, 0);
109         setCentralWidget(_central_widget);
110
111         _view = new pv::view::View(_session, this);
112
113         _vertical_layout->addWidget(_view);
114
115         // Setup the menu bar
116         QMenuBar *const menu_bar = new QMenuBar(this);
117         menu_bar->setGeometry(QRect(0, 0, 400, 25));
118
119         // File Menu
120         QMenu *const menu_file = new QMenu;
121         menu_file->setTitle(tr("&File"));
122
123         QAction *const action_open = new QAction(this);
124         action_open->setText(tr("&Open..."));
125         action_open->setIcon(QIcon::fromTheme("document-open",
126                 QIcon(":/icons/document-open.png")));
127         action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
128         action_open->setObjectName(QString::fromUtf8("actionOpen"));
129         menu_file->addAction(action_open);
130
131         QAction *const action_save_as = new QAction(this);
132         action_save_as->setText(tr("&Save As..."));
133         action_save_as->setIcon(QIcon::fromTheme("document-save-as",
134                 QIcon(":/icons/document-save-as.png")));
135         action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
136         action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
137         menu_file->addAction(action_save_as);
138
139         menu_file->addSeparator();
140
141         QAction *const action_connect = new QAction(this);
142         action_connect->setText(tr("&Connect to Device..."));
143         action_connect->setObjectName(QString::fromUtf8("actionConnect"));
144         menu_file->addAction(action_connect);
145
146         menu_file->addSeparator();
147
148         QAction *action_quit = new QAction(this);
149         action_quit->setText(tr("&Quit"));
150         action_quit->setIcon(QIcon::fromTheme("application-exit",
151                 QIcon(":/icons/application-exit.png")));
152         action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
153         action_quit->setObjectName(QString::fromUtf8("actionQuit"));
154         menu_file->addAction(action_quit);
155
156         // View Menu
157         QMenu *menu_view = new QMenu;
158         menu_view->setTitle(tr("&View"));
159
160         QAction *const action_view_zoom_in = new QAction(this);
161         action_view_zoom_in->setText(tr("Zoom &In"));
162         action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
163                 QIcon(":/icons/zoom-in.png")));
164         // simply using Qt::Key_Plus shows no + in the menu
165         action_view_zoom_in->setShortcut(QKeySequence::ZoomIn);
166         action_view_zoom_in->setObjectName(
167                 QString::fromUtf8("actionViewZoomIn"));
168         menu_view->addAction(action_view_zoom_in);
169
170         QAction *const action_view_zoom_out = new QAction(this);
171         action_view_zoom_out->setText(tr("Zoom &Out"));
172         action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
173                 QIcon(":/icons/zoom-out.png")));
174         action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
175         action_view_zoom_out->setObjectName(
176                 QString::fromUtf8("actionViewZoomOut"));
177         menu_view->addAction(action_view_zoom_out);
178
179         QAction *const action_view_zoom_fit = new QAction(this);
180         action_view_zoom_fit->setText(tr("Zoom to &Fit"));
181         action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
182                 QIcon(":/icons/zoom-fit.png")));
183         action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
184         action_view_zoom_fit->setObjectName(
185                 QString::fromUtf8("actionViewZoomFit"));
186         menu_view->addAction(action_view_zoom_fit);
187
188         QAction *const action_view_zoom_one_to_one = new QAction(this);
189         action_view_zoom_one_to_one->setText(tr("Zoom to &One-to-One"));
190         action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
191                 QIcon(":/icons/zoom-original.png")));
192         action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
193         action_view_zoom_one_to_one->setObjectName(
194                 QString::fromUtf8("actionViewZoomOneToOne"));
195         menu_view->addAction(action_view_zoom_one_to_one);
196
197         menu_view->addSeparator();
198
199         QAction *action_view_show_cursors = new QAction(this);
200         action_view_show_cursors->setCheckable(true);
201         action_view_show_cursors->setChecked(_view->cursors_shown());
202         action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
203         action_view_show_cursors->setObjectName(
204                 QString::fromUtf8("actionViewShowCursors"));
205         action_view_show_cursors->setText(tr("Show &Cursors"));
206         menu_view->addAction(action_view_show_cursors);
207
208         // Decoders Menu
209 #ifdef ENABLE_DECODE
210         QMenu *const menu_decoders = new QMenu;
211         menu_decoders->setTitle(tr("&Decoders"));
212
213         pv::widgets::DecoderMenu *const menu_decoders_add =
214                 new pv::widgets::DecoderMenu(menu_decoders, true);
215         menu_decoders_add->setTitle(tr("&Add"));
216         connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
217                 this, SLOT(add_decoder(srd_decoder*)));
218
219         menu_decoders->addMenu(menu_decoders_add);
220 #endif
221
222         // Help Menu
223         QMenu *const menu_help = new QMenu;
224         menu_help->setTitle(tr("&Help"));
225
226         QAction *const action_about = new QAction(this);
227         action_about->setObjectName(QString::fromUtf8("actionAbout"));
228         action_about->setText(tr("&About..."));
229         menu_help->addAction(action_about);
230
231         menu_bar->addAction(menu_file->menuAction());
232         menu_bar->addAction(menu_view->menuAction());
233 #ifdef ENABLE_DECODE
234         menu_bar->addAction(menu_decoders->menuAction());
235 #endif
236         menu_bar->addAction(menu_help->menuAction());
237
238         setMenuBar(menu_bar);
239         QMetaObject::connectSlotsByName(this);
240
241         // Setup the toolbar
242         QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
243         toolbar->addAction(action_open);
244         toolbar->addAction(action_save_as);
245         toolbar->addSeparator();
246         toolbar->addAction(action_view_zoom_in);
247         toolbar->addAction(action_view_zoom_out);
248         toolbar->addAction(action_view_zoom_fit);
249         toolbar->addAction(action_view_zoom_one_to_one);
250         addToolBar(toolbar);
251
252         // Setup the sampling bar
253         _sampling_bar = new toolbars::SamplingBar(_session, this);
254
255         // Populate the device list and select the initially selected device
256         update_device_list();
257
258         connect(_sampling_bar, SIGNAL(run_stop()), this,
259                 SLOT(run_stop()));
260         addToolBar(_sampling_bar);
261
262         // Set the title
263         setWindowTitle(tr("PulseView"));
264
265         // Setup _session events
266         connect(&_session, SIGNAL(capture_state_changed(int)), this,
267                 SLOT(capture_state_changed(int)));
268
269 }
270
271 void MainWindow::save_ui_settings()
272 {
273         QSettings settings("sigrok", "PulseView");
274
275         settings.beginGroup("MainWindow");
276         settings.setValue("state", saveState());
277         settings.setValue("geometry", saveGeometry());
278         settings.endGroup();
279 }
280
281 void MainWindow::restore_ui_settings()
282 {
283         QSettings settings("sigrok", "PulseView");
284
285         settings.beginGroup("MainWindow");
286
287         if (settings.contains("geometry")) {
288                 restoreGeometry(settings.value("geometry").toByteArray());
289                 restoreState(settings.value("state").toByteArray());
290         } else
291                 resize(1000, 720);
292
293         settings.endGroup();
294 }
295
296 void MainWindow::session_error(
297         const QString text, const QString info_text)
298 {
299         QMetaObject::invokeMethod(this, "show_session_error",
300                 Qt::QueuedConnection, Q_ARG(QString, text),
301                 Q_ARG(QString, info_text));
302 }
303
304 void MainWindow::update_device_list()
305 {
306         assert(_sampling_bar);
307
308         shared_ptr<pv::device::DevInst> selected_device = _session.get_device();
309         list< shared_ptr<device::DevInst> > devices;
310
311         if (_device_manager.devices().size() == 0)
312                 return;
313
314         std::copy(_device_manager.devices().begin(),
315                 _device_manager.devices().end(), std::back_inserter(devices));
316
317         if (std::find(devices.begin(), devices.end(), selected_device) ==
318                 devices.end())
319                 devices.push_back(selected_device);
320         assert(selected_device);
321
322         _sampling_bar->set_device_list(devices, selected_device);
323 }
324
325 void MainWindow::closeEvent(QCloseEvent *event)
326 {
327         save_ui_settings();
328         event->accept();
329 }
330
331 void MainWindow::load_file(QString file_name)
332 {
333         const QString errorMessage(
334                 QString("Failed to load file %1").arg(file_name));
335         const QString infoMessage;
336
337         try {
338                 _session.set_file(file_name.toStdString());
339         } catch(QString e) {
340                 show_session_error(tr("Failed to load ") + file_name, e);
341                 _session.set_default_device();
342                 update_device_list();
343                 return;
344         }
345
346         update_device_list();
347
348         _session.start_capture([&, errorMessage, infoMessage](QString) {
349                 session_error(errorMessage, infoMessage); });
350 }
351
352 void MainWindow::show_session_error(
353         const QString text, const QString info_text)
354 {
355         QMessageBox msg(this);
356         msg.setText(text);
357         msg.setInformativeText(info_text);
358         msg.setStandardButtons(QMessageBox::Ok);
359         msg.setIcon(QMessageBox::Warning);
360         msg.exec();
361 }
362
363 void MainWindow::on_actionOpen_triggered()
364 {
365         QSettings settings;
366         const QString dir = settings.value(SettingOpenDirectory).toString();
367
368         // Show the dialog
369         const QString file_name = QFileDialog::getOpenFileName(
370                 this, tr("Open File"), dir, tr(
371                         "Sigrok Sessions (*.sr);;"
372                         "All Files (*.*)"));
373
374         if (!file_name.isEmpty()) {
375                 load_file(file_name);
376
377                 const QString abs_path = QFileInfo(file_name).absolutePath();
378                 settings.setValue(SettingOpenDirectory, abs_path);
379         }
380 }
381
382 void MainWindow::on_actionSaveAs_triggered()
383 {
384         using pv::dialogs::StoreProgress;
385
386         // Stop any currently running capture session
387         _session.stop_capture();
388
389         QSettings settings;
390         const QString dir = settings.value(SettingSaveDirectory).toString();
391
392         // Show the dialog
393         const QString file_name = QFileDialog::getSaveFileName(
394                 this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)"));
395
396         if (file_name.isEmpty())
397                 return;
398
399         const QString abs_path = QFileInfo(file_name).absolutePath();
400         settings.setValue(SettingSaveDirectory, abs_path);
401
402         StoreProgress *dlg = new StoreProgress(file_name, _session, this);
403         dlg->run();
404 }
405
406 void MainWindow::on_actionConnect_triggered()
407 {
408         // Stop any currently running capture session
409         _session.stop_capture();
410
411         dialogs::Connect dlg(this, _device_manager);
412
413         // If the user selected a device, select it in the device list. Select the
414         // current device otherwise.
415         if (dlg.exec())
416                 _session.set_device(dlg.get_selected_device());
417
418         update_device_list();
419 }
420
421 void MainWindow::on_actionQuit_triggered()
422 {
423         close();
424 }
425
426 void MainWindow::on_actionViewZoomIn_triggered()
427 {
428         _view->zoom(1);
429 }
430
431 void MainWindow::on_actionViewZoomOut_triggered()
432 {
433         _view->zoom(-1);
434 }
435
436 void MainWindow::on_actionViewZoomFit_triggered()
437 {
438         _view->zoom_fit();
439 }
440
441 void MainWindow::on_actionViewZoomOneToOne_triggered()
442 {
443         _view->zoom_one_to_one();
444 }
445
446 void MainWindow::on_actionViewShowCursors_triggered()
447 {
448         assert(_view);
449
450         const bool show = !_view->cursors_shown();
451         if(show)
452                 _view->centre_cursors();
453
454         _view->show_cursors(show);
455 }
456
457 void MainWindow::on_actionAbout_triggered()
458 {
459         dialogs::About dlg(this);
460         dlg.exec();
461 }
462
463 void MainWindow::add_decoder(srd_decoder *decoder)
464 {
465 #ifdef ENABLE_DECODE
466         assert(decoder);
467         _session.add_decoder(decoder);
468 #else
469         (void)decoder;
470 #endif
471 }
472
473 void MainWindow::run_stop()
474 {
475         switch(_session.get_capture_state()) {
476         case SigSession::Stopped:
477                 _session.start_capture([&](QString message) {
478                         session_error("Capture failed", message); });
479                 break;
480
481         case SigSession::AwaitingTrigger:
482         case SigSession::Running:
483                 _session.stop_capture();
484                 break;
485         }
486 }
487
488 void MainWindow::capture_state_changed(int state)
489 {
490         _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
491 }
492
493 } // namespace pv