]> sigrok.org Git - pulseview.git/blame - pv/view/viewwidget.cpp
Viewport: Use fabs instead of branch
[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
54void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
55{
56 (void)item;
40aca27e
JH
57}
58
b434cbaf
JH
59bool ViewWidget::accept_drag() const
60{
61 const vector< shared_ptr<TimeItem> > items(view_.time_items());
62
63 const bool any_row_items_selected = any_of(view_.begin(), view_.end(),
64 [](const shared_ptr<RowItem> &r) { return r->selected(); });
65
66 const bool any_time_items_selected = any_of(items.begin(), items.end(),
67 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
68
69 if (any_row_items_selected && !any_time_items_selected)
70 {
71 // Check all the drag items share a common owner
72 RowItemOwner *item_owner = nullptr;
73 for (shared_ptr<RowItem> r : view_)
74 if (r->dragging()) {
75 if (!item_owner)
76 item_owner = r->owner();
77 else if(item_owner != r->owner())
78 return false;
79 }
80
81 return true;
82 }
83 else if (any_time_items_selected && !any_row_items_selected)
84 {
85 return true;
86 }
87
88 return false;
89}
90
1f1edc09
JH
91bool ViewWidget::mouse_down() const
92{
93 return mouse_down_point_.x() != INT_MIN &&
94 mouse_down_point_.y() != INT_MIN;
95}
96
1dffa582
JH
97void ViewWidget::drag_items(const QPoint &delta)
98{
99 // Drag the row items
100 RowItemOwner *item_owner = nullptr;
101 for (std::shared_ptr<RowItem> r : view_)
102 if (r->dragging()) {
103 item_owner = r->owner();
104 r->drag_by(delta);
105
106 // Ensure the trace is selected
107 r->select();
108 }
109
110 if (item_owner) {
111 item_owner->restack_items();
112 for (const auto &r : *item_owner)
113 r->animate_to_layout_v_offset();
114 }
115
116 // Drag the time items
117 const vector< shared_ptr<TimeItem> > items(view_.time_items());
118 for (auto &i : items)
119 if (i->dragging())
120 i->drag_by(delta);
121}
122
e9e4e5e7
JH
123void ViewWidget::mouse_left_press_event(QMouseEvent *event)
124{
125 (void)event;
126
127 const bool ctrl_pressed =
128 QApplication::keyboardModifiers() & Qt::ControlModifier;
129
130 // Clear selection if control is not pressed and this item is unselected
131 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
132 !ctrl_pressed)
133 clear_selection();
134
135 // Set the signal selection state if the item has been clicked
136 if (mouse_down_item_) {
137 if (ctrl_pressed)
138 mouse_down_item_->select(!mouse_down_item_->selected());
139 else
140 mouse_down_item_->select(true);
141 }
142
143 // Save the offsets of any signals which will be dragged
144 const auto items = this->items();
145 for (auto &i : items)
146 if (i->selected())
147 i->drag();
148
149 selection_changed();
150 update();
151}
152
153void ViewWidget::mouse_left_release_event(QMouseEvent *event)
154{
155 assert(event);
156
157 auto items = this->items();
158 const bool ctrl_pressed =
159 QApplication::keyboardModifiers() & Qt::ControlModifier;
160
161 // Unselect everything if control is not pressed
162 const shared_ptr<ViewItem> mouse_over =
163 get_mouse_over_item(event->pos());
164
165 for (auto &i : items)
166 i->drag_release();
167
803cdac4 168 if (item_dragging_)
e9e4e5e7
JH
169 view_.restack_all_row_items();
170 else
171 {
172 if (!ctrl_pressed) {
173 for (shared_ptr<ViewItem> i : items)
174 if (mouse_down_item_ != i)
175 i->select(false);
176
177 if (mouse_down_item_)
178 item_clicked(mouse_down_item_);
179 }
180 }
181
803cdac4 182 item_dragging_ = false;
e9e4e5e7
JH
183}
184
c9743553
JH
185bool ViewWidget::touch_event(QTouchEvent *e)
186{
187 (void)e;
188 return false;
189}
190
191bool ViewWidget::event(QEvent *event)
192{
193 switch (event->type()) {
194 case QEvent::TouchBegin:
195 case QEvent::TouchUpdate:
196 case QEvent::TouchEnd:
197 if (touch_event(static_cast<QTouchEvent *>(event)))
198 return true;
199 break;
200
201 default:
202 break;
203 }
204
205 return QWidget::event(event);
206}
207
e9e4e5e7
JH
208void ViewWidget::mousePressEvent(QMouseEvent *event)
209{
210 assert(event);
211
212 mouse_down_point_ = event->pos();
213 mouse_down_item_ = get_mouse_over_item(event->pos());
214
215 if (event->button() & Qt::LeftButton)
216 mouse_left_press_event(event);
217}
218
219void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
220{
221 assert(event);
222 if (event->button() & Qt::LeftButton)
223 mouse_left_release_event(event);
224
1f1edc09 225 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
e9e4e5e7
JH
226 mouse_down_item_ = nullptr;
227}
228
229void ViewWidget::mouseMoveEvent(QMouseEvent *event)
230{
231 assert(event);
232 mouse_point_ = event->pos();
233
234 if (!(event->buttons() & Qt::LeftButton))
235 return;
236
237 if ((event->pos() - mouse_down_point_).manhattanLength() <
238 QApplication::startDragDistance())
239 return;
240
241 if (!accept_drag())
242 return;
243
244 // Do the drag
803cdac4 245 item_dragging_ = true;
e9e4e5e7
JH
246 drag_items(event->pos() - mouse_down_point_);
247
248 update();
249}
250
251void ViewWidget::leaveEvent(QEvent*)
252{
253 mouse_point_ = QPoint(-1, -1);
254 update();
255}
256
40aca27e
JH
257} // namespace view
258} // namespace pv