]> sigrok.org Git - pulseview.git/blobdiff - pv/mainwindow.cpp
Removed context bar
[pulseview.git] / pv / mainwindow.cpp
index 3aeb1227edcc1fb00247767002ee3087bb78494a..97103ad514724278debbcb3588c585c8534331b5 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include <sigrokdecode.h>
+#include <libsigrokdecode/libsigrokdecode.h>
 
 #include <boost/bind.hpp>
+#include <boost/foreach.hpp>
 
 #include <QAction>
 #include <QApplication>
 #include <QWidget>
 
 #include "mainwindow.h"
+
+#include "devicemanager.h"
 #include "dialogs/about.h"
 #include "dialogs/connect.h"
+#include "dialogs/decoder.h"
 #include "toolbars/samplingbar.h"
 #include "view/view.h"
 
 #include <glib.h>
 #include <libsigrok/libsigrok.h>
 
+using namespace boost;
+using namespace std;
 
 namespace pv {
 
-MainWindow::MainWindow(const char *open_file_name,
+namespace view {
+class SelectableItem;
+}
+
+MainWindow::MainWindow(DeviceManager &device_manager,
+       const char *open_file_name,
        QWidget *parent) :
-       QMainWindow(parent)
+       QMainWindow(parent),
+       _device_manager(device_manager),
+       _session(device_manager),
+       _decoders_add_mapper(this)
 {
        setup_ui();
        if (open_file_name) {
@@ -83,6 +97,7 @@ void MainWindow::setup_ui()
        setCentralWidget(_central_widget);
 
        _view = new pv::view::View(_session, this);
+
        _vertical_layout->addWidget(_view);
 
        // Setup the menu bar
@@ -150,12 +165,27 @@ 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(
                "MainWindow", "Show &Cursors", 0, QApplication::UnicodeUTF8));
        _menu_view->addAction(_action_view_show_cursors);
 
+       // Decoders Menu
+       _menu_decoders = new QMenu(_menu_bar);
+       _menu_decoders->setTitle(QApplication::translate(
+               "MainWindow", "&Decoders", 0, QApplication::UnicodeUTF8));
+
+       _menu_decoders_add = new QMenu(_menu_decoders);
+       _menu_decoders_add->setTitle(QApplication::translate(
+               "MainWindow", "&Add", 0, QApplication::UnicodeUTF8));
+       setup_add_decoders(_menu_decoders_add);
+
+       _menu_decoders->addMenu(_menu_decoders_add);
+       connect(&_decoders_add_mapper, SIGNAL(mapped(QObject*)),
+               this, SLOT(add_decoder(QObject*)));
+
        // Help Menu
        _menu_help = new QMenu(_menu_bar);
        _menu_help->setTitle(QApplication::translate(
@@ -169,25 +199,33 @@ void MainWindow::setup_ui()
 
        _menu_bar->addAction(_menu_file->menuAction());
        _menu_bar->addAction(_menu_view->menuAction());
+       _menu_bar->addAction(_menu_decoders->menuAction());
        _menu_bar->addAction(_menu_help->menuAction());
 
        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);
 
+       // Setup the sampling bar
        _sampling_bar = new toolbars::SamplingBar(this);
-       scan_devices();
+
+       // 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);
 
+       // Set the title
        setWindowTitle(QApplication::translate("MainWindow", "PulseView", 0,
                QApplication::UnicodeUTF8));
 
@@ -197,23 +235,6 @@ void MainWindow::setup_ui()
 
 }
 
-void MainWindow::scan_devices()
-{
-       _devices.clear();
-
-       /* Scan all drivers for all devices. */
-       struct sr_dev_driver **const drivers = sr_driver_list();
-       for (struct sr_dev_driver **driver = drivers; *driver; driver++) {
-               GSList *const devices = sr_driver_scan(*driver, NULL);
-               for (GSList *l = devices; l; l = l->next)
-                       _devices.push_back((sr_dev_inst*)l->data);
-               g_slist_free(devices);
-       }
-
-       assert(_sampling_bar);
-       _sampling_bar->set_device_list(_devices);
-}
-
 void MainWindow::session_error(
        const QString text, const QString info_text)
 {
@@ -222,6 +243,30 @@ void MainWindow::session_error(
                Q_ARG(QString, info_text));
 }
 
+void MainWindow::update_device_list(struct sr_dev_inst *selected_device)
+{
+       assert(_sampling_bar);
+
+       const list<sr_dev_inst*> &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(
@@ -243,28 +288,54 @@ void MainWindow::show_session_error(
        msg.exec();
 }
 
+gint MainWindow::decoder_name_cmp(gconstpointer a, gconstpointer b)
+{
+       return strcmp(((const srd_decoder*)a)->name,
+               ((const srd_decoder*)b)->name);
+}
+
+void MainWindow::setup_add_decoders(QMenu *parent)
+{
+       GSList *l = g_slist_sort(g_slist_copy(
+               (GSList*)srd_decoder_list()), decoder_name_cmp);
+       for(; l; l = l->next)
+       {
+               QAction *const action = parent->addAction(QString(
+                       ((srd_decoder*)l->data)->name));
+               action->setData(qVariantFromValue(l->data));
+               _decoders_add_mapper.setMapping(action, action);
+               connect(action, SIGNAL(triggered()),
+                       &_decoders_add_mapper, SLOT(map()));
+       }
+       g_slist_free(l);
+}
+
 void MainWindow::on_actionOpen_triggered()
 {
+       // 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"), "",
-               tr("Sigrok Sessions (*.sr)"));
-       load_file(file_name);
+               this, tr("Open File"), "", filters);
+       if (!file_name.isEmpty())
+               load_file(file_name);
 }
 
 void MainWindow::on_actionConnect_triggered()
 {
-       dialogs::Connect dlg(this);
-       if (!dlg.exec())
-               return;
+       // Stop any currently running capture session
+       _session.stop_capture();
 
-       struct sr_dev_inst *const sdi = dlg.get_selected_device();
-       if (sdi) {
-               assert(_sampling_bar);
+       dialogs::Connect dlg(this, _device_manager);
 
-               _devices.push_back(sdi);
-               _sampling_bar->set_device_list(_devices);
-               _sampling_bar->set_selected_device(sdi);
-       }
+       // 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()
@@ -285,7 +356,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()
@@ -294,17 +370,43 @@ void MainWindow::on_actionAbout_triggered()
        dlg.exec();
 }
 
+void MainWindow::device_selected()
+{
+       _session.set_device(_sampling_bar->get_selected_device());
+}
+
+void MainWindow::add_decoder(QObject *action)
+{
+       assert(action);
+       srd_decoder *const dec =
+               (srd_decoder*)((QAction*)action)->data().value<void*>();
+       assert(dec);
+
+       const std::vector< boost::shared_ptr<view::Signal> > &sigs =
+               _session.get_signals();
+
+       GHashTable *const options = g_hash_table_new_full(g_str_hash,
+               g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
+
+       dialogs::Decoder dlg(this, dec, sigs, options);
+       if(dlg.exec() != QDialog::Accepted) {
+               g_hash_table_destroy(options);
+               return;
+       }
+
+       _session.add_decoder(dec, dlg.get_probes(), options);
+}
+
 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(),
+               _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;
@@ -313,7 +415,7 @@ 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);
 }
 
 } // namespace pv