X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=pv%2Fwidgets%2Fdevicetoolbutton.cpp;fp=pv%2Fwidgets%2Fdevicetoolbutton.cpp;h=27ba31a62120d5e97a2085fed6e4337d204116b6;hb=079d39ea735015597a65390e4c5cacc7d272f78c;hp=0000000000000000000000000000000000000000;hpb=168888e25af380e62280acec3e3f316bbfed09d9;p=pulseview.git diff --git a/pv/widgets/devicetoolbutton.cpp b/pv/widgets/devicetoolbutton.cpp new file mode 100644 index 0000000..27ba31a --- /dev/null +++ b/pv/widgets/devicetoolbutton.cpp @@ -0,0 +1,120 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2015 Joel Holdsworth + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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 + */ + +#include + +#include + +#include + +#include "devicetoolbutton.hpp" + +using std::list; +using std::shared_ptr; +using std::string; +using std::weak_ptr; +using std::vector; + +using sigrok::Device; + +namespace pv { +namespace widgets { + +DeviceToolButton::DeviceToolButton(QWidget *parent, + DeviceManager &device_manager, + QAction *connect_action) : + QToolButton(parent), + device_manager_(device_manager), + connect_action_(connect_action), + menu_(this), + mapper_(this), + devices_() +{ + setPopupMode(QToolButton::MenuButtonPopup); + setMenu(&menu_); + setDefaultAction(connect_action_); + setMinimumWidth(QFontMetrics(font()).averageCharWidth() * 24); + + connect(&mapper_, SIGNAL(mapped(QObject*)), + this, SLOT(on_action(QObject*))); +} + +shared_ptr DeviceToolButton::selected_device() +{ + return selected_device_; +} + +void DeviceToolButton::set_device_list( + const list< shared_ptr > &devices, shared_ptr selected) +{ + selected_device_ = selected; + setText(QString::fromStdString( + device_manager_.get_display_name(selected))); + devices_ = vector< weak_ptr >(devices.begin(), devices.end()); + update_device_list(); +} + +void DeviceToolButton::update_device_list() +{ + menu_.clear(); + menu_.addAction(connect_action_); + menu_.setDefaultAction(connect_action_); + menu_.addSeparator(); + + for (weak_ptr dev_weak_ptr : devices_) { + shared_ptr dev(dev_weak_ptr); + if (!dev) + continue; + + QAction *const a = new QAction(QString::fromStdString( + device_manager_.get_display_name(dev)), this); + a->setCheckable(true); + a->setChecked(selected_device_ == dev); + a->setData(qVariantFromValue((void*)dev.get())); + mapper_.setMapping(a, a); + + connect(a, SIGNAL(triggered()), &mapper_, SLOT(map())); + + menu_.addAction(a); + } +} + +void DeviceToolButton::on_action(QObject *action) +{ + assert(action); + + Device *const dev = (Device*)((QAction*)action)->data().value(); + for (weak_ptr dev_weak_ptr : devices_) { + shared_ptr dev_ptr(dev_weak_ptr); + if (dev_ptr.get() == dev) { + selected_device_ = shared_ptr(dev_ptr); + break; + } + } + + update_device_list(); + setText(QString::fromStdString( + device_manager_.get_display_name(selected_device_))); + + device_selected(); +} + +} // widgets +} // pv