]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
Make SigSession release it's device on destruction
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#ifdef ENABLE_SIGROKDECODE
22#include <libsigrokdecode/libsigrokdecode.h>
23#endif
24
25#include <boost/bind.hpp>
26#include <boost/foreach.hpp>
27
28#include <QAction>
29#include <QApplication>
30#include <QButtonGroup>
31#include <QFileDialog>
32#include <QMessageBox>
33#include <QMenu>
34#include <QMenuBar>
35#include <QStatusBar>
36#include <QVBoxLayout>
37#include <QWidget>
38
39#include "mainwindow.h"
40
41#include "devicemanager.h"
42#include "dialogs/about.h"
43#include "dialogs/connect.h"
44#include "toolbars/samplingbar.h"
45#include "view/view.h"
46
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>
54
55using namespace std;
56
57namespace pv {
58
59MainWindow::MainWindow(DeviceManager &device_manager,
60 const char *open_file_name,
61 QWidget *parent) :
62 QMainWindow(parent),
63 _device_manager(device_manager),
64 _session(device_manager)
65{
66 setup_ui();
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 }
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
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
97 // Setup the menu bar
98 _menu_bar = new QMenuBar(this);
99 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
100
101 // File Menu
102 _menu_file = new QMenu(_menu_bar);
103 _menu_file->setTitle(QApplication::translate(
104 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
105
106 _action_open = new QAction(this);
107 _action_open->setText(QApplication::translate(
108 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
109 _action_open->setIcon(QIcon::fromTheme("document-open",
110 QIcon(":/icons/document-open.png")));
111 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
112 _menu_file->addAction(_action_open);
113
114 _menu_file->addSeparator();
115
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
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")));
130 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
131 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
132 _menu_file->addAction(_action_quit);
133
134 // View Menu
135 _menu_view = new QMenu(_menu_bar);
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"));
146 _menu_view->addAction(_action_view_zoom_in);
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"));
155 _menu_view->addAction(_action_view_zoom_out);
156
157 _menu_view->addSeparator();
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());
162 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
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));
167 _menu_view->addAction(_action_view_show_cursors);
168
169 // Help Menu
170 _menu_help = new QMenu(_menu_bar);
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));
178 _menu_help->addAction(_action_about);
179
180 _menu_bar->addAction(_menu_file->menuAction());
181 _menu_bar->addAction(_menu_view->menuAction());
182 _menu_bar->addAction(_menu_help->menuAction());
183
184 setMenuBar(_menu_bar);
185 QMetaObject::connectSlotsByName(this);
186
187 // Setup the toolbar
188 _toolbar = new QToolBar(tr("Main Toolbar"), this);
189 _toolbar->addAction(_action_open);
190 _toolbar->addSeparator();
191 _toolbar->addAction(_action_view_zoom_in);
192 _toolbar->addAction(_action_view_zoom_out);
193 addToolBar(_toolbar);
194
195 // Setup the sampling bar
196 _sampling_bar = new toolbars::SamplingBar(this);
197
198 // Populate the device list and select the initially selected device
199 update_device_list();
200
201 connect(_sampling_bar, SIGNAL(device_selected()), this,
202 SLOT(device_selected()));
203 connect(_sampling_bar, SIGNAL(run_stop()), this,
204 SLOT(run_stop()));
205 addToolBar(_sampling_bar);
206
207 // Set the title
208 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
209 QApplication::UnicodeUTF8));
210
211 // Setup _session events
212 connect(&_session, SIGNAL(capture_state_changed(int)), this,
213 SLOT(capture_state_changed(int)));
214
215}
216
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
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
249void MainWindow::load_file(QString file_name)
250{
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();
268}
269
270void MainWindow::on_actionOpen_triggered()
271{
272 // Enumerate the file formats
273 QString filters(tr("Sigrok Sessions (*.sr)"));
274 filters.append(tr(";;All Files (*.*)"));
275
276 // Show the dialog
277 const QString file_name = QFileDialog::getOpenFileName(
278 this, tr("Open File"), "", filters);
279 if (!file_name.isEmpty())
280 load_file(file_name);
281}
282
283void MainWindow::on_actionConnect_triggered()
284{
285 // Stop any currently running capture session
286 _session.stop_capture();
287
288 dialogs::Connect dlg(this, _device_manager);
289
290 // If the user selected a device, select it in the device list. Select the
291 // current device otherwise.
292 struct sr_dev_inst *const sdi = dlg.exec() ?
293 dlg.get_selected_device() : _session.get_device();
294
295 update_device_list(sdi);
296}
297
298void MainWindow::on_actionQuit_triggered()
299{
300 close();
301}
302
303void MainWindow::on_actionViewZoomIn_triggered()
304{
305 _view->zoom(1);
306}
307
308void MainWindow::on_actionViewZoomOut_triggered()
309{
310 _view->zoom(-1);
311}
312
313void MainWindow::on_actionViewShowCursors_triggered()
314{
315 assert(_view);
316
317 const bool show = !_view->cursors_shown();
318 if(show)
319 _view->centre_cursors();
320
321 _view->show_cursors(show);
322}
323
324void MainWindow::on_actionAbout_triggered()
325{
326 dialogs::About dlg(this);
327 dlg.exec();
328}
329
330void MainWindow::device_selected()
331{
332 _session.set_device(_sampling_bar->get_selected_device());
333}
334
335void MainWindow::run_stop()
336{
337 switch(_session.get_capture_state()) {
338 case SigSession::Stopped:
339 _session.start_capture(_sampling_bar->get_record_length(),
340 boost::bind(&MainWindow::session_error, this,
341 QString("Capture failed"), _1));
342 break;
343
344 case SigSession::Running:
345 _session.stop_capture();
346 break;
347 }
348}
349
350void MainWindow::capture_state_changed(int state)
351{
352 _sampling_bar->set_sampling(state != SigSession::Stopped);
353}
354
355} // namespace pv