]> sigrok.org Git - pulseview.git/blob - pv/mainwindow.cpp
6b3398f22d9a40c7bb8266b24a37e29972fe3bef
[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 #ifdef ENABLE_SIGROKDECODE
22 #include <libsigrokdecode/libsigrokdecode.h>
23 #endif
24
25 #include <boost/bind.hpp>
26 #include <boost/foreach.hpp>
27
28 #include <QAction>
29 #include <QApplication>
30 #include <QButtonGroup>
31 #include <QFileDialog>
32 #include <QMessageBox>
33 #include <QMenu>
34 #include <QMenuBar>
35 #include <QStatusBar>
36 #include <QVBoxLayout>
37 #include <QWidget>
38
39 #include "mainwindow.h"
40
41 #include "devicemanager.h"
42 #include "dialogs/about.h"
43 #include "dialogs/connect.h"
44 #include "toolbars/contextbar.h"
45 #include "toolbars/samplingbar.h"
46 #include "view/view.h"
47
48 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
49 #define __STDC_FORMAT_MACROS
50 #include <inttypes.h>
51 #include <stdint.h>
52 #include <stdarg.h>
53 #include <glib.h>
54 #include <libsigrok/libsigrok.h>
55
56 using namespace std;
57
58 namespace pv {
59
60 MainWindow::MainWindow(DeviceManager &device_manager,
61         const char *open_file_name,
62         QWidget *parent) :
63         QMainWindow(parent),
64         _device_manager(device_manager),
65         _session(device_manager)
66 {
67         setup_ui();
68         if (open_file_name) {
69                 const QString s(QString::fromUtf8(open_file_name));
70                 QMetaObject::invokeMethod(this, "load_file",
71                         Qt::QueuedConnection,
72                         Q_ARG(QString, s));
73         }
74 }
75
76 void MainWindow::setup_ui()
77 {
78         setObjectName(QString::fromUtf8("MainWindow"));
79
80         resize(1024, 768);
81
82         // Set the window icon
83         QIcon icon;
84         icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
85                 QSize(), QIcon::Normal, QIcon::Off);
86         setWindowIcon(icon);
87
88         // Setup the central widget
89         _central_widget = new QWidget(this);
90         _vertical_layout = new QVBoxLayout(_central_widget);
91         _vertical_layout->setSpacing(6);
92         _vertical_layout->setContentsMargins(0, 0, 0, 0);
93         setCentralWidget(_central_widget);
94
95         _view = new pv::view::View(_session, this);
96         _vertical_layout->addWidget(_view);
97
98         // Setup the menu bar
99         _menu_bar = new QMenuBar(this);
100         _menu_bar->setGeometry(QRect(0, 0, 400, 25));
101
102         // File Menu
103         _menu_file = new QMenu(_menu_bar);
104         _menu_file->setTitle(QApplication::translate(
105                 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
106
107         _action_open = new QAction(this);
108         _action_open->setText(QApplication::translate(
109                 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
110         _action_open->setIcon(QIcon::fromTheme("document-open",
111                 QIcon(":/icons/document-open.png")));
112         _action_open->setObjectName(QString::fromUtf8("actionOpen"));
113         _menu_file->addAction(_action_open);
114
115         _menu_file->addSeparator();
116
117         _action_connect = new QAction(this);
118         _action_connect->setText(QApplication::translate(
119                 "MainWindow", "&Connect to Device...", 0,
120                 QApplication::UnicodeUTF8));
121         _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
122         _menu_file->addAction(_action_connect);
123
124         _menu_file->addSeparator();
125
126         _action_quit = new QAction(this);
127         _action_quit->setText(QApplication::translate(
128                 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
129         _action_quit->setIcon(QIcon::fromTheme("application-exit",
130                 QIcon(":/icons/application-exit.png")));
131         _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
132         _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
133         _menu_file->addAction(_action_quit);
134
135         // View Menu
136         _menu_view = new QMenu(_menu_bar);
137         _menu_view->setTitle(QApplication::translate(
138                 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
139
140         _action_view_zoom_in = new QAction(this);
141         _action_view_zoom_in->setText(QApplication::translate(
142                 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
143         _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
144                 QIcon(":/icons/zoom-in.png")));
145         _action_view_zoom_in->setObjectName(
146                 QString::fromUtf8("actionViewZoomIn"));
147         _menu_view->addAction(_action_view_zoom_in);
148
149         _action_view_zoom_out = new QAction(this);
150         _action_view_zoom_out->setText(QApplication::translate(
151                 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
152         _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
153                 QIcon(":/icons/zoom-out.png")));
154         _action_view_zoom_out->setObjectName(
155                 QString::fromUtf8("actionViewZoomOut"));
156         _menu_view->addAction(_action_view_zoom_out);
157
158         _menu_view->addSeparator();
159
160         _action_view_show_cursors = new QAction(this);
161         _action_view_show_cursors->setCheckable(true);
162         _action_view_show_cursors->setChecked(_view->cursors_shown());
163         _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
164         _action_view_show_cursors->setObjectName(
165                 QString::fromUtf8("actionViewShowCursors"));
166         _action_view_show_cursors->setText(QApplication::translate(
167                 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
168         _menu_view->addAction(_action_view_show_cursors);
169
170         // Help Menu
171         _menu_help = new QMenu(_menu_bar);
172         _menu_help->setTitle(QApplication::translate(
173                 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
174
175         _action_about = new QAction(this);
176         _action_about->setObjectName(QString::fromUtf8("actionAbout"));
177         _action_about->setText(QApplication::translate(
178                 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
179         _menu_help->addAction(_action_about);
180
181         _menu_bar->addAction(_menu_file->menuAction());
182         _menu_bar->addAction(_menu_view->menuAction());
183         _menu_bar->addAction(_menu_help->menuAction());
184
185         setMenuBar(_menu_bar);
186         QMetaObject::connectSlotsByName(this);
187
188         // Setup the toolbar
189         _toolbar = new QToolBar(tr("Main Toolbar"), this);
190         _toolbar->addAction(_action_open);
191         _toolbar->addSeparator();
192         _toolbar->addAction(_action_view_zoom_in);
193         _toolbar->addAction(_action_view_zoom_out);
194         addToolBar(_toolbar);
195
196         // Setup the sampling bar
197         _sampling_bar = new toolbars::SamplingBar(this);
198
199         // Populate the device list and select the initially selected device
200         update_device_list();
201
202         connect(_sampling_bar, SIGNAL(device_selected()), this,
203                 SLOT(device_selected()));
204         connect(_sampling_bar, SIGNAL(run_stop()), this,
205                 SLOT(run_stop()));
206         addToolBar(_sampling_bar);
207
208         // Setup the context bar
209         _context_bar = new toolbars::ContextBar(this);
210         addToolBar(_context_bar);
211         insertToolBarBreak(_context_bar);
212
213         // Set the title
214         setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
215                 QApplication::UnicodeUTF8));
216
217         // Setup _session events
218         connect(&_session, SIGNAL(capture_state_changed(int)), this,
219                 SLOT(capture_state_changed(int)));
220
221 }
222
223 void MainWindow::session_error(
224         const QString text, const QString info_text)
225 {
226         QMetaObject::invokeMethod(this, "show_session_error",
227                 Qt::QueuedConnection, Q_ARG(QString, text),
228                 Q_ARG(QString, info_text));
229 }
230
231 void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
232 {
233         assert(_sampling_bar);
234
235         const list<sr_dev_inst*> &devices = _device_manager.devices();
236         _sampling_bar->set_device_list(devices);
237
238         if (!selected_device && !devices.empty()) {
239                 // Fall back to the first device in the list.
240                 selected_device = devices.front();
241
242                 // Try and find the demo device and select that by default
243                 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
244                         if (strcmp(sdi->driver->name, "demo") == 0) {
245                                 selected_device = sdi;
246                         }
247         }
248
249         if (selected_device) {
250                 _sampling_bar->set_selected_device(selected_device);
251                 _session.set_device(selected_device);
252         }
253 }
254
255 void MainWindow::load_file(QString file_name)
256 {
257         const QString errorMessage(
258                 QString("Failed to load file %1").arg(file_name));
259         const QString infoMessage;
260         _session.load_file(file_name.toStdString(),
261                 boost::bind(&MainWindow::session_error, this,
262                         errorMessage, infoMessage));
263 }
264
265 void MainWindow::show_session_error(
266         const QString text, const QString info_text)
267 {
268         QMessageBox msg(this);
269         msg.setText(text);
270         msg.setInformativeText(info_text);
271         msg.setStandardButtons(QMessageBox::Ok);
272         msg.setIcon(QMessageBox::Warning);
273         msg.exec();
274 }
275
276 void MainWindow::on_actionOpen_triggered()
277 {
278         // Enumerate the file formats
279         QString filters(tr("Sigrok Sessions (*.sr)"));
280         filters.append(tr(";;All Files (*.*)"));
281
282         // Show the dialog
283         const QString file_name = QFileDialog::getOpenFileName(
284                 this, tr("Open File"), "", filters);
285         if (!file_name.isEmpty())
286                 load_file(file_name);
287 }
288
289 void MainWindow::on_actionConnect_triggered()
290 {
291         // Stop any currently running capture session
292         _session.stop_capture();
293
294         dialogs::Connect dlg(this, _device_manager);
295
296         // If the user selected a device, select it in the device list. Select the
297         // current device otherwise.
298         struct sr_dev_inst *const sdi = dlg.exec() ?
299                 dlg.get_selected_device() : _session.get_device();
300
301         update_device_list(sdi);
302 }
303
304 void MainWindow::on_actionQuit_triggered()
305 {
306         close();
307 }
308
309 void MainWindow::on_actionViewZoomIn_triggered()
310 {
311         _view->zoom(1);
312 }
313
314 void MainWindow::on_actionViewZoomOut_triggered()
315 {
316         _view->zoom(-1);
317 }
318
319 void MainWindow::on_actionViewShowCursors_triggered()
320 {
321         assert(_view);
322
323         const bool show = !_view->cursors_shown();
324         if(show)
325                 _view->centre_cursors();
326
327         _view->show_cursors(show);
328 }
329
330 void MainWindow::on_actionAbout_triggered()
331 {
332         dialogs::About dlg(this);
333         dlg.exec();
334 }
335
336 void MainWindow::device_selected()
337 {
338         _session.set_device(_sampling_bar->get_selected_device());
339 }
340
341 void MainWindow::run_stop()
342 {
343         switch(_session.get_capture_state()) {
344         case SigSession::Stopped:
345                 _session.start_capture(_sampling_bar->get_record_length(),
346                         boost::bind(&MainWindow::session_error, this,
347                                 QString("Capture failed"), _1));
348                 break;
349
350         case SigSession::Running:
351                 _session.stop_capture();
352                 break;
353         }
354 }
355
356 void MainWindow::capture_state_changed(int state)
357 {
358         _sampling_bar->set_sampling(state != SigSession::Stopped);
359 }
360
361 } // namespace pv