]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Make sigrokdecode a non-optional dependancy
[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
JH
67 _device_manager(device_manager),
68 _session(device_manager)
d7bed479 69{
7d5425ef 70 setup_ui();
1d478458
JH
71 if (open_file_name) {
72 const QString s(QString::fromUtf8(open_file_name));
73 QMetaObject::invokeMethod(this, "load_file",
74 Qt::QueuedConnection,
75 Q_ARG(QString, s));
76 }
7d5425ef
JH
77}
78
79void MainWindow::setup_ui()
80{
81 setObjectName(QString::fromUtf8("MainWindow"));
82
83 resize(1024, 768);
84
85 // Set the window icon
86 QIcon icon;
87 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
88 QSize(), QIcon::Normal, QIcon::Off);
89 setWindowIcon(icon);
90
e072efc8
JH
91 // Setup the central widget
92 _central_widget = new QWidget(this);
93 _vertical_layout = new QVBoxLayout(_central_widget);
94 _vertical_layout->setSpacing(6);
95 _vertical_layout->setContentsMargins(0, 0, 0, 0);
96 setCentralWidget(_central_widget);
97
98 _view = new pv::view::View(_session, this);
b1264f56
JH
99 connect(_view, SIGNAL(selection_changed()), this,
100 SLOT(view_selection_changed()));
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
ab1d13ee 176 // Help Menu
7d5425ef 177 _menu_help = new QMenu(_menu_bar);
ab1d13ee
JH
178 _menu_help->setTitle(QApplication::translate(
179 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
180
181 _action_about = new QAction(this);
182 _action_about->setObjectName(QString::fromUtf8("actionAbout"));
183 _action_about->setText(QApplication::translate(
184 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
7d5425ef
JH
185 _menu_help->addAction(_action_about);
186
187 _menu_bar->addAction(_menu_file->menuAction());
a429590b 188 _menu_bar->addAction(_menu_view->menuAction());
7d5425ef
JH
189 _menu_bar->addAction(_menu_help->menuAction());
190
191 setMenuBar(_menu_bar);
192 QMetaObject::connectSlotsByName(this);
193
5ac961e3 194 // Setup the toolbar
363107a8 195 _toolbar = new QToolBar(tr("Main Toolbar"), this);
0a4db787 196 _toolbar->addAction(_action_open);
a429590b
JH
197 _toolbar->addSeparator();
198 _toolbar->addAction(_action_view_zoom_in);
199 _toolbar->addAction(_action_view_zoom_out);
0a4db787
JH
200 addToolBar(_toolbar);
201
5ac961e3 202 // Setup the sampling bar
f4c92e1c 203 _sampling_bar = new toolbars::SamplingBar(this);
5ac961e3
JH
204
205 // Populate the device list and select the initially selected device
107ca6d3 206 update_device_list();
5ac961e3
JH
207
208 connect(_sampling_bar, SIGNAL(device_selected()), this,
209 SLOT(device_selected()));
274d4f13
JH
210 connect(_sampling_bar, SIGNAL(run_stop()), this,
211 SLOT(run_stop()));
d4984fe7
JH
212 addToolBar(_sampling_bar);
213
404aad0e
JH
214 // Setup the context bar
215 _context_bar = new toolbars::ContextBar(this);
216 addToolBar(_context_bar);
404aad0e 217
5ac961e3 218 // Set the title
a8d3fb2d
JH
219 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
220 QApplication::UnicodeUTF8));
221
6ac96c2e
JH
222 // Setup _session events
223 connect(&_session, SIGNAL(capture_state_changed(int)), this,
224 SLOT(capture_state_changed(int)));
225
d7bed479 226}
30236a54 227
f2edb557
JH
228void MainWindow::session_error(
229 const QString text, const QString info_text)
230{
231 QMetaObject::invokeMethod(this, "show_session_error",
232 Qt::QueuedConnection, Q_ARG(QString, text),
233 Q_ARG(QString, info_text));
234}
235
107ca6d3
JH
236void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
237{
238 assert(_sampling_bar);
239
240 const list<sr_dev_inst*> &devices = _device_manager.devices();
241 _sampling_bar->set_device_list(devices);
242
243 if (!selected_device && !devices.empty()) {
244 // Fall back to the first device in the list.
245 selected_device = devices.front();
246
247 // Try and find the demo device and select that by default
248 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
249 if (strcmp(sdi->driver->name, "demo") == 0) {
250 selected_device = sdi;
251 }
252 }
253
254 if (selected_device) {
255 _sampling_bar->set_selected_device(selected_device);
256 _session.set_device(selected_device);
257 }
258}
259
1d478458
JH
260void MainWindow::load_file(QString file_name)
261{
f2edb557
JH
262 const QString errorMessage(
263 QString("Failed to load file %1").arg(file_name));
264 const QString infoMessage;
265 _session.load_file(file_name.toStdString(),
266 boost::bind(&MainWindow::session_error, this,
267 errorMessage, infoMessage));
268}
269
270void MainWindow::show_session_error(
271 const QString text, const QString info_text)
272{
273 QMessageBox msg(this);
274 msg.setText(text);
275 msg.setInformativeText(info_text);
276 msg.setStandardButtons(QMessageBox::Ok);
277 msg.setIcon(QMessageBox::Warning);
278 msg.exec();
1d478458
JH
279}
280
2953961c
JH
281void MainWindow::on_actionOpen_triggered()
282{
1d43d767
JH
283 // Enumerate the file formats
284 QString filters(tr("Sigrok Sessions (*.sr)"));
285 filters.append(tr(";;All Files (*.*)"));
286
287 // Show the dialog
1d478458 288 const QString file_name = QFileDialog::getOpenFileName(
1d43d767 289 this, tr("Open File"), "", filters);
92b139d0
JH
290 if (!file_name.isEmpty())
291 load_file(file_name);
2953961c
JH
292}
293
9663c82b
JH
294void MainWindow::on_actionConnect_triggered()
295{
b99cfc42
JH
296 // Stop any currently running capture session
297 _session.stop_capture();
298
107ca6d3 299 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 300
dc0867ff
JH
301 // If the user selected a device, select it in the device list. Select the
302 // current device otherwise.
303 struct sr_dev_inst *const sdi = dlg.exec() ?
304 dlg.get_selected_device() : _session.get_device();
305
107ca6d3 306 update_device_list(sdi);
9663c82b
JH
307}
308
2a032dcb
JH
309void MainWindow::on_actionQuit_triggered()
310{
311 close();
312}
313
a429590b
JH
314void MainWindow::on_actionViewZoomIn_triggered()
315{
316 _view->zoom(1);
317}
318
319void MainWindow::on_actionViewZoomOut_triggered()
320{
321 _view->zoom(-1);
322}
323
e072efc8
JH
324void MainWindow::on_actionViewShowCursors_triggered()
325{
326 assert(_view);
b4d91e56
JH
327
328 const bool show = !_view->cursors_shown();
329 if(show)
330 _view->centre_cursors();
331
332 _view->show_cursors(show);
e072efc8
JH
333}
334
30236a54
JH
335void MainWindow::on_actionAbout_triggered()
336{
acda14b8 337 dialogs::About dlg(this);
40eb2ff4 338 dlg.exec();
30236a54 339}
274d4f13 340
5ac961e3
JH
341void MainWindow::device_selected()
342{
343 _session.set_device(_sampling_bar->get_selected_device());
344}
345
274d4f13
JH
346void MainWindow::run_stop()
347{
5b7cf66c
JH
348 switch(_session.get_capture_state()) {
349 case SigSession::Stopped:
d64d1596 350 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
351 boost::bind(&MainWindow::session_error, this,
352 QString("Capture failed"), _1));
5b7cf66c
JH
353 break;
354
2b49eeb0 355 case SigSession::AwaitingTrigger:
5b7cf66c
JH
356 case SigSession::Running:
357 _session.stop_capture();
358 break;
359 }
274d4f13 360}
51e77110 361
6ac96c2e
JH
362void MainWindow::capture_state_changed(int state)
363{
2b49eeb0 364 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
365}
366
b1264f56
JH
367void MainWindow::view_selection_changed()
368{
369 assert(_context_bar);
370
371 const list<weak_ptr<pv::view::SelectableItem> > items(
372 _view->selected_items());
373 _context_bar->set_selected_items(items);
374}
375
51e77110 376} // namespace pv