]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
Make sigrokdecode a non-optional dependancy
[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#include <libsigrokdecode/libsigrokdecode.h>
22
23#include <boost/bind.hpp>
24#include <boost/foreach.hpp>
25
26#include <QAction>
27#include <QApplication>
28#include <QButtonGroup>
29#include <QFileDialog>
30#include <QMessageBox>
31#include <QMenu>
32#include <QMenuBar>
33#include <QStatusBar>
34#include <QVBoxLayout>
35#include <QWidget>
36
37#include "mainwindow.h"
38
39#include "devicemanager.h"
40#include "dialogs/about.h"
41#include "dialogs/connect.h"
42#include "toolbars/contextbar.h"
43#include "toolbars/samplingbar.h"
44#include "view/view.h"
45
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>
53
54using namespace boost;
55using namespace std;
56
57namespace pv {
58
59namespace view {
60class SelectableItem;
61}
62
63MainWindow::MainWindow(DeviceManager &device_manager,
64 const char *open_file_name,
65 QWidget *parent) :
66 QMainWindow(parent),
67 _device_manager(device_manager),
68 _session(device_manager)
69{
70 setup_ui();
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 }
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
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);
99 connect(_view, SIGNAL(selection_changed()), this,
100 SLOT(view_selection_changed()));
101
102 _vertical_layout->addWidget(_view);
103
104 // Setup the menu bar
105 _menu_bar = new QMenuBar(this);
106 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
107
108 // File Menu
109 _menu_file = new QMenu(_menu_bar);
110 _menu_file->setTitle(QApplication::translate(
111 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
112
113 _action_open = new QAction(this);
114 _action_open->setText(QApplication::translate(
115 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
116 _action_open->setIcon(QIcon::fromTheme("document-open",
117 QIcon(":/icons/document-open.png")));
118 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
119 _menu_file->addAction(_action_open);
120
121 _menu_file->addSeparator();
122
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
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")));
137 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
138 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
139 _menu_file->addAction(_action_quit);
140
141 // View Menu
142 _menu_view = new QMenu(_menu_bar);
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"));
153 _menu_view->addAction(_action_view_zoom_in);
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"));
162 _menu_view->addAction(_action_view_zoom_out);
163
164 _menu_view->addSeparator();
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());
169 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
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));
174 _menu_view->addAction(_action_view_show_cursors);
175
176 // Help Menu
177 _menu_help = new QMenu(_menu_bar);
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));
185 _menu_help->addAction(_action_about);
186
187 _menu_bar->addAction(_menu_file->menuAction());
188 _menu_bar->addAction(_menu_view->menuAction());
189 _menu_bar->addAction(_menu_help->menuAction());
190
191 setMenuBar(_menu_bar);
192 QMetaObject::connectSlotsByName(this);
193
194 // Setup the toolbar
195 _toolbar = new QToolBar(tr("Main Toolbar"), this);
196 _toolbar->addAction(_action_open);
197 _toolbar->addSeparator();
198 _toolbar->addAction(_action_view_zoom_in);
199 _toolbar->addAction(_action_view_zoom_out);
200 addToolBar(_toolbar);
201
202 // Setup the sampling bar
203 _sampling_bar = new toolbars::SamplingBar(this);
204
205 // Populate the device list and select the initially selected device
206 update_device_list();
207
208 connect(_sampling_bar, SIGNAL(device_selected()), this,
209 SLOT(device_selected()));
210 connect(_sampling_bar, SIGNAL(run_stop()), this,
211 SLOT(run_stop()));
212 addToolBar(_sampling_bar);
213
214 // Setup the context bar
215 _context_bar = new toolbars::ContextBar(this);
216 addToolBar(_context_bar);
217
218 // Set the title
219 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
220 QApplication::UnicodeUTF8));
221
222 // Setup _session events
223 connect(&_session, SIGNAL(capture_state_changed(int)), this,
224 SLOT(capture_state_changed(int)));
225
226}
227
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
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
260void MainWindow::load_file(QString file_name)
261{
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();
279}
280
281void MainWindow::on_actionOpen_triggered()
282{
283 // Enumerate the file formats
284 QString filters(tr("Sigrok Sessions (*.sr)"));
285 filters.append(tr(";;All Files (*.*)"));
286
287 // Show the dialog
288 const QString file_name = QFileDialog::getOpenFileName(
289 this, tr("Open File"), "", filters);
290 if (!file_name.isEmpty())
291 load_file(file_name);
292}
293
294void MainWindow::on_actionConnect_triggered()
295{
296 // Stop any currently running capture session
297 _session.stop_capture();
298
299 dialogs::Connect dlg(this, _device_manager);
300
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
306 update_device_list(sdi);
307}
308
309void MainWindow::on_actionQuit_triggered()
310{
311 close();
312}
313
314void MainWindow::on_actionViewZoomIn_triggered()
315{
316 _view->zoom(1);
317}
318
319void MainWindow::on_actionViewZoomOut_triggered()
320{
321 _view->zoom(-1);
322}
323
324void MainWindow::on_actionViewShowCursors_triggered()
325{
326 assert(_view);
327
328 const bool show = !_view->cursors_shown();
329 if(show)
330 _view->centre_cursors();
331
332 _view->show_cursors(show);
333}
334
335void MainWindow::on_actionAbout_triggered()
336{
337 dialogs::About dlg(this);
338 dlg.exec();
339}
340
341void MainWindow::device_selected()
342{
343 _session.set_device(_sampling_bar->get_selected_device());
344}
345
346void MainWindow::run_stop()
347{
348 switch(_session.get_capture_state()) {
349 case SigSession::Stopped:
350 _session.start_capture(_sampling_bar->get_record_length(),
351 boost::bind(&MainWindow::session_error, this,
352 QString("Capture failed"), _1));
353 break;
354
355 case SigSession::AwaitingTrigger:
356 case SigSession::Running:
357 _session.stop_capture();
358 break;
359 }
360}
361
362void MainWindow::capture_state_changed(int state)
363{
364 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);
365}
366
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
376} // namespace pv