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