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