]> sigrok.org Git - pulseview.git/blob - pv/widgets/devicetoolbutton.cpp
27ba31a62120d5e97a2085fed6e4337d204116b6
[pulseview.git] / pv / widgets / devicetoolbutton.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <cassert>
22
23 #include <libsigrok/libsigrok.hpp>
24
25 #include <pv/devicemanager.hpp>
26
27 #include "devicetoolbutton.hpp"
28
29 using std::list;
30 using std::shared_ptr;
31 using std::string;
32 using std::weak_ptr;
33 using std::vector;
34
35 using sigrok::Device;
36
37 namespace pv {
38 namespace widgets {
39
40 DeviceToolButton::DeviceToolButton(QWidget *parent,
41         DeviceManager &device_manager,
42         QAction *connect_action) :
43         QToolButton(parent),
44         device_manager_(device_manager),
45         connect_action_(connect_action),
46         menu_(this),
47         mapper_(this),
48         devices_()
49 {
50         setPopupMode(QToolButton::MenuButtonPopup);
51         setMenu(&menu_);
52         setDefaultAction(connect_action_);
53         setMinimumWidth(QFontMetrics(font()).averageCharWidth() * 24);
54
55         connect(&mapper_, SIGNAL(mapped(QObject*)),
56                 this, SLOT(on_action(QObject*)));
57 }
58
59 shared_ptr<Device> DeviceToolButton::selected_device()
60 {
61         return selected_device_;
62 }
63
64 void DeviceToolButton::set_device_list(
65         const list< shared_ptr<Device> > &devices, shared_ptr<Device> selected)
66 {
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());
71         update_device_list();
72 }
73
74 void DeviceToolButton::update_device_list()
75 {
76         menu_.clear();
77         menu_.addAction(connect_action_);
78         menu_.setDefaultAction(connect_action_);
79         menu_.addSeparator();
80
81         for (weak_ptr<Device> dev_weak_ptr : devices_) {
82                 shared_ptr<Device> dev(dev_weak_ptr);
83                 if (!dev)
84                         continue;
85
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);
92
93                 connect(a, SIGNAL(triggered()), &mapper_, SLOT(map()));
94
95                 menu_.addAction(a);
96         }
97 }
98
99 void DeviceToolButton::on_action(QObject *action)
100 {
101         assert(action);
102
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);
108                         break;
109                 }
110         }
111
112         update_device_list();
113         setText(QString::fromStdString(
114                 device_manager_.get_display_name(selected_device_)));
115
116         device_selected();
117 }
118
119 } // widgets
120 } // pv