]> sigrok.org Git - pulseview.git/blob - pv/views/trace/viewwidget.hpp
e7da1dbd25cbec17da10ae49e1aa8b30b5e20c97
[pulseview.git] / pv / views / trace / viewwidget.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2013 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_VIEWWIDGET_HPP
21 #define PULSEVIEW_PV_VIEWWIDGET_HPP
22
23 #include <memory>
24
25 #include <QPoint>
26 #include <QWidget>
27
28 #include <pv/util.hpp>
29
30 using std::shared_ptr;
31 using std::vector;
32
33 class QTouchEvent;
34
35 namespace pv {
36 namespace views {
37 namespace trace {
38
39 class View;
40 class ViewItem;
41
42 class ViewWidget : public QWidget
43 {
44         Q_OBJECT
45
46 protected:
47         ViewWidget(View &parent);
48
49         /**
50          * Indicates when a view item is being hovered over.
51          * @param item The item that is being hovered over, or @c nullptr
52          * if no view item is being hovered over.
53          * @remarks the default implementation does nothing.
54          */
55         virtual void item_hover(const shared_ptr<ViewItem> &item, QPoint pos);
56
57         /**
58          * Indicates the event an a view item has been clicked.
59          * @param item the view item that has been clicked.
60          * @remarks the default implementation does nothing.
61          */
62         virtual void item_clicked(const shared_ptr<ViewItem> &item);
63
64         /**
65          * Returns true if the selection of row items allows dragging.
66          * @return Returns true if the drag is acceptable.
67          */
68         bool accept_drag() const;
69
70         /**
71          * Returns true if the mouse button is down.
72          */
73         bool mouse_down() const;
74
75         /**
76          * Drag the dragging items by the delta offset.
77          * @param delta the drag offset in pixels.
78          */
79         void drag_items(const QPoint &delta);
80
81         /**
82          * Sets this item into the dragged state.
83          */
84         virtual void drag();
85
86         /**
87          * Drag the background by the delta offset.
88          * @param delta the drag offset in pixels.
89          * @remarks The default implementation does nothing.
90          */
91         virtual void drag_by(const QPoint &delta);
92
93         /**
94          * Sets this item into the un-dragged state.
95          */
96         virtual void drag_release();
97
98         /**
99          * Gets the items in the view widget.
100          */
101         virtual vector< shared_ptr<ViewItem> > items() = 0;
102
103         /**
104          * Gets the first view item which has a hit-box that contains @c pt .
105          * @param pt the point to search with.
106          * @return the view item that has been found, or and empty
107          *   @c shared_ptr if no item was found.
108          */
109         virtual shared_ptr<ViewItem> get_mouse_over_item(const QPoint &pt) = 0;
110
111         /**
112          * Handles left mouse button press events.
113          * @param event the mouse event that triggered this handler.
114          */
115         void mouse_left_press_event(QMouseEvent *event);
116
117         /**
118          * Handles left mouse button release events.
119          * @param event the mouse event that triggered this handler.
120          */
121         void mouse_left_release_event(QMouseEvent *event);
122
123         /**
124          * Handles touch begin update and end events.
125          * @param e the event that triggered this handler.
126          */
127         virtual bool touch_event(QTouchEvent *event);
128
129 protected:
130         bool event(QEvent *event);
131
132         void mousePressEvent(QMouseEvent *event);
133         void mouseReleaseEvent(QMouseEvent *event);
134         void mouseMoveEvent(QMouseEvent *event);
135
136         void keyPressEvent(QKeyEvent *event);
137         void keyReleaseEvent(QKeyEvent *event);
138
139         void leaveEvent(QEvent *event);
140
141 public Q_SLOTS:
142         void clear_selection();
143
144 Q_SIGNALS:
145         void selection_changed();
146
147 protected:
148         pv::views::trace::View &view_;
149         QPoint mouse_point_;
150         Qt::KeyboardModifiers mouse_modifiers_;
151         QPoint mouse_down_point_;
152         pv::util::Timestamp mouse_down_offset_;
153         shared_ptr<ViewItem> mouse_down_item_;
154         bool item_dragging_;
155 };
156
157 } // namespace trace
158 } // namespace views
159 } // namespace pv
160
161 #endif // PULSEVIEW_PV_VIEWWIDGET_HPP