]> sigrok.org Git - pulseview.git/blame - pv/widgets/devicetoolbutton.cpp
device: ensure bind_enum() checks availability of Capability::LIST.
[pulseview.git] / pv / widgets / devicetoolbutton.cpp
CommitLineData
079d39ea
JH
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
1982c650
SA
23#include <QTimer>
24#include <QToolTip>
25
fe3a1c21 26#include <libsigrokcxx/libsigrokcxx.hpp>
079d39ea
JH
27
28#include <pv/devicemanager.hpp>
29
30#include "devicetoolbutton.hpp"
31
32using std::list;
33using std::shared_ptr;
34using std::string;
35using std::weak_ptr;
36using std::vector;
37
38using sigrok::Device;
39
40namespace pv {
41namespace widgets {
42
43DeviceToolButton::DeviceToolButton(QWidget *parent,
44 DeviceManager &device_manager,
45 QAction *connect_action) :
46 QToolButton(parent),
47 device_manager_(device_manager),
48 connect_action_(connect_action),
49 menu_(this),
50 mapper_(this),
51 devices_()
52{
53 setPopupMode(QToolButton::MenuButtonPopup);
54 setMenu(&menu_);
55 setDefaultAction(connect_action_);
56 setMinimumWidth(QFontMetrics(font()).averageCharWidth() * 24);
57
58 connect(&mapper_, SIGNAL(mapped(QObject*)),
59 this, SLOT(on_action(QObject*)));
1982c650
SA
60
61 connect(&menu_, SIGNAL(hovered(QAction*)),
62 this, SLOT(on_menu_hovered(QAction*)));
079d39ea
JH
63}
64
65shared_ptr<Device> DeviceToolButton::selected_device()
66{
67 return selected_device_;
68}
69
70void DeviceToolButton::set_device_list(
71 const list< shared_ptr<Device> > &devices, shared_ptr<Device> selected)
72{
73 selected_device_ = selected;
74 setText(QString::fromStdString(
75 device_manager_.get_display_name(selected)));
76 devices_ = vector< weak_ptr<Device> >(devices.begin(), devices.end());
77 update_device_list();
78}
79
80void DeviceToolButton::update_device_list()
81{
82 menu_.clear();
83 menu_.addAction(connect_action_);
84 menu_.setDefaultAction(connect_action_);
85 menu_.addSeparator();
86
87 for (weak_ptr<Device> dev_weak_ptr : devices_) {
88 shared_ptr<Device> dev(dev_weak_ptr);
89 if (!dev)
90 continue;
91
92 QAction *const a = new QAction(QString::fromStdString(
93 device_manager_.get_display_name(dev)), this);
94 a->setCheckable(true);
95 a->setChecked(selected_device_ == dev);
96 a->setData(qVariantFromValue((void*)dev.get()));
1982c650 97 a->setToolTip(QString::fromStdString(device_manager_.get_full_name(dev)));
079d39ea
JH
98 mapper_.setMapping(a, a);
99
100 connect(a, SIGNAL(triggered()), &mapper_, SLOT(map()));
101
102 menu_.addAction(a);
103 }
104}
105
106void DeviceToolButton::on_action(QObject *action)
107{
108 assert(action);
109
110 Device *const dev = (Device*)((QAction*)action)->data().value<void*>();
111 for (weak_ptr<Device> dev_weak_ptr : devices_) {
112 shared_ptr<Device> dev_ptr(dev_weak_ptr);
113 if (dev_ptr.get() == dev) {
114 selected_device_ = shared_ptr<Device>(dev_ptr);
115 break;
116 }
117 }
118
119 update_device_list();
120 setText(QString::fromStdString(
121 device_manager_.get_display_name(selected_device_)));
122
123 device_selected();
124}
125
1982c650
SA
126void DeviceToolButton::on_menu_hovered(QAction *action)
127{
128 assert(action);
129
130 // Only show the tooltip for device entries (they hold
131 // device pointers in their data field)
132 if (!action->data().isValid())
133 return;
134
135 device_tooltip_ = action->toolTip();
136
137 if (QToolTip::isVisible())
138 on_menu_hover_timeout();
139 else
140 QTimer::singleShot(1000, this, SLOT(on_menu_hover_timeout()));
141}
142
143void DeviceToolButton::on_menu_hover_timeout()
144{
145 if (device_tooltip_.isEmpty())
146 return;
147
148 QToolTip::showText(QCursor::pos(), device_tooltip_);
149}
150
079d39ea
JH
151} // widgets
152} // pv