]> sigrok.org Git - pulseview.git/blame_incremental - pv/mainwindow.cpp
device: ensure bind_enum() checks availability of Capability::LIST.
[pulseview.git] / pv / mainwindow.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <cassert>
22
23#ifdef ENABLE_DECODE
24#include <libsigrokdecode/libsigrokdecode.h>
25#endif
26
27#include <algorithm>
28#include <iterator>
29
30#include <boost/algorithm/string/join.hpp>
31
32#include <QAction>
33#include <QApplication>
34#include <QButtonGroup>
35#include <QCloseEvent>
36#include <QFileDialog>
37#include <QMessageBox>
38#include <QMenu>
39#include <QMenuBar>
40#include <QSettings>
41#include <QStatusBar>
42#include <QVBoxLayout>
43#include <QWidget>
44
45#include "mainwindow.hpp"
46
47#include "devicemanager.hpp"
48#include "dialogs/about.hpp"
49#include "dialogs/connect.hpp"
50#include "dialogs/inputoutputoptions.hpp"
51#include "dialogs/storeprogress.hpp"
52#include "toolbars/mainbar.hpp"
53#include "view/logicsignal.hpp"
54#include "view/view.hpp"
55#include "widgets/exportmenu.hpp"
56#ifdef ENABLE_DECODE
57#include "widgets/decodermenu.hpp"
58#endif
59#include "widgets/hidingmenubar.hpp"
60
61#include <inttypes.h>
62#include <stdint.h>
63#include <stdarg.h>
64#include <glib.h>
65#include <libsigrokcxx/libsigrokcxx.hpp>
66
67using std::list;
68using std::map;
69using std::shared_ptr;
70using std::string;
71using std::vector;
72
73using boost::algorithm::join;
74
75using sigrok::Device;
76using sigrok::Error;
77using sigrok::HardwareDevice;
78using sigrok::OutputFormat;
79
80namespace pv {
81
82namespace view {
83class ViewItem;
84}
85
86const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory";
87const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory";
88
89MainWindow::MainWindow(DeviceManager &device_manager,
90 const char *open_file_name,
91 QWidget *parent) :
92 QMainWindow(parent),
93 device_manager_(device_manager),
94 session_(device_manager),
95 action_open_(new QAction(this)),
96 action_save_as_(new QAction(this)),
97 action_connect_(new QAction(this)),
98 action_quit_(new QAction(this)),
99 action_view_zoom_in_(new QAction(this)),
100 action_view_zoom_out_(new QAction(this)),
101 action_view_zoom_fit_(new QAction(this)),
102 action_view_zoom_one_to_one_(new QAction(this)),
103 action_view_show_cursors_(new QAction(this)),
104 action_about_(new QAction(this)),
105 menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
106{
107 setup_ui();
108 restore_ui_settings();
109 if (open_file_name) {
110 const QString s(QString::fromUtf8(open_file_name));
111 QMetaObject::invokeMethod(this, "load_file",
112 Qt::QueuedConnection,
113 Q_ARG(QString, s));
114 }
115}
116
117QAction* MainWindow::action_open() const
118{
119 return action_open_;
120}
121
122QAction* MainWindow::action_save_as() const
123{
124 return action_save_as_;
125}
126
127QAction* MainWindow::action_connect() const
128{
129 return action_connect_;
130}
131
132QAction* MainWindow::action_quit() const
133{
134 return action_quit_;
135}
136
137QAction* MainWindow::action_view_zoom_in() const
138{
139 return action_view_zoom_in_;
140}
141
142QAction* MainWindow::action_view_zoom_out() const
143{
144 return action_view_zoom_out_;
145}
146
147QAction* MainWindow::action_view_zoom_fit() const
148{
149 return action_view_zoom_fit_;
150}
151
152QAction* MainWindow::action_view_zoom_one_to_one() const
153{
154 return action_view_zoom_one_to_one_;
155}
156
157QAction* MainWindow::action_view_show_cursors() const
158{
159 return action_view_show_cursors_;
160}
161
162QAction* MainWindow::action_about() const
163{
164 return action_about_;
165}
166
167QMenu* MainWindow::menu_decoder_add() const
168{
169 return menu_decoders_add_;
170}
171
172void MainWindow::run_stop()
173{
174 switch(session_.get_capture_state()) {
175 case Session::Stopped:
176 session_.start_capture([&](QString message) {
177 session_error("Capture failed", message); });
178 break;
179
180 case Session::AwaitingTrigger:
181 case Session::Running:
182 session_.stop_capture();
183 break;
184 }
185}
186
187void MainWindow::select_device(shared_ptr<Device> device)
188{
189 try {
190 session_.set_device(device);
191 } catch(const QString &e) {
192 QMessageBox msg(this);
193 msg.setText(e);
194 msg.setInformativeText(tr("Failed to Select Device"));
195 msg.setStandardButtons(QMessageBox::Ok);
196 msg.setIcon(QMessageBox::Warning);
197 msg.exec();
198 }
199}
200
201void MainWindow::export_file(shared_ptr<OutputFormat> format)
202{
203 using pv::dialogs::StoreProgress;
204
205 // Stop any currently running capture session
206 session_.stop_capture();
207
208 QSettings settings;
209 const QString dir = settings.value(SettingSaveDirectory).toString();
210
211 // Construct the filter
212 const vector<string> exts = format->extensions();
213 QString filter = tr("%1 files ").arg(
214 QString::fromStdString(format->description()));
215
216 if (exts.empty())
217 filter += "(*.*)";
218 else
219 filter += QString("(*.%1);;%2 (*.*)").arg(
220 QString::fromStdString(join(exts, ", *."))).arg(
221 tr("All Files"));
222
223 // Show the file dialog
224 const QString file_name = QFileDialog::getSaveFileName(
225 this, tr("Save File"), dir, filter);
226
227 if (file_name.isEmpty())
228 return;
229
230 const QString abs_path = QFileInfo(file_name).absolutePath();
231 settings.setValue(SettingSaveDirectory, abs_path);
232
233 // Show the options dialog
234 map<string, Glib::VariantBase> options;
235 if (!format->options().empty()) {
236 dialogs::InputOutputOptions dlg(
237 tr("Export %1").arg(QString::fromStdString(
238 format->description())),
239 format->options(), this);
240 if (!dlg.exec())
241 return;
242 options = dlg.options();
243 }
244
245 StoreProgress *dlg = new StoreProgress(file_name, format, options,
246 session_, this);
247 dlg->run();
248}
249
250void MainWindow::setup_ui()
251{
252 setObjectName(QString::fromUtf8("MainWindow"));
253
254 // Set the window icon
255 QIcon icon;
256 icon.addFile(QString(":/icons/sigrok-logo-notext.svg"));
257 setWindowIcon(icon);
258
259 // Setup the central widget
260 central_widget_ = new QWidget(this);
261 vertical_layout_ = new QVBoxLayout(central_widget_);
262 vertical_layout_->setSpacing(6);
263 vertical_layout_->setContentsMargins(0, 0, 0, 0);
264 setCentralWidget(central_widget_);
265
266 view_ = new pv::view::View(session_, this);
267
268 vertical_layout_->addWidget(view_);
269
270 // Setup the menu bar
271 pv::widgets::HidingMenuBar *const menu_bar =
272 new pv::widgets::HidingMenuBar(this);
273
274 // File Menu
275 QMenu *const menu_file = new QMenu;
276 menu_file->setTitle(tr("&File"));
277
278 action_open_->setText(tr("&Open..."));
279 action_open_->setIcon(QIcon::fromTheme("document-open",
280 QIcon(":/icons/document-open.png")));
281 action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
282 action_open_->setObjectName(QString::fromUtf8("actionOpen"));
283 menu_file->addAction(action_open_);
284
285 action_save_as_->setText(tr("&Save As..."));
286 action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
287 QIcon(":/icons/document-save-as.png")));
288 action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
289 action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
290 menu_file->addAction(action_save_as_);
291
292 menu_file->addSeparator();
293
294 widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
295 device_manager_.context());
296 menu_file_export->setTitle(tr("&Export"));
297 connect(menu_file_export,
298 SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
299 this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
300 menu_file->addAction(menu_file_export->menuAction());
301
302 menu_file->addSeparator();
303
304 action_connect_->setText(tr("&Connect to Device..."));
305 action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
306 menu_file->addAction(action_connect_);
307
308 menu_file->addSeparator();
309
310 action_quit_->setText(tr("&Quit"));
311 action_quit_->setIcon(QIcon::fromTheme("application-exit",
312 QIcon(":/icons/application-exit.png")));
313 action_quit_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
314 action_quit_->setObjectName(QString::fromUtf8("actionQuit"));
315 menu_file->addAction(action_quit_);
316
317 // View Menu
318 QMenu *menu_view = new QMenu;
319 menu_view->setTitle(tr("&View"));
320
321 action_view_zoom_in_->setText(tr("Zoom &In"));
322 action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
323 QIcon(":/icons/zoom-in.png")));
324 // simply using Qt::Key_Plus shows no + in the menu
325 action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
326 action_view_zoom_in_->setObjectName(
327 QString::fromUtf8("actionViewZoomIn"));
328 menu_view->addAction(action_view_zoom_in_);
329
330 action_view_zoom_out_->setText(tr("Zoom &Out"));
331 action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
332 QIcon(":/icons/zoom-out.png")));
333 action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
334 action_view_zoom_out_->setObjectName(
335 QString::fromUtf8("actionViewZoomOut"));
336 menu_view->addAction(action_view_zoom_out_);
337
338 action_view_zoom_fit_->setText(tr("Zoom to &Fit"));
339 action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit",
340 QIcon(":/icons/zoom-fit.png")));
341 action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
342 action_view_zoom_fit_->setObjectName(
343 QString::fromUtf8("actionViewZoomFit"));
344 menu_view->addAction(action_view_zoom_fit_);
345
346 action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
347 action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
348 QIcon(":/icons/zoom-original.png")));
349 action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
350 action_view_zoom_one_to_one_->setObjectName(
351 QString::fromUtf8("actionViewZoomOneToOne"));
352 menu_view->addAction(action_view_zoom_one_to_one_);
353
354 menu_view->addSeparator();
355
356 action_view_show_cursors_->setCheckable(true);
357 action_view_show_cursors_->setChecked(view_->cursors_shown());
358 action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
359 QIcon(":/icons/show-cursors.svg")));
360 action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
361 action_view_show_cursors_->setObjectName(
362 QString::fromUtf8("actionViewShowCursors"));
363 action_view_show_cursors_->setText(tr("Show &Cursors"));
364 menu_view->addAction(action_view_show_cursors_);
365
366 // Decoders Menu
367#ifdef ENABLE_DECODE
368 QMenu *const menu_decoders = new QMenu;
369 menu_decoders->setTitle(tr("&Decoders"));
370
371 menu_decoders_add_->setTitle(tr("&Add"));
372 connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
373 this, SLOT(add_decoder(srd_decoder*)));
374
375 menu_decoders->addMenu(menu_decoders_add_);
376#endif
377
378 // Help Menu
379 QMenu *const menu_help = new QMenu;
380 menu_help->setTitle(tr("&Help"));
381
382 action_about_->setObjectName(QString::fromUtf8("actionAbout"));
383 action_about_->setText(tr("&About..."));
384 menu_help->addAction(action_about_);
385
386 menu_bar->addAction(menu_file->menuAction());
387 menu_bar->addAction(menu_view->menuAction());
388#ifdef ENABLE_DECODE
389 menu_bar->addAction(menu_decoders->menuAction());
390#endif
391 menu_bar->addAction(menu_help->menuAction());
392
393 setMenuBar(menu_bar);
394 QMetaObject::connectSlotsByName(this);
395
396 // Setup the toolbar
397 main_bar_ = new toolbars::MainBar(session_, *this);
398
399 // Populate the device list and select the initially selected device
400 update_device_list();
401
402 addToolBar(main_bar_);
403
404 // Set the title
405 setWindowTitle(tr("PulseView"));
406
407 // Setup session_ events
408 connect(&session_, SIGNAL(capture_state_changed(int)), this,
409 SLOT(capture_state_changed(int)));
410 connect(&session_, SIGNAL(device_selected()), this,
411 SLOT(device_selected()));
412}
413
414void MainWindow::save_ui_settings()
415{
416 QSettings settings;
417
418 map<string, string> dev_info;
419 list<string> key_list;
420
421 settings.beginGroup("MainWindow");
422 settings.setValue("state", saveState());
423 settings.setValue("geometry", saveGeometry());
424 settings.endGroup();
425
426 if (session_.device()) {
427 settings.beginGroup("Device");
428 key_list.push_back("vendor");
429 key_list.push_back("model");
430 key_list.push_back("version");
431 key_list.push_back("serial_num");
432 key_list.push_back("connection_id");
433
434 dev_info = device_manager_.get_device_info(
435 session_.device());
436
437 for (string key : key_list) {
438
439 if (dev_info.count(key))
440 settings.setValue(QString::fromUtf8(key.c_str()),
441 QString::fromUtf8(dev_info.at(key).c_str()));
442 else
443 settings.remove(QString::fromUtf8(key.c_str()));
444 }
445
446 settings.endGroup();
447 }
448}
449
450void MainWindow::restore_ui_settings()
451{
452 QSettings settings;
453
454 shared_ptr<HardwareDevice> device;
455
456 map<string, string> dev_info;
457 list<string> key_list;
458 string value;
459
460 settings.beginGroup("MainWindow");
461
462 if (settings.contains("geometry")) {
463 restoreGeometry(settings.value("geometry").toByteArray());
464 restoreState(settings.value("state").toByteArray());
465 } else
466 resize(1000, 720);
467
468 settings.endGroup();
469
470 // Re-select last used device if possible.
471 settings.beginGroup("Device");
472 key_list.push_back("vendor");
473 key_list.push_back("model");
474 key_list.push_back("version");
475 key_list.push_back("serial_num");
476 key_list.push_back("connection_id");
477
478 for (string key : key_list) {
479 if (!settings.contains(QString::fromUtf8(key.c_str())))
480 continue;
481
482 value = settings.value(QString::fromUtf8(key.c_str())).toString().toStdString();
483
484 if (value.size() > 0)
485 dev_info.insert(std::make_pair(key, value));
486 }
487
488 device = device_manager_.find_device_from_info(dev_info);
489
490 if (device) {
491 select_device(device);
492 update_device_list();
493 }
494
495 settings.endGroup();
496}
497
498void MainWindow::session_error(
499 const QString text, const QString info_text)
500{
501 QMetaObject::invokeMethod(this, "show_session_error",
502 Qt::QueuedConnection, Q_ARG(QString, text),
503 Q_ARG(QString, info_text));
504}
505
506void MainWindow::update_device_list()
507{
508 main_bar_->update_device_list();
509}
510
511void MainWindow::closeEvent(QCloseEvent *event)
512{
513 save_ui_settings();
514 event->accept();
515}
516
517void MainWindow::keyReleaseEvent(QKeyEvent *event)
518{
519 if (event->key() == Qt::Key_Alt) {
520 menuBar()->setHidden(!menuBar()->isHidden());
521 menuBar()->setFocus();
522 }
523 QMainWindow::keyReleaseEvent(event);
524}
525
526void MainWindow::load_file(QString file_name)
527{
528 const QString errorMessage(
529 QString("Failed to load file %1").arg(file_name));
530 const QString infoMessage;
531
532 try {
533 session_.set_session_file(file_name.toStdString());
534 } catch(Error e) {
535 show_session_error(tr("Failed to load ") + file_name, e.what());
536 session_.set_default_device();
537 update_device_list();
538 return;
539 }
540
541 update_device_list();
542
543 session_.start_capture([&, errorMessage, infoMessage](QString) {
544 session_error(errorMessage, infoMessage); });
545}
546
547void MainWindow::show_session_error(
548 const QString text, const QString info_text)
549{
550 QMessageBox msg(this);
551 msg.setText(text);
552 msg.setInformativeText(info_text);
553 msg.setStandardButtons(QMessageBox::Ok);
554 msg.setIcon(QMessageBox::Warning);
555 msg.exec();
556}
557
558void MainWindow::on_actionOpen_triggered()
559{
560 QSettings settings;
561 const QString dir = settings.value(SettingOpenDirectory).toString();
562
563 // Show the dialog
564 const QString file_name = QFileDialog::getOpenFileName(
565 this, tr("Open File"), dir, tr(
566 "Sigrok Sessions (*.sr);;"
567 "All Files (*.*)"));
568
569 if (!file_name.isEmpty()) {
570 load_file(file_name);
571
572 const QString abs_path = QFileInfo(file_name).absolutePath();
573 settings.setValue(SettingOpenDirectory, abs_path);
574 }
575}
576
577void MainWindow::on_actionSaveAs_triggered()
578{
579 export_file(device_manager_.context()->output_formats()["srzip"]);
580}
581
582void MainWindow::on_actionConnect_triggered()
583{
584 // Stop any currently running capture session
585 session_.stop_capture();
586
587 dialogs::Connect dlg(this, device_manager_);
588
589 // If the user selected a device, select it in the device list. Select the
590 // current device otherwise.
591 if (dlg.exec())
592 select_device(dlg.get_selected_device());
593
594 update_device_list();
595}
596
597void MainWindow::on_actionQuit_triggered()
598{
599 close();
600}
601
602void MainWindow::on_actionViewZoomIn_triggered()
603{
604 view_->zoom(1);
605}
606
607void MainWindow::on_actionViewZoomOut_triggered()
608{
609 view_->zoom(-1);
610}
611
612void MainWindow::on_actionViewZoomFit_triggered()
613{
614 view_->zoom_fit();
615}
616
617void MainWindow::on_actionViewZoomOneToOne_triggered()
618{
619 view_->zoom_one_to_one();
620}
621
622void MainWindow::on_actionViewShowCursors_triggered()
623{
624 assert(view_);
625
626 const bool show = !view_->cursors_shown();
627 if(show)
628 view_->centre_cursors();
629
630 view_->show_cursors(show);
631}
632
633void MainWindow::on_actionAbout_triggered()
634{
635 dialogs::About dlg(device_manager_.context(), this);
636 dlg.exec();
637}
638
639void MainWindow::add_decoder(srd_decoder *decoder)
640{
641#ifdef ENABLE_DECODE
642 assert(decoder);
643 session_.add_decoder(decoder);
644#else
645 (void)decoder;
646#endif
647}
648
649void MainWindow::capture_state_changed(int state)
650{
651 main_bar_->set_capture_state((pv::Session::capture_state)state);
652}
653
654void MainWindow::device_selected()
655{
656 // Set the title to include the device/file name
657 const shared_ptr<sigrok::Device> device = session_.device();
658 if (!device)
659 return;
660
661 const string display_name = device_manager_.get_display_name(device);
662 setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));
663}
664
665} // namespace pv