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