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