2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
21 #include <QApplication>
22 #include <QMouseEvent>
23 #include <QTouchEvent>
25 #include "rowitem.hpp"
27 #include "viewwidget.hpp"
30 using std::shared_ptr;
36 ViewWidget::ViewWidget(View &parent) :
41 setFocusPolicy(Qt::ClickFocus);
42 setAttribute(Qt::WA_AcceptTouchEvents, true);
43 setMouseTracking(true);
46 void ViewWidget::clear_selection()
48 const auto items = this->items();
54 void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
59 bool ViewWidget::accept_drag() const
61 const vector< shared_ptr<TimeItem> > items(view_.time_items());
63 const bool any_row_items_selected = any_of(view_.begin(), view_.end(),
64 [](const shared_ptr<RowItem> &r) { return r->selected(); });
66 const bool any_time_items_selected = any_of(items.begin(), items.end(),
67 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
69 if (any_row_items_selected && !any_time_items_selected)
71 // Check all the drag items share a common owner
72 RowItemOwner *item_owner = nullptr;
73 for (shared_ptr<RowItem> r : view_)
76 item_owner = r->owner();
77 else if(item_owner != r->owner())
83 else if (any_time_items_selected && !any_row_items_selected)
91 bool ViewWidget::mouse_down() const
93 return mouse_down_point_.x() != INT_MIN &&
94 mouse_down_point_.y() != INT_MIN;
97 void ViewWidget::drag_items(const QPoint &delta)
100 RowItemOwner *item_owner = nullptr;
101 for (std::shared_ptr<RowItem> r : view_)
103 item_owner = r->owner();
106 // Ensure the trace is selected
111 item_owner->restack_items();
112 for (const auto &r : *item_owner)
113 r->animate_to_layout_v_offset();
116 // Drag the time items
117 const vector< shared_ptr<TimeItem> > items(view_.time_items());
118 for (auto &i : items)
123 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
127 const bool ctrl_pressed =
128 QApplication::keyboardModifiers() & Qt::ControlModifier;
130 // Clear selection if control is not pressed and this item is unselected
131 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
135 // Set the signal selection state if the item has been clicked
136 if (mouse_down_item_) {
138 mouse_down_item_->select(!mouse_down_item_->selected());
140 mouse_down_item_->select(true);
143 // Save the offsets of any signals which will be dragged
144 const auto items = this->items();
145 for (auto &i : items)
153 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
157 auto items = this->items();
158 const bool ctrl_pressed =
159 QApplication::keyboardModifiers() & Qt::ControlModifier;
161 // Unselect everything if control is not pressed
162 const shared_ptr<ViewItem> mouse_over =
163 get_mouse_over_item(event->pos());
165 for (auto &i : items)
169 view_.restack_all_row_items();
173 for (shared_ptr<ViewItem> i : items)
174 if (mouse_down_item_ != i)
177 if (mouse_down_item_)
178 item_clicked(mouse_down_item_);
182 item_dragging_ = false;
185 bool ViewWidget::touch_event(QTouchEvent *e)
191 bool ViewWidget::event(QEvent *event)
193 switch (event->type()) {
194 case QEvent::TouchBegin:
195 case QEvent::TouchUpdate:
196 case QEvent::TouchEnd:
197 if (touch_event(static_cast<QTouchEvent *>(event)))
205 return QWidget::event(event);
208 void ViewWidget::mousePressEvent(QMouseEvent *event)
212 mouse_down_point_ = event->pos();
213 mouse_down_item_ = get_mouse_over_item(event->pos());
215 if (event->button() & Qt::LeftButton)
216 mouse_left_press_event(event);
219 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
222 if (event->button() & Qt::LeftButton)
223 mouse_left_release_event(event);
225 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
226 mouse_down_item_ = nullptr;
229 void ViewWidget::mouseMoveEvent(QMouseEvent *event)
232 mouse_point_ = event->pos();
234 if (!(event->buttons() & Qt::LeftButton))
237 if ((event->pos() - mouse_down_point_).manhattanLength() <
238 QApplication::startDragDistance())
245 item_dragging_ = true;
246 drag_items(event->pos() - mouse_down_point_);
251 void ViewWidget::leaveEvent(QEvent*)
253 mouse_point_ = QPoint(-1, -1);