2 * This file is part of the PulseView project.
4 * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <libsigrok/libsigrok.hpp>
25 #include <pv/devicemanager.hpp>
27 #include "devicetoolbutton.hpp"
30 using std::shared_ptr;
40 DeviceToolButton::DeviceToolButton(QWidget *parent,
41 DeviceManager &device_manager,
42 QAction *connect_action) :
44 device_manager_(device_manager),
45 connect_action_(connect_action),
50 setPopupMode(QToolButton::MenuButtonPopup);
52 setDefaultAction(connect_action_);
53 setMinimumWidth(QFontMetrics(font()).averageCharWidth() * 24);
55 connect(&mapper_, SIGNAL(mapped(QObject*)),
56 this, SLOT(on_action(QObject*)));
59 shared_ptr<Device> DeviceToolButton::selected_device()
61 return selected_device_;
64 void DeviceToolButton::set_device_list(
65 const list< shared_ptr<Device> > &devices, shared_ptr<Device> selected)
67 selected_device_ = selected;
68 setText(QString::fromStdString(
69 device_manager_.get_display_name(selected)));
70 devices_ = vector< weak_ptr<Device> >(devices.begin(), devices.end());
74 void DeviceToolButton::update_device_list()
77 menu_.addAction(connect_action_);
78 menu_.setDefaultAction(connect_action_);
81 for (weak_ptr<Device> dev_weak_ptr : devices_) {
82 shared_ptr<Device> dev(dev_weak_ptr);
86 QAction *const a = new QAction(QString::fromStdString(
87 device_manager_.get_display_name(dev)), this);
88 a->setCheckable(true);
89 a->setChecked(selected_device_ == dev);
90 a->setData(qVariantFromValue((void*)dev.get()));
91 mapper_.setMapping(a, a);
93 connect(a, SIGNAL(triggered()), &mapper_, SLOT(map()));
99 void DeviceToolButton::on_action(QObject *action)
103 Device *const dev = (Device*)((QAction*)action)->data().value<void*>();
104 for (weak_ptr<Device> dev_weak_ptr : devices_) {
105 shared_ptr<Device> dev_ptr(dev_weak_ptr);
106 if (dev_ptr.get() == dev) {
107 selected_device_ = shared_ptr<Device>(dev_ptr);
112 update_device_list();
113 setText(QString::fromStdString(
114 device_manager_.get_display_name(selected_device_)));