]> sigrok.org Git - pulseview.git/blob - pv/widgets/devicetoolbutton.hpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / widgets / devicetoolbutton.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2014 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 #ifndef PULSEVIEW_PV_WIDGETS_DEVICETOOLBUTTON_HPP
21 #define PULSEVIEW_PV_WIDGETS_DEVICETOOLBUTTON_HPP
22
23 #include <list>
24 #include <memory>
25 #include <vector>
26
27 #include <QAction>
28 #include <QMenu>
29 #include <QSignalMapper>
30 #include <QToolButton>
31
32 using std::list;
33 using std::shared_ptr;
34 using std::vector;
35 using std::weak_ptr;
36
37 struct srd_decoder;
38
39 namespace pv {
40
41 class DeviceManager;
42
43 namespace devices {
44 class Device;
45 }
46
47 namespace widgets {
48
49 class DeviceToolButton : public QToolButton
50 {
51         Q_OBJECT;
52
53 public:
54         /**
55          * Constructor
56          * @param parent the parent widget.
57          * @param device_manager the device manager.
58          * @param connect_action the connect-to-device action.
59          */
60         DeviceToolButton(QWidget *parent, DeviceManager &device_manager,
61                 QAction *connect_action);
62
63         /**
64          * Returns a reference to the selected device.
65          */
66         shared_ptr<devices::Device> selected_device();
67
68         /**
69          * Sets the current list of devices.
70          * @param device the list of devices.
71          * @param selected_device the currently active device.
72          */
73         void set_device_list(
74                 const list< shared_ptr<devices::Device> > &devices,
75                 shared_ptr<devices::Device> selected);
76
77         /**
78          * Sets the current device to "no device". Useful for when a selected
79          * device fails to open.
80          */
81         void reset();
82
83 private:
84         /**
85          * Repopulates the menu from the device list.
86          */
87         void update_device_list();
88
89 private Q_SLOTS:
90         void on_action(QObject *action);
91
92         void on_menu_hovered(QAction *action);
93
94         void on_menu_hover_timeout();
95
96 Q_SIGNALS:
97         void device_selected();
98
99 private:
100         DeviceManager &device_manager_;
101         QAction *const connect_action_;
102
103         QMenu menu_;
104         QSignalMapper mapper_;
105
106         shared_ptr<devices::Device> selected_device_;
107         vector< weak_ptr<devices::Device> > devices_;
108
109         QString device_tooltip_;
110 };
111
112 }  // namespace widgets
113 }  // namespace pv
114
115 #endif // PULSEVIEW_PV_WIDGETS_DEVICETOOLBUTTON_HPP