]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Implemented a global decode lock to prevent concurrent decode
[pulseview.git] / pv / mainwindow.cpp
CommitLineData
d7bed479 1/*
b3f22de0 2 * This file is part of the PulseView project.
d7bed479
JH
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
aaabd61b 21#include <libsigrokdecode/libsigrokdecode.h>
30236a54 22
f2edb557 23#include <boost/bind.hpp>
107ca6d3 24#include <boost/foreach.hpp>
f2edb557 25
7d5425ef
JH
26#include <QAction>
27#include <QApplication>
28#include <QButtonGroup>
2953961c 29#include <QFileDialog>
f2edb557 30#include <QMessageBox>
7d5425ef
JH
31#include <QMenu>
32#include <QMenuBar>
33#include <QStatusBar>
34#include <QVBoxLayout>
35#include <QWidget>
2953961c 36
d7bed479 37#include "mainwindow.h"
107ca6d3
JH
38
39#include "devicemanager.h"
acda14b8 40#include "dialogs/about.h"
9663c82b 41#include "dialogs/connect.h"
c8c28626 42#include "dialogs/decoder.h"
f4c92e1c 43#include "toolbars/samplingbar.h"
3045c869 44#include "view/logicsignal.h"
4104d7f3 45#include "view/view.h"
d7bed479 46
30236a54
JH
47/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
48#define __STDC_FORMAT_MACROS
49#include <inttypes.h>
50#include <stdint.h>
51#include <stdarg.h>
52#include <glib.h>
53#include <libsigrok/libsigrok.h>
e82fd481 54
b1264f56 55using namespace boost;
107ca6d3 56using namespace std;
30236a54 57
51e77110
JH
58namespace pv {
59
b1264f56
JH
60namespace view {
61class SelectableItem;
62}
63
107ca6d3
JH
64MainWindow::MainWindow(DeviceManager &device_manager,
65 const char *open_file_name,
1d478458 66 QWidget *parent) :
107ca6d3 67 QMainWindow(parent),
dc0867ff 68 _device_manager(device_manager),
7dd093df
JH
69 _session(device_manager),
70 _decoders_add_mapper(this)
d7bed479 71{
7d5425ef 72 setup_ui();
1d478458
JH
73 if (open_file_name) {
74 const QString s(QString::fromUtf8(open_file_name));
75 QMetaObject::invokeMethod(this, "load_file",
76 Qt::QueuedConnection,
77 Q_ARG(QString, s));
78 }
7d5425ef
JH
79}
80
81void MainWindow::setup_ui()
82{
83 setObjectName(QString::fromUtf8("MainWindow"));
84
85 resize(1024, 768);
86
87 // Set the window icon
88 QIcon icon;
89 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
90 QSize(), QIcon::Normal, QIcon::Off);
91 setWindowIcon(icon);
92
e072efc8
JH
93 // Setup the central widget
94 _central_widget = new QWidget(this);
95 _vertical_layout = new QVBoxLayout(_central_widget);
96 _vertical_layout->setSpacing(6);
97 _vertical_layout->setContentsMargins(0, 0, 0, 0);
98 setCentralWidget(_central_widget);
99
100 _view = new pv::view::View(_session, this);
b1264f56 101
e072efc8
JH
102 _vertical_layout->addWidget(_view);
103
ab1d13ee
JH
104 // Setup the menu bar
105 _menu_bar = new QMenuBar(this);
106 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 107
ab1d13ee
JH
108 // File Menu
109 _menu_file = new QMenu(_menu_bar);
110 _menu_file->setTitle(QApplication::translate(
111 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 112
7d5425ef 113 _action_open = new QAction(this);
ab1d13ee
JH
114 _action_open->setText(QApplication::translate(
115 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
362eea96
JH
116 _action_open->setIcon(QIcon::fromTheme("document-open",
117 QIcon(":/icons/document-open.png")));
7d5425ef 118 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
7d5425ef 119 _menu_file->addAction(_action_open);
009e1503 120
2a032dcb
JH
121 _menu_file->addSeparator();
122
9663c82b
JH
123 _action_connect = new QAction(this);
124 _action_connect->setText(QApplication::translate(
125 "MainWindow", "&Connect to Device...", 0,
126 QApplication::UnicodeUTF8));
127 _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
128 _menu_file->addAction(_action_connect);
129
130 _menu_file->addSeparator();
131
2a032dcb
JH
132 _action_quit = new QAction(this);
133 _action_quit->setText(QApplication::translate(
134 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
135 _action_quit->setIcon(QIcon::fromTheme("application-exit",
136 QIcon(":/icons/application-exit.png")));
d1bb7d7a 137 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
2a032dcb
JH
138 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
139 _menu_file->addAction(_action_quit);
140
ab1d13ee 141 // View Menu
a429590b 142 _menu_view = new QMenu(_menu_bar);
ab1d13ee
JH
143 _menu_view->setTitle(QApplication::translate(
144 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
145
146 _action_view_zoom_in = new QAction(this);
147 _action_view_zoom_in->setText(QApplication::translate(
148 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
149 _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
150 QIcon(":/icons/zoom-in.png")));
151 _action_view_zoom_in->setObjectName(
152 QString::fromUtf8("actionViewZoomIn"));
a429590b 153 _menu_view->addAction(_action_view_zoom_in);
ab1d13ee
JH
154
155 _action_view_zoom_out = new QAction(this);
156 _action_view_zoom_out->setText(QApplication::translate(
157 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
158 _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
159 QIcon(":/icons/zoom-out.png")));
160 _action_view_zoom_out->setObjectName(
161 QString::fromUtf8("actionViewZoomOut"));
a429590b 162 _menu_view->addAction(_action_view_zoom_out);
ab1d13ee 163
e072efc8 164 _menu_view->addSeparator();
ab1d13ee
JH
165
166 _action_view_show_cursors = new QAction(this);
167 _action_view_show_cursors->setCheckable(true);
168 _action_view_show_cursors->setChecked(_view->cursors_shown());
4166ed6c 169 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
ab1d13ee
JH
170 _action_view_show_cursors->setObjectName(
171 QString::fromUtf8("actionViewShowCursors"));
172 _action_view_show_cursors->setText(QApplication::translate(
173 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
e072efc8 174 _menu_view->addAction(_action_view_show_cursors);
a429590b 175
7dd093df
JH
176 // Decoders Menu
177 _menu_decoders = new QMenu(_menu_bar);
178 _menu_decoders->setTitle(QApplication::translate(
179 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
180
181 _menu_decoders_add = new QMenu(_menu_decoders);
182 _menu_decoders_add->setTitle(QApplication::translate(
183 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
184 setup_add_decoders(_menu_decoders_add);
185
186 _menu_decoders->addMenu(_menu_decoders_add);
187 connect(&_decoders_add_mapper, SIGNAL(mapped(QObject*)),
188 this, SLOT(add_decoder(QObject*)));
189
ab1d13ee 190 // Help Menu
7d5425ef 191 _menu_help = new QMenu(_menu_bar);
ab1d13ee
JH
192 _menu_help->setTitle(QApplication::translate(
193 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
194
195 _action_about = new QAction(this);
196 _action_about->setObjectName(QString::fromUtf8("actionAbout"));
197 _action_about->setText(QApplication::translate(
198 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
7d5425ef
JH
199 _menu_help->addAction(_action_about);
200
201 _menu_bar->addAction(_menu_file->menuAction());
a429590b 202 _menu_bar->addAction(_menu_view->menuAction());
7dd093df 203 _menu_bar->addAction(_menu_decoders->menuAction());
7d5425ef
JH
204 _menu_bar->addAction(_menu_help->menuAction());
205
206 setMenuBar(_menu_bar);
207 QMetaObject::connectSlotsByName(this);
208
5ac961e3 209 // Setup the toolbar
363107a8 210 _toolbar = new QToolBar(tr("Main Toolbar"), this);
0a4db787 211 _toolbar->addAction(_action_open);
a429590b
JH
212 _toolbar->addSeparator();
213 _toolbar->addAction(_action_view_zoom_in);
214 _toolbar->addAction(_action_view_zoom_out);
0a4db787
JH
215 addToolBar(_toolbar);
216
5ac961e3 217 // Setup the sampling bar
aca00b1e 218 _sampling_bar = new toolbars::SamplingBar(_session, this);
5ac961e3
JH
219
220 // Populate the device list and select the initially selected device
107ca6d3 221 update_device_list();
5ac961e3 222
274d4f13
JH
223 connect(_sampling_bar, SIGNAL(run_stop()), this,
224 SLOT(run_stop()));
d4984fe7
JH
225 addToolBar(_sampling_bar);
226
5ac961e3 227 // Set the title
a8d3fb2d
JH
228 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
229 QApplication::UnicodeUTF8));
230
6ac96c2e
JH
231 // Setup _session events
232 connect(&_session, SIGNAL(capture_state_changed(int)), this,
233 SLOT(capture_state_changed(int)));
234
d7bed479 235}
30236a54 236
f2edb557
JH
237void MainWindow::session_error(
238 const QString text, const QString info_text)
239{
240 QMetaObject::invokeMethod(this, "show_session_error",
241 Qt::QueuedConnection, Q_ARG(QString, text),
242 Q_ARG(QString, info_text));
243}
244
107ca6d3
JH
245void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
246{
247 assert(_sampling_bar);
248
249 const list<sr_dev_inst*> &devices = _device_manager.devices();
250 _sampling_bar->set_device_list(devices);
251
252 if (!selected_device && !devices.empty()) {
253 // Fall back to the first device in the list.
254 selected_device = devices.front();
255
256 // Try and find the demo device and select that by default
257 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
258 if (strcmp(sdi->driver->name, "demo") == 0) {
259 selected_device = sdi;
260 }
261 }
262
263 if (selected_device) {
264 _sampling_bar->set_selected_device(selected_device);
265 _session.set_device(selected_device);
266 }
267}
268
1d478458
JH
269void MainWindow::load_file(QString file_name)
270{
f2edb557
JH
271 const QString errorMessage(
272 QString("Failed to load file %1").arg(file_name));
273 const QString infoMessage;
274 _session.load_file(file_name.toStdString(),
275 boost::bind(&MainWindow::session_error, this,
276 errorMessage, infoMessage));
277}
278
279void MainWindow::show_session_error(
280 const QString text, const QString info_text)
281{
282 QMessageBox msg(this);
283 msg.setText(text);
284 msg.setInformativeText(info_text);
285 msg.setStandardButtons(QMessageBox::Ok);
286 msg.setIcon(QMessageBox::Warning);
287 msg.exec();
1d478458
JH
288}
289
7dd093df
JH
290gint MainWindow::decoder_name_cmp(gconstpointer a, gconstpointer b)
291{
292 return strcmp(((const srd_decoder*)a)->name,
293 ((const srd_decoder*)b)->name);
294}
295
296void MainWindow::setup_add_decoders(QMenu *parent)
297{
298 GSList *l = g_slist_sort(g_slist_copy(
299 (GSList*)srd_decoder_list()), decoder_name_cmp);
e2e2296c
JH
300 for(; l; l = l->next)
301 {
7dd093df
JH
302 QAction *const action = parent->addAction(QString(
303 ((srd_decoder*)l->data)->name));
304 action->setData(qVariantFromValue(l->data));
305 _decoders_add_mapper.setMapping(action, action);
306 connect(action, SIGNAL(triggered()),
307 &_decoders_add_mapper, SLOT(map()));
e2e2296c 308 }
7dd093df
JH
309 g_slist_free(l);
310}
311
2953961c
JH
312void MainWindow::on_actionOpen_triggered()
313{
1d43d767
JH
314 // Enumerate the file formats
315 QString filters(tr("Sigrok Sessions (*.sr)"));
316 filters.append(tr(";;All Files (*.*)"));
317
318 // Show the dialog
1d478458 319 const QString file_name = QFileDialog::getOpenFileName(
1d43d767 320 this, tr("Open File"), "", filters);
92b139d0
JH
321 if (!file_name.isEmpty())
322 load_file(file_name);
2953961c
JH
323}
324
9663c82b
JH
325void MainWindow::on_actionConnect_triggered()
326{
b99cfc42
JH
327 // Stop any currently running capture session
328 _session.stop_capture();
329
107ca6d3 330 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 331
dc0867ff
JH
332 // If the user selected a device, select it in the device list. Select the
333 // current device otherwise.
334 struct sr_dev_inst *const sdi = dlg.exec() ?
335 dlg.get_selected_device() : _session.get_device();
336
107ca6d3 337 update_device_list(sdi);
9663c82b
JH
338}
339
2a032dcb
JH
340void MainWindow::on_actionQuit_triggered()
341{
342 close();
343}
344
a429590b
JH
345void MainWindow::on_actionViewZoomIn_triggered()
346{
347 _view->zoom(1);
348}
349
350void MainWindow::on_actionViewZoomOut_triggered()
351{
352 _view->zoom(-1);
353}
354
e072efc8
JH
355void MainWindow::on_actionViewShowCursors_triggered()
356{
357 assert(_view);
b4d91e56
JH
358
359 const bool show = !_view->cursors_shown();
360 if(show)
361 _view->centre_cursors();
362
363 _view->show_cursors(show);
e072efc8
JH
364}
365
30236a54
JH
366void MainWindow::on_actionAbout_triggered()
367{
acda14b8 368 dialogs::About dlg(this);
40eb2ff4 369 dlg.exec();
30236a54 370}
274d4f13 371
7dd093df
JH
372void MainWindow::add_decoder(QObject *action)
373{
c8c28626
JH
374 assert(action);
375 srd_decoder *const dec =
376 (srd_decoder*)((QAction*)action)->data().value<void*>();
377 assert(dec);
378
3045c869
JH
379 vector< shared_ptr<view::LogicSignal> > logic_sigs;
380 const vector< shared_ptr<view::Signal> > &sigs =
535554a4 381 _session.get_signals();
3045c869
JH
382 BOOST_FOREACH(shared_ptr<view::Signal> s, sigs) {
383 assert(s);
384 shared_ptr<view::LogicSignal> l =
385 dynamic_pointer_cast<view::LogicSignal>(s);
386 if (l)
387 logic_sigs.push_back(l);
388 }
535554a4 389
67fe5e9c
JH
390 GHashTable *const options = g_hash_table_new_full(g_str_hash,
391 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
392
3045c869 393 dialogs::Decoder dlg(this, dec, logic_sigs, options);
67fe5e9c
JH
394 if(dlg.exec() != QDialog::Accepted) {
395 g_hash_table_destroy(options);
82c7f640 396 return;
67fe5e9c 397 }
82c7f640 398
67fe5e9c 399 _session.add_decoder(dec, dlg.get_probes(), options);
7dd093df
JH
400}
401
274d4f13
JH
402void MainWindow::run_stop()
403{
5b7cf66c
JH
404 switch(_session.get_capture_state()) {
405 case SigSession::Stopped:
d64d1596 406 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
407 boost::bind(&MainWindow::session_error, this,
408 QString("Capture failed"), _1));
5b7cf66c
JH
409 break;
410
2b49eeb0 411 case SigSession::AwaitingTrigger:
5b7cf66c
JH
412 case SigSession::Running:
413 _session.stop_capture();
414 break;
415 }
274d4f13 416}
51e77110 417
6ac96c2e
JH
418void MainWindow::capture_state_changed(int state)
419{
2b49eeb0 420 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
421}
422
51e77110 423} // namespace pv