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.hpp"
45 #include "devicemanager.hpp"
46 #include "dialogs/about.hpp"
47 #include "dialogs/connect.hpp"
48 #include "dialogs/storeprogress.hpp"
49 #include "toolbars/samplingbar.hpp"
50 #include "view/logicsignal.hpp"
51 #include "view/view.hpp"
53 #include "widgets/decodermenu.hpp"
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::run_stop()
100 switch(session_.get_capture_state()) {
101 case Session::Stopped:
102 session_.start_capture([&](QString message) {
103 session_error("Capture failed", message); });
106 case Session::AwaitingTrigger:
107 case Session::Running:
108 session_.stop_capture();
113 void MainWindow::select_device(shared_ptr<Device> device)
116 session_.set_device(device);
117 } catch(const QString &e) {
118 QMessageBox msg(this);
120 msg.setInformativeText(tr("Failed to Select Device"));
121 msg.setStandardButtons(QMessageBox::Ok);
122 msg.setIcon(QMessageBox::Warning);
127 void MainWindow::setup_ui()
129 setObjectName(QString::fromUtf8("MainWindow"));
131 // Set the window icon
133 icon.addFile(QString::fromUtf8(":/icons/sigrok-logo-notext.png"),
134 QSize(), QIcon::Normal, QIcon::Off);
137 // Setup the central widget
138 central_widget_ = new QWidget(this);
139 vertical_layout_ = new QVBoxLayout(central_widget_);
140 vertical_layout_->setSpacing(6);
141 vertical_layout_->setContentsMargins(0, 0, 0, 0);
142 setCentralWidget(central_widget_);
144 view_ = new pv::view::View(session_, this);
146 vertical_layout_->addWidget(view_);
148 // Setup the menu bar
149 QMenuBar *const menu_bar = new QMenuBar(this);
150 menu_bar->setGeometry(QRect(0, 0, 400, 25));
153 QMenu *const menu_file = new QMenu;
154 menu_file->setTitle(tr("&File"));
156 QAction *const action_open = new QAction(this);
157 action_open->setText(tr("&Open..."));
158 action_open->setIcon(QIcon::fromTheme("document-open",
159 QIcon(":/icons/document-open.png")));
160 action_open->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
161 action_open->setObjectName(QString::fromUtf8("actionOpen"));
162 menu_file->addAction(action_open);
164 QAction *const action_save_as = new QAction(this);
165 action_save_as->setText(tr("&Save As..."));
166 action_save_as->setIcon(QIcon::fromTheme("document-save-as",
167 QIcon(":/icons/document-save-as.png")));
168 action_save_as->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
169 action_save_as->setObjectName(QString::fromUtf8("actionSaveAs"));
170 menu_file->addAction(action_save_as);
172 menu_file->addSeparator();
174 QAction *const action_connect = new QAction(this);
175 action_connect->setText(tr("&Connect to Device..."));
176 action_connect->setObjectName(QString::fromUtf8("actionConnect"));
177 menu_file->addAction(action_connect);
179 menu_file->addSeparator();
181 QAction *action_quit = new QAction(this);
182 action_quit->setText(tr("&Quit"));
183 action_quit->setIcon(QIcon::fromTheme("application-exit",
184 QIcon(":/icons/application-exit.png")));
185 action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
186 action_quit->setObjectName(QString::fromUtf8("actionQuit"));
187 menu_file->addAction(action_quit);
190 QMenu *menu_view = new QMenu;
191 menu_view->setTitle(tr("&View"));
193 QAction *const action_view_zoom_in = new QAction(this);
194 action_view_zoom_in->setText(tr("Zoom &In"));
195 action_view_zoom_in->setIcon(QIcon::fromTheme("zoom-in",
196 QIcon(":/icons/zoom-in.png")));
197 // simply using Qt::Key_Plus shows no + in the menu
198 action_view_zoom_in->setShortcut(QKeySequence::ZoomIn);
199 action_view_zoom_in->setObjectName(
200 QString::fromUtf8("actionViewZoomIn"));
201 menu_view->addAction(action_view_zoom_in);
203 QAction *const action_view_zoom_out = new QAction(this);
204 action_view_zoom_out->setText(tr("Zoom &Out"));
205 action_view_zoom_out->setIcon(QIcon::fromTheme("zoom-out",
206 QIcon(":/icons/zoom-out.png")));
207 action_view_zoom_out->setShortcut(QKeySequence::ZoomOut);
208 action_view_zoom_out->setObjectName(
209 QString::fromUtf8("actionViewZoomOut"));
210 menu_view->addAction(action_view_zoom_out);
212 QAction *const action_view_zoom_fit = new QAction(this);
213 action_view_zoom_fit->setText(tr("Zoom to &Fit"));
214 action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
215 QIcon(":/icons/zoom-fit.png")));
216 action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
217 action_view_zoom_fit->setObjectName(
218 QString::fromUtf8("actionViewZoomFit"));
219 menu_view->addAction(action_view_zoom_fit);
221 QAction *const action_view_zoom_one_to_one = new QAction(this);
222 action_view_zoom_one_to_one->setText(tr("Zoom to &One-to-One"));
223 action_view_zoom_one_to_one->setIcon(QIcon::fromTheme("zoom-original",
224 QIcon(":/icons/zoom-original.png")));
225 action_view_zoom_one_to_one->setShortcut(QKeySequence(Qt::Key_O));
226 action_view_zoom_one_to_one->setObjectName(
227 QString::fromUtf8("actionViewZoomOneToOne"));
228 menu_view->addAction(action_view_zoom_one_to_one);
230 menu_view->addSeparator();
232 QAction *action_view_show_cursors = new QAction(this);
233 action_view_show_cursors->setCheckable(true);
234 action_view_show_cursors->setChecked(view_->cursors_shown());
235 action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C));
236 action_view_show_cursors->setObjectName(
237 QString::fromUtf8("actionViewShowCursors"));
238 action_view_show_cursors->setText(tr("Show &Cursors"));
239 menu_view->addAction(action_view_show_cursors);
243 QMenu *const menu_decoders = new QMenu;
244 menu_decoders->setTitle(tr("&Decoders"));
246 pv::widgets::DecoderMenu *const menu_decoders_add =
247 new pv::widgets::DecoderMenu(menu_decoders, true);
248 menu_decoders_add->setTitle(tr("&Add"));
249 connect(menu_decoders_add, SIGNAL(decoder_selected(srd_decoder*)),
250 this, SLOT(add_decoder(srd_decoder*)));
252 menu_decoders->addMenu(menu_decoders_add);
256 QMenu *const menu_help = new QMenu;
257 menu_help->setTitle(tr("&Help"));
259 QAction *const action_about = new QAction(this);
260 action_about->setObjectName(QString::fromUtf8("actionAbout"));
261 action_about->setText(tr("&About..."));
262 menu_help->addAction(action_about);
264 menu_bar->addAction(menu_file->menuAction());
265 menu_bar->addAction(menu_view->menuAction());
267 menu_bar->addAction(menu_decoders->menuAction());
269 menu_bar->addAction(menu_help->menuAction());
271 setMenuBar(menu_bar);
272 QMetaObject::connectSlotsByName(this);
275 QToolBar *const toolbar = new QToolBar(tr("Main Toolbar"), this);
276 toolbar->setObjectName(QString::fromUtf8("MainToolbar"));
277 toolbar->addAction(action_open);
278 toolbar->addAction(action_save_as);
279 toolbar->addSeparator();
280 toolbar->addAction(action_view_zoom_in);
281 toolbar->addAction(action_view_zoom_out);
282 toolbar->addAction(action_view_zoom_fit);
283 toolbar->addAction(action_view_zoom_one_to_one);
286 // Setup the sampling bar
287 sampling_bar_ = new toolbars::SamplingBar(session_, *this);
289 // Populate the device list and select the initially selected device
290 update_device_list();
292 addToolBar(sampling_bar_);
295 setWindowTitle(tr("PulseView"));
297 // Setup session_ events
298 connect(&session_, SIGNAL(capture_state_changed(int)), this,
299 SLOT(capture_state_changed(int)));
300 connect(&session_, SIGNAL(device_selected()), this,
301 SLOT(device_selected()));
304 void MainWindow::save_ui_settings()
308 map<string, string> dev_info;
309 list<string> key_list;
311 settings.beginGroup("MainWindow");
312 settings.setValue("state", saveState());
313 settings.setValue("geometry", saveGeometry());
316 if (session_.device()) {
317 settings.beginGroup("Device");
318 key_list.push_back("vendor");
319 key_list.push_back("model");
320 key_list.push_back("version");
321 key_list.push_back("serial_num");
322 key_list.push_back("connection_id");
324 dev_info = device_manager_.get_device_info(
327 for (string key : key_list) {
329 if (dev_info.count(key))
330 settings.setValue(QString::fromUtf8(key.c_str()),
331 QString::fromUtf8(dev_info.at(key).c_str()));
333 settings.remove(QString::fromUtf8(key.c_str()));
340 void MainWindow::restore_ui_settings()
344 shared_ptr<HardwareDevice> device;
346 map<string, string> dev_info;
347 list<string> key_list;
350 settings.beginGroup("MainWindow");
352 if (settings.contains("geometry")) {
353 restoreGeometry(settings.value("geometry").toByteArray());
354 restoreState(settings.value("state").toByteArray());
360 // Re-select last used device if possible.
361 settings.beginGroup("Device");
362 key_list.push_back("vendor");
363 key_list.push_back("model");
364 key_list.push_back("version");
365 key_list.push_back("serial_num");
366 key_list.push_back("connection_id");
368 for (string key : key_list) {
369 if (!settings.contains(QString::fromUtf8(key.c_str())))
372 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
374 if (value.size() > 0)
375 dev_info.insert(std::make_pair(key, value));
378 device = device_manager_.find_device_from_info(dev_info);
381 select_device(device);
382 update_device_list();
388 void MainWindow::session_error(
389 const QString text, const QString info_text)
391 QMetaObject::invokeMethod(this, "show_session_error",
392 Qt::QueuedConnection, Q_ARG(QString, text),
393 Q_ARG(QString, info_text));
396 void MainWindow::update_device_list()
398 assert(sampling_bar_);
400 shared_ptr<Device> selected_device = session_.device();
401 list< shared_ptr<Device> > devices;
403 if (device_manager_.devices().size() == 0)
406 std::copy(device_manager_.devices().begin(),
407 device_manager_.devices().end(), std::back_inserter(devices));
409 if (std::find(devices.begin(), devices.end(), selected_device) ==
411 devices.push_back(selected_device);
412 assert(selected_device);
414 list< pair< shared_ptr<Device>, string> > device_list;
416 for (auto device : devices)
417 device_list.push_back(make_pair(
418 device, device_manager_.get_display_name(device)));
420 sampling_bar_->set_device_list(device_list, selected_device);
423 void MainWindow::closeEvent(QCloseEvent *event)
429 void MainWindow::load_file(QString file_name)
431 const QString errorMessage(
432 QString("Failed to load file %1").arg(file_name));
433 const QString infoMessage;
436 session_.set_file(file_name.toStdString());
438 show_session_error(tr("Failed to load ") + file_name, e.what());
439 session_.set_default_device();
440 update_device_list();
444 update_device_list();
446 session_.start_capture([&, errorMessage, infoMessage](QString) {
447 session_error(errorMessage, infoMessage); });
450 void MainWindow::show_session_error(
451 const QString text, const QString info_text)
453 QMessageBox msg(this);
455 msg.setInformativeText(info_text);
456 msg.setStandardButtons(QMessageBox::Ok);
457 msg.setIcon(QMessageBox::Warning);
461 void MainWindow::on_actionOpen_triggered()
464 const QString dir = settings.value(SettingOpenDirectory).toString();
467 const QString file_name = QFileDialog::getOpenFileName(
468 this, tr("Open File"), dir, tr(
469 "Sigrok Sessions (*.sr);;"
472 if (!file_name.isEmpty()) {
473 load_file(file_name);
475 const QString abs_path = QFileInfo(file_name).absolutePath();
476 settings.setValue(SettingOpenDirectory, abs_path);
480 void MainWindow::on_actionSaveAs_triggered()
482 using pv::dialogs::StoreProgress;
484 // Stop any currently running capture session
485 session_.stop_capture();
488 const QString dir = settings.value(SettingSaveDirectory).toString();
491 const QString file_name = QFileDialog::getSaveFileName(
492 this, tr("Save File"), dir, tr("Sigrok Sessions (*.sr)"));
494 if (file_name.isEmpty())
497 const QString abs_path = QFileInfo(file_name).absolutePath();
498 settings.setValue(SettingSaveDirectory, abs_path);
500 StoreProgress *dlg = new StoreProgress(file_name, session_, this);
504 void MainWindow::on_actionConnect_triggered()
506 // Stop any currently running capture session
507 session_.stop_capture();
509 dialogs::Connect dlg(this, device_manager_);
511 // If the user selected a device, select it in the device list. Select the
512 // current device otherwise.
514 select_device(dlg.get_selected_device());
516 update_device_list();
519 void MainWindow::on_actionQuit_triggered()
524 void MainWindow::on_actionViewZoomIn_triggered()
529 void MainWindow::on_actionViewZoomOut_triggered()
534 void MainWindow::on_actionViewZoomFit_triggered()
539 void MainWindow::on_actionViewZoomOneToOne_triggered()
541 view_->zoom_one_to_one();
544 void MainWindow::on_actionViewShowCursors_triggered()
548 const bool show = !view_->cursors_shown();
550 view_->centre_cursors();
552 view_->show_cursors(show);
555 void MainWindow::on_actionAbout_triggered()
557 dialogs::About dlg(device_manager_.context(), this);
561 void MainWindow::add_decoder(srd_decoder *decoder)
565 session_.add_decoder(decoder);
571 void MainWindow::capture_state_changed(int state)
573 sampling_bar_->set_capture_state((pv::Session::capture_state)state);
576 void MainWindow::device_selected()
578 // Set the title to include the device/file name
579 const shared_ptr<sigrok::Device> device = session_.device();
583 const string display_name = device_manager_.get_display_name(device);
584 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));