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
22 #include <libsigrokdecode/libsigrokdecode.h>
25 #include <boost/bind.hpp>
31 #include <QApplication>
32 #include <QButtonGroup>
33 #include <QFileDialog>
34 #include <QMessageBox>
38 #include <QVBoxLayout>
41 #include "mainwindow.h"
43 #include "devicemanager.h"
44 #include "device/device.h"
45 #include "dialogs/about.h"
46 #include "dialogs/connect.h"
47 #include "dialogs/storeprogress.h"
48 #include "toolbars/samplingbar.h"
49 #include "view/logicsignal.h"
50 #include "view/view.h"
52 #include "widgets/decodermenu.h"
55 /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */
56 #define __STDC_FORMAT_MACROS
61 #include <libsigrok/libsigrok.h>
64 using std::shared_ptr;
72 MainWindow::MainWindow(DeviceManager &device_manager,
73 const char *open_file_name,
76 _device_manager(device_manager),
77 _session(device_manager)
81 const QString s(QString::fromUtf8(open_file_name));
82 QMetaObject::invokeMethod(this, "load_file",
88 void MainWindow::setup_ui()
90 setObjectName(QString::fromUtf8("MainWindow"));
94 // Set the window icon
96 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
97 QSize(), QIcon::Normal, QIcon::Off);
100 // Setup the central widget
101 _central_widget = new QWidget(this);
102 _vertical_layout = new QVBoxLayout(_central_widget);
103 _vertical_layout->setSpacing(6);
104 _vertical_layout->setContentsMargins(0, 0, 0, 0);
105 setCentralWidget(_central_widget);
107 _view = new pv::view::View(_session, this);
109 _vertical_layout->addWidget(_view);
111 // Setup the menu bar
112 QMenuBar *const menu_bar = new QMenuBar(this);
113 menu_bar->setGeometry(QRect(0, 0, 400, 25));
116 QMenu *const menu_file = new QMenu;
117 menu_file->setTitle(QApplication::translate(
118 "MainWindow", "&File", 0, QApplication::UnicodeUTF8));
120 QAction *const action_open = new QAction(this);
121 action_open->setText(QApplication::translate(
122 "MainWindow", "&Open...", 0, QApplication::UnicodeUTF8));
123 action_open->setIcon(QIcon::fromTheme("document-open",
124 QIcon(":/icons/document-open.png")));
125 action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
126 action_open->setObjectName(QString::fromUtf8("actionOpen"));
127 menu_file->addAction(action_open);
129 QAction *const action_save_as = new QAction(this);
130 action_save_as->setText(QApplication::translate(
131 "MainWindow", "&Save As...", 0, QApplication::UnicodeUTF8));
132 action_save_as->setIcon(QIcon::fromTheme("document-save-as",
133 QIcon(":/icons/document-save-as.png")));
134 action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
135 action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
136 menu_file->addAction(action_save_as);
138 menu_file->addSeparator();
140 QAction *const action_connect = new QAction(this);
141 action_connect->setText(QApplication::translate(
142 "MainWindow", "&Connect to Device...", 0,
143 QApplication::UnicodeUTF8));
144 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
145 menu_file->addAction(action_connect);
147 menu_file->addSeparator();
149 QAction *action_quit = new QAction(this);
150 action_quit->setText(QApplication::translate(
151 "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8));
152 action_quit->setIcon(QIcon::fromTheme("application-exit",
153 QIcon(":/icons/application-exit.png")));
154 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
155 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
156 menu_file->addAction(action_quit);
159 QMenu *menu_view = new QMenu;
160 menu_view->setTitle(QApplication::translate(
161 "MainWindow", "&View", 0, QApplication::UnicodeUTF8));
163 QAction *const action_view_zoom_in = new QAction(this);
164 action_view_zoom_in->setText(QApplication::translate(
165 "MainWindow", "Zoom &In", 0, QApplication::UnicodeUTF8));
166 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
167 QIcon(":/icons/zoom-in.png")));
168 // simply using Qt::Key_Plus shows no + in the menu
169 action_view_zoom_in->setShortcut(QKeySequence::ZoomIn);
170 action_view_zoom_in->setObjectName(
171 QString::fromUtf8("actionViewZoomIn"));
172 menu_view->addAction(action_view_zoom_in);
174 QAction *const action_view_zoom_out = new QAction(this);
175 action_view_zoom_out->setText(QApplication::translate(
176 "MainWindow", "Zoom &Out", 0, QApplication::UnicodeUTF8));
177 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
178 QIcon(":/icons/zoom-out.png")));
179 action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
180 action_view_zoom_out->setObjectName(
181 QString::fromUtf8("actionViewZoomOut"));
182 menu_view->addAction(action_view_zoom_out);
184 QAction *const action_view_zoom_fit = new QAction(this);
185 action_view_zoom_fit->setText(QApplication::translate(
186 "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8));
187 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
188 QIcon(":/icons/zoom-fit.png")));
189 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
190 action_view_zoom_fit->setObjectName(
191 QString::fromUtf8("actionViewZoomFit"));
192 menu_view->addAction(action_view_zoom_fit);
194 QAction *const action_view_zoom_one_to_one = new QAction(this);
195 action_view_zoom_one_to_one->setText(QApplication::translate(
196 "MainWindow", "Zoom to &One-to-One", 0,
197 QApplication::UnicodeUTF8));
198 action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
199 QIcon(":/icons/zoom-original.png")));
200 action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
201 action_view_zoom_one_to_one->setObjectName(
202 QString::fromUtf8("actionViewZoomOneToOne"));
203 menu_view->addAction(action_view_zoom_one_to_one);
205 menu_view->addSeparator();
207 QAction *action_view_show_cursors = new QAction(this);
208 action_view_show_cursors->setCheckable(true);
209 action_view_show_cursors->setChecked(_view->cursors_shown());
210 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
211 action_view_show_cursors->setObjectName(
212 QString::fromUtf8("actionViewShowCursors"));
213 action_view_show_cursors->setText(QApplication::translate(
214 "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
215 menu_view->addAction(action_view_show_cursors);
219 QMenu *const menu_decoders = new QMenu;
220 menu_decoders->setTitle(QApplication::translate(
221 "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
223 pv::widgets::DecoderMenu *const menu_decoders_add =
224 new pv::widgets::DecoderMenu(menu_decoders, true);
225 menu_decoders_add->setTitle(QApplication::translate(
226 "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
227 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
228 this, SLOT(add_decoder(srd_decoder*)));
230 menu_decoders->addMenu(menu_decoders_add);
234 QMenu *const menu_help = new QMenu;
235 menu_help->setTitle(QApplication::translate(
236 "MainWindow", "&Help", 0, QApplication::UnicodeUTF8));
238 QAction *const action_about = new QAction(this);
239 action_about->setObjectName(QString::fromUtf8("actionAbout"));
240 action_about->setText(QApplication::translate(
241 "MainWindow", "&About...", 0, QApplication::UnicodeUTF8));
242 menu_help->addAction(action_about);
244 menu_bar->addAction(menu_file->menuAction());
245 menu_bar->addAction(menu_view->menuAction());
247 menu_bar->addAction(menu_decoders->menuAction());
249 menu_bar->addAction(menu_help->menuAction());
251 setMenuBar(menu_bar);
252 QMetaObject::connectSlotsByName(this);
255 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
256 toolbar->addAction(action_open);
257 toolbar->addAction(action_save_as);
258 toolbar->addSeparator();
259 toolbar->addAction(action_view_zoom_in);
260 toolbar->addAction(action_view_zoom_out);
261 toolbar->addAction(action_view_zoom_fit);
262 toolbar->addAction(action_view_zoom_one_to_one);
265 // Setup the sampling bar
266 _sampling_bar = new toolbars::SamplingBar(_session, this);
268 // Populate the device list and select the initially selected device
269 update_device_list();
271 connect(_sampling_bar, SIGNAL(run_stop()), this,
273 addToolBar(_sampling_bar);
276 setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
277 QApplication::UnicodeUTF8));
279 // Setup _session events
280 connect(&_session, SIGNAL(capture_state_changed(int)), this,
281 SLOT(capture_state_changed(int)));
285 void MainWindow::session_error(
286 const QString text, const QString info_text)
288 QMetaObject::invokeMethod(this, "show_session_error",
289 Qt::QueuedConnection, Q_ARG(QString, text),
290 Q_ARG(QString, info_text));
293 void MainWindow::update_device_list()
295 assert(_sampling_bar);
297 shared_ptr<pv::device::DevInst> selected_device = _session.get_device();
298 list< shared_ptr<device::DevInst> > devices;
299 std::copy(_device_manager.devices().begin(),
300 _device_manager.devices().end(), std::back_inserter(devices));
302 if (std::find(devices.begin(), devices.end(), selected_device) ==
304 devices.push_back(selected_device);
305 assert(selected_device);
307 _sampling_bar->set_device_list(devices, selected_device);
310 void MainWindow::load_file(QString file_name)
312 const QString errorMessage(
313 QString("Failed to load file %1").arg(file_name));
314 const QString infoMessage;
317 _session.set_file(file_name.toStdString());
319 show_session_error(tr("Failed to load ") + file_name, e);
320 _session.set_default_device();
321 update_device_list();
325 update_device_list();
327 _session.start_capture(boost::bind(&MainWindow::session_error, this,
328 errorMessage, infoMessage));
331 void MainWindow::show_session_error(
332 const QString text, const QString info_text)
334 QMessageBox msg(this);
336 msg.setInformativeText(info_text);
337 msg.setStandardButtons(QMessageBox::Ok);
338 msg.setIcon(QMessageBox::Warning);
342 void MainWindow::on_actionOpen_triggered()
345 const QString file_name = QFileDialog::getOpenFileName(
346 this, tr("Open File"), "", tr(
347 "Sigrok Sessions (*.sr);;"
349 if (!file_name.isEmpty())
350 load_file(file_name);
353 void MainWindow::on_actionSaveAs_triggered()
355 using pv::dialogs::StoreProgress;
357 // Stop any currently running capture session
358 _session.stop_capture();
361 const QString file_name = QFileDialog::getSaveFileName(
362 this, tr("Save File"), "", tr("Sigrok Sessions (*.sr)"));
364 if (file_name.isEmpty())
367 StoreProgress *dlg = new StoreProgress(file_name, _session, this);
371 void MainWindow::on_actionConnect_triggered()
373 // Stop any currently running capture session
374 _session.stop_capture();
376 dialogs::Connect dlg(this, _device_manager);
378 // If the user selected a device, select it in the device list. Select the
379 // current device otherwise.
381 _session.set_device(dlg.get_selected_device());
383 update_device_list();
386 void MainWindow::on_actionQuit_triggered()
391 void MainWindow::on_actionViewZoomIn_triggered()
396 void MainWindow::on_actionViewZoomOut_triggered()
401 void MainWindow::on_actionViewZoomFit_triggered()
406 void MainWindow::on_actionViewZoomOneToOne_triggered()
408 _view->zoom_one_to_one();
411 void MainWindow::on_actionViewShowCursors_triggered()
415 const bool show = !_view->cursors_shown();
417 _view->centre_cursors();
419 _view->show_cursors(show);
422 void MainWindow::on_actionAbout_triggered()
424 dialogs::About dlg(this);
428 void MainWindow::add_decoder(srd_decoder *decoder)
432 _session.add_decoder(decoder);
438 void MainWindow::run_stop()
440 switch(_session.get_capture_state()) {
441 case SigSession::Stopped:
442 _session.start_capture(
443 boost::bind(&MainWindow::session_error, this,
444 QString("Capture failed"), _1));
447 case SigSession::AwaitingTrigger:
448 case SigSession::Running:
449 _session.stop_capture();
454 void MainWindow::capture_state_changed(int state)
456 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);