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)
88 // A background drag is beginning
92 bool ViewWidget::mouse_down() const
94 return mouse_down_point_.x() != INT_MIN &&
95 mouse_down_point_.y() != INT_MIN;
98 void ViewWidget::drag_items(const QPoint &delta)
100 bool item_dragged = false;
102 // Drag the row items
103 RowItemOwner *item_owner = nullptr;
104 for (std::shared_ptr<RowItem> r : view_)
106 item_owner = r->owner();
109 // Ensure the trace is selected
115 item_owner->restack_items();
116 for (const auto &r : *item_owner)
117 r->animate_to_layout_v_offset();
120 // Drag the time items
121 const vector< shared_ptr<TimeItem> > items(view_.time_items());
122 for (auto &i : items)
128 // Do the background drag
133 void ViewWidget::drag()
137 void ViewWidget::drag_by(const QPoint &delta)
142 void ViewWidget::drag_release()
146 void ViewWidget::mouse_left_press_event(QMouseEvent *event)
150 const bool ctrl_pressed =
151 QApplication::keyboardModifiers() & Qt::ControlModifier;
153 // Clear selection if control is not pressed and this item is unselected
154 if ((!mouse_down_item_ || !mouse_down_item_->selected()) &&
158 // Set the signal selection state if the item has been clicked
159 if (mouse_down_item_) {
161 mouse_down_item_->select(!mouse_down_item_->selected());
163 mouse_down_item_->select(true);
166 // Save the offsets of any signals which will be dragged
167 bool item_dragged = false;
168 const auto items = this->items();
169 for (auto &i : items)
175 // Do the background drag
183 void ViewWidget::mouse_left_release_event(QMouseEvent *event)
187 auto items = this->items();
188 const bool ctrl_pressed =
189 QApplication::keyboardModifiers() & Qt::ControlModifier;
191 // Unselect everything if control is not pressed
192 const shared_ptr<ViewItem> mouse_over =
193 get_mouse_over_item(event->pos());
195 for (auto &i : items)
199 view_.restack_all_row_items();
203 for (shared_ptr<ViewItem> i : items)
204 if (mouse_down_item_ != i)
207 if (mouse_down_item_)
208 item_clicked(mouse_down_item_);
212 item_dragging_ = false;
215 bool ViewWidget::touch_event(QTouchEvent *e)
221 bool ViewWidget::event(QEvent *event)
223 switch (event->type()) {
224 case QEvent::TouchBegin:
225 case QEvent::TouchUpdate:
226 case QEvent::TouchEnd:
227 if (touch_event(static_cast<QTouchEvent *>(event)))
235 return QWidget::event(event);
238 void ViewWidget::mousePressEvent(QMouseEvent *event)
242 mouse_down_point_ = event->pos();
243 mouse_down_item_ = get_mouse_over_item(event->pos());
245 if (event->button() & Qt::LeftButton)
246 mouse_left_press_event(event);
249 void ViewWidget::mouseReleaseEvent(QMouseEvent *event)
252 if (event->button() & Qt::LeftButton)
253 mouse_left_release_event(event);
255 mouse_down_point_ = QPoint(INT_MIN, INT_MIN);
256 mouse_down_item_ = nullptr;
259 void ViewWidget::mouseMoveEvent(QMouseEvent *event)
262 mouse_point_ = event->pos();
264 if (!(event->buttons() & Qt::LeftButton))
269 if ((event->pos() - mouse_down_point_).manhattanLength() <
270 QApplication::startDragDistance())
276 item_dragging_ = true;
280 drag_items(event->pos() - mouse_down_point_);
285 void ViewWidget::leaveEvent(QEvent*)
287 mouse_point_ = QPoint(-1, -1);