]> sigrok.org Git - pulseview.git/blobdiff - pv/toolbars/mainbar.cpp
Fix the build for older glibmm versions.
[pulseview.git] / pv / toolbars / mainbar.cpp
index 82b5fab04dc097cdaebfcb2f62dc19ff34164a1b..adecf6ac978b7f6f8c652af80b3a0e7c6ebe5a84 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the PulseView project.
  *
- * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
+ * Copyright (C) 2012-2015 Joel Holdsworth <joel@airwebreathe.org.uk>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,7 +20,8 @@
 
 #include <extdef.h>
 
-#include <assert.h>
+#include <algorithm>
+#include <cassert>
 
 #include <QAction>
 #include <QDebug>
 #include <pv/popups/deviceoptions.hpp>
 #include <pv/popups/channels.hpp>
 #include <pv/util.hpp>
+#include <pv/widgets/exportmenu.hpp>
 
-#include <libsigrok/libsigrok.hpp>
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
+using std::back_inserter;
+using std::copy;
+using std::list;
 using std::map;
-using std::vector;
 using std::max;
 using std::min;
 using std::shared_ptr;
 using std::string;
+using std::vector;
 
 using sigrok::Capability;
 using sigrok::ConfigKey;
@@ -61,8 +66,8 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
        QToolBar("Sampling Bar", &main_window),
        session_(session),
        main_window_(main_window),
-       device_selector_(this),
-       updating_device_selector_(false),
+       device_selector_(this, session.device_manager(),
+               main_window.action_connect()),
        configure_button_(this),
        configure_button_action_(NULL),
        channels_button_(this),
@@ -81,6 +86,26 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
 
        setMovable(false);
        setFloatable(false);
+       setContextMenuPolicy(Qt::PreventContextMenu);
+
+       // Save button
+       QToolButton *const save_button = new QToolButton(this);
+
+       widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
+               session.device_manager().context(),
+               main_window.action_save_as());
+       connect(export_menu,
+               SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
+               &main_window_,
+               SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
+
+       save_button->setMenu(export_menu);
+       save_button->setDefaultAction(main_window.action_save_as());
+       save_button->setPopupMode(QToolButton::MenuButtonPopup);
+
+       // Device selector menu
+       connect(&device_selector_, SIGNAL(device_selected()),
+               this, SLOT(on_device_selected()));
 
        // Setup the decoder button
 #ifdef ENABLE_DECODE
@@ -109,7 +134,7 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
 
        // Setup the toolbar
        addAction(main_window.action_open());
-       addAction(main_window.action_save_as());
+       addWidget(save_button);
        addSeparator();
        addAction(main_window.action_view_zoom_in());
        addAction(main_window.action_view_zoom_out());
@@ -121,8 +146,6 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
 
        connect(&run_stop_button_, SIGNAL(clicked()),
                this, SLOT(on_run_stop()));
-       connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
-               this, SLOT(on_device_selected()));
        connect(&sample_count_, SIGNAL(value_changed()),
                this, SLOT(on_sample_count_changed()));
        connect(&sample_rate_, SIGNAL(value_changed()),
@@ -161,48 +184,22 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
        sample_rate_.installEventFilter(this);
 }
 
-void MainBar::set_device_list(
-       const std::list< std::shared_ptr<sigrok::Device> > &devices,
-       shared_ptr<Device> selected)
+void MainBar::update_device_list()
 {
-       int selected_index = -1;
-
-       assert(selected);
-
-       updating_device_selector_ = true;
-
-       device_selector_.clear();
+       DeviceManager &mgr = session_.device_manager();
+       shared_ptr<Device> selected_device = session_.device();
+       list< shared_ptr<Device> > devs;
 
-       for (auto device : devices) {
-               assert(device);
+       copy(mgr.devices().begin(), mgr.devices().end(), back_inserter(devs));
 
-               string display_name =
-                       session_.device_manager().get_display_name(device);
-
-               if (selected == device)
-                       selected_index = device_selector_.count();
-
-               device_selector_.addItem(display_name.c_str(),
-                       qVariantFromValue(device));
-       }
-
-       // The selected device should have been in the list
-       assert(selected_index != -1);
-       device_selector_.setCurrentIndex(selected_index);
+       if (std::find(devs.begin(), devs.end(), selected_device) == devs.end())
+               devs.push_back(selected_device);
+       assert(selected_device);
 
+       device_selector_.set_device_list(devs, selected_device);
        update_device_config_widgets();
-
-       updating_device_selector_ = false;
 }
 
-shared_ptr<Device> MainBar::get_selected_device() const
-{
-       const int index = device_selector_.currentIndex();
-       if (index < 0)
-               return shared_ptr<Device>();
-
-       return device_selector_.itemData(index).value<shared_ptr<Device>>();
-}
 
 void MainBar::set_capture_state(pv::Session::capture_state state)
 {
@@ -223,7 +220,7 @@ void MainBar::update_sample_rate_selector()
        if (updating_sample_rate_)
                return;
 
-       const shared_ptr<Device> device = get_selected_device();
+       const shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -234,8 +231,6 @@ void MainBar::update_sample_rate_selector()
        const auto iter = keys.find(ConfigKey::SAMPLERATE);
        if (iter != keys.end() &&
                (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
-               const auto keys = device->config_keys(
-                       ConfigKey::DEVICE_OPTIONS);
                try {
                        gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
                } catch(const sigrok::Error &e) {
@@ -244,7 +239,7 @@ void MainBar::update_sample_rate_selector()
                }
        }
 
-       if (!gvar_dict) {
+       if (!gvar_dict.gobj()) {
                sample_rate_.show_none();
                updating_sample_rate_ = false;
                return;
@@ -296,7 +291,7 @@ void MainBar::update_sample_rate_selector_value()
        if (updating_sample_rate_)
                return;
 
-       const shared_ptr<Device> device = get_selected_device();
+       const shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -319,7 +314,7 @@ void MainBar::update_sample_count_selector()
        if (updating_sample_count_)
                return;
 
-       const shared_ptr<Device> device = get_selected_device();
+       const shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -347,7 +342,7 @@ void MainBar::update_sample_count_selector()
                try {
                        auto gvar =
                                device->config_list(ConfigKey::LIMIT_SAMPLES);
-                       if (gvar)
+                       if (gvar.gobj())
                                g_variant_get(gvar.gobj(), "(tt)",
                                        &min_sample_count, &max_sample_count);
                } catch(const sigrok::Error &e) {
@@ -380,7 +375,7 @@ void MainBar::update_device_config_widgets()
 {
        using namespace pv::popups;
 
-       const shared_ptr<Device> device = get_selected_device();
+       const shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -438,7 +433,7 @@ void MainBar::commit_sample_count()
        if (updating_sample_count_)
                return;
 
-       const shared_ptr<Device> device = get_selected_device();
+       const shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -468,7 +463,7 @@ void MainBar::commit_sample_rate()
        if (updating_sample_rate_)
                return;
 
-       const shared_ptr<Device> device = get_selected_device();
+       const shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -492,10 +487,7 @@ void MainBar::commit_sample_rate()
 
 void MainBar::on_device_selected()
 {
-       if (updating_device_selector_)
-               return;
-
-       shared_ptr<Device> device = get_selected_device();
+       shared_ptr<Device> device = device_selector_.selected_device();
        if (!device)
                return;