]> sigrok.org Git - pulseview.git/blame - pv/mainwindow.cpp
pv::DeviceManager now manages opening/closing devices
[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
30236a54 22#include <sigrokdecode.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"
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
107ca6d3 55using namespace std;
30236a54 56
51e77110
JH
57namespace pv {
58
107ca6d3
JH
59MainWindow::MainWindow(DeviceManager &device_manager,
60 const char *open_file_name,
1d478458 61 QWidget *parent) :
107ca6d3 62 QMainWindow(parent),
dc0867ff
JH
63 _device_manager(device_manager),
64 _session(device_manager)
d7bed479 65{
7d5425ef 66 setup_ui();
1d478458
JH
67 if (open_file_name) {
68 const QString s(QString::fromUtf8(open_file_name));
69 QMetaObject::invokeMethod(this, "load_file",
70 Qt::QueuedConnection,
71 Q_ARG(QString, s));
72 }
7d5425ef
JH
73}
74
75void MainWindow::setup_ui()
76{
77 setObjectName(QString::fromUtf8("MainWindow"));
78
79 resize(1024, 768);
80
81 // Set the window icon
82 QIcon icon;
83 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
84 QSize(), QIcon::Normal, QIcon::Off);
85 setWindowIcon(icon);
86
e072efc8
JH
87 // Setup the central widget
88 _central_widget = new QWidget(this);
89 _vertical_layout = new QVBoxLayout(_central_widget);
90 _vertical_layout->setSpacing(6);
91 _vertical_layout->setContentsMargins(0, 0, 0, 0);
92 setCentralWidget(_central_widget);
93
94 _view = new pv::view::View(_session, this);
95 _vertical_layout->addWidget(_view);
96
ab1d13ee
JH
97 // Setup the menu bar
98 _menu_bar = new QMenuBar(this);
99 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
a429590b 100
ab1d13ee
JH
101 // File Menu
102 _menu_file = new QMenu(_menu_bar);
103 _menu_file->setTitle(QApplication::translate(
104 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
e072efc8 105
7d5425ef 106 _action_open = new QAction(this);
ab1d13ee
JH
107 _action_open->setText(QApplication::translate(
108 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
362eea96
JH
109 _action_open->setIcon(QIcon::fromTheme("document-open",
110 QIcon(":/icons/document-open.png")));
7d5425ef 111 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
7d5425ef 112 _menu_file->addAction(_action_open);
009e1503 113
2a032dcb
JH
114 _menu_file->addSeparator();
115
9663c82b
JH
116 _action_connect = new QAction(this);
117 _action_connect->setText(QApplication::translate(
118 "MainWindow", "&Connect to Device...", 0,
119 QApplication::UnicodeUTF8));
120 _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
121 _menu_file->addAction(_action_connect);
122
123 _menu_file->addSeparator();
124
2a032dcb
JH
125 _action_quit = new QAction(this);
126 _action_quit->setText(QApplication::translate(
127 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
128 _action_quit->setIcon(QIcon::fromTheme("application-exit",
129 QIcon(":/icons/application-exit.png")));
d1bb7d7a 130 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
2a032dcb
JH
131 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
132 _menu_file->addAction(_action_quit);
133
ab1d13ee 134 // View Menu
a429590b 135 _menu_view = new QMenu(_menu_bar);
ab1d13ee
JH
136 _menu_view->setTitle(QApplication::translate(
137 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
138
139 _action_view_zoom_in = new QAction(this);
140 _action_view_zoom_in->setText(QApplication::translate(
141 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
142 _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
143 QIcon(":/icons/zoom-in.png")));
144 _action_view_zoom_in->setObjectName(
145 QString::fromUtf8("actionViewZoomIn"));
a429590b 146 _menu_view->addAction(_action_view_zoom_in);
ab1d13ee
JH
147
148 _action_view_zoom_out = new QAction(this);
149 _action_view_zoom_out->setText(QApplication::translate(
150 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
151 _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
152 QIcon(":/icons/zoom-out.png")));
153 _action_view_zoom_out->setObjectName(
154 QString::fromUtf8("actionViewZoomOut"));
a429590b 155 _menu_view->addAction(_action_view_zoom_out);
ab1d13ee 156
e072efc8 157 _menu_view->addSeparator();
ab1d13ee
JH
158
159 _action_view_show_cursors = new QAction(this);
160 _action_view_show_cursors->setCheckable(true);
161 _action_view_show_cursors->setChecked(_view->cursors_shown());
4166ed6c 162 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
ab1d13ee
JH
163 _action_view_show_cursors->setObjectName(
164 QString::fromUtf8("actionViewShowCursors"));
165 _action_view_show_cursors->setText(QApplication::translate(
166 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
e072efc8 167 _menu_view->addAction(_action_view_show_cursors);
a429590b 168
ab1d13ee 169 // Help Menu
7d5425ef 170 _menu_help = new QMenu(_menu_bar);
ab1d13ee
JH
171 _menu_help->setTitle(QApplication::translate(
172 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
173
174 _action_about = new QAction(this);
175 _action_about->setObjectName(QString::fromUtf8("actionAbout"));
176 _action_about->setText(QApplication::translate(
177 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
7d5425ef
JH
178 _menu_help->addAction(_action_about);
179
180 _menu_bar->addAction(_menu_file->menuAction());
a429590b 181 _menu_bar->addAction(_menu_view->menuAction());
7d5425ef
JH
182 _menu_bar->addAction(_menu_help->menuAction());
183
184 setMenuBar(_menu_bar);
185 QMetaObject::connectSlotsByName(this);
186
5ac961e3 187 // Setup the toolbar
363107a8 188 _toolbar = new QToolBar(tr("Main Toolbar"), this);
0a4db787 189 _toolbar->addAction(_action_open);
a429590b
JH
190 _toolbar->addSeparator();
191 _toolbar->addAction(_action_view_zoom_in);
192 _toolbar->addAction(_action_view_zoom_out);
0a4db787
JH
193 addToolBar(_toolbar);
194
5ac961e3 195 // Setup the sampling bar
f4c92e1c 196 _sampling_bar = new toolbars::SamplingBar(this);
5ac961e3
JH
197
198 // Populate the device list and select the initially selected device
107ca6d3 199 update_device_list();
5ac961e3
JH
200
201 connect(_sampling_bar, SIGNAL(device_selected()), this,
202 SLOT(device_selected()));
274d4f13
JH
203 connect(_sampling_bar, SIGNAL(run_stop()), this,
204 SLOT(run_stop()));
d4984fe7
JH
205 addToolBar(_sampling_bar);
206
5ac961e3 207 // Set the title
a8d3fb2d
JH
208 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
209 QApplication::UnicodeUTF8));
210
6ac96c2e
JH
211 // Setup _session events
212 connect(&_session, SIGNAL(capture_state_changed(int)), this,
213 SLOT(capture_state_changed(int)));
214
d7bed479 215}
30236a54 216
f2edb557
JH
217void MainWindow::session_error(
218 const QString text, const QString info_text)
219{
220 QMetaObject::invokeMethod(this, "show_session_error",
221 Qt::QueuedConnection, Q_ARG(QString, text),
222 Q_ARG(QString, info_text));
223}
224
107ca6d3
JH
225void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
226{
227 assert(_sampling_bar);
228
229 const list<sr_dev_inst*> &devices = _device_manager.devices();
230 _sampling_bar->set_device_list(devices);
231
232 if (!selected_device && !devices.empty()) {
233 // Fall back to the first device in the list.
234 selected_device = devices.front();
235
236 // Try and find the demo device and select that by default
237 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
238 if (strcmp(sdi->driver->name, "demo") == 0) {
239 selected_device = sdi;
240 }
241 }
242
243 if (selected_device) {
244 _sampling_bar->set_selected_device(selected_device);
245 _session.set_device(selected_device);
246 }
247}
248
1d478458
JH
249void MainWindow::load_file(QString file_name)
250{
f2edb557
JH
251 const QString errorMessage(
252 QString("Failed to load file %1").arg(file_name));
253 const QString infoMessage;
254 _session.load_file(file_name.toStdString(),
255 boost::bind(&MainWindow::session_error, this,
256 errorMessage, infoMessage));
257}
258
259void MainWindow::show_session_error(
260 const QString text, const QString info_text)
261{
262 QMessageBox msg(this);
263 msg.setText(text);
264 msg.setInformativeText(info_text);
265 msg.setStandardButtons(QMessageBox::Ok);
266 msg.setIcon(QMessageBox::Warning);
267 msg.exec();
1d478458
JH
268}
269
2953961c
JH
270void MainWindow::on_actionOpen_triggered()
271{
1d478458 272 const QString file_name = QFileDialog::getOpenFileName(
2953961c
JH
273 this, tr("Open File"), "",
274 tr("Sigrok Sessions (*.sr)"));
92b139d0
JH
275 if (!file_name.isEmpty())
276 load_file(file_name);
2953961c
JH
277}
278
9663c82b
JH
279void MainWindow::on_actionConnect_triggered()
280{
107ca6d3 281 dialogs::Connect dlg(this, _device_manager);
5eb0fa13 282
dc0867ff
JH
283 // If the user selected a device, select it in the device list. Select the
284 // current device otherwise.
285 struct sr_dev_inst *const sdi = dlg.exec() ?
286 dlg.get_selected_device() : _session.get_device();
287
107ca6d3 288 update_device_list(sdi);
9663c82b
JH
289}
290
2a032dcb
JH
291void MainWindow::on_actionQuit_triggered()
292{
293 close();
294}
295
a429590b
JH
296void MainWindow::on_actionViewZoomIn_triggered()
297{
298 _view->zoom(1);
299}
300
301void MainWindow::on_actionViewZoomOut_triggered()
302{
303 _view->zoom(-1);
304}
305
e072efc8
JH
306void MainWindow::on_actionViewShowCursors_triggered()
307{
308 assert(_view);
b4d91e56
JH
309
310 const bool show = !_view->cursors_shown();
311 if(show)
312 _view->centre_cursors();
313
314 _view->show_cursors(show);
e072efc8
JH
315}
316
30236a54
JH
317void MainWindow::on_actionAbout_triggered()
318{
acda14b8 319 dialogs::About dlg(this);
40eb2ff4 320 dlg.exec();
30236a54 321}
274d4f13 322
5ac961e3
JH
323void MainWindow::device_selected()
324{
325 _session.set_device(_sampling_bar->get_selected_device());
326}
327
274d4f13
JH
328void MainWindow::run_stop()
329{
5b7cf66c
JH
330 switch(_session.get_capture_state()) {
331 case SigSession::Stopped:
d64d1596 332 _session.start_capture(_sampling_bar->get_record_length(),
f2edb557
JH
333 boost::bind(&MainWindow::session_error, this,
334 QString("Capture failed"), _1));
5b7cf66c
JH
335 break;
336
337 case SigSession::Running:
338 _session.stop_capture();
339 break;
340 }
274d4f13 341}
51e77110 342
6ac96c2e
JH
343void MainWindow::capture_state_changed(int state)
344{
345 _sampling_bar->set_sampling(state != SigSession::Stopped);
346}
347
51e77110 348} // namespace pv