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