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