Introduce DecodeSignal class
[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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <cassert>
21
22 #include <QTimer>
23 #include <QToolTip>
24
25 #include <libsigrokcxx/libsigrokcxx.hpp>
26
27 #include <pv/devicemanager.hpp>
28 #include <pv/devices/device.hpp>
29
30 #include "devicetoolbutton.hpp"
31
32 using std::list;
33 using std::shared_ptr;
34 using std::string;
35 using std::weak_ptr;
36 using std::vector;
37
38 using pv::devices::Device;
39
40 namespace pv {
41 namespace widgets {
42
43 DeviceToolButton::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*)));
60
61         connect(&menu_, SIGNAL(hovered(QAction*)),
62                 this, SLOT(on_menu_hovered(QAction*)));
63 }
64
65 shared_ptr<Device> DeviceToolButton::selected_device()
66 {
67         return selected_device_;
68 }
69
70 void DeviceToolButton::set_device_list(
71         const list< shared_ptr<Device> > &devices, shared_ptr<Device> selected)
72 {
73         selected_device_ = selected;
74         setText(selected ? QString::fromStdString(
75                 selected->display_name(device_manager_)) : tr("<No Device>"));
76         devices_ = vector< weak_ptr<Device> >(devices.begin(), devices.end());
77         update_device_list();
78 }
79
80 void DeviceToolButton::reset()
81 {
82         setText(tr("<No Device>"));
83         selected_device_.reset();
84         update_device_list();
85 }
86
87 void DeviceToolButton::update_device_list()
88 {
89         menu_.clear();
90         menu_.addAction(connect_action_);
91         menu_.setDefaultAction(connect_action_);
92         menu_.addSeparator();
93
94         for (weak_ptr<Device> dev_weak_ptr : devices_) {
95                 shared_ptr<Device> dev(dev_weak_ptr.lock());
96                 if (!dev)
97                         continue;
98
99                 QAction *const a = new QAction(QString::fromStdString(
100                         dev->display_name(device_manager_)), this);
101                 a->setCheckable(true);
102                 a->setChecked(selected_device_ == dev);
103                 a->setData(qVariantFromValue((void*)dev.get()));
104                 a->setToolTip(QString::fromStdString(dev->full_name()));
105                 mapper_.setMapping(a, a);
106
107                 connect(a, SIGNAL(triggered()), &mapper_, SLOT(map()));
108
109                 menu_.addAction(a);
110         }
111 }
112
113 void DeviceToolButton::on_action(QObject *action)
114 {
115         assert(action);
116
117         selected_device_.reset();
118
119         Device *const dev = (Device*)((QAction*)action)->data().value<void*>();
120         for (weak_ptr<Device> dev_weak_ptr : devices_) {
121                 shared_ptr<Device> dev_ptr(dev_weak_ptr);
122                 if (dev_ptr.get() == dev) {
123                         selected_device_ = shared_ptr<Device>(dev_ptr);
124                         break;
125                 }
126         }
127
128         update_device_list();
129         setText(QString::fromStdString(
130                 selected_device_->display_name(device_manager_)));
131
132         device_selected();
133 }
134
135 void DeviceToolButton::on_menu_hovered(QAction *action)
136 {
137         assert(action);
138
139         // Only show the tooltip for device entries (they hold
140         // device pointers in their data field)
141         if (!action->data().isValid())
142                 return;
143
144         device_tooltip_ = action->toolTip();
145
146         if (QToolTip::isVisible())
147                 on_menu_hover_timeout();
148         else
149                 QTimer::singleShot(1000, this, SLOT(on_menu_hover_timeout()));
150 }
151
152 void DeviceToolButton::on_menu_hover_timeout()
153 {
154         if (device_tooltip_.isEmpty())
155                 return;
156
157         QToolTip::showText(QCursor::pos(), device_tooltip_);
158 }
159
160 }  // namespace widgets
161 }  // namespace pv