]> sigrok.org Git - pulseview.git/blame - pv/view/viewwidget.cpp
ViewWidget: Moved in mouse handlers
[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
JH
37 view_(parent),
38 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
1dffa582
JH
89void ViewWidget::drag_items(const QPoint &delta)
90{
91 // Drag the row items
92 RowItemOwner *item_owner = nullptr;
93 for (std::shared_ptr<RowItem> r : view_)
94 if (r->dragging()) {
95 item_owner = r->owner();
96 r->drag_by(delta);
97
98 // Ensure the trace is selected
99 r->select();
100 }
101
102 if (item_owner) {
103 item_owner->restack_items();
104 for (const auto &r : *item_owner)
105 r->animate_to_layout_v_offset();
106 }
107
108 // Drag the time items
109 const vector< shared_ptr<TimeItem> > items(view_.time_items());
110 for (auto &i : items)
111 if (i->dragging())
112 i->drag_by(delta);
113}
114
e9e4e5e7
JH
115void ViewWidget::mouse_left_press_event(QMouseEvent *event)
116{
117 (void)event;
118
119 const bool ctrl_pressed =
120 QApplication::keyboardModifiers() & Qt::ControlModifier;
121
122 // Clear selection if control is not pressed and this item is unselected
123 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
124 !ctrl_pressed)
125 clear_selection();
126
127 // Set the signal selection state if the item has been clicked
128 if (mouse_down_item_) {
129 if (ctrl_pressed)
130 mouse_down_item_->select(!mouse_down_item_->selected());
131 else
132 mouse_down_item_->select(true);
133 }
134
135 // Save the offsets of any signals which will be dragged
136 const auto items = this->items();
137 for (auto &i : items)
138 if (i->selected())
139 i->drag();
140
141 selection_changed();
142 update();
143}
144
145void ViewWidget::mouse_left_release_event(QMouseEvent *event)
146{
147 assert(event);
148
149 auto items = this->items();
150 const bool ctrl_pressed =
151 QApplication::keyboardModifiers() & Qt::ControlModifier;
152
153 // Unselect everything if control is not pressed
154 const shared_ptr<ViewItem> mouse_over =
155 get_mouse_over_item(event->pos());
156
157 for (auto &i : items)
158 i->drag_release();
159
160 if (dragging_)
161 view_.restack_all_row_items();
162 else
163 {
164 if (!ctrl_pressed) {
165 for (shared_ptr<ViewItem> i : items)
166 if (mouse_down_item_ != i)
167 i->select(false);
168
169 if (mouse_down_item_)
170 item_clicked(mouse_down_item_);
171 }
172 }
173
174 dragging_ = false;
175}
176
177void ViewWidget::mousePressEvent(QMouseEvent *event)
178{
179 assert(event);
180
181 mouse_down_point_ = event->pos();
182 mouse_down_item_ = get_mouse_over_item(event->pos());
183
184 if (event->button() & Qt::LeftButton)
185 mouse_left_press_event(event);
186}
187
188void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
189{
190 assert(event);
191 if (event->button() & Qt::LeftButton)
192 mouse_left_release_event(event);
193
194 mouse_down_item_ = nullptr;
195}
196
197void ViewWidget::mouseMoveEvent(QMouseEvent *event)
198{
199 assert(event);
200 mouse_point_ = event->pos();
201
202 if (!(event->buttons() & Qt::LeftButton))
203 return;
204
205 if ((event->pos() - mouse_down_point_).manhattanLength() <
206 QApplication::startDragDistance())
207 return;
208
209 if (!accept_drag())
210 return;
211
212 // Do the drag
213 dragging_ = true;
214 drag_items(event->pos() - mouse_down_point_);
215
216 update();
217}
218
219void ViewWidget::leaveEvent(QEvent*)
220{
221 mouse_point_ = QPoint(-1, -1);
222 update();
223}
224
40aca27e
JH
225} // namespace view
226} // namespace pv