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/contextbar.h"
45 #include "toolbars/samplingbar.h"
46 #include "view/view.h"
48 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
49 #define __STDC_FORMAT_MACROS
54 #include <libsigrok/libsigrok.h>
60 MainWindow::MainWindow(DeviceManager &device_manager,
61 const char *open_file_name,
64 _device_manager(device_manager),
65 _session(device_manager)
69 const QString s(QString::fromUtf8(open_file_name));
70 QMetaObject::invokeMethod(this, "load_file",
76 void MainWindow::setup_ui()
78 setObjectName(QString::fromUtf8("MainWindow"));
82 // Set the window icon
84 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
85 QSize(), QIcon::Normal, QIcon::Off);
88 // Setup the central widget
89 _central_widget = new QWidget(this);
90 _vertical_layout = new QVBoxLayout(_central_widget);
91 _vertical_layout->setSpacing(6);
92 _vertical_layout->setContentsMargins(0, 0, 0, 0);
93 setCentralWidget(_central_widget);
95 _view = new pv::view::View(_session, this);
96 _vertical_layout->addWidget(_view);
99 _menu_bar = new QMenuBar(this);
100 _menu_bar->setGeometry(QRect(0, 0, 400, 25));
103 _menu_file = new QMenu(_menu_bar);
104 _menu_file->setTitle(QApplication::translate(
105 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
107 _action_open = new QAction(this);
108 _action_open->setText(QApplication::translate(
109 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
110 _action_open->setIcon(QIcon::fromTheme("document-open",
111 QIcon(":/icons/document-open.png")));
112 _action_open->setObjectName(QString::fromUtf8("actionOpen"));
113 _menu_file->addAction(_action_open);
115 _menu_file->addSeparator();
117 _action_connect = new QAction(this);
118 _action_connect->setText(QApplication::translate(
119 "MainWindow", "&Connect to Device...", 0,
120 QApplication::UnicodeUTF8));
121 _action_connect->setObjectName(QString::fromUtf8("actionConnect"));
122 _menu_file->addAction(_action_connect);
124 _menu_file->addSeparator();
126 _action_quit = new QAction(this);
127 _action_quit->setText(QApplication::translate(
128 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
129 _action_quit->setIcon(QIcon::fromTheme("application-exit",
130 QIcon(":/icons/application-exit.png")));
131 _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
132 _action_quit->setObjectName(QString::fromUtf8("actionQuit"));
133 _menu_file->addAction(_action_quit);
136 _menu_view = new QMenu(_menu_bar);
137 _menu_view->setTitle(QApplication::translate(
138 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
140 _action_view_zoom_in = new QAction(this);
141 _action_view_zoom_in->setText(QApplication::translate(
142 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
143 _action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
144 QIcon(":/icons/zoom-in.png")));
145 _action_view_zoom_in->setObjectName(
146 QString::fromUtf8("actionViewZoomIn"));
147 _menu_view->addAction(_action_view_zoom_in);
149 _action_view_zoom_out = new QAction(this);
150 _action_view_zoom_out->setText(QApplication::translate(
151 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
152 _action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
153 QIcon(":/icons/zoom-out.png")));
154 _action_view_zoom_out->setObjectName(
155 QString::fromUtf8("actionViewZoomOut"));
156 _menu_view->addAction(_action_view_zoom_out);
158 _menu_view->addSeparator();
160 _action_view_show_cursors = new QAction(this);
161 _action_view_show_cursors->setCheckable(true);
162 _action_view_show_cursors->setChecked(_view->cursors_shown());
163 _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
164 _action_view_show_cursors->setObjectName(
165 QString::fromUtf8("actionViewShowCursors"));
166 _action_view_show_cursors->setText(QApplication::translate(
167 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
168 _menu_view->addAction(_action_view_show_cursors);
171 _menu_help = new QMenu(_menu_bar);
172 _menu_help->setTitle(QApplication::translate(
173 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
175 _action_about = new QAction(this);
176 _action_about->setObjectName(QString::fromUtf8("actionAbout"));
177 _action_about->setText(QApplication::translate(
178 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
179 _menu_help->addAction(_action_about);
181 _menu_bar->addAction(_menu_file->menuAction());
182 _menu_bar->addAction(_menu_view->menuAction());
183 _menu_bar->addAction(_menu_help->menuAction());
185 setMenuBar(_menu_bar);
186 QMetaObject::connectSlotsByName(this);
189 _toolbar = new QToolBar(tr("Main Toolbar"), this);
190 _toolbar->addAction(_action_open);
191 _toolbar->addSeparator();
192 _toolbar->addAction(_action_view_zoom_in);
193 _toolbar->addAction(_action_view_zoom_out);
194 addToolBar(_toolbar);
196 // Setup the sampling bar
197 _sampling_bar = new toolbars::SamplingBar(this);
199 // Populate the device list and select the initially selected device
200 update_device_list();
202 connect(_sampling_bar, SIGNAL(device_selected()), this,
203 SLOT(device_selected()));
204 connect(_sampling_bar, SIGNAL(run_stop()), this,
206 addToolBar(_sampling_bar);
208 // Setup the context bar
209 _context_bar = new toolbars::ContextBar(this);
210 addToolBar(_context_bar);
211 insertToolBarBreak(_context_bar);
214 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
215 QApplication::UnicodeUTF8));
217 // Setup _session events
218 connect(&_session, SIGNAL(capture_state_changed(int)), this,
219 SLOT(capture_state_changed(int)));
223 void MainWindow::session_error(
224 const QString text, const QString info_text)
226 QMetaObject::invokeMethod(this, "show_session_error",
227 Qt::QueuedConnection, Q_ARG(QString, text),
228 Q_ARG(QString, info_text));
231 void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
233 assert(_sampling_bar);
235 const list<sr_dev_inst*> &devices = _device_manager.devices();
236 _sampling_bar->set_device_list(devices);
238 if (!selected_device && !devices.empty()) {
239 // Fall back to the first device in the list.
240 selected_device = devices.front();
242 // Try and find the demo device and select that by default
243 BOOST_FOREACH (struct sr_dev_inst *sdi, devices)
244 if (strcmp(sdi->driver->name, "demo") == 0) {
245 selected_device = sdi;
249 if (selected_device) {
250 _sampling_bar->set_selected_device(selected_device);
251 _session.set_device(selected_device);
255 void MainWindow::load_file(QString file_name)
257 const QString errorMessage(
258 QString("Failed to load file %1").arg(file_name));
259 const QString infoMessage;
260 _session.load_file(file_name.toStdString(),
261 boost::bind(&MainWindow::session_error, this,
262 errorMessage, infoMessage));
265 void MainWindow::show_session_error(
266 const QString text, const QString info_text)
268 QMessageBox msg(this);
270 msg.setInformativeText(info_text);
271 msg.setStandardButtons(QMessageBox::Ok);
272 msg.setIcon(QMessageBox::Warning);
276 void MainWindow::on_actionOpen_triggered()
278 // Enumerate the file formats
279 QString filters(tr("Sigrok Sessions (*.sr)"));
280 filters.append(tr(";;All Files (*.*)"));
283 const QString file_name = QFileDialog::getOpenFileName(
284 this, tr("Open File"), "", filters);
285 if (!file_name.isEmpty())
286 load_file(file_name);
289 void MainWindow::on_actionConnect_triggered()
291 // Stop any currently running capture session
292 _session.stop_capture();
294 dialogs::Connect dlg(this, _device_manager);
296 // If the user selected a device, select it in the device list. Select the
297 // current device otherwise.
298 struct sr_dev_inst *const sdi = dlg.exec() ?
299 dlg.get_selected_device() : _session.get_device();
301 update_device_list(sdi);
304 void MainWindow::on_actionQuit_triggered()
309 void MainWindow::on_actionViewZoomIn_triggered()
314 void MainWindow::on_actionViewZoomOut_triggered()
319 void MainWindow::on_actionViewShowCursors_triggered()
323 const bool show = !_view->cursors_shown();
325 _view->centre_cursors();
327 _view->show_cursors(show);
330 void MainWindow::on_actionAbout_triggered()
332 dialogs::About dlg(this);
336 void MainWindow::device_selected()
338 _session.set_device(_sampling_bar->get_selected_device());
341 void MainWindow::run_stop()
343 switch(_session.get_capture_state()) {
344 case SigSession::Stopped:
345 _session.start_capture(_sampling_bar->get_record_length(),
346 boost::bind(&MainWindow::session_error, this,
347 QString("Capture failed"), _1));
350 case SigSession::Running:
351 _session.stop_capture();
356 void MainWindow::capture_state_changed(int state)
358 _sampling_bar->set_sampling(state != SigSession::Stopped);