]> sigrok.org Git - pulseview.git/blame - pv/view/viewwidget.cpp
SamplingBar: Handle a failure to list the LIMIT_SAMPLES key
[pulseview.git] / pv / view / viewwidget.cpp
CommitLineData
40aca27e
JH
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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
e9e4e5e7
JH
21#include <QApplication>
22#include <QMouseEvent>
c9743553 23#include <QTouchEvent>
b434cbaf
JH
24
25#include "rowitem.hpp"
40aca27e
JH
26#include "view.hpp"
27#include "viewwidget.hpp"
28
b434cbaf
JH
29using std::any_of;
30using std::shared_ptr;
31using std::vector;
32
40aca27e
JH
33namespace pv {
34namespace view {
35
36ViewWidget::ViewWidget(View &parent) :
37 QWidget(&parent),
e9e4e5e7 38 view_(parent),
803cdac4 39 item_dragging_(false)
40aca27e 40{
e9e4e5e7 41 setFocusPolicy(Qt::ClickFocus);
c9743553 42 setAttribute(Qt::WA_AcceptTouchEvents, true);
e9e4e5e7
JH
43 setMouseTracking(true);
44}
45
46void ViewWidget::clear_selection()
47{
48 const auto items = this->items();
49 for (auto &i : items)
50 i->select(false);
51 update();
52}
53
e8b969a9
JH
54void ViewWidget::item_hover(const shared_ptr<ViewItem> &item)
55{
56 (void)item;
57}
58
e9e4e5e7
JH
59void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
60{
61 (void)item;
40aca27e
JH
62}
63
b434cbaf
JH
64bool ViewWidget::accept_drag() const
65{
66 const vector< shared_ptr<TimeItem> > items(view_.time_items());
67
68 const bool any_row_items_selected = any_of(view_.begin(), view_.end(),
69 [](const shared_ptr<RowItem> &r) { return r->selected(); });
70
71 const bool any_time_items_selected = any_of(items.begin(), items.end(),
72 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
73
74 if (any_row_items_selected && !any_time_items_selected)
75 {
76 // Check all the drag items share a common owner
77 RowItemOwner *item_owner = nullptr;
78 for (shared_ptr<RowItem> r : view_)
79 if (r->dragging()) {
80 if (!item_owner)
81 item_owner = r->owner();
82 else if(item_owner != r->owner())
83 return false;
84 }
85
86 return true;
87 }
88 else if (any_time_items_selected && !any_row_items_selected)
89 {
90 return true;
91 }
92
28290534
JH
93 // A background drag is beginning
94 return true;
b434cbaf
JH
95}
96
1f1edc09
JH
97bool ViewWidget::mouse_down() const
98{
99 return mouse_down_point_.x() != INT_MIN &&
100 mouse_down_point_.y() != INT_MIN;
101}
102
1dffa582
JH
103void ViewWidget::drag_items(const QPoint &delta)
104{
28290534
JH
105 bool item_dragged = false;
106
1dffa582
JH
107 // Drag the row items
108 RowItemOwner *item_owner = nullptr;
109 for (std::shared_ptr<RowItem> r : view_)
110 if (r->dragging()) {
111 item_owner = r->owner();
112 r->drag_by(delta);
113
114 // Ensure the trace is selected
115 r->select();
116 }
117
118 if (item_owner) {
28290534 119 item_dragged = true;
1dffa582
JH
120 item_owner->restack_items();
121 for (const auto &r : *item_owner)
122 r->animate_to_layout_v_offset();
123 }
124
125 // Drag the time items
126 const vector< shared_ptr<TimeItem> > items(view_.time_items());
127 for (auto &i : items)
28290534 128 if (i->dragging()) {
1dffa582 129 i->drag_by(delta);
28290534
JH
130 item_dragged = true;
131 }
132
133 // Do the background drag
134 if (!item_dragged)
135 drag_by(delta);
136}
137
138void ViewWidget::drag()
139{
140}
141
142void ViewWidget::drag_by(const QPoint &delta)
143{
144 (void)delta;
145}
146
147void ViewWidget::drag_release()
148{
1dffa582
JH
149}
150
e9e4e5e7
JH
151void ViewWidget::mouse_left_press_event(QMouseEvent *event)
152{
153 (void)event;
154
155 const bool ctrl_pressed =
156 QApplication::keyboardModifiers() & Qt::ControlModifier;
157
158 // Clear selection if control is not pressed and this item is unselected
159 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
160 !ctrl_pressed)
161 clear_selection();
162
163 // Set the signal selection state if the item has been clicked
164 if (mouse_down_item_) {
165 if (ctrl_pressed)
166 mouse_down_item_->select(!mouse_down_item_->selected());
167 else
168 mouse_down_item_->select(true);
169 }
170
171 // Save the offsets of any signals which will be dragged
28290534 172 bool item_dragged = false;
e9e4e5e7
JH
173 const auto items = this->items();
174 for (auto &i : items)
28290534
JH
175 if (i->selected()) {
176 item_dragged = true;
e9e4e5e7 177 i->drag();
28290534
JH
178 }
179
180 // Do the background drag
181 if (!item_dragged)
182 drag();
e9e4e5e7
JH
183
184 selection_changed();
185 update();
186}
187
188void ViewWidget::mouse_left_release_event(QMouseEvent *event)
189{
190 assert(event);
191
192 auto items = this->items();
193 const bool ctrl_pressed =
194 QApplication::keyboardModifiers() & Qt::ControlModifier;
195
196 // Unselect everything if control is not pressed
197 const shared_ptr<ViewItem> mouse_over =
198 get_mouse_over_item(event->pos());
199
200 for (auto &i : items)
201 i->drag_release();
202
803cdac4 203 if (item_dragging_)
e9e4e5e7
JH
204 view_.restack_all_row_items();
205 else
206 {
207 if (!ctrl_pressed) {
208 for (shared_ptr<ViewItem> i : items)
209 if (mouse_down_item_ != i)
210 i->select(false);
211
212 if (mouse_down_item_)
213 item_clicked(mouse_down_item_);
214 }
215 }
216
803cdac4 217 item_dragging_ = false;
e9e4e5e7
JH
218}
219
c9743553
JH
220bool ViewWidget::touch_event(QTouchEvent *e)
221{
222 (void)e;
223 return false;
224}
225
226bool ViewWidget::event(QEvent *event)
227{
228 switch (event->type()) {
229 case QEvent::TouchBegin:
230 case QEvent::TouchUpdate:
231 case QEvent::TouchEnd:
232 if (touch_event(static_cast<QTouchEvent *>(event)))
233 return true;
234 break;
235
236 default:
237 break;
238 }
239
240 return QWidget::event(event);
241}
242
e9e4e5e7
JH
243void ViewWidget::mousePressEvent(QMouseEvent *event)
244{
245 assert(event);
246
247 mouse_down_point_ = event->pos();
248 mouse_down_item_ = get_mouse_over_item(event->pos());
249
250 if (event->button() & Qt::LeftButton)
251 mouse_left_press_event(event);
252}
253
254void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
255{
256 assert(event);
257 if (event->button() & Qt::LeftButton)
258 mouse_left_release_event(event);
259
1f1edc09 260 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
e9e4e5e7
JH
261 mouse_down_item_ = nullptr;
262}
263
e8b969a9 264void ViewWidget::mouseMoveEvent(QMouseEvent *e)
e9e4e5e7 265{
e8b969a9
JH
266 assert(e);
267 mouse_point_ = e->pos();
e9e4e5e7 268
e8b969a9
JH
269 if (!e->buttons())
270 item_hover(get_mouse_over_item(e->pos()));
271 else if (e->buttons() & Qt::LeftButton)
539deb10 272 {
e8b969a9
JH
273 if (!item_dragging_)
274 {
275 if ((e->pos() - mouse_down_point_).manhattanLength() <
276 QApplication::startDragDistance())
277 return;
e9e4e5e7 278
e8b969a9
JH
279 if (!accept_drag())
280 return;
539deb10 281
e8b969a9
JH
282 item_dragging_ = true;
283 }
e9e4e5e7 284
e8b969a9
JH
285 // Do the drag
286 drag_items(e->pos() - mouse_down_point_);
e9e4e5e7 287
e8b969a9
JH
288 update();
289 }
e9e4e5e7
JH
290}
291
292void ViewWidget::leaveEvent(QEvent*)
293{
294 mouse_point_ = QPoint(-1, -1);
295 update();
296}
297
40aca27e
JH
298} // namespace view
299} // namespace pv