X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fmainwindow.cpp;h=8ab1a24b70b0f8ccf8c073231640880e93e3e28a;hp=e6f1a5e156c5fa36b72194e9cc6394c704a7a4f5;hb=a45b9b9ee6f15da272c2e743122097e6696fc7b5;hpb=f32905530347e1020d5ce7959123cf797c9a4829 diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index e6f1a5e1..8ab1a24b 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -42,9 +42,13 @@ #include #include +#include + #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" @@ -71,7 +75,9 @@ using std::cerr; using std::endl; using std::list; +using std::make_shared; using std::map; +using std::max; using std::pair; using std::shared_ptr; using std::string; @@ -100,6 +106,7 @@ MainWindow::MainWindow(DeviceManager &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)), @@ -107,12 +114,15 @@ MainWindow::MainWindow(DeviceManager &device_manager, 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.empty()) @@ -121,6 +131,16 @@ MainWindow::MainWindow(DeviceManager &device_manager, load_init_file(open_file_name, open_file_format); } +MainWindow::~MainWindow() +{ + for (auto entry : view_docks_) { + const std::shared_ptr dock = entry.first; + dock->setWidget(0); + const std::shared_ptr view = entry.second; + session_.deregister_view(view); + } +} + QAction* MainWindow::action_open() const { return action_open_; @@ -131,6 +151,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_; @@ -166,6 +191,11 @@ 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_; @@ -183,14 +213,75 @@ QMenu* MainWindow::menu_decoder_add() const } #endif +shared_ptr MainWindow::get_active_view() const +{ + // If there's only one view, use it... + if (view_docks_.size() == 1) + return view_docks_.begin()->second; + + // ...otherwise find the dock widget the widget with focus is contained in + QObject *w = QApplication::focusWidget(); + QDockWidget *dock = 0; + + while (w) { + dock = qobject_cast(w); + if (dock) + break; + w = w->parent(); + } + + // Get the view contained in the dock widget + for (auto entry : view_docks_) + if (entry.first.get() == dock) + return entry.second; + + return shared_ptr(); +} + +shared_ptr MainWindow::add_view(const QString &title, + view::ViewType type, Session &session) +{ + shared_ptr v; + + if (type == pv::view::TraceView) + v = make_shared(session, this); + + if (v) { + shared_ptr dock = make_shared(title, this); + dock->setWidget(v.get()); + dock->setObjectName(title); + addDockWidget(Qt::TopDockWidgetArea, dock.get()); + view_docks_[dock] = v; + + dock->setFeatures(QDockWidget::DockWidgetMovable | + QDockWidget::DockWidgetFloatable); + + if (type == view::TraceView) { + connect(&session, SIGNAL(trigger_event(util::Timestamp)), v.get(), + SLOT(trigger_event(util::Timestamp))); + connect(v.get(), SIGNAL(sticky_scrolling_changed(bool)), this, + SLOT(sticky_scrolling_changed(bool))); + connect(v.get(), SIGNAL(always_zoom_to_fit_changed(bool)), this, + SLOT(always_zoom_to_fit_changed(bool))); + + v->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); + v->enable_coloured_bg(action_view_coloured_bg_->isChecked()); + action_view_show_cursors_->setChecked(v->cursors_shown()); + } + + session.register_view(v); + } + + return v; +} + 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(); @@ -205,7 +296,7 @@ void MainWindow::select_device(shared_ptr device) session_.set_device(device); else session_.set_default_device(); - } catch(const QString &e) { + } catch (const QString &e) { QMessageBox msg(this); msg.setText(e); msg.setInformativeText(tr("Failed to Select Device")); @@ -215,16 +306,50 @@ 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; + // Make sure there's a view selected to pull the data from + shared_ptr view = get_active_view(); + if (!view) { + show_session_error(tr("No View Selected"), tr("Please click on the " \ + "view whose data you want to save and try again.")); + return; + } + // Stop any currently running capture session session_.stop_capture(); 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 = + std::max((double)0, 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( @@ -234,7 +359,7 @@ void MainWindow::export_file(shared_ptr format) filter += "(*.*)"; else filter += QString("(*.%1);;%2 (*.*)").arg( - QString::fromStdString(join(exts, ", *."))).arg( + QString::fromStdString(join(exts, ", *.")), tr("All Files")); // Show the file dialog @@ -260,7 +385,7 @@ void MainWindow::export_file(shared_ptr format) } StoreProgress *dlg = new StoreProgress(file_name, format, options, - session_, this); + sample_range, session_, this); dlg->run(); } @@ -275,7 +400,7 @@ void MainWindow::import_file(shared_ptr format) const vector exts = format->extensions(); const QString filter = exts.empty() ? "" : tr("%1 files (*.%2)").arg( - QString::fromStdString(format->description())).arg( + QString::fromStdString(format->description()), QString::fromStdString(join(exts, ", *."))); // Show the file dialog @@ -311,20 +436,9 @@ void MainWindow::setup_ui() // Set the window icon QIcon icon; - icon.addFile(QString(":/icons/sigrok-logo-notext.svg")); + icon.addFile(QString(":/icons/sigrok-logo-notext.png")); setWindowIcon(icon); - // Setup the central widget - central_widget_ = new QWidget(this); - vertical_layout_ = new QVBoxLayout(central_widget_); - vertical_layout_->setSpacing(6); - vertical_layout_->setContentsMargins(0, 0, 0, 0); - setCentralWidget(central_widget_); - - view_ = new pv::view::View(session_, this); - - vertical_layout_->addWidget(view_); - // Setup the menu bar pv::widgets::HidingMenuBar *const menu_bar = new pv::widgets::HidingMenuBar(this); @@ -347,6 +461,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, @@ -418,22 +539,29 @@ void MainWindow::setup_ui() QString::fromUtf8("actionViewZoomOneToOne")); menu_view->addAction(action_view_zoom_one_to_one_); - menu_file->addSeparator(); + menu_view->addSeparator(); action_view_sticky_scrolling_->setCheckable(true); action_view_sticky_scrolling_->setChecked(true); - action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_R)); + action_view_sticky_scrolling_->setShortcut(QKeySequence(Qt::Key_S)); action_view_sticky_scrolling_->setObjectName( QString::fromUtf8("actionViewStickyScrolling")); - action_view_sticky_scrolling_->setText(tr("Sticky Sc&rolling")); + 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_); 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", QIcon(":/icons/show-cursors.svg"))); action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C)); @@ -479,6 +607,9 @@ void MainWindow::setup_ui() // Setup the toolbar main_bar_ = new toolbars::MainBar(session_, *this); + // Set up the initial view + add_view(tr("Untitled"), pv::view::TraceView, session_); + // Populate the device list and select the initially selected device update_device_list(); @@ -492,20 +623,16 @@ void MainWindow::setup_ui() SLOT(capture_state_changed(int))); connect(&session_, SIGNAL(device_selected()), this, SLOT(device_selected())); - - // 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() { +void MainWindow::select_init_device() +{ QSettings settings; map dev_info; list key_list; + shared_ptr device; - // Re-select last used device if possible. + // 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"); @@ -523,8 +650,24 @@ void MainWindow::select_init_device() { dev_info.insert(std::make_pair(key, value)); } - const shared_ptr device = - device_manager_.find_device_from_info(dev_info); + 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(); @@ -532,7 +675,8 @@ void MainWindow::select_init_device() { } void MainWindow::load_init_file(const std::string &file_name, - const std::string &format) { + const std::string &format) +{ shared_ptr input_format; if (!format.empty()) { @@ -577,7 +721,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())); @@ -623,7 +766,6 @@ void MainWindow::load_file(QString file_name, { const QString errorMessage( QString("Failed to load file %1").arg(file_name)); - const QString infoMessage; try { if (format) @@ -637,7 +779,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(); @@ -646,7 +788,7 @@ 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); }); } @@ -700,6 +842,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 @@ -722,38 +869,57 @@ void MainWindow::on_actionQuit_triggered() void MainWindow::on_actionViewZoomIn_triggered() { - view_->zoom(1); + shared_ptr view = get_active_view(); + if (view) + view->zoom(1); } void MainWindow::on_actionViewZoomOut_triggered() { - view_->zoom(-1); + shared_ptr view = get_active_view(); + if (view) + view->zoom(-1); } void MainWindow::on_actionViewZoomFit_triggered() { - view_->zoom_fit(action_view_zoom_fit_->isChecked()); + shared_ptr view = get_active_view(); + if (view) + view->zoom_fit(action_view_zoom_fit_->isChecked()); } void MainWindow::on_actionViewZoomOneToOne_triggered() { - view_->zoom_one_to_one(); + shared_ptr view = get_active_view(); + if (view) + view->zoom_one_to_one(); } void MainWindow::on_actionViewStickyScrolling_triggered() { - view_->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); + shared_ptr view = get_active_view(); + if (view) + view->enable_sticky_scrolling(action_view_sticky_scrolling_->isChecked()); +} + +void MainWindow::on_actionViewColouredBg_triggered() +{ + shared_ptr view = get_active_view(); + if (view) + view->enable_coloured_bg(action_view_coloured_bg_->isChecked()); } void MainWindow::on_actionViewShowCursors_triggered() { - assert(view_); + shared_ptr view = get_active_view(); + if (!view) + return; - const bool show = !view_->cursors_shown(); + const bool show = !view->cursors_shown(); if (show) - view_->centre_cursors(); + view->centre_cursors(); - view_->show_cursors(show); + view->show_cursors(show); } void MainWindow::on_actionAbout_triggered() @@ -791,8 +957,11 @@ void MainWindow::device_selected() { // Set the title to include the device/file name const shared_ptr device = session_.device(); - if (!device) + + if (!device) { + main_bar_->reset_device_selector(); return; + } const string display_name = device->display_name(device_manager_); setWindowTitle(tr("%1 - PulseView").arg(display_name.c_str()));