]>
Commit | Line | Data |
---|---|---|
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
079d39ea JH |
18 | */ |
19 | ||
20 | #include <cassert> | |
21 | ||
1982c650 SA |
22 | #include <QTimer> |
23 | #include <QToolTip> | |
24 | ||
fe3a1c21 | 25 | #include <libsigrokcxx/libsigrokcxx.hpp> |
079d39ea JH |
26 | |
27 | #include <pv/devicemanager.hpp> | |
da30ecb7 | 28 | #include <pv/devices/device.hpp> |
079d39ea JH |
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 | ||
da30ecb7 | 38 | using pv::devices::Device; |
079d39ea JH |
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 | ||
1ed73ebd VPP |
58 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
59 | connect(&mapper_, SIGNAL(mappedObject(QObject*)), | |
60 | this, SLOT(on_action(QObject*))); | |
61 | #else | |
079d39ea JH |
62 | connect(&mapper_, SIGNAL(mapped(QObject*)), |
63 | this, SLOT(on_action(QObject*))); | |
1ed73ebd | 64 | #endif |
1982c650 SA |
65 | |
66 | connect(&menu_, SIGNAL(hovered(QAction*)), | |
67 | this, SLOT(on_menu_hovered(QAction*))); | |
079d39ea JH |
68 | } |
69 | ||
70 | shared_ptr<Device> DeviceToolButton::selected_device() | |
71 | { | |
72 | return selected_device_; | |
73 | } | |
74 | ||
75 | void DeviceToolButton::set_device_list( | |
76 | const list< shared_ptr<Device> > &devices, shared_ptr<Device> selected) | |
77 | { | |
78 | selected_device_ = selected; | |
2a53532c | 79 | setText(selected ? QString::fromStdString( |
7e0c99bf | 80 | selected->display_name(device_manager_)) : tr("<No Device>")); |
079d39ea JH |
81 | devices_ = vector< weak_ptr<Device> >(devices.begin(), devices.end()); |
82 | update_device_list(); | |
83 | } | |
84 | ||
7e0c99bf SA |
85 | void DeviceToolButton::reset() |
86 | { | |
87 | setText(tr("<No Device>")); | |
88 | selected_device_.reset(); | |
89 | update_device_list(); | |
90 | } | |
91 | ||
079d39ea JH |
92 | void DeviceToolButton::update_device_list() |
93 | { | |
94 | menu_.clear(); | |
95 | menu_.addAction(connect_action_); | |
96 | menu_.setDefaultAction(connect_action_); | |
97 | menu_.addSeparator(); | |
98 | ||
f4ab4b5c | 99 | for (weak_ptr<Device>& dev_weak_ptr : devices_) { |
2a53532c | 100 | shared_ptr<Device> dev(dev_weak_ptr.lock()); |
079d39ea JH |
101 | if (!dev) |
102 | continue; | |
103 | ||
104 | QAction *const a = new QAction(QString::fromStdString( | |
3084ed4b | 105 | dev->display_name(device_manager_)), this); |
079d39ea JH |
106 | a->setCheckable(true); |
107 | a->setChecked(selected_device_ == dev); | |
009fc9ae | 108 | a->setData(QVariant::fromValue((void*)dev.get())); |
3084ed4b | 109 | a->setToolTip(QString::fromStdString(dev->full_name())); |
079d39ea JH |
110 | mapper_.setMapping(a, a); |
111 | ||
112 | connect(a, SIGNAL(triggered()), &mapper_, SLOT(map())); | |
113 | ||
114 | menu_.addAction(a); | |
115 | } | |
116 | } | |
117 | ||
118 | void DeviceToolButton::on_action(QObject *action) | |
119 | { | |
120 | assert(action); | |
121 | ||
7e0c99bf SA |
122 | selected_device_.reset(); |
123 | ||
079d39ea | 124 | Device *const dev = (Device*)((QAction*)action)->data().value<void*>(); |
f4ab4b5c | 125 | for (weak_ptr<Device>& dev_weak_ptr : devices_) { |
079d39ea JH |
126 | shared_ptr<Device> dev_ptr(dev_weak_ptr); |
127 | if (dev_ptr.get() == dev) { | |
128 | selected_device_ = shared_ptr<Device>(dev_ptr); | |
129 | break; | |
130 | } | |
131 | } | |
132 | ||
133 | update_device_list(); | |
134 | setText(QString::fromStdString( | |
3084ed4b | 135 | selected_device_->display_name(device_manager_))); |
079d39ea JH |
136 | |
137 | device_selected(); | |
138 | } | |
139 | ||
1982c650 SA |
140 | void DeviceToolButton::on_menu_hovered(QAction *action) |
141 | { | |
142 | assert(action); | |
143 | ||
144 | // Only show the tooltip for device entries (they hold | |
145 | // device pointers in their data field) | |
146 | if (!action->data().isValid()) | |
147 | return; | |
148 | ||
149 | device_tooltip_ = action->toolTip(); | |
150 | ||
151 | if (QToolTip::isVisible()) | |
152 | on_menu_hover_timeout(); | |
153 | else | |
154 | QTimer::singleShot(1000, this, SLOT(on_menu_hover_timeout())); | |
155 | } | |
156 | ||
157 | void DeviceToolButton::on_menu_hover_timeout() | |
158 | { | |
159 | if (device_tooltip_.isEmpty()) | |
160 | return; | |
161 | ||
162 | QToolTip::showText(QCursor::pos(), device_tooltip_); | |
163 | } | |
164 | ||
870ea3db UH |
165 | } // namespace widgets |
166 | } // namespace pv |