X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Fmainwindow.cpp;h=ca532ee2044ca598cbb1d99fefa6a29a354d3b42;hp=3a59bea10bfa0fc6103a929d969e65060ad26b4f;hb=2b49eeb02d92b4744f8ebfcfd38ef27384cdd751;hpb=ab1d13eefe929b466699eac93d173f45d59e60fd diff --git a/pv/mainwindow.cpp b/pv/mainwindow.cpp index 3a59bea1..ca532ee2 100644 --- a/pv/mainwindow.cpp +++ b/pv/mainwindow.cpp @@ -18,14 +18,18 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -extern "C" { -#include -} +#ifdef ENABLE_SIGROKDECODE +#include +#endif + +#include +#include #include #include #include #include +#include #include #include #include @@ -33,11 +37,14 @@ extern "C" { #include #include "mainwindow.h" -#include "samplingbar.h" + +#include "devicemanager.h" #include "dialogs/about.h" +#include "dialogs/connect.h" +#include "toolbars/contextbar.h" +#include "toolbars/samplingbar.h" #include "view/view.h" -extern "C" { /* __STDC_FORMAT_MACROS is required for PRIu64 and friends (in C++). */ #define __STDC_FORMAT_MACROS #include @@ -45,14 +52,30 @@ extern "C" { #include #include #include -} + +using namespace boost; +using namespace std; namespace pv { -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent) +namespace view { +class SelectableItem; +} + +MainWindow::MainWindow(DeviceManager &device_manager, + const char *open_file_name, + QWidget *parent) : + QMainWindow(parent), + _device_manager(device_manager), + _session(device_manager) { setup_ui(); + if (open_file_name) { + const QString s(QString::fromUtf8(open_file_name)); + QMetaObject::invokeMethod(this, "load_file", + Qt::QueuedConnection, + Q_ARG(QString, s)); + } } void MainWindow::setup_ui() @@ -75,6 +98,9 @@ void MainWindow::setup_ui() setCentralWidget(_central_widget); _view = new pv::view::View(_session, this); + connect(_view, SIGNAL(selection_changed()), this, + SLOT(view_selection_changed())); + _vertical_layout->addWidget(_view); // Setup the menu bar @@ -92,9 +118,28 @@ void MainWindow::setup_ui() _action_open->setIcon(QIcon::fromTheme("document-open", QIcon(":/icons/document-open.png"))); _action_open->setObjectName(QString::fromUtf8("actionOpen")); - _menu_file->addAction(_action_open); + _menu_file->addSeparator(); + + _action_connect = new QAction(this); + _action_connect->setText(QApplication::translate( + "MainWindow", "&Connect to Device...", 0, + QApplication::UnicodeUTF8)); + _action_connect->setObjectName(QString::fromUtf8("actionConnect")); + _menu_file->addAction(_action_connect); + + _menu_file->addSeparator(); + + _action_quit = new QAction(this); + _action_quit->setText(QApplication::translate( + "MainWindow", "&Quit", 0, QApplication::UnicodeUTF8)); + _action_quit->setIcon(QIcon::fromTheme("application-exit", + QIcon(":/icons/application-exit.png"))); + _action_quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q)); + _action_quit->setObjectName(QString::fromUtf8("actionQuit")); + _menu_file->addAction(_action_quit); + // View Menu _menu_view = new QMenu(_menu_bar); _menu_view->setTitle(QApplication::translate( @@ -123,6 +168,7 @@ void MainWindow::setup_ui() _action_view_show_cursors = new QAction(this); _action_view_show_cursors->setCheckable(true); _action_view_show_cursors->setChecked(_view->cursors_shown()); + _action_view_show_cursors->setShortcut(QKeySequence(Qt::Key_C)); _action_view_show_cursors->setObjectName( QString::fromUtf8("actionViewShowCursors")); _action_view_show_cursors->setText(QApplication::translate( @@ -147,19 +193,32 @@ void MainWindow::setup_ui() setMenuBar(_menu_bar); QMetaObject::connectSlotsByName(this); - // Setup the toolbars - _toolbar = new QToolBar(this); + // Setup the toolbar + _toolbar = new QToolBar(tr("Main Toolbar"), this); _toolbar->addAction(_action_open); _toolbar->addSeparator(); _toolbar->addAction(_action_view_zoom_in); _toolbar->addAction(_action_view_zoom_out); addToolBar(_toolbar); - _sampling_bar = new SamplingBar(this); + // Setup the sampling bar + _sampling_bar = new toolbars::SamplingBar(this); + + // Populate the device list and select the initially selected device + update_device_list(); + + connect(_sampling_bar, SIGNAL(device_selected()), this, + SLOT(device_selected())); connect(_sampling_bar, SIGNAL(run_stop()), this, SLOT(run_stop())); addToolBar(_sampling_bar); + // Setup the context bar + _context_bar = new toolbars::ContextBar(this); + addToolBar(_context_bar); + insertToolBarBreak(_context_bar); + + // Set the title setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0, QApplication::UnicodeUTF8)); @@ -169,12 +228,90 @@ void MainWindow::setup_ui() } +void MainWindow::session_error( + const QString text, const QString info_text) +{ + QMetaObject::invokeMethod(this, "show_session_error", + Qt::QueuedConnection, Q_ARG(QString, text), + Q_ARG(QString, info_text)); +} + +void MainWindow::update_device_list(struct sr_dev_inst *selected_device) +{ + assert(_sampling_bar); + + const list &devices = _device_manager.devices(); + _sampling_bar->set_device_list(devices); + + if (!selected_device && !devices.empty()) { + // Fall back to the first device in the list. + selected_device = devices.front(); + + // Try and find the demo device and select that by default + BOOST_FOREACH (struct sr_dev_inst *sdi, devices) + if (strcmp(sdi->driver->name, "demo") == 0) { + selected_device = sdi; + } + } + + if (selected_device) { + _sampling_bar->set_selected_device(selected_device); + _session.set_device(selected_device); + } +} + +void MainWindow::load_file(QString file_name) +{ + const QString errorMessage( + QString("Failed to load file %1").arg(file_name)); + const QString infoMessage; + _session.load_file(file_name.toStdString(), + boost::bind(&MainWindow::session_error, this, + errorMessage, infoMessage)); +} + +void MainWindow::show_session_error( + const QString text, const QString info_text) +{ + QMessageBox msg(this); + msg.setText(text); + msg.setInformativeText(info_text); + msg.setStandardButtons(QMessageBox::Ok); + msg.setIcon(QMessageBox::Warning); + msg.exec(); +} + void MainWindow::on_actionOpen_triggered() { - QString file_name = QFileDialog::getOpenFileName( - this, tr("Open File"), "", - tr("Sigrok Sessions (*.sr)")); - _session.load_file(file_name.toStdString()); + // Enumerate the file formats + QString filters(tr("Sigrok Sessions (*.sr)")); + filters.append(tr(";;All Files (*.*)")); + + // Show the dialog + const QString file_name = QFileDialog::getOpenFileName( + this, tr("Open File"), "", filters); + if (!file_name.isEmpty()) + load_file(file_name); +} + +void MainWindow::on_actionConnect_triggered() +{ + // Stop any currently running capture session + _session.stop_capture(); + + dialogs::Connect dlg(this, _device_manager); + + // If the user selected a device, select it in the device list. Select the + // current device otherwise. + struct sr_dev_inst *const sdi = dlg.exec() ? + dlg.get_selected_device() : _session.get_device(); + + update_device_list(sdi); +} + +void MainWindow::on_actionQuit_triggered() +{ + close(); } void MainWindow::on_actionViewZoomIn_triggered() @@ -190,7 +327,12 @@ void MainWindow::on_actionViewZoomOut_triggered() void MainWindow::on_actionViewShowCursors_triggered() { assert(_view); - _view->show_cursors(_action_view_show_cursors->isChecked()); + + const bool show = !_view->cursors_shown(); + if(show) + _view->centre_cursors(); + + _view->show_cursors(show); } void MainWindow::on_actionAbout_triggered() @@ -199,16 +341,21 @@ void MainWindow::on_actionAbout_triggered() dlg.exec(); } +void MainWindow::device_selected() +{ + _session.set_device(_sampling_bar->get_selected_device()); +} + void MainWindow::run_stop() { switch(_session.get_capture_state()) { case SigSession::Stopped: - _session.start_capture( - _sampling_bar->get_selected_device(), - _sampling_bar->get_record_length(), - _sampling_bar->get_sample_rate()); + _session.start_capture(_sampling_bar->get_record_length(), + boost::bind(&MainWindow::session_error, this, + QString("Capture failed"), _1)); break; + case SigSession::AwaitingTrigger: case SigSession::Running: _session.stop_capture(); break; @@ -217,7 +364,16 @@ void MainWindow::run_stop() void MainWindow::capture_state_changed(int state) { - _sampling_bar->set_sampling(state != SigSession::Stopped); + _sampling_bar->set_capture_state((pv::SigSession::capture_state)state); +} + +void MainWindow::view_selection_changed() +{ + assert(_context_bar); + + const list > items( + _view->selected_items()); + _context_bar->set_selected_items(items); } } // namespace pv