]> sigrok.org Git - pulseview.git/blobdiff - pv/toolbars/mainbar.cpp
Fix #1147 by implementing decoder selector subwindow
[pulseview.git] / pv / toolbars / mainbar.cpp
index 6a5992bf53ce60e7872d0e895ba09f49f35646d6..85e55fb3b17aa3194ae481c0b5fde9a1f1c20ceb 100644 (file)
@@ -14,8 +14,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <extdef.h>
 #include <pv/dialogs/inputoutputoptions.hpp>
 #include <pv/dialogs/storeprogress.hpp>
 #include <pv/mainwindow.hpp>
-#include <pv/popups/deviceoptions.hpp>
 #include <pv/popups/channels.hpp>
+#include <pv/popups/deviceoptions.hpp>
 #include <pv/util.hpp>
-#include <pv/view/view.hpp>
+#include <pv/views/trace/view.hpp>
 #include <pv/widgets/exportmenu.hpp>
 #include <pv/widgets/importmenu.hpp>
 #ifdef ENABLE_DECODE
-#include <pv/widgets/decodermenu.hpp>
+#include <pv/data/decodesignal.hpp>
 #endif
 
 #include <libsigrokcxx/libsigrokcxx.hpp>
 
 using std::back_inserter;
-using std::cerr;
 using std::copy;
-using std::endl;
 using std::list;
+using std::make_pair;
 using std::map;
 using std::max;
 using std::min;
 using std::pair;
+using std::set;
 using std::shared_ptr;
 using std::string;
 using std::vector;
@@ -87,20 +86,18 @@ const uint64_t MainBar::DefaultSampleCount = 1000000;
 const char *MainBar::SettingOpenDirectory = "MainWindow/OpenDirectory";
 const char *MainBar::SettingSaveDirectory = "MainWindow/SaveDirectory";
 
-MainBar::MainBar(Session &session, MainWindow &main_window) :
-       QToolBar("Sampling Bar", &main_window),
+MainBar::MainBar(Session &session, QWidget *parent, pv::views::trace::View *view) :
+       StandardBar(session, parent, view, false),
+       action_new_view_(new QAction(this)),
        action_open_(new QAction(this)),
        action_save_as_(new QAction(this)),
        action_save_selection_as_(new QAction(this)),
+       action_restore_setup_(new QAction(this)),
+       action_save_setup_(new QAction(this)),
        action_connect_(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_show_cursors_(new QAction(this)),
-       session_(session),
-       device_selector_(&main_window, session.device_manager(),
-               action_connect_),
+       open_button_(new QToolButton()),
+       save_button_(new QToolButton()),
+       device_selector_(parent, session.device_manager(), action_connect_),
        configure_button_(this),
        configure_button_action_(nullptr),
        channels_button_(this),
@@ -109,129 +106,101 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
        sample_rate_("Hz", this),
        updating_sample_rate_(false),
        updating_sample_count_(false),
-       sample_count_supported_(false),
-       icon_red_(":/icons/status-red.svg"),
-       icon_green_(":/icons/status-green.svg"),
-       icon_grey_(":/icons/status-grey.svg"),
-       run_stop_button_(this),
-       run_stop_button_action_(nullptr),
-       menu_button_(this)
+       sample_count_supported_(false)
 #ifdef ENABLE_DECODE
-       , menu_decoders_add_(new pv::widgets::DecoderMenu(this, true))
+       , add_decoder_button_(new QToolButton())
 #endif
 {
        setObjectName(QString::fromUtf8("MainBar"));
 
-       setMovable(false);
-       setFloatable(false);
        setContextMenuPolicy(Qt::PreventContextMenu);
 
        // Actions
+       action_new_view_->setText(tr("New &View"));
+       action_new_view_->setIcon(QIcon::fromTheme("window-new",
+               QIcon(":/icons/window-new.png")));
+       connect(action_new_view_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionNewView_triggered()));
+
        action_open_->setText(tr("&Open..."));
        action_open_->setIcon(QIcon::fromTheme("document-open",
                QIcon(":/icons/document-open.png")));
        action_open_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
-       action_open_->setObjectName(QString::fromUtf8("actionOpen"));
+       connect(action_open_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionOpen_triggered()));
+
+       action_restore_setup_->setText(tr("Restore Session Setu&p..."));
+       connect(action_restore_setup_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionRestoreSetup_triggered()));
 
        action_save_as_->setText(tr("&Save As..."));
        action_save_as_->setIcon(QIcon::fromTheme("document-save-as",
                QIcon(":/icons/document-save-as.png")));
        action_save_as_->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
-       action_save_as_->setObjectName(QString::fromUtf8("actionSaveAs"));
+       connect(action_save_as_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionSaveAs_triggered()));
 
        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"));
+       connect(action_save_selection_as_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionSaveSelectionAs_triggered()));
+
+       action_save_setup_->setText(tr("Save Session Setu&p..."));
+       connect(action_save_setup_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionSaveSetup_triggered()));
 
        widgets::ExportMenu *menu_file_export = new widgets::ExportMenu(this,
                session.device_manager().context());
        menu_file_export->setTitle(tr("&Export"));
-       connect(menu_file_export,
-               SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
-               this, SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
+       connect(menu_file_export, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
+               this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
 
        widgets::ImportMenu *menu_file_import = new widgets::ImportMenu(this,
                session.device_manager().context());
        menu_file_import->setTitle(tr("&Import"));
-       connect(menu_file_import,
-               SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
-               this, SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
+       connect(menu_file_import, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
+               this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
 
        action_connect_->setText(tr("&Connect to Device..."));
-       action_connect_->setObjectName(QString::fromUtf8("actionConnect"));
-
-       action_view_zoom_in_->setText(tr("Zoom &In"));
-       action_view_zoom_in_->setIcon(QIcon::fromTheme("zoom-in",
-               QIcon(":/icons/zoom-in.png")));
-       // simply using Qt::Key_Plus shows no + in the menu
-       action_view_zoom_in_->setShortcut(QKeySequence::ZoomIn);
-       action_view_zoom_in_->setObjectName(
-               QString::fromUtf8("actionViewZoomIn"));
-
-       action_view_zoom_out_->setText(tr("Zoom &Out"));
-       action_view_zoom_out_->setIcon(QIcon::fromTheme("zoom-out",
-               QIcon(":/icons/zoom-out.png")));
-       action_view_zoom_out_->setShortcut(QKeySequence::ZoomOut);
-       action_view_zoom_out_->setObjectName(
-               QString::fromUtf8("actionViewZoomOut"));
-
-       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")));
-       action_view_zoom_fit_->setShortcut(QKeySequence(Qt::Key_F));
-       action_view_zoom_fit_->setObjectName(
-               QString::fromUtf8("actionViewZoomFit"));
-
-       action_view_zoom_one_to_one_->setText(tr("Zoom to O&ne-to-One"));
-       action_view_zoom_one_to_one_->setIcon(QIcon::fromTheme("zoom-original",
-               QIcon(":/icons/zoom-original.png")));
-       action_view_zoom_one_to_one_->setShortcut(QKeySequence(Qt::Key_O));
-       action_view_zoom_one_to_one_->setObjectName(
-               QString::fromUtf8("actionViewZoomOneToOne"));
-
-       action_view_show_cursors_->setCheckable(true);
-       action_view_show_cursors_->setIcon(QIcon::fromTheme("show-cursors",
-               QIcon(":/icons/show-cursors.svg")));
-       action_view_show_cursors_->setShortcut(QKeySequence(Qt::Key_C));
-       action_view_show_cursors_->setObjectName(
-               QString::fromUtf8("actionViewShowCursors"));
-       action_view_show_cursors_->setText(tr("Show &Cursors"));
+       connect(action_connect_, SIGNAL(triggered(bool)),
+               this, SLOT(on_actionConnect_triggered()));
 
        // Open button
-       QToolButton *const open_button = new QToolButton(this);
+       vector<QAction*> open_actions;
+       open_actions.push_back(action_open_);
+       QAction* separator_o = new QAction(this);
+       separator_o->setSeparator(true);
+       open_actions.push_back(separator_o);
+       open_actions.push_back(action_restore_setup_);
 
        widgets::ImportMenu *import_menu = new widgets::ImportMenu(this,
-               session.device_manager().context(), action_open_);
-       connect(import_menu,
-               SIGNAL(format_selected(std::shared_ptr<sigrok::InputFormat>)),
-               &main_window,
-               SLOT(import_file(std::shared_ptr<sigrok::InputFormat>)));
+               session.device_manager().context(), open_actions);
+       connect(import_menu, SIGNAL(format_selected(shared_ptr<sigrok::InputFormat>)),
+               this, SLOT(import_file(shared_ptr<sigrok::InputFormat>)));
 
-       open_button->setMenu(import_menu);
-       open_button->setDefaultAction(action_open_);
-       open_button->setPopupMode(QToolButton::MenuButtonPopup);
+       open_button_->setMenu(import_menu);
+       open_button_->setDefaultAction(action_open_);
+       open_button_->setPopupMode(QToolButton::MenuButtonPopup);
 
        // Save button
-       QToolButton *const save_button = new QToolButton(this);
-
-       vector<QAction *> open_actions;
-       open_actions.push_back(action_save_as_);
-       open_actions.push_back(action_save_selection_as_);
+       vector<QAction*> save_actions;
+       save_actions.push_back(action_save_as_);
+       save_actions.push_back(action_save_selection_as_);
+       QAction* separator_s = new QAction(this);
+       separator_s->setSeparator(true);
+       save_actions.push_back(separator_s);
+       save_actions.push_back(action_save_setup_);
 
        widgets::ExportMenu *export_menu = new widgets::ExportMenu(this,
-               session.device_manager().context(),
-               open_actions);
-       connect(export_menu,
-               SIGNAL(format_selected(std::shared_ptr<sigrok::OutputFormat>)),
-               &main_window,
-               SLOT(export_file(std::shared_ptr<sigrok::OutputFormat>)));
+               session.device_manager().context(), save_actions);
+       connect(export_menu, SIGNAL(format_selected(shared_ptr<sigrok::OutputFormat>)),
+               this, SLOT(export_file(shared_ptr<sigrok::OutputFormat>)));
 
-       save_button->setMenu(export_menu);
-       save_button->setDefaultAction(action_save_as_);
-       save_button->setPopupMode(QToolButton::MenuButtonPopup);
+       save_button_->setMenu(export_menu);
+       save_button_->setDefaultAction(action_save_as_);
+       save_button_->setPopupMode(QToolButton::MenuButtonPopup);
 
        // Device selector menu
        connect(&device_selector_, SIGNAL(device_selected()),
@@ -239,31 +208,14 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
 
        // Setup the decoder button
 #ifdef ENABLE_DECODE
-       menu_decoders_add_->setTitle(tr("&Add"));
-       connect(menu_decoders_add_, SIGNAL(decoder_selected(srd_decoder*)),
-               this, SLOT(add_decoder(srd_decoder*)));
-
-       QToolButton *add_decoder_button = new QToolButton(this);
-       add_decoder_button->setIcon(QIcon::fromTheme("add-decoder",
-               QIcon(":/icons/add-decoder.svg")));
-       add_decoder_button->setPopupMode(QToolButton::InstantPopup);
-       add_decoder_button->setMenu(menu_decoders_add_);
-#endif
+       add_decoder_button_->setIcon(QIcon(":/icons/add-decoder.svg"));
+       add_decoder_button_->setPopupMode(QToolButton::InstantPopup);
+       add_decoder_button_->setToolTip(tr("Add protocol decoder"));
 
-       // Setup the toolbar
-       addWidget(open_button);
-       addWidget(save_button);
-       addSeparator();
-       addAction(action_view_zoom_in_);
-       addAction(action_view_zoom_out_);
-       addAction(action_view_zoom_fit_);
-       addAction(action_view_zoom_one_to_one_);
-       addSeparator();
-       addAction(action_view_show_cursors_);
-       addSeparator();
+       connect(add_decoder_button_, SIGNAL(clicked()),
+               this, SLOT(on_add_decoder_clicked()));
+#endif
 
-       connect(&run_stop_button_, SIGNAL(clicked()),
-               this, SLOT(on_run_stop()));
        connect(&sample_count_, SIGNAL(value_changed()),
                this, SLOT(on_sample_count_changed()));
        connect(&sample_rate_, SIGNAL(value_changed()),
@@ -273,50 +225,27 @@ MainBar::MainBar(Session &session, MainWindow &main_window) :
 
        set_capture_state(pv::Session::Stopped);
 
-       configure_button_.setIcon(QIcon::fromTheme("configure",
-               QIcon(":/icons/configure.png")));
+       configure_button_.setToolTip(tr("Configure Device"));
+       configure_button_.setIcon(QIcon::fromTheme("preferences-system",
+               QIcon(":/icons/preferences-system.png")));
 
-       channels_button_.setIcon(QIcon::fromTheme("channels",
-               QIcon(":/icons/channels.svg")));
-
-       run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
-
-       addWidget(&device_selector_);
-       configure_button_action_ = addWidget(&configure_button_);
-       channels_button_action_ = addWidget(&channels_button_);
-       addWidget(&sample_count_);
-       addWidget(&sample_rate_);
-       run_stop_button_action_ = addWidget(&run_stop_button_);
-#ifdef ENABLE_DECODE
-       addSeparator();
-       addWidget(add_decoder_button);
-#endif
+       channels_button_.setToolTip(tr("Configure Channels"));
+       channels_button_.setIcon(QIcon(":/icons/channels.svg"));
 
-       QWidget *const spacer = new QWidget();
-       spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
-       addWidget(spacer);
-
-       addWidget(&menu_button_);
+       add_toolbar_widgets();
 
        sample_count_.installEventFilter(this);
        sample_rate_.installEventFilter(this);
 
-       QMetaObject::connectSlotsByName(this);
-
        // Setup session_ events
-       connect(&session_, SIGNAL(capture_state_changed(int)), this,
-               SLOT(capture_state_changed(int)));
-       connect(&session_, SIGNAL(device_selected()), this,
-               SLOT(device_selected()));
+       connect(&session_, SIGNAL(capture_state_changed(int)),
+               this, SLOT(on_capture_state_changed(int)));
+       connect(&session, SIGNAL(device_changed()),
+               this, SLOT(on_device_changed()));
 
        update_device_list();
 }
 
-Session &MainBar::session(void) const
-{
-       return session_;
-}
-
 void MainBar::update_device_list()
 {
        DeviceManager &mgr = session_.device_manager();
@@ -332,15 +261,8 @@ void MainBar::update_device_list()
        update_device_config_widgets();
 }
 
-
 void MainBar::set_capture_state(pv::Session::capture_state state)
 {
-       const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
-       run_stop_button_.setIcon(*icons[state]);
-       run_stop_button_.setText((state == pv::Session::Stopped) ?
-               tr("Run") : tr("Stop"));
-       run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
-
        bool ui_enabled = (state == pv::Session::Stopped) ? true : false;
 
        device_selector_.setEnabled(ui_enabled);
@@ -355,47 +277,6 @@ void MainBar::reset_device_selector()
        device_selector_.reset();
 }
 
-void MainBar::select_device(shared_ptr<devices::Device> device)
-{
-       try {
-               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"));
-               msg.setStandardButtons(QMessageBox::Ok);
-               msg.setIcon(QMessageBox::Warning);
-               msg.exec();
-       }
-}
-
-void MainBar::load_init_file(const std::string &file_name,
-       const std::string &format)
-{
-       shared_ptr<InputFormat> input_format;
-
-       DeviceManager& device_manager = session_.device_manager();
-
-       if (!format.empty()) {
-               const map<string, shared_ptr<InputFormat> > formats =
-                       device_manager.context()->input_formats();
-               const auto iter = find_if(formats.begin(), formats.end(),
-                       [&](const pair<string, shared_ptr<InputFormat> > 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);
-}
-
 QAction* MainBar::action_open() const
 {
        return action_open_;
@@ -416,96 +297,20 @@ QAction* MainBar::action_connect() const
        return action_connect_;
 }
 
-QAction* MainBar::action_view_zoom_in() const
-{
-       return action_view_zoom_in_;
-}
-
-QAction* MainBar::action_view_zoom_out() const
-{
-       return action_view_zoom_out_;
-}
-
-QAction* MainBar::action_view_zoom_fit() const
-{
-       return action_view_zoom_fit_;
-}
-
-QAction* MainBar::action_view_zoom_one_to_one() const
-{
-       return action_view_zoom_one_to_one_;
-}
-
-QAction* MainBar::action_view_show_cursors() const
-{
-       return action_view_show_cursors_;
-}
-
-void MainBar::run_stop()
-{
-       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();
-               break;
-       }
-}
-
-void MainBar::load_file(QString file_name,
-       std::shared_ptr<sigrok::InputFormat> format,
-       const std::map<std::string, Glib::VariantBase> &options)
-{
-       DeviceManager& device_manager = session_.device_manager();
-
-       const QString errorMessage(
-               QString("Failed to load file %1").arg(file_name));
-
-       try {
-               if (format)
-                       session_.set_device(shared_ptr<devices::Device>(
-                               new devices::InputFile(
-                                       device_manager.context(),
-                                       file_name.toStdString(),
-                                       format, options)));
-               else
-                       session_.set_device(shared_ptr<devices::Device>(
-                               new devices::SessionFile(
-                                       device_manager.context(),
-                                       file_name.toStdString())));
-       } catch (Error e) {
-               show_session_error(tr("Failed to load ") + file_name, e.what());
-               session_.set_default_device();
-               update_device_list();
-               return;
-       }
-
-       session_.set_name(QFileInfo(file_name).fileName());
-
-       update_device_list();
-
-       session_.start_capture([&, errorMessage](QString infoMessage) {
-               session_error(errorMessage, infoMessage); });
-}
-
 void MainBar::update_sample_rate_selector()
 {
        Glib::VariantContainerBase gvar_dict;
        GVariant *gvar_list;
        const uint64_t *elements = nullptr;
        gsize num_elements;
-       map< const ConfigKey*, std::set<Capability> > keys;
+       map< const ConfigKey*, set<Capability> > keys;
 
        if (updating_sample_rate_) {
                sample_rate_.show_none();
                return;
        }
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -514,8 +319,27 @@ void MainBar::update_sample_rate_selector()
 
        const shared_ptr<sigrok::Device> sr_dev = device->device();
 
+       sample_rate_.allow_user_entered_values(false);
+       if (sr_dev->config_check(ConfigKey::EXTERNAL_CLOCK, Capability::GET)) {
+               try {
+                       auto gvar = sr_dev->config_get(ConfigKey::EXTERNAL_CLOCK);
+                       if (gvar.gobj()) {
+                               bool value = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(
+                                       gvar).get();
+                               sample_rate_.allow_user_entered_values(value);
+                       }
+               } catch (Error& error) {
+                       // Do nothing
+               }
+       }
+
+
        if (sr_dev->config_check(ConfigKey::SAMPLERATE, Capability::LIST)) {
-               gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
+               try {
+                       gvar_dict = sr_dev->config_list(ConfigKey::SAMPLERATE);
+               } catch (Error& error) {
+                       qDebug() << tr("Failed to get sample rate list:") << error.what();
+               }
        } else {
                sample_rate_.show_none();
                updating_sample_rate_ = false;
@@ -525,7 +349,7 @@ void MainBar::update_sample_rate_selector()
        if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
                        "samplerate-steps", G_VARIANT_TYPE("at")))) {
                elements = (const uint64_t *)g_variant_get_fixed_array(
-                               gvar_list, &num_elements, sizeof(uint64_t));
+                       gvar_list, &num_elements, sizeof(uint64_t));
 
                const uint64_t min = elements[0];
                const uint64_t max = elements[1];
@@ -550,7 +374,7 @@ void MainBar::update_sample_rate_selector()
        } else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
                        "samplerates", G_VARIANT_TYPE("at")))) {
                elements = (const uint64_t *)g_variant_get_fixed_array(
-                               gvar_list, &num_elements, sizeof(uint64_t));
+                       gvar_list, &num_elements, sizeof(uint64_t));
                sample_rate_.show_list(elements, num_elements);
                g_variant_unref(gvar_list);
        }
@@ -564,8 +388,7 @@ void MainBar::update_sample_rate_selector_value()
        if (updating_sample_rate_)
                return;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -577,9 +400,8 @@ void MainBar::update_sample_rate_selector_value()
                updating_sample_rate_ = true;
                sample_rate_.set_value(samplerate);
                updating_sample_rate_ = false;
-       } catch (Error error) {
-               qDebug() << "WARNING: Failed to get value of sample rate";
-               return;
+       } catch (Error& error) {
+               qDebug() << tr("Failed to get sample rate:") << error.what();
        }
 }
 
@@ -588,8 +410,7 @@ void MainBar::update_sample_count_selector()
        if (updating_sample_count_)
                return;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -607,28 +428,36 @@ void MainBar::update_sample_count_selector()
        uint64_t sample_count = sample_count_.value();
        uint64_t min_sample_count = 0;
        uint64_t max_sample_count = MaxSampleCount;
+       bool default_count_set = false;
 
-       if (sample_count == 0)
+       if (sample_count == 0) {
                sample_count = DefaultSampleCount;
+               default_count_set = true;
+       }
 
        if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::LIST)) {
-               auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
-               if (gvar.gobj())
-                       g_variant_get(gvar.gobj(), "(tt)",
-                               &min_sample_count, &max_sample_count);
+               try {
+                       auto gvar = sr_dev->config_list(ConfigKey::LIMIT_SAMPLES);
+                       if (gvar.gobj())
+                               g_variant_get(gvar.gobj(), "(tt)",
+                                       &min_sample_count, &max_sample_count);
+               } catch (Error& error) {
+                       qDebug() << tr("Failed to get sample limit list:") << error.what();
+               }
        }
 
        min_sample_count = min(max(min_sample_count, MinSampleCount),
                max_sample_count);
 
-       sample_count_.show_125_list(
-               min_sample_count, max_sample_count);
+       sample_count_.show_125_list(min_sample_count, max_sample_count);
 
        if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::GET)) {
                auto gvar = sr_dev->config_get(ConfigKey::LIMIT_SAMPLES);
                sample_count = g_variant_get_uint64(gvar.gobj());
-               if (sample_count == 0)
+               if (sample_count == 0) {
                        sample_count = DefaultSampleCount;
+                       default_count_set = true;
+               }
                sample_count = min(max(sample_count, MinSampleCount),
                        max_sample_count);
        }
@@ -636,18 +465,20 @@ void MainBar::update_sample_count_selector()
        sample_count_.set_value(sample_count);
 
        updating_sample_count_ = false;
+
+       // If we show the default rate then make sure the device uses the same
+       if (default_count_set)
+               commit_sample_count();
 }
 
 void MainBar::update_device_config_widgets()
 {
        using namespace pv::popups;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
 
        // Hide the widgets if no device is selected
        channels_button_action_->setVisible(!!device);
-       run_stop_button_action_->setVisible(!!device);
        if (!device) {
                configure_button_action_->setVisible(false);
                sample_count_.show_none();
@@ -661,8 +492,7 @@ void MainBar::update_device_config_widgets()
 
        // Update the configure popup
        DeviceOptions *const opts = new DeviceOptions(sr_dev, this);
-       configure_button_action_->setVisible(
-               !opts->binding().properties().empty());
+       configure_button_action_->setVisible(!opts->binding().properties().empty());
        configure_button_.set_popup(opts);
 
        // Update the channels popup
@@ -675,12 +505,6 @@ void MainBar::update_device_config_widgets()
        if (sr_dev->config_check(ConfigKey::LIMIT_SAMPLES, Capability::SET))
                sample_count_supported_ = true;
 
-       if (sr_dev->config_check(ConfigKey::LIMIT_FRAMES, Capability::SET)) {
-               sr_dev->config_set(ConfigKey::LIMIT_FRAMES,
-                       Glib::Variant<guint64>::create(1));
-                       on_config_changed();
-       }
-
        // Add notification of reconfigure events
        disconnect(this, SLOT(on_config_changed()));
        connect(&opts->binding(), SIGNAL(config_changed()),
@@ -695,23 +519,20 @@ void MainBar::commit_sample_rate()
 {
        uint64_t sample_rate = 0;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
        const shared_ptr<sigrok::Device> sr_dev = device->device();
 
        sample_rate = sample_rate_.value();
-       if (sample_rate == 0)
-               return;
 
        try {
                sr_dev->config_set(ConfigKey::SAMPLERATE,
                        Glib::Variant<guint64>::create(sample_rate));
                update_sample_rate_selector();
-       } catch (Error error) {
-               qDebug() << "Failed to configure samplerate.";
+       } catch (Error& error) {
+               qDebug() << tr("Failed to configure samplerate:") << error.what();
                return;
        }
 
@@ -725,8 +546,7 @@ void MainBar::commit_sample_count()
 {
        uint64_t sample_count = 0;
 
-       const shared_ptr<devices::Device> device =
-               device_selector_.selected_device();
+       const shared_ptr<devices::Device> device = device_selector_.selected_device();
        if (!device)
                return;
 
@@ -738,8 +558,8 @@ void MainBar::commit_sample_count()
                        sr_dev->config_set(ConfigKey::LIMIT_SAMPLES,
                                Glib::Variant<guint64>::create(sample_count));
                        update_sample_count_selector();
-               } catch (Error error) {
-                       qDebug() << "Failed to configure sample count.";
+               } catch (Error& error) {
+                       qDebug() << tr("Failed to configure sample count:") << error.what();
                        return;
                }
        }
@@ -750,40 +570,16 @@ void MainBar::commit_sample_count()
        update_sample_rate_selector();
 }
 
-void MainBar::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 MainBar::show_session_error(const QString text, const QString info_text)
 {
        QMessageBox msg(this);
-       msg.setText(text);
-       msg.setInformativeText(info_text);
+       msg.setText(text + "\n\n" + info_text);
        msg.setStandardButtons(QMessageBox::Ok);
        msg.setIcon(QMessageBox::Warning);
        msg.exec();
 }
 
-void MainBar::capture_state_changed(int state)
-{
-       set_capture_state((pv::Session::capture_state)state);
-}
-
-void MainBar::add_decoder(srd_decoder *decoder)
-{
-#ifdef ENABLE_DECODE
-       assert(decoder);
-       session_.add_decoder(decoder);
-#else
-       (void)decoder;
-#endif
-}
-
-void MainBar::export_file(shared_ptr<OutputFormat> format,
-       bool selection_only)
+void MainBar::export_file(shared_ptr<OutputFormat> format, bool selection_only)
 {
        using pv::dialogs::StoreProgress;
 
@@ -793,29 +589,40 @@ void MainBar::export_file(shared_ptr<OutputFormat> format,
        QSettings settings;
        const QString dir = settings.value(SettingSaveDirectory).toString();
 
-       std::pair<uint64_t, uint64_t> sample_range;
+       pair<uint64_t, uint64_t> sample_range;
 
        // Selection only? Verify that the cursors are active and fetch their values
        if (selection_only) {
-               if (!session_.main_view()->cursors()->enabled()) {
+               views::trace::View *trace_view =
+                       qobject_cast<views::trace::View*>(session_.main_view().get());
+
+               if (!trace_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)."));
+                               "cursors before you can save the data enclosed by them " \
+                               "to a session file (e.g. using the Show Cursors button)."));
                        return;
                }
 
                const double samplerate = session_.get_samplerate();
 
-               const pv::util::Timestamp& start_time = session_.main_view()->cursors()->first()->time();
-               const pv::util::Timestamp& end_time = session_.main_view()->cursors()->second()->time();
+               const pv::util::Timestamp& start_time = trace_view->cursors()->first()->time();
+               const pv::util::Timestamp& end_time = trace_view->cursors()->second()->time();
 
-               const uint64_t start_sample =
-                       std::max((double)0, start_time.convert_to<double>() * samplerate);
-               const uint64_t end_sample = end_time.convert_to<double>() * samplerate;
+               const uint64_t start_sample = (uint64_t)max(
+                       0.0, start_time.convert_to<double>() * samplerate);
+               const uint64_t end_sample = (uint64_t)max(
+                       0.0, end_time.convert_to<double>() * samplerate);
 
-               sample_range = std::make_pair(start_sample, end_sample);
+               if ((start_sample == 0) && (end_sample == 0)) {
+                       // Both cursors are negative and were clamped to 0
+                       show_session_error(tr("Invalid Range"), tr("The cursors don't " \
+                               "define a valid range of samples."));
+                       return;
+               }
+
+               sample_range = make_pair(start_sample, end_sample);
        } else {
-               sample_range = std::make_pair(0, 0);
+               sample_range = make_pair(0, 0);
        }
 
        // Construct the filter
@@ -824,9 +631,9 @@ void MainBar::export_file(shared_ptr<OutputFormat> format,
                QString::fromStdString(format->description()));
 
        if (exts.empty())
-               filter += "(*.*)";
+               filter += "(*)";
        else
-               filter += QString("(*.%1);;%2 (*.*)").arg(
+               filter += QString("(*.%1);;%2 (*)").arg(
                        QString::fromStdString(join(exts, ", *.")),
                        tr("All Files"));
 
@@ -852,7 +659,8 @@ void MainBar::export_file(shared_ptr<OutputFormat> format,
                options = dlg.options();
        }
 
-       session_.set_name(QFileInfo(file_name).fileName());
+       if (!selection_only)
+               session_.set_name(QFileInfo(file_name).fileName());
 
        StoreProgress *dlg = new StoreProgress(file_name, format, options,
                sample_range, session_, this);
@@ -868,16 +676,19 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
 
        // Construct the filter
        const vector<string> exts = format->extensions();
-       const QString filter = exts.empty() ? "" :
-               tr("%1 files (*.%2)").arg(
-                       QString::fromStdString(format->description()),
-                       QString::fromStdString(join(exts, ", *.")));
+       const QString filter_exts = exts.empty() ? "" : QString::fromStdString("%1 (%2)").arg(
+               tr("%1 files").arg(QString::fromStdString(format->description())),
+               QString::fromStdString("*.%1").arg(QString::fromStdString(join(exts, " *."))));
+       const QString filter_all = QString::fromStdString("%1 (%2)").arg(
+               tr("All Files"), QString::fromStdString("*"));
+       const QString filter = QString::fromStdString("%1%2%3").arg(
+               exts.empty() ? "" : filter_exts,
+               exts.empty() ? "" : ";;",
+               filter_all);
 
        // Show the file dialog
        const QString file_name = QFileDialog::getOpenFileName(
-               this, tr("Import File"), dir, tr(
-                       "%1 files (*.*);;All Files (*.*)").arg(
-                       QString::fromStdString(format->description())));
+               this, tr("Import File"), dir, filter);
 
        if (file_name.isEmpty())
                return;
@@ -894,7 +705,7 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
                options = dlg.options();
        }
 
-       load_file(file_name, format, options);
+       session_.load_file(file_name, format, options);
 
        const QString abs_path = QFileInfo(file_name).absolutePath();
        settings.setValue(SettingOpenDirectory, abs_path);
@@ -903,16 +714,24 @@ void MainBar::import_file(shared_ptr<InputFormat> format)
 void MainBar::on_device_selected()
 {
        shared_ptr<devices::Device> device = device_selector_.selected_device();
-       if (!device) {
-               reset_device_selector();
-               return;
-       }
 
-       select_device(device);
+       if (device)
+               session_.select_device(device);
+       else
+               reset_device_selector();
+}
 
+void MainBar::on_device_changed()
+{
+       update_device_list();
        update_device_config_widgets();
 }
 
+void MainBar::on_capture_state_changed(int state)
+{
+       set_capture_state((pv::Session::capture_state)state);
+}
+
 void MainBar::on_sample_count_changed()
 {
        if (!updating_sample_count_)
@@ -925,17 +744,19 @@ void MainBar::on_sample_rate_changed()
                commit_sample_rate();
 }
 
-void MainBar::on_run_stop()
+void MainBar::on_config_changed()
 {
+       // We want to also call update_sample_rate_selector() here in case
+       // the user changed the SR_CONF_EXTERNAL_CLOCK option. However,
+       // commit_sample_rate() does this already, so we don't call it here
+
        commit_sample_count();
-       commit_sample_rate();   
-       run_stop();
+       commit_sample_rate();
 }
 
-void MainBar::on_config_changed()
+void MainBar::on_actionNewView_triggered()
 {
-       commit_sample_count();
-       commit_sample_rate();   
+       new_view(&session_);
 }
 
 void MainBar::on_actionOpen_triggered()
@@ -946,11 +767,11 @@ void MainBar::on_actionOpen_triggered()
        // Show the dialog
        const QString file_name = QFileDialog::getOpenFileName(
                this, tr("Open File"), dir, tr(
-                       "Sigrok Sessions (*.sr);;"
-                       "All Files (*.*)"));
+                       "sigrok Sessions (*.sr);;"
+                       "All Files (*)"));
 
        if (!file_name.isEmpty()) {
-               load_file(file_name);
+               session_.load_file(file_name);
 
                const QString abs_path = QFileInfo(file_name).absolutePath();
                settings.setValue(SettingOpenDirectory, abs_path);
@@ -967,6 +788,40 @@ void MainBar::on_actionSaveSelectionAs_triggered()
        export_file(session_.device_manager().context()->output_formats()["srzip"], true);
 }
 
+void MainBar::on_actionSaveSetup_triggered()
+{
+       QSettings settings;
+       const QString dir = settings.value(SettingSaveDirectory).toString();
+
+       const QString file_name = QFileDialog::getSaveFileName(
+               this, tr("Save File"), dir, tr(
+                               "PulseView Session Setups (*.pvs);;"
+                               "All Files (*)"));
+
+       if (file_name.isEmpty())
+               return;
+
+       QSettings settings_storage(file_name, QSettings::IniFormat);
+       session_.save_setup(settings_storage);
+}
+
+void MainBar::on_actionRestoreSetup_triggered()
+{
+       QSettings settings;
+       const QString dir = settings.value(SettingSaveDirectory).toString();
+
+       const QString file_name = QFileDialog::getOpenFileName(
+               this, tr("Open File"), dir, tr(
+                               "PulseView Session Setups (*.pvs);;"
+                               "All Files (*)"));
+
+       if (file_name.isEmpty())
+               return;
+
+       QSettings settings_storage(file_name, QSettings::IniFormat);
+       session_.restore_setup(settings_storage);
+}
+
 void MainBar::on_actionConnect_triggered()
 {
        // Stop any currently running capture session
@@ -977,45 +832,43 @@ void MainBar::on_actionConnect_triggered()
        // If the user selected a device, select it in the device list. Select the
        // current device otherwise.
        if (dlg.exec())
-               select_device(dlg.get_selected_device());
+               session_.select_device(dlg.get_selected_device());
 
        update_device_list();
 }
 
-void MainBar::on_actionViewZoomIn_triggered()
-{
-       session_.main_view()->zoom(1);
-}
-
-void MainBar::on_actionViewZoomOut_triggered()
-{
-       session_.main_view()->zoom(-1);
-}
-
-void MainBar::on_actionViewZoomFit_triggered()
+void MainBar::on_add_decoder_clicked()
 {
-       session_.main_view()->zoom_fit(action_view_zoom_fit_->isChecked());
+       show_decoder_selector(&session_);
 }
 
-void MainBar::on_actionViewZoomOneToOne_triggered()
+void MainBar::add_toolbar_widgets()
 {
-       session_.main_view()->zoom_one_to_one();
-}
+       addAction(action_new_view_);
+       addSeparator();
+       addWidget(open_button_);
+       addWidget(save_button_);
+       addSeparator();
 
-void MainBar::on_actionViewShowCursors_triggered()
-{
-       const bool show = !session_.main_view()->cursors_shown();
-       if (show)
-               session_.main_view()->centre_cursors();
+       StandardBar::add_toolbar_widgets();
 
-       session_.main_view()->show_cursors(show);
+       addWidget(&device_selector_);
+       configure_button_action_ = addWidget(&configure_button_);
+       channels_button_action_ = addWidget(&channels_button_);
+       addWidget(&sample_count_);
+       addWidget(&sample_rate_);
+#ifdef ENABLE_DECODE
+       addSeparator();
+       addWidget(add_decoder_button_);
+#endif
 }
 
 bool MainBar::eventFilter(QObject *watched, QEvent *event)
 {
        if (sample_count_supported_ && (watched == &sample_count_ ||
-                       watched == &sample_rate_) &&
-                       (event->type() == QEvent::ToolTip)) {
+               watched == &sample_rate_) &&
+               (event->type() == QEvent::ToolTip)) {
+
                auto sec = pv::util::Timestamp(sample_count_.value()) / sample_rate_.value();
                QHelpEvent *help_event = static_cast<QHelpEvent*>(event);