]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Renamed SignalData::clear_snapshots to clear
[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"
f4c92e1c 42#include "toolbars/samplingbar.h"
3045c869 43#include "view/logicsignal.h"
4104d7f3 44#include "view/view.h"
f0d37dab 45#include "widgets/decodermenu.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),
f0d37dab 69 _session(device_manager)
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 100
e072efc8
JH
101 _vertical_layout->addWidget(_view);
102
ab1d13ee 103 // Setup the menu bar
ced1cc13
JH
104 QMenuBar *const menu_bar = new QMenuBar(this);
105 menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 106
ab1d13ee 107 // File Menu
ced1cc13
JH
108 QMenu *const menu_file = new QMenu;
109 menu_file->setTitle(QApplication::translate(
ab1d13ee 110 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 111
ced1cc13
JH
112 QAction *const action_open = new QAction(this);
113 action_open->setText(QApplication::translate(
ab1d13ee 114 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
ced1cc13 115 action_open->setIcon(QIcon::fromTheme("document-open",
362eea96 116 QIcon(":/icons/document-open.png")));
ced1cc13
JH
117 action_open->setObjectName(QString::fromUtf8("actionOpen"));
118 menu_file->addAction(action_open);
009e1503 119
ced1cc13 120 menu_file->addSeparator();
2a032dcb 121
ced1cc13
JH
122 QAction *const action_connect = new QAction(this);
123 action_connect->setText(QApplication::translate(
9663c82b
JH
124 "MainWindow", "&Connect to Device...", 0,
125 QApplication::UnicodeUTF8));
ced1cc13
JH
126 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
127 menu_file->addAction(action_connect);
9663c82b 128
ced1cc13 129 menu_file->addSeparator();
9663c82b 130
ced1cc13
JH
131 QAction *action_quit = new QAction(this);
132 action_quit->setText(QApplication::translate(
2a032dcb 133 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
ced1cc13 134 action_quit->setIcon(QIcon::fromTheme("application-exit",
2a032dcb 135 QIcon(":/icons/application-exit.png")));
ced1cc13
JH
136 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
137 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
138 menu_file->addAction(action_quit);
2a032dcb 139
ab1d13ee 140 // View Menu
ced1cc13
JH
141 QMenu *menu_view = new QMenu;
142 menu_view->setTitle(QApplication::translate(
ab1d13ee
JH
143 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
144
ced1cc13
JH
145 QAction *const action_view_zoom_in = new QAction(this);
146 action_view_zoom_in->setText(QApplication::translate(
ab1d13ee 147 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
ced1cc13 148 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
ab1d13ee 149 QIcon(":/icons/zoom-in.png")));
ced1cc13 150 action_view_zoom_in->setObjectName(
ab1d13ee 151 QString::fromUtf8("actionViewZoomIn"));
ced1cc13 152 menu_view->addAction(action_view_zoom_in);
ab1d13ee 153
ced1cc13
JH
154 QAction *const action_view_zoom_out = new QAction(this);
155 action_view_zoom_out->setText(QApplication::translate(
ab1d13ee 156 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
ced1cc13 157 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
ab1d13ee 158 QIcon(":/icons/zoom-out.png")));
ced1cc13 159 action_view_zoom_out->setObjectName(
ab1d13ee 160 QString::fromUtf8("actionViewZoomOut"));
ced1cc13 161 menu_view->addAction(action_view_zoom_out);
ab1d13ee 162
ced1cc13 163 menu_view->addSeparator();
ab1d13ee 164
ced1cc13
JH
165 QAction *action_view_show_cursors = new QAction(this);
166 action_view_show_cursors->setCheckable(true);
167 action_view_show_cursors->setChecked(_view->cursors_shown());
168 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
169 action_view_show_cursors->setObjectName(
ab1d13ee 170 QString::fromUtf8("actionViewShowCursors"));
ced1cc13 171 action_view_show_cursors->setText(QApplication::translate(
ab1d13ee 172 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
ced1cc13 173 menu_view->addAction(action_view_show_cursors);
a429590b 174
7dd093df 175 // Decoders Menu
ced1cc13
JH
176 QMenu *const menu_decoders = new QMenu;
177 menu_decoders->setTitle(QApplication::translate(
7dd093df
JH
178 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
179
ced1cc13
JH
180 pv::widgets::DecoderMenu *const menu_decoders_add =
181 new pv::widgets::DecoderMenu(menu_decoders);
182 menu_decoders_add->setTitle(QApplication::translate(
7dd093df 183 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
ced1cc13 184 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
f0d37dab 185 this, SLOT(add_decoder(srd_decoder*)));
7dd093df 186
ced1cc13 187 menu_decoders->addMenu(menu_decoders_add);
7dd093df 188
ab1d13ee 189 // Help Menu
ced1cc13
JH
190 QMenu *const menu_help = new QMenu;
191 menu_help->setTitle(QApplication::translate(
ab1d13ee
JH
192 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
193
ced1cc13
JH
194 QAction *const action_about = new QAction(this);
195 action_about->setObjectName(QString::fromUtf8("actionAbout"));
196 action_about->setText(QApplication::translate(
ab1d13ee 197 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
ced1cc13 198 menu_help->addAction(action_about);
7d5425ef 199
ced1cc13
JH
200 menu_bar->addAction(menu_file->menuAction());
201 menu_bar->addAction(menu_view->menuAction());
202 menu_bar->addAction(menu_decoders->menuAction());
203 menu_bar->addAction(menu_help->menuAction());
7d5425ef 204
ced1cc13 205 setMenuBar(menu_bar);
7d5425ef
JH
206 QMetaObject::connectSlotsByName(this);
207
5ac961e3 208 // Setup the toolbar
ced1cc13
JH
209 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
210 toolbar->addAction(action_open);
211 toolbar->addSeparator();
212 toolbar->addAction(action_view_zoom_in);
213 toolbar->addAction(action_view_zoom_out);
214 addToolBar(toolbar);
0a4db787 215
5ac961e3 216 // Setup the sampling bar
aca00b1e 217 _sampling_bar = new toolbars::SamplingBar(_session, this);
5ac961e3
JH
218
219 // Populate the device list and select the initially selected device
107ca6d3 220 update_device_list();
5ac961e3 221
274d4f13
JH
222 connect(_sampling_bar, SIGNAL(run_stop()), this,
223 SLOT(run_stop()));
d4984fe7
JH
224 addToolBar(_sampling_bar);
225
5ac961e3 226 // Set the title
a8d3fb2d
JH
227 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
228 QApplication::UnicodeUTF8));
229
6ac96c2e
JH
230 // Setup _session events
231 connect(&_session, SIGNAL(capture_state_changed(int)), this,
232 SLOT(capture_state_changed(int)));
233
d7bed479 234}
30236a54 235
f2edb557
JH
236void MainWindow::session_error(
237 const QString text, const QString info_text)
238{
239 QMetaObject::invokeMethod(this, "show_session_error",
240 Qt::QueuedConnection, Q_ARG(QString, text),
241 Q_ARG(QString, info_text));
242}
243
107ca6d3
JH
244void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
245{
246 assert(_sampling_bar);
247
248 const list<sr_dev_inst*> &devices = _device_manager.devices();
249 _sampling_bar->set_device_list(devices);
250
251 if (!selected_device && !devices.empty()) {
252 // Fall back to the first device in the list.
253 selected_device = devices.front();
254
255 // Try and find the demo device and select that by default
256 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
257 if (strcmp(sdi->driver->name, "demo") == 0) {
258 selected_device = sdi;
259 }
260 }
261
262 if (selected_device) {
263 _sampling_bar->set_selected_device(selected_device);
264 _session.set_device(selected_device);
265 }
266}
267
1d478458
JH
268void MainWindow::load_file(QString file_name)
269{
f2edb557
JH
270 const QString errorMessage(
271 QString("Failed to load file %1").arg(file_name));
272 const QString infoMessage;
273 _session.load_file(file_name.toStdString(),
274 boost::bind(&MainWindow::session_error, this,
275 errorMessage, infoMessage));
276}
277
278void MainWindow::show_session_error(
279 const QString text, const QString info_text)
280{
281 QMessageBox msg(this);
282 msg.setText(text);
283 msg.setInformativeText(info_text);
284 msg.setStandardButtons(QMessageBox::Ok);
285 msg.setIcon(QMessageBox::Warning);
286 msg.exec();
1d478458
JH
287}
288
2953961c
JH
289void MainWindow::on_actionOpen_triggered()
290{
1d43d767
JH
291 // Enumerate the file formats
292 QString filters(tr("Sigrok Sessions (*.sr)"));
293 filters.append(tr(";;All Files (*.*)"));
294
295 // Show the dialog
1d478458 296 const QString file_name = QFileDialog::getOpenFileName(
1d43d767 297 this, tr("Open File"), "", filters);
92b139d0
JH
298 if (!file_name.isEmpty())
299 load_file(file_name);
2953961c
JH
300}
301
9663c82b
JH
302void MainWindow::on_actionConnect_triggered()
303{
b99cfc42
JH
304 // Stop any currently running capture session
305 _session.stop_capture();
306
107ca6d3 307 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 308
dc0867ff
JH
309 // If the user selected a device, select it in the device list. Select the
310 // current device otherwise.
311 struct sr_dev_inst *const sdi = dlg.exec() ?
312 dlg.get_selected_device() : _session.get_device();
313
107ca6d3 314 update_device_list(sdi);
9663c82b
JH
315}
316
2a032dcb
JH
317void MainWindow::on_actionQuit_triggered()
318{
319 close();
320}
321
a429590b
JH
322void MainWindow::on_actionViewZoomIn_triggered()
323{
324 _view->zoom(1);
325}
326
327void MainWindow::on_actionViewZoomOut_triggered()
328{
329 _view->zoom(-1);
330}
331
e072efc8
JH
332void MainWindow::on_actionViewShowCursors_triggered()
333{
334 assert(_view);
b4d91e56
JH
335
336 const bool show = !_view->cursors_shown();
337 if(show)
338 _view->centre_cursors();
339
340 _view->show_cursors(show);
e072efc8
JH
341}
342
30236a54
JH
343void MainWindow::on_actionAbout_triggered()
344{
acda14b8 345 dialogs::About dlg(this);
40eb2ff4 346 dlg.exec();
30236a54 347}
274d4f13 348
f0d37dab 349void MainWindow::add_decoder(srd_decoder *decoder)
7dd093df 350{
f0d37dab
JH
351 assert(decoder);
352 _session.add_decoder(decoder);
7dd093df
JH
353}
354
274d4f13
JH
355void MainWindow::run_stop()
356{
5b7cf66c
JH
357 switch(_session.get_capture_state()) {
358 case SigSession::Stopped:
d64d1596 359 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
360 boost::bind(&MainWindow::session_error, this,
361 QString("Capture failed"), _1));
5b7cf66c
JH
362 break;
363
2b49eeb0 364 case SigSession::AwaitingTrigger:
5b7cf66c
JH
365 case SigSession::Running:
366 _session.stop_capture();
367 break;
368 }
274d4f13 369}
51e77110 370
6ac96c2e
JH
371void MainWindow::capture_state_changed(int state)
372{
2b49eeb0 373 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
6ac96c2e
JH
374}
375
51e77110 376} // namespace pv