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_hover(const shared_ptr<ViewItem> &item)
59 void ViewWidget::item_clicked(const shared_ptr<ViewItem> &item)
64 bool ViewWidget::accept_drag() const
66 const vector< shared_ptr<TimeItem> > items(view_.time_items());
68 const bool any_row_items_selected = any_of(view_.begin(), view_.end(),
69 [](const shared_ptr<RowItem> &r) { return r->selected(); });
71 const bool any_time_items_selected = any_of(items.begin(), items.end(),
72 [](const shared_ptr<TimeItem> &i) { return i->selected(); });
74 if (any_row_items_selected && !any_time_items_selected)
76 // Check all the drag items share a common owner
77 RowItemOwner *item_owner = nullptr;
78 for (shared_ptr<RowItem> r : view_)
81 item_owner = r->owner();
82 else if(item_owner != r->owner())
88 else if (any_time_items_selected && !any_row_items_selected)
93 // A background drag is beginning
97 bool ViewWidget::mouse_down() const
99 return mouse_down_point_.x() != INT_MIN &&
100 mouse_down_point_.y() != INT_MIN;
103 void ViewWidget::drag_items(const QPoint &delta)
105 bool item_dragged = false;
107 // Drag the row items
108 RowItemOwner *item_owner = nullptr;
109 for (std::shared_ptr<RowItem> r : view_)
111 item_owner = r->owner();
114 // Ensure the trace is selected
120 item_owner->restack_items();
121 for (const auto &r : *item_owner)
122 r->animate_to_layout_v_offset();
125 // Drag the time items
126 const vector< shared_ptr<TimeItem> > items(view_.time_items());
127 for (auto &i : items)
133 // Do the background drag
138 void ViewWidget::drag()
142 void ViewWidget::drag_by(const QPoint &delta)
147 void ViewWidget::drag_release()
151 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
155 const bool ctrl_pressed =
156 QApplication::keyboardModifiers() & Qt::ControlModifier;
158 // Clear selection if control is not pressed and this item is unselected
159 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
163 // Set the signal selection state if the item has been clicked
164 if (mouse_down_item_) {
166 mouse_down_item_->select(!mouse_down_item_->selected());
168 mouse_down_item_->select(true);
171 // Save the offsets of any signals which will be dragged
172 bool item_dragged = false;
173 const auto items = this->items();
174 for (auto &i : items)
180 // Do the background drag
188 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
192 auto items = this->items();
193 const bool ctrl_pressed =
194 QApplication::keyboardModifiers() & Qt::ControlModifier;
196 // Unselect everything if control is not pressed
197 const shared_ptr<ViewItem> mouse_over =
198 get_mouse_over_item(event->pos());
200 for (auto &i : items)
204 view_.restack_all_row_items();
208 for (shared_ptr<ViewItem> i : items)
209 if (mouse_down_item_ != i)
212 if (mouse_down_item_)
213 item_clicked(mouse_down_item_);
217 item_dragging_ = false;
220 bool ViewWidget::touch_event(QTouchEvent *e)
226 bool ViewWidget::event(QEvent *event)
228 switch (event->type()) {
229 case QEvent::TouchBegin:
230 case QEvent::TouchUpdate:
231 case QEvent::TouchEnd:
232 if (touch_event(static_cast<QTouchEvent *>(event)))
240 return QWidget::event(event);
243 void ViewWidget::mousePressEvent(QMouseEvent *event)
247 mouse_down_point_ = event->pos();
248 mouse_down_item_ = get_mouse_over_item(event->pos());
250 if (event->button() & Qt::LeftButton)
251 mouse_left_press_event(event);
254 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
257 if (event->button() & Qt::LeftButton)
258 mouse_left_release_event(event);
260 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
261 mouse_down_item_ = nullptr;
264 void ViewWidget::mouseMoveEvent(QMouseEvent *e)
267 mouse_point_ = e->pos();
270 item_hover(get_mouse_over_item(e->pos()));
271 else if (e->buttons() & Qt::LeftButton)
275 if ((e->pos() - mouse_down_point_).manhattanLength() <
276 QApplication::startDragDistance())
282 item_dragging_ = true;
286 drag_items(e->pos() - mouse_down_point_);
292 void ViewWidget::leaveEvent(QEvent*)
294 mouse_point_ = QPoint(-1, -1);