X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fmainwindow.cpp;h=65071330da07e1d24c5955448ad44efe3f929383;hp=767ab6f31149bd4881b0db163635c86025c08818;hb=b926e4eeef6db657601ebd4bbededdf9d329cdd6;hpb=951b5ae77957c8086f05fcd2b26beaf32f3e8e3e diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 767ab6f3..65071330 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -45,6 +45,8 @@ #include "mainwindow.hpp" #include "devicemanager.hpp" +#include "util.hpp" +#include "data/segment.hpp" #include "devices/hardwaredevice.hpp" #include "devices/inputfile.hpp" #include "devices/sessionfile.hpp" @@ -68,8 +70,11 @@ #include #include +using std::cerr; +using std::endl; using std::list; using std::map; +using std::pair; using std::shared_ptr; using std::string; using std::vector; @@ -90,33 +95,36 @@ const char *MainWindow::SettingOpenDirectory = "MainWindow/OpenDirectory"; const char *MainWindow::SettingSaveDirectory = "MainWindow/SaveDirectory"; MainWindow::MainWindow(DeviceManager &device_manager, - const char *open_file_name, + string open_file_name, string open_file_format, QWidget *parent) : QMainWindow(parent), device_manager_(device_manager), session_(device_manager), action_open_(new QAction(this)), action_save_as_(new QAction(this)), + action_save_selection_as_(new QAction(this)), action_connect_(new QAction(this)), action_quit_(new QAction(this)), action_view_zoom_in_(new QAction(this)), action_view_zoom_out_(new QAction(this)), action_view_zoom_fit_(new QAction(this)), action_view_zoom_one_to_one_(new QAction(this)), + action_view_sticky_scrolling_(new QAction(this)), + action_view_coloured_bg_(new QAction(this)), action_view_show_cursors_(new QAction(this)), action_about_(new QAction(this)) #ifdef ENABLE_DECODE , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true)) #endif { + qRegisterMetaType("util::Timestamp"); + setup_ui(); restore_ui_settings(); - if (open_file_name) { - const QString s(QString::fromUtf8(open_file_name)); - QMetaObject::invokeMethod(this, "load_file", - Qt::QueuedConnection, - Q_ARG(QString, s)); - } + if (open_file_name.empty()) + select_init_device(); + else + load_init_file(open_file_name, open_file_format); } QAction* MainWindow::action_open() const @@ -129,6 +137,11 @@ QAction* MainWindow::action_save_as() const return action_save_as_; } +QAction* MainWindow::action_save_selection_as() const +{ + return action_save_selection_as_; +} + QAction* MainWindow::action_connect() const { return action_connect_; @@ -159,6 +172,16 @@ QAction* MainWindow::action_view_zoom_one_to_one() const return action_view_zoom_one_to_one_; } +QAction* MainWindow::action_view_sticky_scrolling() const +{ + return action_view_sticky_scrolling_; +} + +QAction* MainWindow::action_view_coloured_bg() const +{ + return action_view_coloured_bg_; +} + QAction* MainWindow::action_view_show_cursors() const { return action_view_show_cursors_; @@ -178,12 +201,11 @@ QMenu* MainWindow::menu_decoder_add() const void MainWindow::run_stop() { - switch(session_.get_capture_state()) { + switch (session_.get_capture_state()) { case Session::Stopped: session_.start_capture([&](QString message) { session_error("Capture failed", message); }); break; - case Session::AwaitingTrigger: case Session::Running: session_.stop_capture(); @@ -194,8 +216,11 @@ void MainWindow::run_stop() void MainWindow::select_device(shared_ptr device) { try { - session_.set_device(device); - } catch(const QString &e) { + if (device) + session_.set_device(device); + else + session_.set_default_device(); + } catch (const QString &e) { QMessageBox msg(this); msg.setText(e); msg.setInformativeText(tr("Failed to Select Device")); @@ -205,7 +230,8 @@ void MainWindow::select_device(shared_ptr device) } } -void MainWindow::export_file(shared_ptr format) +void MainWindow::export_file(shared_ptr format, + bool selection_only) { using pv::dialogs::StoreProgress; @@ -215,6 +241,30 @@ void MainWindow::export_file(shared_ptr format) QSettings settings; const QString dir = settings.value(SettingSaveDirectory).toString(); + std::pair sample_range; + + // Selection only? Verify that the cursors are active and fetch their values + if (selection_only) { + if (!view_->cursors()->enabled()) { + show_session_error(tr("Missing Cursors"), tr("You need to set the " \ + "cursors before you can save the data enclosed by them " \ + "to a session file (e.g. using ALT-V - Show Cursors).")); + return; + } + + const double samplerate = session_.get_samplerate(); + + const pv::util::Timestamp& start_time = view_->cursors()->first()->time(); + const pv::util::Timestamp& end_time = view_->cursors()->second()->time(); + + const uint64_t start_sample = start_time.convert_to() * samplerate; + const uint64_t end_sample = end_time.convert_to() * samplerate; + + sample_range = std::make_pair(start_sample, end_sample); + } else { + sample_range = std::make_pair(0, 0); + } + // Construct the filter const vector exts = format->extensions(); QString filter = tr("%1 files ").arg( @@ -250,7 +300,7 @@ void MainWindow::export_file(shared_ptr format) } StoreProgress *dlg = new StoreProgress(file_name, format, options, - session_, this); + sample_range, session_, this); dlg->run(); } @@ -337,6 +387,13 @@ void MainWindow::setup_ui() action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs")); menu_file->addAction(action_save_as_); + action_save_selection_as_->setText(tr("Save Selected &Range As...")); + action_save_selection_as_->setIcon(QIcon::fromTheme("document-save-as", + QIcon(":/icons/document-save-as.png"))); + action_save_selection_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); + action_save_selection_as_->setObjectName(QString::fromUtf8("actionSaveSelectionAs")); + menu_file->addAction(action_save_selection_as_); + menu_file->addSeparator(); widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this, @@ -391,6 +448,7 @@ void MainWindow::setup_ui() QString::fromUtf8("actionViewZoomOut")); menu_view->addAction(action_view_zoom_out_); + action_view_zoom_fit_->setCheckable(true); action_view_zoom_fit_->setText(tr("Zoom to &Fit")); action_view_zoom_fit_->setIcon(QIcon::fromTheme("zoom-fit", QIcon(":/icons/zoom-fit.png"))); @@ -409,6 +467,30 @@ void MainWindow::setup_ui() menu_view->addSeparator(); + action_view_sticky_scrolling_->setCheckable(true); + action_view_sticky_scrolling_->setChecked(true); + action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S)); + action_view_sticky_scrolling_->setObjectName( + QString::fromUtf8("actionViewStickyScrolling")); + action_view_sticky_scrolling_->setText(tr("&Sticky Scrolling")); + menu_view->addAction(action_view_sticky_scrolling_); + + view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); + + menu_view->addSeparator(); + + action_view_coloured_bg_->setCheckable(true); + action_view_coloured_bg_->setChecked(true); + action_view_coloured_bg_->setShortcut(QKeySequence(Qt::Key_B)); + action_view_coloured_bg_->setObjectName( + QString::fromUtf8("actionViewColouredBg")); + action_view_coloured_bg_->setText(tr("Use &coloured backgrounds")); + menu_view->addAction(action_view_coloured_bg_); + + view_->enable_coloured_bg(action_view_coloured_bg_->isChecked()); + + menu_view->addSeparator(); + action_view_show_cursors_->setCheckable(true); action_view_show_cursors_->setChecked(view_->cursors_shown()); action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors", @@ -449,6 +531,10 @@ void MainWindow::setup_ui() setMenuBar(menu_bar); QMetaObject::connectSlotsByName(this); + // Also add all actions to the main window for always-enabled hotkeys + for (QAction* action : menu_bar->actions()) + this->addAction(action); + // Setup the toolbar main_bar_ = new toolbars::MainBar(session_, *this); @@ -465,8 +551,89 @@ void MainWindow::setup_ui() SLOT(capture_state_changed(int))); connect(&session_, SIGNAL(device_selected()), this, SLOT(device_selected())); + connect(&session_, SIGNAL(trigger_event(util::Timestamp)), view_, + SLOT(trigger_event(util::Timestamp))); + + // Setup view_ events + connect(view_, SIGNAL(sticky_scrolling_changed(bool)), this, + SLOT(sticky_scrolling_changed(bool))); + connect(view_, SIGNAL(always_zoom_to_fit_changed(bool)), this, + SLOT(always_zoom_to_fit_changed(bool))); + +} + +void MainWindow::select_init_device() +{ + QSettings settings; + map dev_info; + list key_list; + shared_ptr device; + + // Re-select last used device if possible but only if it's not demo + settings.beginGroup("Device"); + key_list.push_back("vendor"); + key_list.push_back("model"); + key_list.push_back("version"); + key_list.push_back("serial_num"); + key_list.push_back("connection_id"); + + for (string key : key_list) { + const QString k = QString::fromStdString(key); + if (!settings.contains(k)) + continue; + + const string value = settings.value(k).toString().toStdString(); + if (!value.empty()) + dev_info.insert(std::make_pair(key, value)); + } + + if (dev_info.count("model") > 0) + if (dev_info.at("model").find("Demo device") == std::string::npos) + device = device_manager_.find_device_from_info(dev_info); + + // When we can't find a device similar to the one we used last + // time and there is at least one device aside from demo, use it + if (!device) { + for (shared_ptr dev : device_manager_.devices()) { + dev_info = device_manager_.get_device_info(dev); + + if (dev_info.count("model") > 0) + if (dev_info.at("model").find("Demo device") == std::string::npos) { + device = dev; + break; + } + } + } + + select_device(device); + update_device_list(); + + settings.endGroup(); } +void MainWindow::load_init_file(const std::string &file_name, + const std::string &format) +{ + shared_ptr input_format; + + if (!format.empty()) { + const map > formats = + device_manager_.context()->input_formats(); + const auto iter = find_if(formats.begin(), formats.end(), + [&](const pair > f) { + return f.first == format; }); + if (iter == formats.end()) { + cerr << "Unexpected input format: " << format << endl; + return; + } + + input_format = (*iter).second; + } + + load_file(QString::fromStdString(file_name), input_format); +} + + void MainWindow::save_ui_settings() { QSettings settings; @@ -491,7 +658,6 @@ void MainWindow::save_ui_settings() session_.device()); for (string key : key_list) { - if (dev_info.count(key)) settings.setValue(QString::fromUtf8(key.c_str()), QString::fromUtf8(dev_info.at(key).c_str())); @@ -507,9 +673,6 @@ void MainWindow::restore_ui_settings() { QSettings settings; - map dev_info; - list key_list; - settings.beginGroup("MainWindow"); if (settings.contains("geometry")) { @@ -519,33 +682,6 @@ void MainWindow::restore_ui_settings() resize(1000, 720); settings.endGroup(); - - // Re-select last used device if possible. - settings.beginGroup("Device"); - key_list.push_back("vendor"); - key_list.push_back("model"); - key_list.push_back("version"); - key_list.push_back("serial_num"); - key_list.push_back("connection_id"); - - for (string key : key_list) { - const QString k = QString::fromStdString(key); - if (!settings.contains(k)) - continue; - - const string value = settings.value(k).toString().toStdString(); - if (!value.empty()) - dev_info.insert(std::make_pair(key, value)); - } - - const shared_ptr device = - device_manager_.find_device_from_info(dev_info); - if (device) { - select_device(device); - update_device_list(); - } - - settings.endGroup(); } void MainWindow::session_error( @@ -561,28 +697,12 @@ void MainWindow::update_device_list() main_bar_->update_device_list(); } -void MainWindow::closeEvent(QCloseEvent *event) -{ - save_ui_settings(); - event->accept(); -} - -void MainWindow::keyReleaseEvent(QKeyEvent *event) -{ - if (event->key() == Qt::Key_Alt) { - menuBar()->setHidden(!menuBar()->isHidden()); - menuBar()->setFocus(); - } - QMainWindow::keyReleaseEvent(event); -} - void MainWindow::load_file(QString file_name, std::shared_ptr format, const std::map &options) { const QString errorMessage( QString("Failed to load file %1").arg(file_name)); - const QString infoMessage; try { if (format) @@ -596,7 +716,7 @@ void MainWindow::load_file(QString file_name, new devices::SessionFile( device_manager_.context(), file_name.toStdString()))); - } catch(Error e) { + } catch (Error e) { show_session_error(tr("Failed to load ") + file_name, e.what()); session_.set_default_device(); update_device_list(); @@ -605,10 +725,25 @@ void MainWindow::load_file(QString file_name, update_device_list(); - session_.start_capture([&, errorMessage, infoMessage](QString) { + session_.start_capture([&, errorMessage](QString infoMessage) { session_error(errorMessage, infoMessage); }); } +void MainWindow::closeEvent(QCloseEvent *event) +{ + save_ui_settings(); + event->accept(); +} + +void MainWindow::keyReleaseEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Alt) { + menuBar()->setHidden(!menuBar()->isHidden()); + menuBar()->setFocus(); + } + QMainWindow::keyReleaseEvent(event); +} + void MainWindow::show_session_error( const QString text, const QString info_text) { @@ -644,6 +779,11 @@ void MainWindow::on_actionSaveAs_triggered() export_file(device_manager_.context()->output_formats()["srzip"]); } +void MainWindow::on_actionSaveSelectionAs_triggered() +{ + export_file(device_manager_.context()->output_formats()["srzip"], true); +} + void MainWindow::on_actionConnect_triggered() { // Stop any currently running capture session @@ -676,7 +816,7 @@ void MainWindow::on_actionViewZoomOut_triggered() void MainWindow::on_actionViewZoomFit_triggered() { - view_->zoom_fit(); + view_->zoom_fit(action_view_zoom_fit_->isChecked()); } void MainWindow::on_actionViewZoomOneToOne_triggered() @@ -684,12 +824,22 @@ void MainWindow::on_actionViewZoomOneToOne_triggered() view_->zoom_one_to_one(); } +void MainWindow::on_actionViewStickyScrolling_triggered() +{ + view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); +} + +void MainWindow::on_actionViewColouredBg_triggered() +{ + view_->enable_coloured_bg(action_view_coloured_bg_->isChecked()); +} + void MainWindow::on_actionViewShowCursors_triggered() { assert(view_); const bool show = !view_->cursors_shown(); - if(show) + if (show) view_->centre_cursors(); view_->show_cursors(show); @@ -701,6 +851,16 @@ void MainWindow::on_actionAbout_triggered() dlg.exec(); } +void MainWindow::sticky_scrolling_changed(bool state) +{ + action_view_sticky_scrolling_->setChecked(state); +} + +void MainWindow::always_zoom_to_fit_changed(bool state) +{ + action_view_zoom_fit_->setChecked(state); +} + void MainWindow::add_decoder(srd_decoder *decoder) { #ifdef ENABLE_DECODE