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
24 #include <libsigrokdecode/libsigrokdecode.h>
31 #include <QApplication>
32 #include <QButtonGroup>
33 #include <QCloseEvent>
34 #include <QFileDialog>
35 #include <QMessageBox>
40 #include <QVBoxLayout>
43 #include "mainwindow.h"
45 #include "devicemanager.h"
46 #include "dialogs/about.h"
47 #include "dialogs/connect.h"
48 #include "dialogs/storeprogress.h"
49 #include "toolbars/samplingbar.h"
50 #include "view/logicsignal.h"
51 #include "view/view.h"
53 #include "widgets/decodermenu.h"
60 #include <libsigrok/libsigrok.hpp>
65 using std::shared_ptr;
70 using sigrok::HardwareDevice;
78 const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
79 const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
81 MainWindow::MainWindow(DeviceManager &device_manager,
82 const char *open_file_name,
85 _device_manager(device_manager),
86 _session(device_manager)
89 restore_ui_settings();
91 const QString s(QString::fromUtf8(open_file_name));
92 QMetaObject::invokeMethod(this, "load_file",
98 void MainWindow::setup_ui()
100 setObjectName(QString::fromUtf8("MainWindow"));
102 // Set the window icon
104 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
105 QSize(), QIcon::Normal, QIcon::Off);
108 // Setup the central widget
109 _central_widget = new QWidget(this);
110 _vertical_layout = new QVBoxLayout(_central_widget);
111 _vertical_layout->setSpacing(6);
112 _vertical_layout->setContentsMargins(0, 0, 0, 0);
113 setCentralWidget(_central_widget);
115 _view = new pv::view::View(_session, this);
117 _vertical_layout->addWidget(_view);
119 // Setup the menu bar
120 QMenuBar *const menu_bar = new QMenuBar(this);
121 menu_bar->setGeometry(QRect(0, 0, 400, 25));
124 QMenu *const menu_file = new QMenu;
125 menu_file->setTitle(tr("&File"));
127 QAction *const action_open = new QAction(this);
128 action_open->setText(tr("&Open..."));
129 action_open->setIcon(QIcon::fromTheme("document-open",
130 QIcon(":/icons/document-open.png")));
131 action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
132 action_open->setObjectName(QString::fromUtf8("actionOpen"));
133 menu_file->addAction(action_open);
135 QAction *const action_save_as = new QAction(this);
136 action_save_as->setText(tr("&Save As..."));
137 action_save_as->setIcon(QIcon::fromTheme("document-save-as",
138 QIcon(":/icons/document-save-as.png")));
139 action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
140 action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
141 menu_file->addAction(action_save_as);
143 menu_file->addSeparator();
145 QAction *const action_connect = new QAction(this);
146 action_connect->setText(tr("&Connect to Device..."));
147 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
148 menu_file->addAction(action_connect);
150 menu_file->addSeparator();
152 QAction *action_quit = new QAction(this);
153 action_quit->setText(tr("&Quit"));
154 action_quit->setIcon(QIcon::fromTheme("application-exit",
155 QIcon(":/icons/application-exit.png")));
156 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
157 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
158 menu_file->addAction(action_quit);
161 QMenu *menu_view = new QMenu;
162 menu_view->setTitle(tr("&View"));
164 QAction *const action_view_zoom_in = new QAction(this);
165 action_view_zoom_in->setText(tr("Zoom &In"));
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(tr("Zoom &Out"));
176 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
177 QIcon(":/icons/zoom-out.png")));
178 action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
179 action_view_zoom_out->setObjectName(
180 QString::fromUtf8("actionViewZoomOut"));
181 menu_view->addAction(action_view_zoom_out);
183 QAction *const action_view_zoom_fit = new QAction(this);
184 action_view_zoom_fit->setText(tr("Zoom to &Fit"));
185 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
186 QIcon(":/icons/zoom-fit.png")));
187 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
188 action_view_zoom_fit->setObjectName(
189 QString::fromUtf8("actionViewZoomFit"));
190 menu_view->addAction(action_view_zoom_fit);
192 QAction *const action_view_zoom_one_to_one = new QAction(this);
193 action_view_zoom_one_to_one->setText(tr("Zoom to &One-to-One"));
194 action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
195 QIcon(":/icons/zoom-original.png")));
196 action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
197 action_view_zoom_one_to_one->setObjectName(
198 QString::fromUtf8("actionViewZoomOneToOne"));
199 menu_view->addAction(action_view_zoom_one_to_one);
201 menu_view->addSeparator();
203 QAction *action_view_show_cursors = new QAction(this);
204 action_view_show_cursors->setCheckable(true);
205 action_view_show_cursors->setChecked(_view->cursors_shown());
206 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
207 action_view_show_cursors->setObjectName(
208 QString::fromUtf8("actionViewShowCursors"));
209 action_view_show_cursors->setText(tr("Show &Cursors"));
210 menu_view->addAction(action_view_show_cursors);
214 QMenu *const menu_decoders = new QMenu;
215 menu_decoders->setTitle(tr("&Decoders"));
217 pv::widgets::DecoderMenu *const menu_decoders_add =
218 new pv::widgets::DecoderMenu(menu_decoders, true);
219 menu_decoders_add->setTitle(tr("&Add"));
220 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
221 this, SLOT(add_decoder(srd_decoder*)));
223 menu_decoders->addMenu(menu_decoders_add);
227 QMenu *const menu_help = new QMenu;
228 menu_help->setTitle(tr("&Help"));
230 QAction *const action_about = new QAction(this);
231 action_about->setObjectName(QString::fromUtf8("actionAbout"));
232 action_about->setText(tr("&About..."));
233 menu_help->addAction(action_about);
235 menu_bar->addAction(menu_file->menuAction());
236 menu_bar->addAction(menu_view->menuAction());
238 menu_bar->addAction(menu_decoders->menuAction());
240 menu_bar->addAction(menu_help->menuAction());
242 setMenuBar(menu_bar);
243 QMetaObject::connectSlotsByName(this);
246 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
247 toolbar->setObjectName(QString::fromUtf8("MainToolbar"));
248 toolbar->addAction(action_open);
249 toolbar->addAction(action_save_as);
250 toolbar->addSeparator();
251 toolbar->addAction(action_view_zoom_in);
252 toolbar->addAction(action_view_zoom_out);
253 toolbar->addAction(action_view_zoom_fit);
254 toolbar->addAction(action_view_zoom_one_to_one);
257 // Setup the sampling bar
258 _sampling_bar = new toolbars::SamplingBar(_session, this);
260 // Populate the device list and select the initially selected device
261 update_device_list();
263 connect(_sampling_bar, SIGNAL(run_stop()), this,
265 addToolBar(_sampling_bar);
268 setWindowTitle(tr("PulseView"));
270 // Setup _session events
271 connect(&_session, SIGNAL(capture_state_changed(int)), this,
272 SLOT(capture_state_changed(int)));
275 void MainWindow::save_ui_settings()
279 map<string, string> dev_info;
280 list<string> key_list;
282 settings.beginGroup("MainWindow");
283 settings.setValue("state", saveState());
284 settings.setValue("geometry", saveGeometry());
287 if (_session.device()) {
288 settings.beginGroup("Device");
289 key_list.push_back("vendor");
290 key_list.push_back("model");
291 key_list.push_back("version");
292 key_list.push_back("serial_num");
293 key_list.push_back("connection_id");
295 dev_info = _device_manager.get_device_info(
298 for (string key : key_list) {
300 if (dev_info.count(key))
301 settings.setValue(QString::fromUtf8(key.c_str()),
302 QString::fromUtf8(dev_info.at(key).c_str()));
304 settings.remove(QString::fromUtf8(key.c_str()));
311 void MainWindow::restore_ui_settings()
315 shared_ptr<HardwareDevice> device;
317 map<string, string> dev_info;
318 list<string> key_list;
321 settings.beginGroup("MainWindow");
323 if (settings.contains("geometry")) {
324 restoreGeometry(settings.value("geometry").toByteArray());
325 restoreState(settings.value("state").toByteArray());
331 // Re-select last used device if possible.
332 settings.beginGroup("Device");
333 key_list.push_back("vendor");
334 key_list.push_back("model");
335 key_list.push_back("version");
336 key_list.push_back("serial_num");
337 key_list.push_back("connection_id");
339 for (string key : key_list) {
340 if (!settings.contains(QString::fromUtf8(key.c_str())))
343 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
345 if (value.size() > 0)
346 dev_info.insert(std::make_pair(key, value));
349 device = _device_manager.find_device_from_info(dev_info);
352 _session.set_device(device);
353 update_device_list();
359 void MainWindow::session_error(
360 const QString text, const QString info_text)
362 QMetaObject::invokeMethod(this, "show_session_error",
363 Qt::QueuedConnection, Q_ARG(QString, text),
364 Q_ARG(QString, info_text));
367 void MainWindow::update_device_list()
369 assert(_sampling_bar);
371 shared_ptr<Device> selected_device = _session.device();
372 list< shared_ptr<Device> > devices;
374 if (_device_manager.devices().size() == 0)
377 std::copy(_device_manager.devices().begin(),
378 _device_manager.devices().end(), std::back_inserter(devices));
380 if (std::find(devices.begin(), devices.end(), selected_device) ==
382 devices.push_back(selected_device);
383 assert(selected_device);
385 list< pair< shared_ptr<Device>, string> > device_list;
387 for (auto device : devices)
388 device_list.push_back(make_pair(
389 device, _device_manager.get_display_name(device)));
391 _sampling_bar->set_device_list(device_list, selected_device);
394 void MainWindow::closeEvent(QCloseEvent *event)
400 void MainWindow::load_file(QString file_name)
402 const QString errorMessage(
403 QString("Failed to load file %1").arg(file_name));
404 const QString infoMessage;
407 _session.set_file(file_name.toStdString());
409 show_session_error(tr("Failed to load ") + file_name, e.what());
410 _session.set_default_device();
411 update_device_list();
415 update_device_list();
417 _session.start_capture([&, errorMessage, infoMessage](QString) {
418 session_error(errorMessage, infoMessage); });
421 void MainWindow::show_session_error(
422 const QString text, const QString info_text)
424 QMessageBox msg(this);
426 msg.setInformativeText(info_text);
427 msg.setStandardButtons(QMessageBox::Ok);
428 msg.setIcon(QMessageBox::Warning);
432 void MainWindow::on_actionOpen_triggered()
435 const QString dir = settings.value(SettingOpenDirectory).toString();
438 const QString file_name = QFileDialog::getOpenFileName(
439 this, tr("Open File"), dir, tr(
440 "Sigrok Sessions (*.sr);;"
443 if (!file_name.isEmpty()) {
444 load_file(file_name);
446 const QString abs_path = QFileInfo(file_name).absolutePath();
447 settings.setValue(SettingOpenDirectory, abs_path);
451 void MainWindow::on_actionSaveAs_triggered()
453 using pv::dialogs::StoreProgress;
455 // Stop any currently running capture session
456 _session.stop_capture();
459 const QString dir = settings.value(SettingSaveDirectory).toString();
462 const QString file_name = QFileDialog::getSaveFileName(
463 this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)"));
465 if (file_name.isEmpty())
468 const QString abs_path = QFileInfo(file_name).absolutePath();
469 settings.setValue(SettingSaveDirectory, abs_path);
471 StoreProgress *dlg = new StoreProgress(file_name, _session, this);
475 void MainWindow::on_actionConnect_triggered()
477 // Stop any currently running capture session
478 _session.stop_capture();
480 dialogs::Connect dlg(this, _device_manager);
482 // If the user selected a device, select it in the device list. Select the
483 // current device otherwise.
485 _session.set_device(dlg.get_selected_device());
487 update_device_list();
490 void MainWindow::on_actionQuit_triggered()
495 void MainWindow::on_actionViewZoomIn_triggered()
500 void MainWindow::on_actionViewZoomOut_triggered()
505 void MainWindow::on_actionViewZoomFit_triggered()
510 void MainWindow::on_actionViewZoomOneToOne_triggered()
512 _view->zoom_one_to_one();
515 void MainWindow::on_actionViewShowCursors_triggered()
519 const bool show = !_view->cursors_shown();
521 _view->centre_cursors();
523 _view->show_cursors(show);
526 void MainWindow::on_actionAbout_triggered()
528 dialogs::About dlg(_device_manager.context(), this);
532 void MainWindow::add_decoder(srd_decoder *decoder)
536 _session.add_decoder(decoder);
542 void MainWindow::run_stop()
544 switch(_session.get_capture_state()) {
545 case SigSession::Stopped:
546 _session.start_capture([&](QString message) {
547 session_error("Capture failed", message); });
550 case SigSession::AwaitingTrigger:
551 case SigSession::Running:
552 _session.stop_capture();
557 void MainWindow::capture_state_changed(int state)
559 _sampling_bar->set_capture_state((pv::SigSession::capture_state)state);