2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #ifdef ENABLE_SIGROKDECODE
22 #include <libsigrokdecode/libsigrokdecode.h>
25 #include <boost/bind.hpp>
26 #include <boost/foreach.hpp>
29 #include <QApplication>
30 #include <QButtonGroup>
31 #include <QFileDialog>
32 #include <QMessageBox>
36 #include <QVBoxLayout>
39 #include "mainwindow.h"
41 #include "devicemanager.h"
42 #include "dialogs/about.h"
43 #include "dialogs/connect.h"
44 #include "toolbars/samplingbar.h"
45 #include "view/view.h"
47 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
48 #define __STDC_FORMAT_MACROS
53 #include <libsigrok/libsigrok.h>
59 MainWindow::MainWindow(DeviceManager &device_manager,
60 const char *open_file_name,
63 _device_manager(device_manager),
64 _session(device_manager)
68 const QString s(QString::fromUtf8(open_file_name));
69 QMetaObject::invokeMethod(this, "load_file",
75 void MainWindow::setup_ui()
77 setObjectName(QString::fromUtf8("MainWindow"));
81 // Set the window icon
83 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
84 QSize(), QIcon::Normal, QIcon::Off);
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);
94 _view = new pv::view::View(_session, this);
95 _vertical_layout->addWidget(_view);
98 _menu_bar = new QMenuBar(this);
99 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
102 _menu_file = new QMenu(_menu_bar);
103 _menu_file->setTitle(QApplication::translate(
104 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
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);
114 _menu_file->addSeparator();
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);
123 _menu_file->addSeparator();
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);
135 _menu_view = new QMenu(_menu_bar);
136 _menu_view->setTitle(QApplication::translate(
137 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
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);
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);
157 _menu_view->addSeparator();
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);
170 _menu_help = new QMenu(_menu_bar);
171 _menu_help->setTitle(QApplication::translate(
172 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
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);
180 _menu_bar->addAction(_menu_file->menuAction());
181 _menu_bar->addAction(_menu_view->menuAction());
182 _menu_bar->addAction(_menu_help->menuAction());
184 setMenuBar(_menu_bar);
185 QMetaObject::connectSlotsByName(this);
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);
195 // Setup the sampling bar
196 _sampling_bar = new toolbars::SamplingBar(this);
198 // Populate the device list and select the initially selected device
199 update_device_list();
201 connect(_sampling_bar, SIGNAL(device_selected()), this,
202 SLOT(device_selected()));
203 connect(_sampling_bar, SIGNAL(run_stop()), this,
205 addToolBar(_sampling_bar);
208 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
209 QApplication::UnicodeUTF8));
211 // Setup _session events
212 connect(&_session, SIGNAL(capture_state_changed(int)), this,
213 SLOT(capture_state_changed(int)));
217 void MainWindow::session_error(
218 const QString text, const QString info_text)
220 QMetaObject::invokeMethod(this, "show_session_error",
221 Qt::QueuedConnection, Q_ARG(QString, text),
222 Q_ARG(QString, info_text));
225 void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
227 assert(_sampling_bar);
229 const list<sr_dev_inst*> &devices = _device_manager.devices();
230 _sampling_bar->set_device_list(devices);
232 if (!selected_device && !devices.empty()) {
233 // Fall back to the first device in the list.
234 selected_device = devices.front();
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;
243 if (selected_device) {
244 _sampling_bar->set_selected_device(selected_device);
245 _session.set_device(selected_device);
249 void MainWindow::load_file(QString file_name)
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));
259 void MainWindow::show_session_error(
260 const QString text, const QString info_text)
262 QMessageBox msg(this);
264 msg.setInformativeText(info_text);
265 msg.setStandardButtons(QMessageBox::Ok);
266 msg.setIcon(QMessageBox::Warning);
270 void MainWindow::on_actionOpen_triggered()
272 const QString file_name = QFileDialog::getOpenFileName(
273 this, tr("Open File"), "",
274 tr("Sigrok Sessions (*.sr)"));
275 if (!file_name.isEmpty())
276 load_file(file_name);
279 void MainWindow::on_actionConnect_triggered()
281 // Stop any currently running capture session
282 _session.stop_capture();
284 dialogs::Connect dlg(this, _device_manager);
286 // If the user selected a device, select it in the device list. Select the
287 // current device otherwise.
288 struct sr_dev_inst *const sdi = dlg.exec() ?
289 dlg.get_selected_device() : _session.get_device();
291 update_device_list(sdi);
294 void MainWindow::on_actionQuit_triggered()
299 void MainWindow::on_actionViewZoomIn_triggered()
304 void MainWindow::on_actionViewZoomOut_triggered()
309 void MainWindow::on_actionViewShowCursors_triggered()
313 const bool show = !_view->cursors_shown();
315 _view->centre_cursors();
317 _view->show_cursors(show);
320 void MainWindow::on_actionAbout_triggered()
322 dialogs::About dlg(this);
326 void MainWindow::device_selected()
328 _session.set_device(_sampling_bar->get_selected_device());
331 void MainWindow::run_stop()
333 switch(_session.get_capture_state()) {
334 case SigSession::Stopped:
335 _session.start_capture(_sampling_bar->get_record_length(),
336 boost::bind(&MainWindow::session_error, this,
337 QString("Capture failed"), _1));
340 case SigSession::Running:
341 _session.stop_capture();
346 void MainWindow::capture_state_changed(int state)
348 _sampling_bar->set_sampling(state != SigSession::Stopped);