]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
Added trigger selection handlers
[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
d52d8455 21#ifdef ENABLE_SIGROKDECODE
aaabd61b 22#include <libsigrokdecode/libsigrokdecode.h>
d52d8455 23#endif
30236a54 24
f2edb557 25#include <boost/bind.hpp>
107ca6d3 26#include <boost/foreach.hpp>
f2edb557 27
7d5425ef
JH
28#include <QAction>
29#include <QApplication>
30#include <QButtonGroup>
2953961c 31#include <QFileDialog>
f2edb557 32#include <QMessageBox>
7d5425ef
JH
33#include <QMenu>
34#include <QMenuBar>
35#include <QStatusBar>
36#include <QVBoxLayout>
37#include <QWidget>
2953961c 38
d7bed479 39#include "mainwindow.h"
107ca6d3
JH
40
41#include "devicemanager.h"
acda14b8 42#include "dialogs/about.h"
9663c82b 43#include "dialogs/connect.h"
404aad0e 44#include "toolbars/contextbar.h"
f4c92e1c 45#include "toolbars/samplingbar.h"
4104d7f3 46#include "view/view.h"
d7bed479 47
30236a54
JH
48/* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
49#define __STDC_FORMAT_MACROS
50#include <inttypes.h>
51#include <stdint.h>
52#include <stdarg.h>
53#include <glib.h>
54#include <libsigrok/libsigrok.h>
e82fd481 55
b1264f56 56using namespace boost;
107ca6d3 57using namespace std;
30236a54 58
51e77110
JH
59namespace pv {
60
b1264f56
JH
61namespace view {
62class SelectableItem;
63}
64
107ca6d3
JH
65MainWindow::MainWindow(DeviceManager &device_manager,
66 const char *open_file_name,
1d478458 67 QWidget *parent) :
107ca6d3 68 QMainWindow(parent),
dc0867ff
JH
69 _device_manager(device_manager),
70 _session(device_manager)
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
ab1d13ee 178 // Help Menu
7d5425ef 179 _menu_help = new QMenu(_menu_bar);
ab1d13ee
JH
180 _menu_help->setTitle(QApplication::translate(
181 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
182
183 _action_about = new QAction(this);
184 _action_about->setObjectName(QString::fromUtf8("actionAbout"));
185 _action_about->setText(QApplication::translate(
186 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
7d5425ef
JH
187 _menu_help->addAction(_action_about);
188
189 _menu_bar->addAction(_menu_file->menuAction());
a429590b 190 _menu_bar->addAction(_menu_view->menuAction());
7d5425ef
JH
191 _menu_bar->addAction(_menu_help->menuAction());
192
193 setMenuBar(_menu_bar);
194 QMetaObject::connectSlotsByName(this);
195
5ac961e3 196 // Setup the toolbar
363107a8 197 _toolbar = new QToolBar(tr("Main Toolbar"), this);
0a4db787 198 _toolbar->addAction(_action_open);
a429590b
JH
199 _toolbar->addSeparator();
200 _toolbar->addAction(_action_view_zoom_in);
201 _toolbar->addAction(_action_view_zoom_out);
0a4db787
JH
202 addToolBar(_toolbar);
203
5ac961e3 204 // Setup the sampling bar
f4c92e1c 205 _sampling_bar = new toolbars::SamplingBar(this);
5ac961e3
JH
206
207 // Populate the device list and select the initially selected device
107ca6d3 208 update_device_list();
5ac961e3
JH
209
210 connect(_sampling_bar, SIGNAL(device_selected()), this,
211 SLOT(device_selected()));
274d4f13
JH
212 connect(_sampling_bar, SIGNAL(run_stop()), this,
213 SLOT(run_stop()));
d4984fe7
JH
214 addToolBar(_sampling_bar);
215
404aad0e
JH
216 // Setup the context bar
217 _context_bar = new toolbars::ContextBar(this);
218 addToolBar(_context_bar);
219 insertToolBarBreak(_context_bar);
220
5ac961e3 221 // Set the title
a8d3fb2d
JH
222 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
223 QApplication::UnicodeUTF8));
224
6ac96c2e
JH
225 // Setup _session events
226 connect(&_session, SIGNAL(capture_state_changed(int)), this,
227 SLOT(capture_state_changed(int)));
228
d7bed479 229}
30236a54 230
f2edb557
JH
231void MainWindow::session_error(
232 const QString text, const QString info_text)
233{
234 QMetaObject::invokeMethod(this, "show_session_error",
235 Qt::QueuedConnection, Q_ARG(QString, text),
236 Q_ARG(QString, info_text));
237}
238
107ca6d3
JH
239void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
240{
241 assert(_sampling_bar);
242
243 const list<sr_dev_inst*> &devices = _device_manager.devices();
244 _sampling_bar->set_device_list(devices);
245
246 if (!selected_device && !devices.empty()) {
247 // Fall back to the first device in the list.
248 selected_device = devices.front();
249
250 // Try and find the demo device and select that by default
251 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
252 if (strcmp(sdi->driver->name, "demo") == 0) {
253 selected_device = sdi;
254 }
255 }
256
257 if (selected_device) {
258 _sampling_bar->set_selected_device(selected_device);
259 _session.set_device(selected_device);
260 }
261}
262
1d478458
JH
263void MainWindow::load_file(QString file_name)
264{
f2edb557
JH
265 const QString errorMessage(
266 QString("Failed to load file %1").arg(file_name));
267 const QString infoMessage;
268 _session.load_file(file_name.toStdString(),
269 boost::bind(&MainWindow::session_error, this,
270 errorMessage, infoMessage));
271}
272
273void MainWindow::show_session_error(
274 const QString text, const QString info_text)
275{
276 QMessageBox msg(this);
277 msg.setText(text);
278 msg.setInformativeText(info_text);
279 msg.setStandardButtons(QMessageBox::Ok);
280 msg.setIcon(QMessageBox::Warning);
281 msg.exec();
1d478458
JH
282}
283
2953961c
JH
284void MainWindow::on_actionOpen_triggered()
285{
1d43d767
JH
286 // Enumerate the file formats
287 QString filters(tr("Sigrok Sessions (*.sr)"));
288 filters.append(tr(";;All Files (*.*)"));
289
290 // Show the dialog
1d478458 291 const QString file_name = QFileDialog::getOpenFileName(
1d43d767 292 this, tr("Open File"), "", filters);
92b139d0
JH
293 if (!file_name.isEmpty())
294 load_file(file_name);
2953961c
JH
295}
296
9663c82b
JH
297void MainWindow::on_actionConnect_triggered()
298{
b99cfc42
JH
299 // Stop any currently running capture session
300 _session.stop_capture();
301
107ca6d3 302 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 303
dc0867ff
JH
304 // If the user selected a device, select it in the device list. Select the
305 // current device otherwise.
306 struct sr_dev_inst *const sdi = dlg.exec() ?
307 dlg.get_selected_device() : _session.get_device();
308
107ca6d3 309 update_device_list(sdi);
9663c82b
JH
310}
311
2a032dcb
JH
312void MainWindow::on_actionQuit_triggered()
313{
314 close();
315}
316
a429590b
JH
317void MainWindow::on_actionViewZoomIn_triggered()
318{
319 _view->zoom(1);
320}
321
322void MainWindow::on_actionViewZoomOut_triggered()
323{
324 _view->zoom(-1);
325}
326
e072efc8
JH
327void MainWindow::on_actionViewShowCursors_triggered()
328{
329 assert(_view);
b4d91e56
JH
330
331 const bool show = !_view->cursors_shown();
332 if(show)
333 _view->centre_cursors();
334
335 _view->show_cursors(show);
e072efc8
JH
336}
337
30236a54
JH
338void MainWindow::on_actionAbout_triggered()
339{
acda14b8 340 dialogs::About dlg(this);
40eb2ff4 341 dlg.exec();
30236a54 342}
274d4f13 343
5ac961e3
JH
344void MainWindow::device_selected()
345{
346 _session.set_device(_sampling_bar->get_selected_device());
347}
348
274d4f13
JH
349void MainWindow::run_stop()
350{
5b7cf66c
JH
351 switch(_session.get_capture_state()) {
352 case SigSession::Stopped:
d64d1596 353 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
354 boost::bind(&MainWindow::session_error, this,
355 QString("Capture failed"), _1));
5b7cf66c
JH
356 break;
357
358 case SigSession::Running:
359 _session.stop_capture();
360 break;
361 }
274d4f13 362}
51e77110 363
6ac96c2e
JH
364void MainWindow::capture_state_changed(int state)
365{
366 _sampling_bar->set_sampling(state != SigSession::Stopped);
367}
368
b1264f56
JH
369void MainWindow::view_selection_changed()
370{
371 assert(_context_bar);
372
373 const list<weak_ptr<pv::view::SelectableItem> > items(
374 _view->selected_items());
375 _context_bar->set_selected_items(items);
376}
377
51e77110 378} // namespace pv